Skip to content

Instantly share code, notes, and snippets.

@axelson
Created December 19, 2012 20:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save axelson/4340197 to your computer and use it in GitHub Desktop.
Save axelson/4340197 to your computer and use it in GitHub Desktop.
Ref-updated gerrit script to send emails via the mail command when commits are pushed directly to gerrit. Install into the gerrit/hooks directory and make sure it is executable. You may want to change the "gitblit.rams" url to point to your own source code viewing website (such as a gitblit server). This has been tested on Gerrit 2.5
#!/bin/bash
#ref-updated --oldrev <old rev> --newrev <new rev> --refname <ref name> --project <project name> --submitter <submitter>
OLD_REV="$2"
NEW_REV="$4"
REF_NAME="$6"
PROJECT_NAME="$8"
SUBMITTER="${10}"
LOG="/tmp/log.txt"
date >> $LOG
echo "args: $@" >> $LOG
echo "old rev $OLD_REV" >> $LOG
echo "new rev $NEW_REV" >> $LOG
echo "ref name $REF_NAME" >> $LOG
echo "project name $PROJECT_NAME" >> $LOG
echo "submitter $SUBMITTER" >> $LOG
NAME="$SUBMITTER"
BRANCH="$REF_NAME"
NUM_COMMITS=$(git log $OLD_REV..$NEW_REV --oneline|wc -l)
SUBJECT="$NAME pushed $NUM_COMMITS commit(s) to $BRANCH on project $PROJECT_NAME"
BODY="Insert summary of commits"
TEAM1_EMAILS="stacy@example.org"
TEAM2_EMAILS="t@example.org alice@example.org"
ADMIN_EMAILS="bob@example.org"
case $PROJECT_NAME in
project1*)
EMAIL_LIST="$TEAM1_EMAILS"
;;
project2*)
EMAIL_LIST="$TEAM2_EMAILS"
;;
*)
EMAIL_LIST="$ADMIN_EMAILS"
;;
esac
echo "Subject is $SUBJECT" >> $LOG
#for commit in $(git rev-list $OLD_REV..$NEW_REV); do
# echo "on commit sha1 $commit" >>$LOG
#done
# Example gitblit URL:
# http://gitblit.rams/commit/hello-world.git/87926995aa1c64225520f0620972df92f6df6879
BODY=$(git shortlog $OLD_REV..$NEW_REV --format="%s - http://gitblit.rams/commit/$PROJECT_NAME.git/%h")
echo "Body is $BODY" >> $LOG
# Send the email!
echo "$BODY" | mail -s "$SUBJECT" $EMAIL_LIST
echo >> $LOG
@scm20008
Copy link

scm20008 commented Jul 21, 2017

Hi, have you wrote ref-update hook for gerrit in windows?

I have a ref-update.bat hook, and in ref-update.bak I call/run the python script, then it disappears an error when I push changes to gerrit.
the python script can be run, because I can get the log from the python script.

If I don't call/run python script or others scripts, the ref-update.bak works. why?
Tips: the gerrit version is 2.13
Do you have any idea? thanks.

2017-07-21_111457

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment