Enforce the custom template commit message in Gerrit

637 views
Skip to first unread message

Mohan S

unread,
Jul 20, 2018, 12:49:32 AM7/20/18
to Repo and Gerrit Discussion

My Environment:

Gerrit: 2.15.2

User Side: We are using Git (On Windows & Linux).

Currently for required Gerrit projects we upload our custom commit template message hook and request our developers to create symbolic link to their local .git/hooks/ folder. This is method working fine.

I am looking a best way to configure the custom template commit message for each commit by default in gerrit itself? Since commit(s) are local machine based seeking your inputs to go with this. Kindly advise.

Gert van Dijk

unread,
Jul 20, 2018, 4:04:32 AM7/20/18
to Repo and Gerrit Discussion
On Friday, 20 July 2018 06:49:32 UTC+2, Mohan S wrote:

Currently for required Gerrit projects we upload our custom commit template message hook and request our developers to create symbolic link to their local .git/hooks/ folder. This is method working fine.

I am looking a best way to configure the custom template commit message for each commit by default in gerrit itself? Since commit(s) are local machine based seeking your inputs to go with this. Kindly advise.


I think you are asking for a way in Gerrit to run a commit-msg hook on push/review time, right? Commit messages are constructed on the client side in Git and Gerrit does/should not change them on-push time in my opinion (commit hash will change, commit relation chains will be far from obvious, etc.). The only case that I know of that Gerrit will adjust your commit message is on-submit time with any of the submit types that rebases/cherry-picks and two of them will even adjust some trailer lines in the message.

Having that said, I can think of the following options to consider:
  • Create a CI rule that checks the commit message (just like you would do a code style check). In case you're using Jenkins, you can set up a single job that triggers on changes in all repositories for example.
  • Create a server-side patchset-created hook that validates the message the same way as the aforementioned approach, but then using scripts without CI. This hook is called after the patch set has been accepted. More information via the hooks plugin documentation (<your-gerrit-url/>plugins/hooks/Documentation/hooks.md)
  • Similar as above, you might be able to do some checks using the commit-received hook, but since this is called before the commit is available in Gerrit and I don't see the commit message being passed to it, I think this won't help, but I haven't looked at it extensively.
  • Make it easier to not forget to install the existing commit hook: either use 'core.hooksPath' or 'init.templatedir' git global configurations on the clients. This is not a solution if you don't trust your clients and need a server-side check. It will only drop the need of having to create that symlink all the time (protip: if using init.templatedir, use symlinks in that template dir or else you will get a copy and it's hard to change it in all places).
In case you were looking for a way to rewrite/adjust the commit message on-submit time, you can do this using a plugin; see this thread for a similar question: https://groups.google.com/d/msg/repo-discuss/h7YtcILsPTU/vCAcHIoLBAAJ

HTH

Mohan S

unread,
Jul 20, 2018, 9:11:30 AM7/20/18
to Repo and Gerrit Discussion
Hi Gert, Thanks for the clue. Yes right once users pushed their changes from local machine git. Gerrit need to validate our commit message. If user did not follow the commit message Gerrit has to set -1. As per your suggestion I have installed Gerrit Plugin in our CI-Jenkins.Configred Gerrit Server connection, Now connection status shows in green colour.I have created CI-jenkins freestyle Job and configred Gerrit event part. But with improper commit message the change pushed to gerrit. But Build did not triggered.

So do I need to enable any other settings. Plz advise me.

Mohan S

unread,
Jul 23, 2018, 8:50:39 AM7/23/18
to Repo and Gerrit Discussion
Thanks team for the perfect direction. I have completed the same through our CI-Jenkins.
Reply all
Reply to author
Forward
0 new messages