This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-06-09
Channels
- # announcements (1)
- # babashka (14)
- # calva (8)
- # chlorine-clover (3)
- # clerk (6)
- # clj-kondo (27)
- # cljdoc (20)
- # clojars (6)
- # clojure (53)
- # clojure-denver (8)
- # clojure-europe (17)
- # clojure-nl (1)
- # clojure-norway (270)
- # clojure-uk (5)
- # clojurescript (35)
- # community-development (7)
- # cursive (12)
- # datalevin (3)
- # datomic (26)
- # etaoin (23)
- # exercism (1)
- # hyperfiddle (3)
- # java (14)
- # nrepl (2)
- # off-topic (12)
- # pathom (3)
- # portal (44)
- # practicalli (2)
- # reagent (7)
- # releases (1)
- # shadow-cljs (13)
- # timbre (3)
- # xtdb (4)
Is it possible to change the directory in which process/sh
runs for one command?
I have the following code:
(defn clone-repo
"Clone a Git repo to a destination path."
([repo-url] (clone-repo repo-url "./repos"))
([repo-url dest-path] (if-not (file/exists? dest-path) (file/create-dir dest-path) nil)
(process/sh "git clone" repo-url dest-path)))
(defn clone-all-repos
"Clone all Git repos from Azure DevOps."
[] (let [repo-urls (->> (get-all-repo-data)
(tree-seq coll? identity)
(keep :sshUrl))]
(->> repo-urls
(map clone-repo))))
The problem right now is that all Git repos get cloned into the same directory (`./repos`), but I want each Git repo to be cloned into its own parent directory within ./repos
. It doesn't seem like Git offers this as an option to clone
, so I'm looking for a way to temporarily change Babashka's working directory (for that one sh
command) or something along those lines.The docs are pretty good! The problem is more that I don't always brain correctly ๐
> It doesnโt seem like Git offers this as an option to clone
Maybe this helps
/tmp/some-dir
$ mkdir repos
/tmp/some-dir
$ git clone ./repos/process
Cloning into './repos/process'...
/tmp/some-dir
$ git clone ./repos/process-again
Cloning into './repos/process-again'...
/tmp/some-dir
$ git clone ./repos/process-3
Cloning into './repos/process-3'...
/tmp/some-dir
$ ls repos/
process/ process-3/ process-again/
With Bash, I can do the following to find all directories named .git
(find all Git repos on my machine):
find / -name ".git" -type d
Babashka has babashka.fs/glob
, which can be used to find files.
But can it also find directories? Something akin to -type d
.
I naively tried this:
(defn find-repos
"Find all Git repos in a directory."
[root-path] (babashka.fs/glob root-path "**.git"))
(find-repos "./repos")
But it yields an empty vector. Presumably, because it looks for files, not directories? :thinking_face:Good point. I think this is because visit-file is never called on a directory:
(fs/walk-file-tree "."
{:pre-visit-dir (fn [dir _]
#_(when (str/includes? (str dir) ".git")
(prn dir)) :continue)
:visit-file (fn [path attrs]
(when (and (fs/directory? path)
(str/includes? (str path) ".git"))
(prn path))
:continue)})
and match
is only ever called on files here:
https://github.com/babashka/fs/blob/c5c5365585efa44a6c4e8ca91bd84d9adc631a02/src/babashka/fs.cljc#L314
As I'm writing a repro case for the bug report, I discovered something interesting. This:
(babashka.fs/create-dir "./test-dir")
(babashka.fs/glob "." "**test-dir")
Seems to find the directory:
=> [#object[sun.nio.fs.UnixPath 0x35cba6ab "test-dir"]]
But this:
(babashka.fs/create-dir "./.test-dir")
(babashka.fs/glob "." "**.test-dir")
Does not:
=> []
Could it perhaps have something to do with the .
in the directory name? :thinking_face:No rush! Just leaving the info here in case it's useful, and to make a better bug report.
FYI, all the info from this thread is gathered https://github.com/babashka/fs/issues/105.