Skip to content
Browse files

Add (some) support for using tracking branches other than master.

  • Loading branch information...
1 parent 0ce73b0 commit 07b30b57e932461c584280934bafcfadc512af67 Brian Donovan committed Oct 2, 2009
Showing with 47 additions and 22 deletions.
  1. +47 −22 bin/git-review
View
69 bin/git-review
@@ -110,16 +110,17 @@ printUsage() {
echo " $this assists in code reviews with gerrit, both pushing or pulling changesets to review."
echo
echo " push [<change number>]"
- echo " Pushes a single patch to gerrit for review. If your HEAD is master, $this will"
- echo " reset master to origin/master after successfully pushing the changeset. If you are"
- echo " working on a topic branch, that branch will be left alone."
+ echo " Pushes a single patch to gerrit for review. If your HEAD is a tracking branch"
+ echo " $this will reset it to the remote branch after successfully pushing the changeset."
+ echo " If you are working on a non-tracking branch, that branch will be left alone."
echo
echo " Given a change number, the current patch will replace the existing one, following"
echo " the same reset rules as above."
echo
echo " <change number>"
- echo " Applies the latest patch for the change at <change number> on top of master on a"
- echo " branch named for <change number>, allowing you to review the change."
+ echo " Applies the latest patch for the change at <change number> on top of the current"
+ echo " branch, if it's a tracking branch, or master on a branch named for <change number>,"
+ echo " allowing you to review the change."
echo
echo " update"
echo " Updates the review branch that you're currently on to the latest patch."
@@ -142,8 +143,25 @@ printUsage() {
}
+getBranchInfo() {
+ REMOTE=`$GIT config branch.$CURRENT.remote`
+
+ if [ -z "$REMOTE" ]; then
+ REMOTE=origin
+ fi
+
+ TRACK=`$GIT config branch.$CURRENT.merge`
+
+ if [ -z "$TRACK" ]; then
+ TRACK=refs/heads/master
+ fi
+
+ TRACK=${TRACK#refs/heads/}
+}
+
pushReview() {
abort_on_bad_repo_state
+ getBranchInfo
local change=$1
if [ -z "$change" ]; then
@@ -152,8 +170,8 @@ pushReview() {
fi
fi
- local LOG=$($GIT log --pretty="format:%s" origin/master..HEAD)
- local CHANGESETS=$($GIT rev-list origin/master..HEAD | wc -l)
+ local LOG=$($GIT log --pretty="format:%s" $REMOTE/$TRACK..HEAD)
+ local CHANGESETS=$($GIT rev-list $REMOTE/$TRACK..HEAD | wc -l)
local CHANGESETS=${CHANGESETS//[[:space:]]}
if [ "$run" = true ]; then
@@ -164,18 +182,22 @@ pushReview() {
fi
fi
- if [ "$change" != "" ]; then
- local change_args="origin HEAD:refs/changes/$change"
+ local change_args=''
+
+ if [ -z "$change" ]; then
+ change_args="$REMOTE HEAD:refs/for/$TRACK"
+ else
+ change_args="origin HEAD:refs/changes/$change"
fi
git push $change_args ||
die "Could not push your changes for review. Perhaps you need to update first?"
git_push_output="$git_output"
- if [ "$CURRENT" = "master" ]; then
- git reset --hard origin/master ||
- die "Could not reset your master branch to origin/master."
+ if [ "$CURRENT" = "$TRACK" ]; then
+ git reset --hard $REMOTE/$TRACK ||
+ die "Could not reset your $TRACK branch to $REMOTE/$TRACK."
fi
if [ "$run" = true ]; then
@@ -185,8 +207,8 @@ pushReview() {
eval "$BROWSER $url"
fi
- if [ "$CURRENT" = "master" ]; then
- good "Successfully pushed '$LOG' for review and reset master"
+ if [ "$CURRENT" = "$TRACK" ]; then
+ good "Successfully pushed '$LOG' for review and reset $TRACK"
else
good "Successfully pushed '$LOG' for review"
fi
@@ -196,6 +218,7 @@ pushReview() {
# git review 100
startReview() {
abort_on_bad_repo_state
+ getBranchInfo
$GIT remote show changes 2>/dev/null 1>&2 ||
die "In order to use $this to start a review, you need a 'changes' remote. Please add it before trying again."
@@ -213,15 +236,15 @@ startReview() {
die "Unable to find a patch for ±$change"
fi
- # create a new branch based on master to work in
+ # create a new branch based on the current tracking branch to work in
local branch=r$change
$GIT branch -D $branch 2>/dev/null 1>&2
- git checkout -b $branch master ||
+ git checkout -b $branch $TRACK ||
die "Unable to create a new branch '$branch'"
# apply the change to the new branch
git cherry-pick $patchref ||
- die "Unable to apply ±$change on top of master"
+ die "Unable to apply ±$change on top of $TRACK"
if [ "$run" = true ]; then
good "Successfully cherry-picked ±$change so you can begin reviewing it"
@@ -249,11 +272,13 @@ approveChange() {
local host=${host_and_project%:*}
local project=${host_and_project#*:}
+ getBranchInfo
+
get_change_number_from_branch ||
die "Unable to get change number from $CURRENT"
local PATCH=$(patch_count_for_change $CHANGE)
- local LOG=$($GIT log --pretty="format:%s" origin/master..HEAD)
+ local LOG=$($GIT log --pretty="format:%s" $REMOTE/$TRACK..HEAD)
good "Approving '$LOG'..."
ssh $host gerrit approve \
--verified=+1 \
@@ -292,16 +317,16 @@ diffPatches() {
local branch2="r$change,$patch2"
# create a branch for patch1
- $GIT checkout -b $branch1 master >/dev/null 2>&1 ||
+ $GIT checkout -b $branch1 $TRACK >/dev/null 2>&1 ||
die "Unable to create a new branch '$branch1'"
$GIT cherry-pick $patchref1 >/dev/null 2>&1 ||
- die "Unable to apply ±$branch1 on top of master"
+ die "Unable to apply ±$branch1 on top of $TRACK"
# create a branch for patch2
- $GIT checkout -b $branch2 master >/dev/null 2>&1 ||
+ $GIT checkout -b $branch2 $TRACK >/dev/null 2>&1 ||
die "Unable to create a new branch '$branch2'"
$GIT cherry-pick $patchref2 >/dev/null 2>&1 ||
- die "Unable to apply ±$branch2 on top of master"
+ die "Unable to apply ±$branch2 on top of $TRACK"
# actually show the diff
$GIT diff --src-prefix=$branch1: --dst-prefix=$branch2: --color $branch1 $branch2 | less -rf

0 comments on commit 07b30b5

Please sign in to comment.
Something went wrong with that request. Please try again.