3

I have a "central" repository that I want to ensure that no one pushes changes in to with a wrong user name.

But I can not figure out how to make a hook that tests the user name against a positive list. I have found in the Mercurial API a

ctx.user()
call that seems to be what I want to test my positive list against.

Also the hook could be a precommit hook that is distributed as part of the repository clone or it could be a hook on the central repository as a pre-incoming or something like that.

Any help or pointers would be greatly appreciated.

place
  • 73
  • 6

2 Answers2

4

I have posted two functional examples on Bitbucket. Both examples are for searching a commit message for some specifically formatted text (like an issue tracked case ID), but could be easily modified to check a user against a list of valid users.

The first example is actually a Mercurial extension that wraps the 'commit' command. If it fails to find the appropriate text (or valid user in your case), it will prevent the commit from occurring at all. You can enable this in your .hgrc file by adding these lines:

[extensions]
someName = path/to/script/commit-msg-check.py

The second example uses a in-process pretxncommit hook, which runs between when the commit has been made, but before it becomes permanent. If this check fails it will automatically roll back the commit. You can enable this in your .hgrc file by adding these lines (assuming you kept the same file/function names):

[hooks]
pretxncommit.example = python:commit-msg-check-hook.CheckForIssueRecord

You can execute any Python code you like inside of these hooks, so user validation could be done in many ways.

dls
  • 4,146
  • 2
  • 24
  • 26
1

Thanks for the examples dls.

In the end I decided to run it as a pretxnchangegroup hook and then use the hg log and grep to test the author field of the commits:

[hooks]
pretxnchangegroup.usercheck = hg log --template '{author}\n' -r \ 
$HG_NODE: | grep -qe 'user1\|user2\|etc'

It does of course not provide a very good feedback other than usercheck failed. But I think it is good enough for now.

place
  • 73
  • 6