Created by
Ben Buchanan
last modified
| #!/bin/bash
ORIGIN=$(git remote | grep -i origin)
if [[ ! $ORIGIN ]];then
echo "Remote named 'origin' is required."
exit
fi
# if there's a remote called "upstream" presume we're on a fork
ONFORK=$(git remote | grep -i upstream)
# get branch name
BRANCH=$(git rev-parse --abbrev-ref HEAD)
spacer() {
echo "------------------------------------------------------------------------------------------"
}
echoAndRun() {
echo "$1"
$1
spacer
}
gitmaintenance() {
# gc calls prune as well https://git-scm.com/docs/git-gc
echoAndRun "git gc"
}
checkoutMaster() {
echoAndRun "git checkout master"
}
checkoutLastBranch() {
echoAndRun "git checkout -"
}
pullUpstreamMaster() {
echoAndRun "git pull upstream master"
}
pullMaster() {
echoAndRun "git pull origin master"
}
pushOriginMaster() {
echoAndRun "git push origin master"
}
offerToMergeFromMaster() {
read -r -p "Merge from master? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
echoAndRun "git merge master"
fi
}
spacer
if [ "$ONFORK" ]
then
echo "On a fork"
echo "Upstream remote detected. Will update fork and push to origin master."
spacer
if [ "$BRANCH" == "master" ]; then
pullUpstreamMaster
gitmaintenance
pushOriginMaster
else
checkoutMaster
pullUpstreamMaster
gitmaintenance
pushOriginMaster
checkoutLastBranch
offerToMergeFromMaster
fi;
else
if [ "$BRANCH" == 'master' ]; then
echo "On master"
echoAndRun "git pull"
gitmaintenance
else
echo "On a branch"
checkoutMaster
pullMaster
gitmaintenance
checkoutLastBranch
offerToMergeFromMaster
fi;
fi
|