Add script to track a particular gerrit change
authorGan Shun <ganshun@gmail.com>
Fri, 28 Oct 2016 19:30:27 +0000 (12:30 -0700)
committerGan Shun <ganshun@gmail.com>
Mon, 31 Oct 2016 18:51:21 +0000 (11:51 -0700)
This script pulls the latest patch-set from gerrit for a particular
change and creates a branch

Signed-off-by: Gan Shun <ganshun@gmail.com>
Change-Id: I56120268935ecca38b978a8e519d5cab6430e70f

scripts/git/git-gerrit-track [new file with mode: 0755]

diff --git a/scripts/git/git-gerrit-track b/scripts/git/git-gerrit-track
new file mode 100755 (executable)
index 0000000..ceacb52
--- /dev/null
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+# Grab the latest patch set from gerrit
+# Change the following to match your remote name for gerrit.
+GERRIT_REMOTE=gerrit
+GERRIT_URL="https://akaros-review.googlesource.com/#/c/"
+
+if [ "$#" -ne 2 ]
+then
+       echo "USAGE: git gerrit-track number branch-name"
+       exit -1
+elif ! [[ $1 =~ ^[0-9]+$ ]]
+then
+       echo "Gerrit change number is not a number: $1"
+       exit -1
+fi
+
+BRANCHES=`git ls-remote --exit-code $GERRIT_REMOTE`
+
+if [ "$?" -ne 0 ]
+then
+       echo "Error occurred during ls-remote. Please make sure that your remote for gerrit exists, and that GERRIT_REMOTE in this script is pointing to the correct remote."
+       exit -1
+fi
+
+VERSION=`echo "$BRANCHES" | grep "refs/changes/.*/$1/[0-9]\+" | cut -f 5 -d '/' | sort -n | tail -1`
+BRANCH=`echo "$BRANCHES" | grep "refs/changes/.*/$1/$VERSION" | cut -f 2`
+echo "Gerrit remote branch is: $BRANCH"
+echo "The review can be found at: $GERRIT_URL$1/"
+git fetch gerrit $BRANCH
+git branch -f $2 FETCH_HEAD
+echo "The local branch is now at $2"
+