Skip to content

Commit 3b24510

Browse files
authored
feat: functionality improvements to util script (#112)
pull-all-repos-in-directory enhancements: * -q flag to make it less noisy * -c flag so that it will do garbage collection and committed branch cleanup * -d flag so you can specify a top-level directory to run in without any of these flags, behavior should be unchanged.
1 parent 483c6f8 commit 3b24510

File tree

1 file changed

+80
-7
lines changed

1 file changed

+80
-7
lines changed

utils/pull-all-repos-in-directory.sh

100644100755
Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
#!/bin/bash
22

3-
################
3+
GIT_CLEANUP=0
4+
REPOSITORIES="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
5+
DIR=$REPOSITORIES
6+
QUIET_FLAG=""
7+
QUIET_REDIRECT=""
8+
UNCLEAN=()
9+
10+
usage()
11+
{
12+
echo "Usage: pull-all-repos-in-directory [ -d parent_dir ] [ -c ] [ -q ]"
13+
echo " -d: top level directory if not location of script"
14+
echo " parent_dir default: $REPOSITORIES"
15+
echo " -c: git garbage collect, merged branch cleanup, etc"
16+
echo " -q: make git be quiet"
17+
exit 2
18+
}
19+
20+
while getopts ":cqd:" opt; do
21+
case ${opt} in
22+
c )
23+
GIT_CLEANUP=1
24+
;;
25+
q )
26+
QUIET=1
27+
QUIET_FLAG="--quiet"
28+
# never redirect stderr
29+
QUIET_REDIRECT="> /dev/null"
30+
;;
31+
d )
32+
DIR=$OPTARG
33+
;;
34+
\? )
35+
usage
36+
;;
37+
esac
38+
done
439

540
sync_directory() {
641
IFS=$'\n'
@@ -9,19 +44,57 @@ sync_directory() {
944
do
1045
if [ -d "$1/$REPO" ]
1146
then
12-
echo "Updating $1/$REPO at `date`"
13-
if [ -d "$1/$REPO/.git" ]
47+
if [ -e "$1/$REPO/.git" ]
1448
then
15-
git -C "$1/$REPO" pull
49+
if [[ -z ${QUIET+x} ]]
50+
then
51+
echo "Updating $1/$REPO at `date`"
52+
else
53+
echo "$1/$REPO"
54+
fi
55+
cmd_flags=(-C "${1}/${REPO}")
56+
if ! git "${cmd_flags[@]}" pull ${QUIET_FLAG}
57+
then
58+
UNCLEAN+=($REPO)
59+
fi
60+
if [ $GIT_CLEANUP = 1 ]
61+
then
62+
git ${cmd_flags[@]} gc ${QUIET_FLAG}
63+
git ${cmd_flags[@]} remote prune origin
64+
master_branch=$( git "${cmd_flags[@]}" symbolic-ref --short refs/remotes/origin/HEAD|cut -f2 -d'/' )
65+
git -C "${1}/${REPO}" checkout -q ${master_branch}
66+
for branch in $( git "${cmd_flags[@]}" for-each-ref refs/heads/ "--format=%(refname:short)")
67+
do
68+
mergeBase=$( git "${cmd_flags[@]}" merge-base ${master_branch} $branch)
69+
rev=$( git "${cmd_flags[@]}" rev-parse "$branch^{tree}" )
70+
committree=$( git "${cmd_flags[@]}" commit-tree ${rev} -p $mergeBase -m _ )
71+
if [[ $( git "${cmd_flags[@]}" cherry ${master_branch} ${committree} ) == "-"* ]]
72+
then
73+
git -C "${1}/${REPO}" branch -D ${branch}
74+
fi
75+
$( git "${cmd_flags[@]}" checkout -q ${master_branch} )
76+
done
77+
if [[ -z ${QUIET+x} ]]
78+
then
79+
echo "Done at `date`"
80+
fi
81+
fi
1682
else
1783
sync_directory "$1/$REPO"
1884
fi
19-
echo "Done at `date`"
2085
fi
2186
done
2287
}
2388

24-
REPOSITORIES="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2589

26-
sync_directory $REPOSITORIES
90+
if [[ ! -z ${QUIET+x} && ${GIT_CLEANUP} -eq 1 ]]
91+
then
92+
echo "WARNING: with both '-q' and '-c' the slow garbage collection on edx-platform might appear to hang. It will be fine."
93+
fi
94+
sync_directory $DIR
2795

96+
if [[ ${#UNCLEAN[@]} -gt 0 ]]
97+
then
98+
echo "The following directories weren't in clean state and couldn't be updated:"
99+
echo ${UNCLEAN[@]}
100+
fi

0 commit comments

Comments
 (0)