Idan K
Idan K

Reputation: 20881

Prevent git push against remote repository when username of pusher is not configured

I want to prevent users from pushing to my repository when their usernames aren't set correctly. In practice I'd like this:

foreach commit in pushed_stuff:
   if not commit.username in some_list:
       reject push

The hook pre-receive seems appropriate but how do I extract the username from each commit object that is received? update seems to get the object names but from my understanding that happens when it's already in my repository (just before the ref has moved).

EDIT: This isn't meant to be some security mechanism to only allow certain people to push to me. I trust everyone, but sometimes people mess up and forget to configure their .gitconfig.

UPDATE: I had a problem with VonC's suggestion. When using pre-receive, if someone pushes a new branch the <old-value> is "000000000". So if he had made several commits to that new branch and he tries to push it, doing git rev-list ... $new only gives me one commit. But maybe other commits before it have had a bad username that I want to reject. I couldn't find a way to tell git to give me all the new commits. When the branch already exists doing git rev-list ... $old..$new does the job.

Upvotes: 1

Views: 875

Answers (1)

VonC
VonC

Reputation: 1323183

I believe this is addressed a bit in "Git/gitosis: How to check validity of user name and email?".

Now, the referenced SO question is about a security mechanism (which you don't want), but :

git log -1 --pretty=format:%ae $new

can help in a pre-receive hook, as suggested by Bombe in his answer.
%an or %aN might be more adapted to your case here.

This hook executes once for the receive operation. It takes no arguments, but for each ref to be updated it receives on standard input a line of the format:

<old-value> SP <new-value> SP <ref-name> LF

where:

  • <old-value> is the old object name stored in the ref,
  • <new-value> is the new object name to be stored in the ref and
  • <ref-name> is the full name of the ref.

When creating a new ref, <old-value> is 40 0.

Upvotes: 1

Related Questions