Created
February 22, 2013 13:31
-
-
Save chmouel/5013391 to your computer and use it in GitHub Desktop.
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 | |
REVIEW=$1 | |
GERRIT_USER=$(git config --get gitreview.username) | |
WHEREAMI=$(git rev-parse --abbrev-ref HEAD) | |
[[ -z ${WHEREAMI} ]] && { | |
echo "Cannot detect the current branch" | |
exit 1 | |
} | |
[[ -z ${GERRIT_USER} ]] && { | |
echo "you need to configure your gerrit user" | |
exit 1 | |
} | |
[[ -z ${REVIEW} ]] && { | |
echo "i need a review"; exit 1; | |
} | |
REF=$(ssh -n -x -p 29418 ${GERRIT_USER}@review.openstack.org gerrit query --format=JSON --current-patch-set change:${REVIEW} | sed -n '/"number"/ { s/.*ref.:.//;s/".*//;p;}') | |
[[ -z ${REF} || ${REF} != refs/changes/* ]] && { | |
echo "cannot find your branch reference." | |
exit 1; | |
} | |
CURRENT=${REF##*/} | |
if [[ ${CURRENT} -le 1 ]];then | |
echo "This is revision 1" | |
exit 1; | |
fi | |
LAST=$(($CURRENT-1)) | |
REF_OLD=${REF%/*} | |
REF_OLD="${REF_OLD}/${LAST}" | |
BRANCH_CURRENT=${REF//\/} | |
BRANCH_OLD=${REF_OLD//\/} | |
git fetch gerrit ${REF} | |
if $(git branch -l >/dev/null 2>/dev/null|grep -q ${BRANCH_CURRENT});then | |
git checkout ${BRANCH_CURRENT} FETCH_HEAD | |
git reset --hard FETCH_HEAD | |
else | |
git checkout -b ${BRANCH_CURRENT} FETCH_HEAD | |
fi | |
git fetch gerrit ${REF_OLD} | |
if $(git branch -l >/dev/null 2>/dev/null|grep -q ${BRANCH_OLD});then | |
git checkout ${BRANCH_OLD} FETCH_HEAD | |
git reset --hard FETCH_HEAD | |
else | |
git checkout -b ${BRANCH_OLD} FETCH_HEAD | |
fi | |
git diff --color ${BRANCH_OLD}..${BRANCH_CURRENT} | |
git checkout ${WHEREAMI} | |
git branch -D ${BRANCH_CURRENT} | |
git branch -D ${BRANCH_OLD} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment