]> njoseph.me Git - babashka-scripts.git/blame - git-pull-all
git-pull-all: Fix failure on non-git directories
[babashka-scripts.git] / git-pull-all
CommitLineData
fa20f4c6 1#! /usr/bin/env bb
57e618f2 2; -*- mode: clojure -*-
fa20f4c6
JN
3
4; Runs `git pull` on all the git repositories in a directory
5
0b0f2aa3 6(require '[babashka.process :as p] '[clojure.java.io :as io])
fa20f4c6
JN
7
8(def default-root ".")
fa20f4c6 9
269ff235
JN
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)))))
0b0f2aa3
JN
20
21(defn git-pull [dir] (p/process ["git" "-C" dir "pull" "--rebase"]))
fa20f4c6
JN
22
23(when (= *file* (System/getProperty "babashka.file"))
0b0f2aa3
JN
24 (let [root (get (into [] *command-line-args*) 0 default-root)
25 dirs (list-dirs root)
fa20f4c6
JN
26 pulls (->> root
27 list-dirs
28 (map git-pull)
29 doall)
0b0f2aa3
JN
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)))))