Skip to content

Commit 24628c8

Browse files
committed
some small improvements to git/pull
1 parent 1be0204 commit 24628c8

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

git/pull/bb.edn

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
{:bbin/bin {pull-projects {:main-opts ["-f" "main.clj"]}}
1+
{:bbin/bin {pull {:main-opts ["-f" "main.clj"]}}
22
:deps {io.github.joakimen/pull {:local/root "."}}}

git/pull/main.clj

+52-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
(ns git.pull.main
2-
"WIP - rebase-pull all tracked repos. dirty repos are skipped"
2+
"rebase-pull main branches of all tracked repos.
3+
4+
Repos are pulled if
5+
- repo has a supported remote
6+
- work tree is clean
7+
- checked out branch is master/main
8+
9+
idea: split into 3 parts:
10+
- collect all repo metadata (branch, remote, clean, etc)
11+
- validate repos by inspecting metadata
12+
- pull repos in parallel"
313
(:require [babashka.fs :as fs]
414
[babashka.process :refer [sh]]
515
[clojure.string :as str]
616
[doric.core :as doric])
717
(:import [java.util.concurrent Executors ExecutorService Future]))
818

19+
(def ^:const supported-remotes
20+
21+
"https://github.com"])
22+
923
(defn- run [& args]
1024
(let [{:keys [out err exit]} (apply sh args)]
1125
(when-not (zero? exit)
@@ -24,6 +38,9 @@
2438
(defn- is-clean? [repo-path]
2539
(zero-exit? repo-path "git diff --quiet"))
2640

41+
(defn- get-repo-remote [repo]
42+
(:out (sh "git" "-C" repo "config" "--get" "remote.origin.url")))
43+
2744
(defn- get-current-branch [repo]
2845
(run "git" "-C" repo "branch" "--show-current"))
2946

@@ -34,6 +51,18 @@
3451
[s n]
3552
(subs s 0 (min (count s) n)))
3653

54+
(defn has-supported-remote? [repo]
55+
(let [substr-in? (fn [e coll]
56+
(some #(str/includes? e %) coll))
57+
supported-remote? #(substr-in? % supported-remotes)
58+
remote-url (get-repo-remote repo)]
59+
(when remote-url
60+
(supported-remote? remote-url))))
61+
62+
(defn is-on-default-branch? [repo]
63+
(let [branch (get-current-branch repo)]
64+
(some #{branch} ["master" "main"])))
65+
3766
(defn- pull-repo [repo]
3867
(let [branch (get-current-branch repo)
3968
{:keys [exit out err]} (git-pull repo branch)]
@@ -59,9 +88,15 @@
5988
:err (fmt-msg err)})
6089

6190
(defn -main [& _]
62-
(let [clean-projects (filter is-clean? (list-projects))
91+
(let [projects (list-projects)
92+
filtered-projects
93+
(filter (apply every-pred
94+
[is-clean?
95+
has-supported-remote?
96+
is-on-default-branch?])
97+
projects)
6398
executor (Executors/newFixedThreadPool 128)
64-
tasks (mapv #(fn [] (pull-repo %)) clean-projects)
99+
tasks (mapv #(fn [] (pull-repo %)) filtered-projects)
65100
execution-results (->> (.invokeAll ^ExecutorService executor tasks)
66101
(map #(.get ^Future %)))
67102
result (mapv prettify execution-results)
@@ -75,7 +110,20 @@
75110

76111
(comment
77112

78-
(def clean-projects (filter is-clean? (list-projects)))
113+
(def projects (list-projects))
114+
(def clean-projects (filter is-clean? projects))
115+
(def supported-remote-projects (filter has-supported-remote? projects))
116+
117+
(filter (apply every-pred [is-clean? has-supported-remote?]) projects)
118+
119+
(->> clean-projects
120+
(filter #(not (has-supported-remote? %))))
121+
122+
(get-repo-remote (nth clean-projects 60))
123+
(has-supported-remote? (nth clean-projects 60))
124+
(-> clean-projects last has-supported-remote?)
125+
126+
(-> clean-projects last get-repo-remote)
79127
(def pulled (->> clean-projects
80128
(take 5)))
81129
pulled

0 commit comments

Comments
 (0)