]> njoseph.me Git - babashka-scripts.git/blame - scripts/git.clj
git: minor changes
[babashka-scripts.git] / scripts / git.clj
CommitLineData
ec37ad05
JN
1(ns git
2 (:require [babashka.process :as p]
3 [clojure.java.io :as io]
4 [clojure.string :as str]
5 [lib :refer [run-cmd]]))
6
7; Utilities for git operations
8
ec37ad05
JN
9(defn- has-git-repo
10 [dir]
11 (first (filter #(= ".git" %)
12 (map #(.getName %)
13 (filter #(.isDirectory %) (.listFiles (io/file dir)))))))
14
15(defn- list-dirs
16 [root]
17 (filter #(has-git-repo %)
18 (filter #(.isDirectory %) (.listFiles (io/file root)))))
19
4dcdd4a9
JN
20(defn- git-pull
21 "Do a git pull with rebase."
22 [dir]
23 (p/process ["git" "-C" dir "pull" "--rebase"]))
ec37ad05
JN
24
25(defn git-pull-all
4dcdd4a9 26 "Runs `git-pull` on all the git repositories in a directory."
ec37ad05
JN
27 [root]
28 (let [dirs (list-dirs root)
29 pulls (->> root
30 list-dirs
31 (map git-pull)
32 doall)
33 outputs (map #(->> %
34 p/check
35 :out
36 slurp)
e1035b9f 37 pulls)]
ec37ad05
JN
38 ;; Print corresponding directory name when pulling
39 ;; Skip printing already up to date repos
40 (doseq [[dir out] (filter #(not (.contains (second %) "is up to date."))
e1035b9f 41 (map vector dirs outputs))]
ec37ad05
JN
42 (println (str dir "\n" out)))))
43
44
45(defn git-pull-rebase-branch
4dcdd4a9 46 "Do git pull and rebase branch with master."
ec37ad05 47 [dir]
4dcdd4a9 48 (git-pull dir)
ec37ad05 49 (let [current-branch (str/trim (run-cmd ["git" "branch" "--show-current"]))]
ec37ad05
JN
50 (when (not (contains? #{"master" "main"} current-branch))
51 (run! print
52 (map run-cmd
e1035b9f
JN
53 [["git" "checkout" "master"] ["git" "pull" "--rebase"]
54 ["git" "checkout" current-branch] ["git" "rebase" "master"]])))))