-
Notifications
You must be signed in to change notification settings - Fork 4
/
git-all
executable file
·61 lines (53 loc) · 2.11 KB
/
git-all
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
#=======================================================================================================================
# Description
# Run a git command for all available repositories, starting from $CODE_ROOT_DIR.
# NB: it ignores all directories whose name starts with a '_' and everything below them unless you supply '.' as the
# first argument (see below).
# Arguments
# $1 - if '.' is passed as first argument, search starts from the current dir instead of $CODE_ROOT_DIR and
# directories starting with '_' are not ignored
# $@ - Any arguments to be passed to git
#=======================================================================================================================
# shellcheck source=./common.sh
. "$(dirname "$(realpath "$0")")/common.sh"
CODE_ROOT_DIR="$HOME/code"
USAGE_INFO="Usage: $0 <git_command> [arg...]"
if [[ "$1" == '.' ]]; then
shift
find_cmd="find \"$PWD\" -type d -name .git"
else
# Ignore dirs starting with '_'
find_cmd="find \"$CODE_ROOT_DIR\" -type d -name .git ! -path '*/_*'"
fi
# The first argument must be a git command
cmd="$1"
shift
[[ -z "$cmd" ]] && usage "No git command specified"
# Change the pipe behaviour to allow modifying vars in a subshell (https://github.com/koalaman/shellcheck/wiki/SC2031)
shopt -s lastpipe
# Iterate through all known repositories
declare -a err_repos=()
eval $find_cmd |
xargs dirname |
while read -r dir; do
log "Running ${CLR_OK}git $cmd${CLR_OFF} in $(dirname "$dir")/${CLR_VAR}$(basename "$dir")${CLR_OFF}"
git -C "$dir" "$cmd" "$@"
rc=$?
if ((rc != 0)); then
error "git returned non-zero exit code $rc"
err_repos+=("$dir")
fi
log '--------------------------------------------------------------------------------'
done
# If there was any failure, output a list of failed repositories
if ((${#err_repos[@]})); then
echo
error "✘ Failed in the following repos:"
for dir in "${err_repos[@]}"; do
echo -e "* $(dirname "$dir")/${CLR_VAR}$(basename "$dir")${CLR_OFF}"
done
exit 1
fi
ok "✔ Succeeded"
exit 0