Skip to content

Instantly share code, notes, and snippets.

@delphym
Last active July 29, 2021 02:15
Show Gist options
  • Save delphym/33ad3dbe74a7f76edef88c2595abba58 to your computer and use it in GitHub Desktop.
Save delphym/33ad3dbe74a7f76edef88c2595abba58 to your computer and use it in GitHub Desktop.
GIT Repos update and bulk checkout
#!/bin/bash
changed=`tempfile -p changed -s .txt` # For macOs, see: https://stackoverflow.com/q/19408005/2234369
nochanges=`tempfile -p nochanges -s .txt`
updated=`tempfile -p updated -s .txt`
cantupd=`tempfile -p cantupd -s .txt`
notonAKAdev=`tempfile -p notonAKAdev -s .txt`
switchedto=`tempfile -p switchedto -s .txt`
failedtoswitch=`tempfile -p failedtoswitch -s .txt`
# Setting default values
BRANCH_TO_BRANCH_OFF="master" ### probably the ideal value: "dev"
BRANCH_AKA_MASTER="dev" ### probably the ideal value: "master"
REPO_ROOT_DIR="${HOME}/SRC/TRIMBLE/BSG/"
FORCE_CHECKOUT_OF_REFBRANCH="no"
CLEAN_ECLIPSE_NOISE="no"
#Print acceptable params
helpme() {
echo
echo "`basename $0` help page"
echo "Parameters might be:...and their default values are:"
echo "----------------------------------------------------"
echo -n "-refBranch ${refBranch}";
echo " ...the branch to branch off. The reference branch.";
echo -n "-forceCheckout ${forceCheckout}";
echo " ...force checkout of the reference branch.";
echo -n "-mBranch ${mbranch}";
echo " ...the branch which should be treated as Master regardless of its name.";
echo -n "-repoRoot ${repoRoot}";
echo " ...top level directory of your nested repositories. Only one level.";
echo -n "-cleanEclipseNoise ${cleanEclipseNoise}";
echo " ...restores Eclipse modified its metadata files.";
echo "*******************************************************************************"
echo "************* !!! DO NOT ENCLOSE VALUES IN DOUBLE QUOTES !!!! ***************"
echo "*******************************************************************************"
echo "Example call:"
echo 'BSG_reposUpdate.sh -refBranch "master" -mBranch "dev" -repoRoot "~/SRC/TRIMBLE/BSG-V2" -forceCheckout "Yes"'
echo
exit 1
} #helpme()
#Parse various arguments
parse_args() {
echo "#########################################################################"
echo "######################### `basename $0` ############################"
echo "#########################################################################"
while [ $# -gt 0 ] ; do
echo "PROCESSING ARGs ...: '$1'"
case "$1" in
-refBranch) shift; if [ $# -gt 0 ] ; then BRANCH_TO_BRANCH_OFF=$1; fi;;
-mBranch) shift; if [ $# -gt 0 ] ; then BRANCH_AKA_MASTER=$1; fi;;
-repoRoot) shift; if [ $# -gt 0 ] ; then REPO_ROOT_DIR=$1; fi;;
-forceCheckout) shift; if [ $# -gt 0 ] ; then FORCE_CHECKOUT_OF_REFBRANCH=$1; fi;;
-cleanEclipseNoise) shift; if [ $# -gt 0 ] ; then CLEAN_ECLIPSE_NOISE=$1; fi;;
-h|--help|-?|/?|/h) echo "DONE.";helpme;
esac
shift
done
echo " -refBranch: ${BRANCH_TO_BRANCH_OFF}";
echo " -mBranch: ${BRANCH_AKA_MASTER}";
echo " -repoRoot: ${REPO_ROOT_DIR}";
echo " -forceCheckout: ${FORCE_CHECKOUT_OF_REFBRANCH}"
echo " -cleanEclipseNoise: ${CLEAN_ECLIPSE_NOISE}"
echo "DONE."
} #parse_args()
forceSwitchBranch() {
## Read those articleas about passing multi words arguments
# * https://stackoverflow.com/questions/9484364/how-to-substitute-quoted-multi-word-strings-as-arguments
# * http://mywiki.wooledge.org/BashFAQ/050
# * https://www.grymoire.com/Unix/Quote.html
## TODO fix those arguments
echo -e "arg#1: $1; \targ#2: $2"
if [ -z "${2}" ]; then
echo -e "Status: unclean"
fi
if [ "${1}" -ne 0 ]; then
echo -e "There are differences: $1"
fi
if [ ${FORCE_CHECKOUT_OF_REFBRANCH} = "Yes" ]; then
echo -e "\n\tTrying to switch to ${BRANCH_TO_BRANCH_OFF}"
git checkout ${BRANCH_TO_BRANCH_OFF}
if [ $? -eq 0 ]; then
echo -e "Repo: " + $d + " \t\tswitched from a branch: $local_branch to $BRANCH_TO_BRANCH_OFF" >> $switchedto
git pull --all
if [ $? -eq 0 ]; then
echo -e "Repo: " + $d + " \t\t on a switched branch: $BRANCH_TO_BRANCH_OFF \t\t is unclean" >> $updated
else
echo -e "Repo: " + $d + " \t\ton a switched branch: $BRANCH_TO_BRANCH_OFF" >> $cantupd
fi
else
echo -e "Repo: " + $d + " \tfailed to switch from $local_branch to $BRANCH_TO_BRANCH_OFF branch" >>$failedtoswitch
fi
fi
} #forceSwitchBranch
cleanEclipseNoise() {
git restore .classpath &> /dev/null
git restore .project &> /dev/null
git restore .sts4-cache &> /dev/null
git restore .settings/org.eclipse.wst.common.component &> /dev/null
git restore .settings/org.eclipse.m2e.core.prefs &> /dev/null
git restore .settings/org.eclipse.jdt.core.prefs &> /dev/null
} #doCleanEclipseNoise
# Process arguments given on the command line.
parse_args "$@"
echo
if [ ${FORCE_CHECKOUT_OF_REFBRANCH} = "Yes" ]; then
echo "Are you sure you want to force checkout this branch: ${BRANCH_TO_BRANCH_OFF} on update?"
select yn in "Yes" "No"; do
case $yn in
Yes) echo "Will proceed your request then..."; break;;
No) echo "Will exit now. If you change your mind try it again."; exit 2;;
esac
done
fi
echo "############### Processing of your request began. ##################"
cd ${REPO_ROOT_DIR}
echo "Repositories where there are local changes:" > $changed
echo "Repositories which successfully been updated:" > $updated
echo "Repositories without local changes:" > $nochanges
echo "Repositories failed to update:" > $cantupd
echo "Repositories not on ${BRANCH_TO_BRANCH_OFF} branch:" > $notonAKAdev
echo "Repositories switched to ${BRANCH_TO_BRANCH_OFF}:" > $switchedto
echo "Repositories failed to switch to ${BRANCH_TO_BRANCH_OFF}:" > $failedtoswitch
for d in *; do
cd $d; &> /dev/null
local_branch=$(git rev-parse --abbrev-ref HEAD)
echo -en "Repo: " + $d + " \t\ton a branch: $local_branch"
if [ $local_branch != $BRANCH_TO_BRANCH_OFF -a $local_branch != $BRANCH_AKA_MASTER -a $local_branch != "trimble-tsunami-master-on-demand" ]; then
echo -e "Repo: " + $d + " \t\t on a branch: \t $local_branch" >> $notonAKAdev
fi
[[ ${CLEAN_ECLIPSE_NOISE} = "Yes" ]] && cleanEclipseNoise
exit_status="$(git status --porcelain)"
if [ -z "${exit_status}" ]; then
exit_code=$(git diff --exit-code &> /dev/null; echo $?)
if [ $exit_code -eq 1 ]; then
echo -e "\n\tThere are local changes in:\n\t$d, ... attempting to update"
[ $d_temp ! = $d ] && echo -e "Repo: " + $d + " \t\ton a branch: $local_branch" >> $changed
git pull --all
if [ $? -eq 0 ]; then
echo -e "Repo: " + $d + " \t\t on a branch: $local_branch \t\t is unclean" >> $updated
else
echo -e "Repo: " + $d + " \t\ton a branch: $local_branch" >> $cantupd
fi
forceSwitchBranch $exit_code "${exit_status}"; # force branch switch
fi
git pull --all
if [ $? -eq 0 ]; then
echo -e "\tNo changes - ready to update"
echo -e "Repo: " + $d + " \t\t on a branch: $local_branch" >> $nochanges
else
echo -e "Repo: " + $d + " \t\ton a branch: $local_branch" >> $cantupd
fi
forceSwitchBranch $exit_code "${exit_status}"; # force branch switch
echo -e "Repo: " + $d + " \t\t on a branch: $local_branch" >> $updated
else
#echo -e "\e[33;1m There are local changes \e[0m"
echo -e "\n\tThere are local changes here, attempting to update..."
echo -e "Repo: " + $d + " \t\ton a branch: $local_branch" >> $changed
d_temp=$d
git pull --all
if [ $? -eq 0 ]; then
echo -e "Repo: " + $d + " \t\t on a branch: $local_branch \t\t is unclean" >> $updated
else
echo -e "Repo: " + $d + " \t\ton a branch: $local_branch" >> $cantupd
fi
exit_code=$(git diff --exit-code &> /dev/null; echo $?)
forceSwitchBranch $exit_code "${exit_status}"; # force branch switch
fi
git fetch --all;
cd - &> /dev/null;
done
cd - &> /dev/null;
echo "##############################################################"
cat $updated
echo "=============================================================="
cat $changed
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
cat $cantupd
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
cat $nochanges
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
cat $notonAKAdev
if [ ${FORCE_CHECKOUT_OF_REFBRANCH} = "Yes" ]; then
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
cat $switchedto
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
cat $failedtoswitch
fi
echo "##############################################################"
rm $updated
rm $changed
rm $cantupd
rm $nochanges
rm $notonAKAdev
rm $switchedto
rm $failedtoswitch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment