Created
May 16, 2019 16:45
-
-
Save gilesdring/ac5a69564f75723e53d9d51571305122 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
set -e | |
repos=$(find . -name .git -a -type d -exec dirname {} \;) | |
eachRepo() { | |
local f | |
f=$* | |
for repo in ${repos}; do | |
pushd ${repo} > /dev/null | |
$f | |
popd > /dev/null | |
done | |
} | |
line() { | |
echo ============================================================================================================================= | |
} | |
header() { | |
line | |
echo 'Repo:' $(pwd) | |
} | |
footer() { | |
cat - <<FOOTEREND | |
$(line) | |
FOOTEREND | |
} | |
listRemotes() { | |
local r | |
for r in $(git remote); do | |
echo $r $(git remote get-url $r) | |
done | |
} | |
fixUpstream() { | |
local origin | |
origin=$(listRemotes | matchRemote 'gitlab.nonprod.dwpcloud.uk' | awk '{ print $1 }') | |
echo $origin | |
} | |
checkoutBranch() { | |
git checkout $1 || echo " > No ${1} branch" | |
} | |
pullBranch() { | |
git pull || echo " >> Can't pull branch " currentBranch | |
} | |
currentBranch() { | |
git branch -q | awk '$1 ~ /\*/ {print $2}' | |
} | |
matchRemote() { | |
local test | |
test=$1 | |
awk -v test="${test}" '$2 ~ test { print }' | |
} | |
ppBranch() { | |
awk '{ printf " Branch: %s\n", $1 }' | |
} | |
ppRemotes() { | |
awk '{ printf " Remote: %-15s -> %s\n", $1, $2}' | |
} | |
ppIndent() { | |
awk '{ printf " >> %s\n", $0}' | |
} | |
function report { # Report status of a git repo | |
header | |
currentBranch | ppBranch | |
git status | ppIndent | |
listRemotes | ppRemotes | |
footer | |
} | |
function update { # Update the git repo: pull develop and master branch, fetch all branches | |
local currentBranch | |
currentBranch=$(currentBranch) | |
header | |
echo ' Fetching all' | |
git fetch --all --tags --verbose | |
echo ' Updating develop and master' | |
checkoutBranch master && fixUpstream && pullBranch | |
checkoutBranch develop && fixUpstream && pullBranch | |
footer | |
checkoutBranch ${currentBranch} | |
} | |
# Maintenance tasks (ideally idempotent!) | |
function fixPermissions { # Fix permissions in the git repo | |
header | |
git diff -p -R --no-color | grep -E "^(diff|(old|new) mode)" --color=never | git apply || true | |
footer | |
} | |
function fixNonprodRemotes { # Rename old format remote repositories to new format | |
local old new borkedRemotes | |
old=gitlab.itsshared.net | |
new=gitlab.nonprod.dwpcloud.uk | |
borkedRemotes=$(listRemotes | matchRemote ${old} | sed s/${old}/${new}/) | |
header | |
[ -z "$borkedRemotes" ] && echo 'Nothing to do!' || git remote set-url ${borkedRemotes} | |
footer | |
} | |
function removeAwsRemotes { # Remove AWS remoted | |
local aws awsRemotes | |
aws=git.mgmt.health-dev.dwpcloud.uk | |
awsRemotes=$(listRemotes | matchRemote ${aws} | awk '{print $1}') | |
[ -z "${awsRemotes}" ] || { | |
header | |
git remote remove ${awsRemotes} | |
footer | |
} | |
} | |
function help { # Display this help | |
cat ${0} | awk ' | |
/^function/ { printf(" %-20s | %s\n"), $2, substr($0, index($0, "# ") + 2) } | |
' | |
} | |
action=$* | |
[ -z "${action}" ] && action='report' | |
eachRepo $action |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment