问题
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?
回答1:
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...
回答2:
#!/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
回答3:
#!/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
来源:https://stackoverflow.com/questions/48350468/how-to-configure-server-side-hooks-in-gerrit-2-16