馃敡 Where my dotfiles lives in harmony and peace, most of the time
1#!/usr/bin/env bash
2# pull-all-dirs - Pull git repositories in all subdirectories that are behind upstream
3# Usage: pull-all-dirs [directory]
4
5set -euo pipefail
6
7target_dir="${1:-.}"
8
9if [[ ! -d "$target_dir" ]]; then
10 printf 'Error: Directory "%s" does not exist\n' "$target_dir" >&2
11 exit 1
12fi
13
14cd "$target_dir"
15
16updated_file="$(mktemp)"
17trap 'rm -f "$updated_file"' EXIT
18
19process_repo() {
20 local dir=$1
21
22 [[ -d "$dir/.git" ]] || return 0
23
24 if ! git -C "$dir" remote update >/dev/null 2>&1; then
25 printf 'Warning: could not update remotes for "%s"\n' "$dir" >&2
26 return 1
27 fi
28
29 if git -C "$dir" status -uno | grep -q "Your branch is behind"; then
30 printf 'Pulling updates for: %s\n' "$dir"
31 if git -C "$dir" pull --ff-only; then
32 printf '%s\n' "$dir" >>"$updated_file"
33 else
34 printf 'Error: failed to pull updates for "%s"\n' "$dir" >&2
35 return 1
36 fi
37 fi
38}
39
40pids=()
41
42for dir in */; do
43 [[ -d "$dir" ]] || continue
44 dir=${dir%/}
45 process_repo "$dir" &
46 pids+=("$!")
47done
48
49exit_status=0
50
51for pid in "${pids[@]}"; do
52 if ! wait "$pid"; then
53 exit_status=1
54 fi
55done
56
57if [[ ! -s "$updated_file" ]]; then
58 echo "All repositories are up to date."
59else
60 echo "Updated repositories:"
61 sort -u "$updated_file" | sed 's/^/ - /'
62fi
63
64exit "$exit_status"