git-gerrit 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/sh
  2. #
  3. # Copyright 2014 Savoir-faire Linux Inc.
  4. # Author: Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  5. # Licensed under the terms of the GNU GPL v3, or at your option, any later version.
  6. #
  7. # You can install this script as a Git subcommand with one of the 2 methods below:
  8. #
  9. # 1) git config alias.gerrit '!tools/git-gerrit'
  10. # 2) or put this script in your $PATH
  11. usage () {
  12. echo "Usage:"
  13. echo " $0 <url|open|fetch> [<git-rev>]"
  14. echo " $0 help"
  15. echo
  16. echo "Examples:"
  17. echo " git gerrit open"
  18. echo " git gerrit url HEAD~2"
  19. echo " git gerrit fetch ; git diff FETCH_HEAD"
  20. }
  21. test $# -ge 1 -a $# -le 2 || {
  22. echo "Invalid syntax."
  23. usage
  24. exit 1
  25. }
  26. test "$1" = "help" && {
  27. usage
  28. exit
  29. }
  30. GERRIT_USER=`git config gerrit.user`
  31. GERRIT_HOST=`git config gerrit.host`
  32. GERRIT_PORT=`git config gerrit.port`
  33. test -n "$GERRIT_USER" -a -n "$GERRIT_HOST" -a -n "$GERRIT_PORT" || {
  34. echo "You must configure your Gerrit host, e.g.:"
  35. echo
  36. echo " git config gerrit.user vivien"
  37. echo " git config gerrit.host gerrit-ring.savoirfairelinux.com"
  38. echo " git config gerrit.port 29420"
  39. echo
  40. exit 1
  41. }
  42. alias _gerrit="ssh -p $GERRIT_PORT $GERRIT_HOST gerrit query"
  43. CHANGE_ID=`git show --summary --format=%b $2 | perl -n -e '/^Change-Id: (I[0-9a-f]+)$/ && print $1'`
  44. test -n "$CHANGE_ID" || {
  45. echo "no Change ID!"
  46. exit 1
  47. }
  48. test "$1" = "fetch" && {
  49. GERRIT_REMOTE=`git config gerrit.remote`
  50. test -n "$GERRIT_REMOTE" || {
  51. echo "You must specify the Git remote pointing to Gerrit, e.g.:"
  52. echo
  53. echo " git config gerrit.remote origin"
  54. echo
  55. exit 1
  56. }
  57. _gerrit --current-patch-set $CHANGE_ID | awk '/ref:/ { print $2 }' | while read ref ; do
  58. git fetch $GERRIT_REMOTE $ref:$ref
  59. done
  60. exit
  61. }
  62. URL=`_gerrit $CHANGE_ID | awk '/url:/ { print $2 }'`
  63. case $1 in
  64. url) echo $URL ;;
  65. open) xdg-open $URL ;;
  66. *) echo "Oops, bad command" ; usage ; exit 1 ;;
  67. esac
  68. exit