Skip to content

Instantly share code, notes, and snippets.

@VitaliyR
Created February 10, 2016 14:07
Show Gist options
  • Save VitaliyR/ffd1297f45b2b935d509 to your computer and use it in GitHub Desktop.
Save VitaliyR/ffd1297f45b2b935d509 to your computer and use it in GitHub Desktop.
Update all git repositories recursively in current directory to provided branch
# Use gitcheckout -h to see available flags
function gitcheckout {
local defaultBranch='master'
local branch
local foundBranch
local force=false
local update=true
local verbose=false
local exclude
local errors=''
local output
while [ ! $# -eq 0 ]
do
case "$1" in
-f)
force=true
;;
-u)
update=false
;;
-d)
defaultBranch=d
;;
-e)
exclude=$2
shift
;;
-v)
verbose=true
;;
-h)
echo 'Usage: -f[reset hard] -u[disable fetch] -d[default branch:master] -e[exclude regexp] -v[verbose log] -h[print help] BRANCH_NAME'
return
;;
*)
branch=$1
;;
esac
shift
done
echo 'Searching for repositories'
local cwd=$(pwd)
local directories=$(find . -name '.git' -type d)
if [ -z $branch ]; then
branch=$defaultBranch
fi
echo ''
echo -e '\x1B[4mUpdating repositories to' $branch '\x1B[24m'
echo -e '\x1B[4mForce\x1B[24m:' $force ' \x1B[4mFetch\x1B[24m:' $update ' \x1B[4mDefault branch\x1B[24m:' $defaultBranch '\x1B[24m'
if [ ! -z "$exclude" ]; then
echo -e '\x1B[4mExclude\x1B[24m:' $exclude
fi
echo ''
for REPO in $directories
do
cd $REPO/../
local repoPWD=$(pwd)
if [ ! -z "$exclude" ] && [[ $repoPWD =~ $exclude ]]; then
echo -e '\x1B[93mSkipping' ${PWD##*/} '\x1B[0m'
echo ''
echo ''
cd $cwd
continue
fi
echo -e '\x1B[4mUpdating' ${PWD##*/}'\x1B[24m'
echo ''
if [ $force == 'true' ]; then
output=$(git reset --hard 2>&1)
else
output=$(git reset 2>&1)
fi
if [ $verbose == 'true' ]; then
echo -e "$output"
fi
if [ $update == 'true' ]; then
output=$(git fetch 2>&1)
if [ $? -ne 0 ]; then
echo -e "$output"
errors+=$(echo 'Fetching\x1B[1m' ${PWD##*/} '\x1B[0m\n')
else
if [ $verbose == 'true' ]; then
echo -e "$output"
fi
fi
fi
foundBranch=$(git branch -r | grep -w -E '^. (origin/'$branch')$')
if [ -n "$foundBranch" ]; then
echo -e '\x1B[92mFound' $branch '\x1B[39m'
output=$(git checkout $branch 2>&1)
else
echo -e '\x1B[91mNot found' $branch 'Updating to' $defaultBranch '\x1B[39m'
output=$(git checkout $defaultBranch 2>&1)
fi
if [ $? -ne 0 ]; then
echo -e "$output"
errors+=$(echo 'Checkout\x1B[1m' ${PWD##*/} '\x1B[0m\n')
else
if [ $verbose == 'true' ]; then
echo -e "$output"
fi
fi
if [ $update == 'true' ]; then
output=$(git pull 2>&1)
if [ $? -ne 0 ]; then
echo -e "$output"
errors+=$(echo 'Pull\x1B[1m' ${PWD##*/} '\x1B[0m\n')
else
if [ $verbose == 'true' ]; then
echo -e "$output"
fi
fi
fi
echo ''
cd $cwd
done
if [ ! -z "$errors" ]; then
echo -e '\x1B[91m\x1B[4mErrors with repositories:\x1B[24m\x1B[39m'
echo -e "$errors"
fi
}
function cliocheckout {
cd ~/hostroot
gitcheckout -e 'Packages\/((Framework)|(Libraries))' $@
cd flow
./flow doctrine:update
./flow translation:generatejson
cd ../clioui
grunt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment