akaros/scripts/git/git-gerrit-track
<<
>>
Prefs
   1#!/bin/bash
   2
   3# Grab the latest patch set from gerrit
   4# Change the following to match your remote name for gerrit.
   5GERRIT_REMOTE=gerrit
   6GERRIT_URL="https://akaros-review.googlesource.com/#/c/"
   7
   8if [ "$#" -ne 2 ]
   9then
  10        echo "USAGE: git gerrit-track number branch-name"
  11        exit -1
  12elif ! [[ $1 =~ ^[0-9]+$ ]]
  13then
  14        echo "Gerrit change number is not a number: $1"
  15        exit -1
  16fi
  17
  18BRANCHES=`git ls-remote --exit-code $GERRIT_REMOTE`
  19
  20if [ "$?" -ne 0 ]
  21then
  22        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."
  23        exit -1
  24fi
  25
  26VERSION=`echo "$BRANCHES" | grep "refs/changes/.*/$1/[0-9]\+" | cut -f 5 -d '/' | sort -n | tail -1`
  27BRANCH=`echo "$BRANCHES" | grep "refs/changes/.*/$1/$VERSION" | cut -f 2`
  28echo "Gerrit remote branch is: $BRANCH"
  29echo "The review can be found at: $GERRIT_URL$1/"
  30git fetch gerrit $BRANCH
  31git branch -f $2 FETCH_HEAD
  32echo "The local branch is now at $2"
  33
  34