Skip to content

Instantly share code, notes, and snippets.

@zoren
Last active September 27, 2022 11:21
Show Gist options
  • Save zoren/371baf6afbc2327832bb86ae70d5c4cd to your computer and use it in GitHub Desktop.
Save zoren/371baf6afbc2327832bb86ae70d5c4cd to your computer and use it in GitHub Desktop.
babashka script to git clone repos in to hostname/path subpath
#!/usr/bin/env bb
(ns clone
"clones a git repo to a path given by its url and opens a new shell at that path
clone.clj https://github.com/babashka/sci will clone to ~/github.com/babashka/sci"
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[babashka.fs :as fs]
[babashka.process :refer [process check shell]]))
(def repo-path (System/getenv "HOME"))
(let [[url] *command-line-args*
_ (when-not url
(throw (ex-info "missing url" {})))
_ (shell "git ls-remote" url)
{:keys [host file]} (bean (io/as-url url))
file (if (str/ends-with? file ".git")
(subs file 0 (- (count file) (count ".git")))
file)
destination-dir (fs/path repo-path (str host file))]
(when (fs/exists? destination-dir)
(throw (ex-info "destination directory already exists, maybe you already cloned this"
{:dir destination-dir})))
(fs/create-dirs destination-dir)
(-> (process ['git 'clone url destination-dir] {:out :inherit
:err :inherit})
check)
(shell {:dir (str destination-dir)} (System/getenv "SHELL"))
nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment