0

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 = johndoe@example.com

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?

  • Can you post what `git config --list` returns? – karatedog Dec 03 '15 at 11:23
  • 1
    Does the following 2 commands work on terminal ? ; $ git config --global user.name "John Nash" $ git config --global user.email johndoe@example.com – Nishant Singh Dec 03 '15 at 11:23
  • @karatedog server side: git config --list user.name=John Doe user.email=johndoe@example.com client side: user.name=Not the real one user.email=some_email@example.com –  Dec 03 '15 at 12:49
  • @NishantSingh yes, that's what I used to set the options in the gitconfig file –  Dec 03 '15 at 12:51

2 Answers2

0

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
user3159253
  • 16,836
  • 3
  • 30
  • 56
0

Thanks to user user3159253 and this other question Can git pre-receive hooks evaulate the incoming commit? I managed to do the following whitelist filter:

#!/bin/bash
#
#   Git pre-receive hook for validating commits authorship
#   with linux account full name format : Firstname FULLNAME <email@address>
#
#   Adapted from blacklist https://github.com/spuder/git-hooks/blob/master/pre-commit
#

# Extract full linux user name and email address
realName=$(getent passwd `whoami`| cut -d ':' -f 5 | cut -d ',' -f 1 |cut -d '<' -f 1|cut -d' ' -f1,2)
realEmail=$(getent passwd `whoami`| cut -d ':' -f 5 | cut -d ',' -f 1 |cut -d '<' -f 2|cut -d'>' -f1)

check_user() {
     if [[ "$1" != "$realName" ]]; then
      echo "You are commiting $sha1 as $2 $1 user which I don't like. Reveal your identity $realName!"
      exit 1
     fi
}

check_email() {
     if [[ "$1" != "$realEmail" ]]; then
      echo "You are commiting with $2 email $1 which I don't like. Reveal your email $realEmail!"
      exit 1
     fi

}
check_commit() {

    # Check Author Email
    check_email $(git log -1 --pretty=format:%ae $1) "author"
    # Check Comitter Email
    check_email $(git log -1 --pretty=format:%ce $1) "commiter"

    # Check Author Name
    check_user "$(git log -1 --pretty=format:%an $1)" "author"
    # Check Comitter Name
    check_user "$(git log -1 --pretty=format:%cn $1)" "commiter"
}


# Check all incoming commits
# https://stackoverflow.com/questions/22546393/can-git-pre-receive-hooks-evaulate-the-incoming-commit

NULL_SHA1="0000000000000000000000000000000000000000" # 40 0's
new_list=
any_deleted=false
while read oldsha newsha refname; do
    case $oldsha,$newsha in
    *,$NULL_SHA1) # it's a delete
    any_deleted=true;;
    $NULL_SHA1,*) # it's a create
    new_list="$new_list $newsha";;
    *,*) # it's an update
    new_list="$new_list $newsha";;
    esac
done

git rev-list $new_list --not --all |
while read sha1; do
    objtype=$(git cat-file -t $sha1)
    case $objtype in
    commit) check_commit $sha1;;
    esac
done
Community
  • 1
  • 1