Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Parallel git pulls
#!/usr/bin/env bash
superpull() {
dir=$1
error_repos=$2
cd $dir
STATUS=`mktemp`
echo -e "\n************************************\n* Pulling $dir\n************************************" >$STATUS
git checkout master 1>>$STATUS 2>&1
if [[ $? != 0 ]]; then
# we probably have uncommitted changes
set -x
echo -e "\t$(tput setaf 1)${dir}$(tput sgr0)" >>"$error_repos" 2>&1
echo -e "$(tput setaf 1)$(cat $STATUS)$(tput sgr0)"
set +x
else
git pull 1>>$STATUS 2>&1
if [[ $? != 0 ]]; then
# pull failure; most likely because origin has been deleted
echo -e "\t$(tput setaf 1)${dir}$(tput sgr0)" >>"$error_repos" 2>&1
echo -e "$(tput setaf 1)$(cat $STATUS)$(tput sgr0)"
else
# re-colorize pull output
# match repeating '+' or '-' at EOL, and the dash in '(-)'
sed -i '' "s/[+]/$(tput setaf 2)&$(tput sgr0)/g" $STATUS
sed -i '' "s/-*$/$(tput setaf 1)&$(tput sgr0)/g" $STATUS
sed -i '' "s/(-)/\($(tput setaf 1)-$(tput sgr0)\)/g" $STATUS
cat "$STATUS"
git fetch -p
fi
fi
rm -f $STATUS
}
export -f superpull
error_repos=`mktemp`
pushd ~/git/basedir 1>/dev/null 2>&1
find -L $base -type d -depth 1 | xargs -P20 -I{} bash -c "superpull {} $error_repos"
test -s "$error_repos" && echo -e "\n$(tput setaf 1)*** Repos with errors: ***$(tput sgr0)" && cat $error_repos
rm -f $error_repos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment