]> njoseph.me Git - babashka-scripts.git/blob - git-pull-all
git-pull-all: Fix failure on non-git directories
[babashka-scripts.git] / git-pull-all
1 #! /usr/bin/env bb
2 ; -*- mode: clojure -*-
3
4 ; Runs `git pull` on all the git repositories in a directory
5
6 (require '[babashka.process :as p] '[clojure.java.io :as io])
7
8 (def default-root ".")
9
10 (defn has-git-repo
11 [dir]
12 (first (filter #(= ".git" %)
13 (map #(.getName %)
14 (filter #(.isDirectory %) (.listFiles (io/file dir)))))))
15
16 (defn list-dirs
17 [root]
18 (filter #(has-git-repo %)
19 (filter #(.isDirectory %) (.listFiles (io/file root)))))
20
21 (defn git-pull [dir] (p/process ["git" "-C" dir "pull" "--rebase"]))
22
23 (when (= *file* (System/getProperty "babashka.file"))
24 (let [root (get (into [] *command-line-args*) 0 default-root)
25 dirs (list-dirs root)
26 pulls (->> root
27 list-dirs
28 (map git-pull)
29 doall)
30 outputs (map #(->> %
31 p/check
32 :out
33 slurp)
34 pulls)]
35 ;; Print corresponding directory name when pulling
36 ;; Skip printing already up to date repos
37 (doseq [[dir out] (filter #(not (.contains (second %) "is up to date."))
38 (map vector dirs outputs))]
39 (println (str dir "\n" out)))))