Search code examples
gitgit-shell

Force git-shell username to overwrite given username


I have a bare git repository sitting on a server and a user with git-shell for ssh communication.

The problem is that I can't force the user name and user email when I push my commits on the server with that user.

I set up in the user's home ~/.gitshrc:

export GIT_AUTHOR_NAME="John Doe"
export GIT_COMMITTER_NAME="John Doe"

and also ~/.gitconfig file

[user]
    name = John Doe
    email = [email protected]

But all I got in the git log are the user name and user email set up client side.

How to rewrite user name and user email in git-shell?


Solution

  • The simple answer: you can't. When git pushes commits to a remote repo, it pushes it completely unchanged. The name of the committer is a part of the commit, so changing the name would change the commit.

    However you could

    1. force you clients to use only certain usernames
    2. or rewrite the commits to have certain usernames (using git filter-branch)

    The latter would be somewhat inconvenient for the users of the repo because they would fetch not what they've just pushed but technically it's possible. Nevertherless I would stay with simple control of the name on commit and push stages.

    If you're going with the former way, use a pre-receive git hook like this:

    #!/bin/sh
    
    while read oldrev newrev refname; do
        for commit in $(git rev-list $newrev --not $oldrev); do
            USER_NAME=$(git show -s --pretty=format:%an)
            USER_EMAIL=$(git show -s --pretty=format:%ae)
            COMMITTER_NAME=$(git show -s --pretty=format:%cn)
            COMMITTER_EMAIL=$(git show -s --pretty=format:%ce)
            # now perform checks you need and ...
            if <check-failed> ; then
                echo "Some explaining messages"
                exit 1
            fi
        done
    done