git: Add git-pull-all
[shutils.git] / git / git-pull-all
1 #!/bin/sh
2
3 # A script to recursively pull git repositories behind origin.
4
5 # Copyright (c) 2022 Stefan Huber
6 #
7 # Permission is hereby granted, free of charge, to any person
8 # obtaining a copy of this software and associated documentation
9 # files (the "Software"), to deal in the Software without
10 # restriction, including without limitation the rights to use,
11 # copy, modify, merge, publish, distribute, sublicense, and/or sell
12 # copies of the Software, and to permit persons to whom the
13 # Software is furnished to do so, subject to the following
14 # conditions:
15 #
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 # OTHER DEALINGS IN THE SOFTWARE.
27
28 set -e
29 set -u
30
31 function showHelp
32 {
33 echo "Usage: $0 [-h|--help]"
34 echo " $0 [OPTIONS]"
35 echo ""
36 echo "OPTIONS:"
37 echo " -a, --ask ask user for each repo"
38 echo " -h, --help show this message"
39 echo " -n, --dry-run do not actually perform pull"
40 }
41
42 if ! options=$(getopt -u -o ahn -l ask,help,dry-run -- "$@"); then
43 showHelp >&2
44 exit 1
45 fi
46 set -- $options
47
48 optDryRun=0
49 optAsk=0
50
51 #Parse the arguments
52 while [ $# -gt 0 ]; do
53 case "$1" in
54 -h|--help) showHelp; exit 0 ;;
55 -n|--dry-run) optDryRun=1 ;;
56 -a|--ask) optAsk=1 ;;
57 --) shift; break ;;
58 -*) echo "Unrecognized option $1" >&2; showHelp >&2; exit 1 ;;
59 *) break ;;
60 esac
61 shift
62 done
63
64 for REPODIR in $(find "$@" -name .git -type d 2> /dev/null); do
65
66 REPO=$(dirname ${REPODIR})
67 echo "Run ${REPO}…"
68
69 if [ "${optAsk}" = 1 ]; then
70 read -p " Process [y/N]? " -n1 response
71 echo ""
72
73 [ "${response}" = "y" ] || continue
74 fi
75
76 git -C ${REPO} remote update
77
78 if [ -z "$(git -C ${REPO} status -uno | grep 'Your branch is behind')" ] ; then
79 echo -e "\e[1;32m Already up to date\e[0m"
80 else
81 [ "${optDryRun}" = 1 ] && continue
82 git -C ${REPO} pull
83 fi
84
85 done