Skip to content

Instantly share code, notes, and snippets.

@kdambekalns
Last active August 20, 2020 05:15
Show Gist options
  • Save kdambekalns/630971458dfb3bd2d6e7 to your computer and use it in GitHub Desktop.
Save kdambekalns/630971458dfb3bd2d6e7 to your computer and use it in GitHub Desktop.
Gerrit commit message check hook
#!/bin/bash
BASE="/home/gerrit/review/git"
while [ "$1" != "" ]; do
case $1 in
--change) CHANGE=$2; shift;;
--project) PROJECT=$2; shift;;
--branch) BRANCH=$2; shift;;
--uploader) UPLOADER=$2; shift;;
--commit) COMMIT=$2; shift;;
--patchset) PATCHSET=$2; shift;;
esac
shift;
done
cd "$BASE/$PROJECT.git"
if ! [ -f "HEAD" ]; then
echo "No bare git repository"
exit 0
fi
# Check for WIP marker
if [[ $(git show --pretty=format:%s --no-patch $COMMIT) =~ ^(\[!!!\])?\[WIP\] ]]; then
ssh localhost gerrit review --code-review -2 -m '"WIP changes should not be merged, follow up to be expected"' $COMMIT
exit 0
fi
# Check commit message subject length (max 80 characters)
long_subject=$(git show --pretty=format:%s --no-patch $COMMIT | egrep -m 1 '.{81}')
if [ -n "$long_subject" ]; then
ssh localhost gerrit review --code-review -2 -m '"Commit message subject over 80 characters"' $COMMIT
exit 0
fi
# Check commit message marker
if ! [[ $(git show --pretty=format:%s --no-patch $COMMIT) =~ ^(\[!!!\])?(\[WIP\])?\[(FEATURE|BUGFIX|SECURITY|TASK)\] ]]; then
ssh localhost gerrit review --code-review -2 -m '"Commit message does not start with correct marker
Possible markers:
[!!!] Optional, marks a breaking change
[WIP] Optional, marks a change as Work in Progress. Will automatically get a -2 do not submit review to prevent publish
<TYPE> Required, shows the type of commit. <TYPE> is one of the following:
[FEATURE]
A new feature is introduced by this change
[BUGFIX]
A bug is fixed by this change
[TASK]
Everything not fitting into one of the above
[SECURITY]
A security related change. Only to be used by active contributors!"' $COMMIT
exit 0
fi
# Check space and capital character after the marker
if ! [[ $(git show --pretty=format:%s --no-patch $COMMIT) =~ ^(\[[A-Z!]{3,}\]){1,3}\ [A-Z] ]]; then
ssh localhost gerrit review --code-review -2 -m '"The commit message marker should be followed by 1 space and a capital character (A-Z)"' $COMMIT
exit 0
fi
# Check empty line after subject
secondLine=$(git show --no-patch $COMMIT | sed -n '6p')
if [[ $secondLine =~ [^[:space:]] ]]; then
ssh localhost gerrit review --code-review -2 -m '"The second line of the commit message must be blank"' $COMMIT
exit 0
fi
# Check for Releases in footer
if ! [[ $(git show --no-patch --pretty=format:%b $COMMIT) =~ Releases: ]]; then
ssh localhost gerrit review --code-review -2 -m '"Missing releases line in commit message footer.
Download the commit-msg hook using scp -p -P 29418 review.typo3.org:hooks/commit-msg .git/hooks/"' $COMMIT
exit 0
fi
# Check for Change-Id in footer
if ! [[ $(git show --no-patch --pretty=format:%b $COMMIT) =~ Change-Id: ]]; then
ssh localhost gerrit review --code-review -2 -m '"Missing Change-Id.
Download the commit-msg hook using scp -p -P 29418 review.typo3.org:hooks/commit-msg .git/hooks/"' $COMMIT
exit 0
fi
ssh localhost gerrit review --code-review +1 -m '"Commit message valid"' $COMMIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment