Skip to content

Instantly share code, notes, and snippets.

@diffficult
Forked from armeenm/statusgit
Created May 16, 2018 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diffficult/eea52eed67c63cc4f961e1f10a45f1b9 to your computer and use it in GitHub Desktop.
Save diffficult/eea52eed67c63cc4f961e1f10a45f1b9 to your computer and use it in GitHub Desktop.
Update all git repositories recursively.
#!/bin/bash
declare -a COLORS
j=0
# Default colors are light for dark terminals.
# Use '-l' option for normal (darker) text.
# Red, green, yellow, blue, magenta, cyan.
for i in {91..96}; do COLORS[$j]=$i; let "j++"; done
usage() {
echo -e "Usage: $0 [OPTIONS] [DIR]" >&2
echo -e "Recursively returns status for Git repos.\nFLAGS:" >&2
echo -e " -h\t--help Show this help message.\n"
echo -e " -l\t--light-background Change colors for light background terminals.\n"
echo -e " -c\t--no-color No colors.\n"
}
for FLAG in "$@"; do
if [[ $FLAG == -* ]]; then
case $FLAG in
'-l'|'--light-background')
j=0
for i in {31..36}; do COLORS[$j]=$i; let "j++"; done
;;
'-c'|'--no-color')
for i in {0..5}; do COLORS[$i]=0; done
;;
'-h'|'--help')
usage
exit 0
;;
*)
echo -e "\e["${COLORS[0]}";1mInvalid option: $FLAG\e[0m\n" >&2
usage
exit 1
;;
esac
else
DIR=$(readlink -f "$FLAG")
CDERR=$(cd "$DIR" 2>&1 >/dev/null)
if (grep -iq "permission" <<< $CDERR); then
echo -e "\e["${COLORS[0]}";1mFailed, permision denied.\e[0m\n" >&2
usage
exit 1
elif (grep -iq "no such" <<< $CDERR); then
echo -e "\e["${COLORS[0]}";1mFailed, no such file or directory \""$DIR"\".\e[0m\n" >&2
usage
exit 1
fi
fi
done
if [[ -z $DIR ]]; then
DIR="$PWD"
fi
echo -e "\n\e["${COLORS[5]}";1mSearching for Git repositories...\e[0m\n"
REPOLIST=$(find "$DIR" -name ".git" -type d 2>/dev/null)
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
REPOCOUNT=0
for REPO in $REPOLIST; do
let "REPOCOUNT++"
done
IFS=$SAVEIFS
if [ $REPOCOUNT -eq 0 ]; then
echo -e "\e["${COLORS[0]}";1mNo repos found!\e[0m\n" >&2
exit 1
fi
if [ $REPOCOUNT -eq 1 ]; then
echo -e "\e["${COLORS[3]}";1mFound 1 repository!\e[0m"
else
echo -e "\e["${COLORS[3]}";1mFound $REPOCOUNT repositories!\e[0m"
fi
for REPO in $REPOLIST; do
REPO=${REPO%/*}
BASEREPO=$(basename "$REPO")
echo -e "\n\e["${COLORS[5]}"mStatus for repository "$BASEREPO":\e[0m"
GITOUT=$(git -C "$REPO" status)
ERRORCODE=$?
if [ $ERRORCODE -eq 0 ] && ! $(grep -iq "nothing to commit, working directory clean" <<< "$GITOUT"); then
echo "$GITOUT"
echo -e "\e["${COLORS[1]}"mSuccessfully updated!\e[0m"
elif [ $ERRORCODE != 0 ]; then
echo "$GITOUT"
echo -e "\n\e["${COLORS[0]}"mFailed to get status!\e[0m"
FAILURE+="\n$BASEREPO"
else
echo -e "\e["${COLORS[2]}"mAlready up-to-date.\e[0m"
fi
done
echo -e "\n\e["${COLORS[1]}";1mComplete!\e[0m\n"
if [[ -n $FAILURE ]]; then
echo -e "\e["${COLORS[0]}";1mFailed to get status for:\e[0m$FAILURE\n"
exit 1
fi
#!/bin/bash
CONFIRM=true
declare -a COLORS
j=0
# Default colors are light for dark terminals.
# Use '-l' option for normal (darker) text.
# Red, green, yellow, blue, magenta, cyan.
for i in {91..96}; do COLORS[$j]=$i; let "j++"; done
usage() {
echo -e "Usage: $0 [OPTIONS] [DIR]" >&2
echo -e "Recursively updates Git repos.\nFLAGS:" >&2
echo -e " -h\t--help Show this help message.\n"
echo -e " -y\t--noconfirm No confirmation.\n"
echo -e " -q\t--quiet Suppresses stdout and invokes '-y'.\n"
echo -e " -l\t--light-background Change colors for light background terminals.\n"
echo -e " -c\t--no-color No colors.\n"
}
for FLAG in "$@"; do
if [[ $FLAG == -* ]]; then
case $FLAG in
'-l'|'--light-background')
j=0
for i in {31..36}; do COLORS[$j]=$i; let "j++"; done
;;
'-c'|'--no-color')
for i in {0..5}; do COLORS[$i]=0; done
;;
'-y'|'--noconfirm')
CONFIRM=false
;;
'-q'|'--quiet')
exec 2>&1 >/dev/null
CONFIRM=false
;;
'-h'|'--help')
usage
exit 0
;;
*)
echo -e "\e["${COLORS[0]}";1mInvalid option: $FLAG\e[0m\n" >&2
usage
exit 1
;;
esac
else
DIR=$(readlink -f "$FLAG")
CDERR=$(cd "$DIR" 2>&1 >/dev/null)
if (grep -iq "permission" <<< $CDERR); then
echo -e "\e["${COLORS[0]}";1mFailed, permision denied.\e[0m\n" >&2
usage
exit 1
elif (grep -iq "no such" <<< $CDERR); then
echo -e "\e["${COLORS[0]}";1mFailed, no such file or directory \""$DIR"\".\e[0m\n" >&2
usage
exit 1
fi
fi
done
if [[ -z $DIR ]]; then
DIR=$PWD
fi
echo -e "\n\e["${COLORS[5]}";1mSearching for Git repositories...\e[0m\n"
REPOLIST=$(find "$DIR" -name ".git" -type d 2>/dev/null)
SAVEIFS=$IFS
OTHERIFS=$(echo -en "\n\b")
IFS=$OTHERIFS
REPOCOUNT=0
for REPO in $REPOLIST; do
let "REPOCOUNT++"
done
IFS=$SAVEIFS
if [ $REPOCOUNT -eq 0 ]; then
echo -e "\e["${COLORS[0]}";1mNo repos found!\e[0m\n" >&2
exit 1
fi
while : ; do
if $CONFIRM; then
if [ $REPOCOUNT -eq 1 ]; then
echo -e "\e["${COLORS[3]}";1m1 repository will be updated, continue (Y/n/l)?\e[0m"
else
echo -e "\e["${COLORS[3]}";1m$REPOCOUNT repositories will be updated, continue (Y/n/l)?\e[0m"
fi
read ANSWER
ANSWER=${ANSWER,,}
else
if [ $REPOCOUNT -eq 1 ]; then
echo -e "\e["${COLORS[3]}";1mFound 1 repository!\e[0m"
else
echo -e "\e["${COLORS[3]}";1mFound $REPOCOUNT repositories!\e[0m"
fi
ANSWER="yes"
fi
if [[ $ANSWER = y* ]] || [[ $ANSWER == "" ]]; then
IFS=$OTHERIFS
for REPO in $REPOLIST; do
REPO=$(dirname "$REPO")
BASEREPO=$(basename "$REPO")
echo -e "\n\e["${COLORS[5]}"mUpdating repository "$BASEREPO"...\e[0m"
IFS=$SAVEIFS
GITOUT=$(git -C "$REPO" pull)
ERRORCODE=$?
if [ $ERRORCODE -eq 0 ] && ! $(grep -iq "Already up-to-date" <<< "$GITOUT"); then
echo "$GITOUT"
echo -e "\e["${COLORS[1]}"mSuccessfully updated!\e[0m"
elif [ $ERRORCODE != 0 ]; then
echo "$GITOUT"
echo -e "\n\e["${COLORS[0]}"mFailed to update!\e[0m"
FAILURE+="\n$BASEREPO"
else
echo -e "\e["${COLORS[2]}"mAlready up-to-date.\e[0m"
fi
done
echo -e "\n\e["${COLORS[1]}";1mComplete!\e[0m\n"
if [[ -n $FAILURE ]]; then
echo -e "\e["${COLORS[0]}";1mFailed to update:\e[0m$FAILURE\n"
exit 1
fi
exit 0
elif [[ $ANSWER = n* ]]; then
exit 0
elif [[ $ANSWER = l* ]]; then
IFS=$OTHERIFS
for REPO in $REPOLIST
do
REPO=$(dirname "$REPO")
basename "$REPO"
done
IFS=$SAVEIFS
else
echo -e "\e["${COLORS[0]}";1mInvalid entry.\e[0m\n"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment