git-gerrit 1.88 KB
Newer Older
1 2
#!/bin/sh
#
3
# Copyright 2014 Savoir-faire Linux Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# Author: Vivien Didelot <vivien.didelot@savoirfairelinux.com>
# Licensed under the terms of the GNU GPL v3, or at your option, any later version.
#
# You can install this script as a Git subcommand with one of the 2 methods below:
#
#   1) git config alias.gerrit '!tools/git-gerrit'
#   2) or put this script in your $PATH

usage () {
  echo "Usage:"
  echo "  $0 <url|open|fetch> [<git-rev>]"
  echo "  $0 help"
  echo
  echo "Examples:"
  echo "  git gerrit open"
  echo "  git gerrit url HEAD~2"
  echo "  git gerrit fetch ; git diff FETCH_HEAD"
}

test $# -ge 1 -a $# -le 2 || {
  echo "Invalid syntax."
  usage
  exit 1
}

test "$1" = "help" && {
  usage
  exit
}

GERRIT_USER=`git config gerrit.user`
GERRIT_HOST=`git config gerrit.host`
GERRIT_PORT=`git config gerrit.port`

test -n "$GERRIT_USER" -a -n "$GERRIT_HOST" -a -n "$GERRIT_PORT" || {
  echo "You must configure your Gerrit host, e.g.:"
  echo
  echo "  git config gerrit.user vivien"
Adrien Béraud's avatar
Adrien Béraud committed
42
  echo "  git config gerrit.host gerrit-ring.savoirfairelinux.com"
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  echo "  git config gerrit.port 29420"
  echo
  exit 1
}

alias _gerrit="ssh -p $GERRIT_PORT $GERRIT_HOST gerrit query"

CHANGE_ID=`git show --summary --format=%b $2 | perl -n -e '/^Change-Id: (I[0-9a-f]+)$/ && print $1'`

test -n "$CHANGE_ID" || {
  echo "no Change ID!"
  exit 1
}

test "$1" = "fetch" && {
  GERRIT_REMOTE=`git config gerrit.remote`

  test -n "$GERRIT_REMOTE" || {
    echo "You must specify the Git remote pointing to Gerrit, e.g.:"
    echo
    echo "  git config gerrit.remote origin"
    echo
    exit 1
  }

  _gerrit --current-patch-set $CHANGE_ID | awk '/ref:/ { print $2 }' | while read ref ; do
    git fetch $GERRIT_REMOTE $ref:$ref
  done
  exit
}

URL=`_gerrit $CHANGE_ID | awk '/url:/ { print $2 }'`

case $1 in
  url) echo $URL ;;
  open) xdg-open $URL ;;
  *) echo "Oops, bad command" ; usage ; exit 1 ;;
esac

exit