Last active
September 27, 2019 15:22
-
-
Save dzogrim/d833c12e9ccb9f3d0a0be846d5294210 to your computer and use it in GitHub Desktop.
Updates and clean private git local repositories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| NAME=$(basename "$0") | |
| # Customized colors | |
| export cRST='\033[0m' \ | |
| cRed='\033[31;1m' cDRed='\033[31m' cRedBG='\033[41;1m' cDRedBG='\033[41m' \ | |
| cGreen='\033[32;1m' cDGreen='\033[32m' cGreenBG='\033[42;1m' cDGreenBG='\033[42m' \ | |
| cBlue='\033[34;1m' cDBlue='\033[34m' cBlueBG='\033[44;1m' cDBlueBG='\033[44m' \ | |
| cYellow='\033[33;1m' cDYellow='\033[33m' cYellowBG='\033[43;1m' cDYellowBG='\033[43m' \ | |
| cMagenta='\033[35;1m' cDMagenta='\033[35m' cMagentaBG='\033[45;1m' cDMagentaBG='\033[45m' \ | |
| cCyan='\033[36;1m' cDCyan='\033[36m' cCyanBG='\033[46;1m' cDCyanBG='\033[46m' \ | |
| cWhite='\033[37;1m' cBright='\033[37;1m' cGrey='\033[37m' cGreyBG='\033[47m' \ | |
| cWhiteBG='\033[47;1m' cBrightBG='\033[47;1m' | |
| checkTools() | |
| { | |
| local misstools | |
| # Check presence of necessary tools | |
| for entry in "${@}" | |
| do | |
| if ! command -v "${entry}" >/dev/null 2>&1 | |
| then | |
| misstools="${misstools} ${entry}" | |
| fi | |
| done | |
| [ -z "${misstools}" ] \ | |
| && return \ | |
| || { | |
| printf "\n${cRed}* [ERROR]${cRST} ${NAME} requires tools that could not be found: \n ${misstools}" | |
| exit 1 | |
| } | |
| } | |
| Warning() | |
| { | |
| read -r -p "Are you sure you want to pull all the repos right now? (y/N) " RESP | |
| if [ "${RESP}" = "y" ]; | |
| then | |
| return | |
| else | |
| printf "\nExiting...\n" | |
| exit 1 | |
| fi | |
| } | |
| LOCK="/tmp/${NAME}.lock" | |
| # Check presence of required tools | |
| checkTools lockfile git | |
| Warning | |
| # lock | |
| lockfile -r 0 "${LOCK}" || exit 1 | |
| repo_base=$HOME/Documents/workspaces | |
| excluded_subdir="_repositories_pub" | |
| [[ ! -d ${repo_base} ]] && printf "\n [ERROR] Set base directory was not found.\n\n" && exit 1 | |
| find_opt="-mindepth 1 -maxdepth 1 -type d" | |
| repo_list="$( find ${repo_base} $find_opt -not -name ${excluded_subdir} -print0 | xargs -0 -n 1 basename | tr '\n' ' ' )" | |
| for i in ${repo_list}; do | |
| printf "\n${cGreen}For repository %s:${cRST}\n" "${i}" | |
| cd "${repo_base}/${i}" && git pull >/dev/null 2>&1 | |
| printf "Aggressively ${cYellow}optimize${cRST} the repository %s:\n" "${i}" | |
| git gc --aggressive | |
| # Remove any remote-tracking references that no longer exist on the remote repository before fetching | |
| printf "\n${cYellow}Sync${cRST} local registry of remote branches..." "${i}" | |
| # "git prune remote origin": | |
| git fetch -p >/dev/null 2>&1 | |
| printf "\n${cYellow}Remove${cRST} tracking branches no longer on remote for %s if any:\n" "${i}" | |
| git for-each-ref refs/heads --format='%(refname:short)' | grep -v -e "HEAD" -e "master" | xargs -n 1 -r git branch -D | |
| done | |
| # unlock | |
| rm -f "${LOCK}" || exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment