summaryrefslogtreecommitdiff
path: root/vimulti.clj
diff options
context:
space:
mode:
authoreltrac <hi@eltr.ac>2026-07-05 12:04:05 +0800
committereltrac <hi@eltr.ac>2026-07-05 12:04:05 +0800
commitd483aad0ee38ba5d4dcae2c3f87490611db32871 (patch)
tree32cd735ac65a86777da8e0ac56e6dc76dd25251b /vimulti.clj
parent4a3c0b43a7df7479939abcfb2675a6a33c4c1c2a (diff)
support multiple repos and tags, refactor vimulti.clj
Diffstat (limited to 'vimulti.clj')
-rw-r--r--vimulti.clj71
1 files changed, 58 insertions, 13 deletions
diff --git a/vimulti.clj b/vimulti.clj
index bfba0b1..d33b53c 100644
--- a/vimulti.clj
+++ b/vimulti.clj
@@ -2,7 +2,8 @@
(require '[babashka.tasks :refer [shell]]
'[babashka.fs :as fs]
- '[clojure.edn :as edn])
+ '[clojure.edn :as edn]
+ '[clojure.string :as str])
(def default-config
{:editor-command "nvim"
@@ -26,25 +27,69 @@
(spit path default-config))
path)
+(def config (-> (config-path) init-config slurp edn/read-string))
+
;; git helpers
(defn- get-current-branch [dir]
(shell {:dir dir
:out :string}
"git branch --show-current"))
-;; main
-(let [[target-branch & args] *command-line-args*
- config (-> (config-path) init-config slurp edn/read-string)
- current-branch (or
- (get-current-branch (:editor-config config))
- (:default-branch config))]
- ;; switch to target branch
- (when-not (= current-branch target-branch)
- (shell {:dir (:editor-config config)}
- (str "git switch " target-branch)))
+;; config switcher
+(defn- use-branch [branch args]
+ (let [current-branch (or
+ (get-current-branch (:editor-config config))
+ (:default-branch config))]
+ (when-not (= current-branch branch)
+ (shell {:dir (:editor-config config)}
+ (str "git switch " branch)))
;; then use editor
- (shell (str (:editor-command config) " " (apply str args)))
+ (shell (str (:editor-command config) " " (apply str args)))
;; lastly, swtich back
- (when-not (= current-branch target-branch)
+ (when-not (= current-branch branch)
+ (shell {:dir (:editor-config config)}
+ (str "git switch " current-branch)))))
+
+;; target should be a string like "app-name:branch" or "app-name@tag"
+;; returns a map with keys :app-name :branch :tag...
+(defn- parse-target-string [target]
+ (let [branch-style? (str/includes? target ":")
+ tag-style? (str/includes? target "@")
+ alias-style? (and (not branch-style?) (not tag-style?))]
+ (if alias-style?
+ ;; if use alias style
+ (if-let [alias-config (get (:aliases config) target)]
+ alias-config
+ (do (println "No such alias or invalid target.")
+ (System/exit 1)))
+ ;; if not alias style
+ (let [separator (if branch-style? #":" (if tag-style? #"@" #" "))
+ target-array (str/split target separator)
+ app-name (first target-array)
+ repo (str #_(TODO))]
+ (when-not (> (count target-array) 2)
+ (println "Invalid target string.")
+ (System/exit 1))
+ (cond-> {:app-name app-name}
+ branch-style? (assoc :branch (last target-array))
+ tag-style? (assoc :tag (last target-array))
+ ;; TODO add repo location
+ )))))
+
+;; main
+;; target should be a string like "app-name:branch" or "app-name@tag"
+(let [[target & args] *command-line-args*
+ target-map (parse-target-string target)]
+ (let [current-branch (or
+ (get-current-branch (:editor-config config))
+ (:default-branch config))]
+ (when-let [switch-to (or (:branch target-map)
+ (:tag target-map))]
+ (let [tag? (boolean (:tag target-map))]
+ ;; TODO replace (:editor-config config) with (:repo target-map)
+ (shell {:dir (:editor-config config)}
+ (str "git switch " switch-to (if tag? "--detach" "")))))
+ (shell {:extra-env {"NVIM_APPNAME" (:app-name target-map)}}
+ (:editor-command config) " " (apply str args))
(shell {:dir (:editor-config config)}
(str "git switch " current-branch))))