Unfortunately I couldn't find a really satisfying solution either. I will list all solutions I found so far (I think you proposed already the best 3 ones in your question).
(1) Forbid committing without --author
Save the following script as pre-commit
hook in in .git/hooks/pre-commit
to forbid :
#!/bin/sh
if echo $GIT_AUTHOR_NAME | grep -q "XXX" 2> /dev/null; then
echo 'You tried to commit as XXX, please use git commit --author="your name".'
return 1
fi
Set user name to XXX:
git config user.name XXX
Warning: This works only for normal commits. Merge commits will be committed as XXX. Probably there is way getting this work too
(2a) Prompt for author info before commit (using hooks)
The only way to set the author using hooks, seems to be to amend the commit with post-commit
hook, see also here.
This is a weird hack with ugly side effects (e.g. author in commit message displayed wrong).
#!/bin/sh
# env variable to prevent recursion
test -z $AMMEND_COMMIT || exit 0
export AMMEND_COMMIT=1
exec < /dev/tty
echo -n "Author for previous commit: "
read author
git commit -q --no-edit --amend --author "$author"
Use the following script to get a author suggestions based on last commits:
#!/bin/sh
# TODO this doesn't work for repos with <15 commits
NUM_LAST_COMMITS=15 # how many commits in the past to look for authors
# env variable to prevent recursion
test -z $ammend_commit || exit 0
export ammend_commit=1
exec < /dev/tty
last_authors=$(git shortlog -s -e HEAD~${NUM_LAST_COMMITS}..HEAD|sed 's/^\W*[0-9]*\W*//g')
echo "Select an author from list or enter an author:"
echo
echo "$last_authors" | awk '{print " [" NR "] " $s }'
echo
echo -n "Enter number or author: "
read author
if echo "$author" | egrep -q '^[0-9]+$'; then
author=$(echo "$last_authors" | sed "${author}q;d")
fi
git commit -q --no-edit --amend --author "$author"
Looks like this:
$ git commit -am "My message"
Select an author from list or enter an author:
[1] First Author <first_author@domain.com>
[2] Second Author <second_author@domain.com>
[3] Third Author <third_author@domain.com>
Enter number or author: 3
(2b) Prompt for author info before commit (using a custom script or alias)
You can create a script git-commit.sh
or a git alias git commit-author
which prompts for the author to be used and then call git commit --author=<user's selection>
. Unfortunately it is not possible to overwrite git commit
.
A git-commit.sh
script in /usr/local/bin
might look like this:
#!/bin/sh
# TODO this doesn't work for repos with <15 commits
NUM_LAST_COMMITS=15 # how many commits in the past to look for authors
last_authors=$(git shortlog -s -e HEAD~${NUM_LAST_COMMITS}..HEAD|sed 's/^\W*[0-9]*\W*//g')
echo "Select an author from list or enter an author:"
echo
echo "$last_authors" | awk '{print " [" NR "] " $s }'
echo
echo -n "Enter number or author: "
read author
if echo "$author" | egrep -q '^[0-9]+$'; then
author=$(echo "$last_authors" | sed "${author}q;d")
fi
git commit "$@" --author "$author"
To add the script as alias run:
$ git config alias.commit-author '!git-commit.sh'
(3) Automatically set author based on the ssh-key used to login
That's your own answer, copied here for better overview:
In the shared deploy user's /home/deploy/.ssh/authorized_keys
file I added an environment variable per key so that mine, for example, looks like this:
environment="GIT_AUTHOR_EMAIL=ry4an@host.com",environment="GIT_AUTHOR_NAME=Ry4an on Dev" ssh-rsa AAAA...cXBcmHr ry4an@host.com
which also required adding:
PermitUserEnvironment yes
to the /etc/ssh/sshd_config
file.