2

I have started using Gerrit 2.16 as code review tool and want to configure server side hooks to validate the git commit message when a change is committed/pushed to gerrit.

Tried using hooks by copying scripts to $GIT_DIR/hooks (scripts like ref-update, patchset-created, change-merged), gave permission on the gerrit server but nothing works.

commit-msg hook can be enabled on local repository by using command give in gerrit UI

example: git clone ssh://@:29418/Project1 && scp -p -P 29418 @:hooks/commit-msg /.git/hooks/

change_ID will be automatically generated if this hook is enabled.

This script commit-msg gets downloaded to local repository when above command is executed.
My question; can we find path of this script on the gerrit server, so that I can modify and enforce git commit message validation?

Or is there any other way to enable gerrit server side hooks?

Chenmunka
  • 685
  • 4
  • 21
  • 25
cloudops
  • 31
  • 1
  • 5

3 Answers3

4

No, you will not find the commit-msg path on the Gerrit server and Git/Gerrit will not use any hook you put on $GIT_DIR/hooks automatically. If you want to have local hooks you will need to manually install them in the REPOSITORY/.git/hooks local directory.

Gerrit does not run any of the standard git hooks in the repositories it works with, but it does have its own hook mechanism included via the Hooks plugin. See here more info about the supported Gerrit hooks. The Hooks plugin is a core plugin (it's packaged within the Gerrit war file and can easily be installed during the Gerrit initialization).

I suggest you to take a look at the Git::Hooks (a Perl framework for implementing Git/Gerrit hooks). We use at our company and it's really great. You can use it to implement what you want and a lot more...

  • Thanks for the answer Marcelo Ávila de Oliveira. To add further in Gerrit in gerrit 2.14 , the behavior of gerrit hook is changed as per below documentaion https://www.gerritcodereview.com/releases/2.14.md#Behavior-change-in-ref_update-hook, commit-received is the file name that gets executed from the directory /hooks on the gerrit server, when git commit is executed from git client. so commit-received can be a simple shellscript to validate the git commit messages. – cloudops Jan 22 '18 at 15:59
  • Hello Marcelo Ávila de Oliveira, I have installed the GIT:Hooks from cpan, trying to find how can we implement gerrit server side plugin to validate commit messages in GERRIT_DIR (/opt/gerrrit/gerrit_site): – cloudops Jan 22 '18 at 22:24
  • cd /opt/gerrit/gerrit_site/git/Project_Example.git; cat config [core] repositoryformatversion = 0 filemode = true bare = true logallrefupdates = true [githooks] plugin = CheckLog nocarp = 1 [githooks "CheckLog"] title-required = 1 title-max-width = 60 body-max-width = 80 the gerrit trigger is still not invoked, when commit is executed, am I missing anything here ? – cloudops Jan 22 '18 at 22:30
  • Have you created the "patchset-created" hook at "GERRIT-SITE/hooks" directory (Configuration step 1 at https://stackoverflow.com/questions/46100437/how-to-install-config-githooks-to-use-with-gerrit)? – Marcelo Ávila de Oliveira Jan 23 '18 at 15:19
  • I am using Gerrit 2.16, For me only one hook is working, i.e commit-received. no other hook is working, below link also says same: https://www.gerritcodereview.com/releases/2.14.md#Behavior-change-in-ref_update-hook Behavior change in ref-update hook Instead of being invoked on every commit received, the ref-update hook is now invoked before the ref update operation is finalized. The previous behavior of the ref-update hook is moved into a new hook named commit-received.Sites using the ref-update hook should rename the hook file to commit-received. – cloudops Jan 23 '18 at 17:42
  • this is working if I keep commit-received file in GERRIT_DIR/hooks, how do I create project specific hooks? example: GERRIT_DIR/git/project123.git/hooks/commit-received what should be the content of file in GERRIT_DIR/hooks/commit_received file, so that it redirects the hook to a project specific hook – cloudops Jan 23 '18 at 17:45
  • You can't do that on Gerrit. All hooks stay on GERRIT_DIR/hooks general directory, you create specific project configurations setting different GERRIT_DIR/git/project123.git/config files, as said in Configuration step 2 at https://stackoverflow.com/questions/46100437/how-to-install-config-githooks-to-use-with-gerrit. – Marcelo Ávila de Oliveira Jan 23 '18 at 23:10
1
#!/bin/bash
echo "Executing hook from Gerrit_DIR "
bold=$(tput bold)
normal=$(tput sgr0)
RED='\033[0;31m'
NC='\033[0m' # No Color
GIT_DIR="/opt/gerrit/site/git"
PROJECT=$2
REFNAME=$4
UPLOADER=$6
OLDREV=$8
NEWREV="${10}"
BASE="<baseDir>"
RepoDir="$GIT_DIR/$PROJECT.git"
echo $RepoDir
echo $PROJECT
echo $8
echo ${10}


# execute the project specific hook
if [ -f "$RepoDir/hooks/commit-received" ]
    then
    echo "Executing the project specific hook"
    $RepoDir/hooks/commit-received $RepoDir ${10}
else
    echo "There is no project specific hook"
fi
cloudops
  • 31
  • 1
  • 5
  • not sure about the latest version of Gerrit, with 2.16 I was able to make this working: the above script is from /hooks/commit-received – cloudops Jan 24 '18 at 16:39
  • nice solution, perhaps you could merge your answers into one post? – gybandi Sep 26 '19 at 05:14
0
#!/bin/bash
echo "Executing hook for patchset"
bold=$(tput bold)
normal=$(tput sgr0)
RED='\033[0;31m'
NC='\033[0m' # No Color
RepoDir=$1
NEWREV=$2
MSG=$(git --git-dir=$RepoDir log --format=%B -n 1 $NEWREV | awk -F"Change-Id:" '{print $1}')

Val=`echo $MSG | cut -c1-3`
if  [ $Val == "TR_" ] || [ $Val == "CR_" ] || [ $Val == "PU_" ]  || [ $Val == "FR_" ] || [ $Val == "CSR" ]
    then
    echo "The commit message is valid"
    exit 0

else    
    echo -e "The commit message ${RED}\"$MSG\"${NC} is not valid, please enter a valid commit message"
    exit 1
fi
cloudops
  • 31
  • 1
  • 5
  • Place the above script in project speficic hooks directory GERRIT_DIR/git//hooks/commit-received. This is working fine. – cloudops Jan 24 '18 at 16:42