Last active
July 11, 2018 11:23
-
-
Save tankery/5bcda390d90c5617b3da3423020c875d to your computer and use it in GitHub Desktop.
Submit latest git commit
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
#!/bin/bash | |
# Submit latest git commit. | |
# Usage: | |
# submit-latest.sh [topic] | |
# | |
# Reset | |
Color_Off='\033[0m' | |
# Regular Colors | |
Color_Red='\033[0;31m' # Red | |
Color_Green='\033[0;32m' # Green | |
Color_Yellow='\033[0;33m' # Yellow | |
Color_Blue='\033[0;34m' # Blue | |
Color_Purple='\033[0;35m' # Purple | |
Color_Cyan='\033[0;36m' # Cyan | |
Color_White='\033[0;37m' # White | |
function echo_good { | |
echo -e "$Color_Green$1$Color_Off" | |
} | |
function echo_bad { | |
echo -e "$Color_Red$1$Color_Off" | |
} | |
function echo_warn { | |
echo -e "$Color_Yellow$1$Color_Off" | |
} | |
topic=$1 | |
latest_hash=$(git rev-parse HEAD) | |
branch=$(git name-rev --name-only HEAD) | |
remote=$(git config branch.$branch.remote) | |
function submit_push { | |
if [ -z $topic ]; then | |
remote_branch=refs/for/$branch | |
else | |
remote_branch=refs/for/$branch/$topic | |
fi | |
echo_good "Submit to $remote_branch" | |
git push $remote HEAD:$remote_branch | |
} | |
ahead=$(git rev-list --count HEAD...gerrit/master) | |
if [ $ahead -eq 0 ]; then | |
echo_good "Up to date, nothing to do" | |
exit 0 | |
fi | |
if [ $ahead -eq 1 ]; then | |
echo_good "Has 1 commit, submit $latest_hash" | |
submit_push | |
exit $? | |
fi | |
echo_good "Has $ahead commits, submit lastest commit $latest_hash" | |
git diff-index --quiet HEAD --; | |
has_change=$? | |
if [ $has_change -ne 0 ]; then | |
git stash | |
fi | |
function clean_up { | |
echo_good "Restore working environment" | |
git reset --hard $latest_hash | |
if [ $has_change -ne 0 ]; then | |
git stash pop | |
fi | |
} | |
echo_good "Pick commit to $remote/$branch" | |
git reset --hard $remote/$branch | |
git cherry-pick $latest_hash | |
if [ $? -ne 0 ]; then | |
echo_bad "Cherry pick failed, please submit manually." | |
git cherry-pick --abort | |
clean_up | |
exit 1 | |
fi | |
submit_push | |
clean_up | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment