8000 Fix #141: fs/match doesn't match with path that contains glob/regex chars by borkdude · Pull Request #143 · babashka/fs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix #141: fs/match doesn't match with path that contains glob/regex chars #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ For a list of breaking changes, check [here](#breaking-changes).

Babashka [fs](https://github.com/babashka/fs): file system utility library for Clojure

## Unreleased

- #141: `fs/match` doesn't match when root dir contains glob or regex characters in path
- [#138](https://github.com/babashka/fs/issues/138): Fix `fs/update-file` to support paths ([@rfhayashi](https://github.com/rfhayashi))

## v0.5.24 (2025-01-09)
Expand Down
28 changes: 22 additions & 6 deletions src/babashka/fs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@
(str/lower-case)
(str/includes? "win")))

(defn- escape-glob-chars
"Escapes special glob characters in the input string."
[s]
(let [special-chars #{\\ \* \? \[ \] \{ \}}
escape-char (fn [c]
(if (contains? special-chars c)
(str "\\" c)
(str c)))]
(apply str (map escape-char s))))

(defn- escape-regex-chars
"Escapes a string so it can be used literally in a regular expression."
[s]
(java.util.regex.Pattern/quote s))

(defn match
"Given a file and match pattern, returns matches as vector of
paths. Pattern interpretation is done using the rules described in
Expand All @@ -276,19 +291,20 @@
`(fs/match \".\" \"regex:.*\\\\.clj\" {:recursive true})`"
([root pattern] (match root pattern nil))
([root pattern {:keys [hidden follow-links max-depth recursive]}]
(let [base-path (-> root absolutize normalize)
base-path (if win?
(str/replace base-path file-separator (str "\\" file-separator))
(str base-path))
(let [[prefix pattern] (str/split pattern #":")
base-path (-> root absolutize normalize str)
escaped-base-path (case prefix
"glob" (escape-glob-chars base-path)
"regex" (escape-regex-chars base-path)
base-path)
skip-hidden? (not hidden)
results (atom (transient []))
past-root? (volatile! nil)
[prefix pattern] (str/split pattern #":")
pattern (let [separator (when-not (str/ends-with? base-path file-separator)
;; we need to escape the file separator on Windows
(str (when win? "\\")
file-separator))]
(str base-path
(str escaped-base-path
separator
(if win?
(str/replace pattern "/" "\\\\")
Expand Down
11 changes: 10 additions & 1 deletion test/babashka/fs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,16 @@
_ (spit (fs/file nested-dir "dude.txt") "contents")]
(is (= 1 (count (if windows?
(fs/match tmp-dir1 "regex:foo\\\\bar\\\\baz\\\\.*" {:recursive true})
(fs/match tmp-dir1 "regex:foo/bar/baz/.*" {:recursive true}))))))))
(fs/match tmp-dir1 "regex:foo/bar/baz/.*" {:recursive true})))))))
(when-not windows?
(testing "match on root with special chars"
(let [dir (fs/create-dirs (fs/path "test-resources" "foo*{[]}"))
txt-file (fs/file dir "test.txt")]
(fs/delete-on-exit dir)
(fs/delete-on-exit txt-file)
(spit txt-file "hello"))
(is (= "test.txt" (fs/file-name (first (fs/match "test-resources/foo*{[]}" "glob:*.txt")))))
(is (= "test.txt" (fs/file-name (first (fs/match "test-resources/foo*{[]}" "regex:.*\\.txt"))))))))

(deftest glob-test
(let [readme-match (fs/glob "." "README.md")]
Expand Down
0