Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change author email at commit time with hooks

Tags:

git

commit

hook

Question says it all.

I want to give my users some privacy by obfuscating their real email addresses in commits by aliases of my own. Is there a hook that can help me do this?

like image 878
Alexandre Nizoux Avatar asked Dec 16 '22 17:12

Alexandre Nizoux


2 Answers

You could just get them to change the user.email property in their local repository.

git config user.email [email protected]
like image 187
Abizern Avatar answered Dec 19 '22 08:12

Abizern


No matter what else happens, if you change something—even a single character in the user name and/or email—in some commit, you get a new, different commit. (This is because the commit ID consists of a crypographic checksum of the contents of the commit. So, OK, if you can break the cryptography, you can come up with two different commits that would have the same ID. But then you've completely broken git, and probably won a Turing award or a Nobel prize too. :-) )

The "working hook" you've linked to in comments on another answer is probably your best bet. This problem boils down to one thing: you can't make any changes to a published commit (without causing problems for consumers of that publication), but you can make any changes you want to a private commit as long as you "know what you're doing". Using git commit --amend --author "$user <$email>" -C HEAD on a per-commit basis, as they go in to your copy of the repo, guarantees that you replace an unpublished commit with a new, slightly different unpublished commit. (I assume you have put this in the post-commit hook.)

I'm not sure which part you're unhappy with, maybe the [ -n "$richo_git_rewrite" ] && exit 0? That's a reasonably clever method of detecting recursion. An alternative is to skip the recursion detection, and instead compare the existing user and email in the commit with the desired ones.

Here's a script that does that (except I used the env variable SWITCHY=true to do my testing):

#! /bin/sh
# first, pick which git config variables to get
if ${SWITCHY-false}; then
    config=user.work
else
    config=user
fi
# next, find out if they're set
can_rewrite=false
target_author=$(git config --get $config.name) &&
    target_email=$(git config --get $config.email) &&
    can_rewrite=true
# If they ARE set, we can "rewrite" (replace) the commit;
# if not, we can't.  Just because we can, though, does not
# mean we should.  Find out if the current author and email
# differ from the desired ones.
if $can_rewrite; then
    current_author=$(git log --pretty=format:%an HEAD -n 1)
    current_email=$(git log --pretty=format:%ae HEAD -n 1)
    if [ "$current_author" != "$target_author" -o \
          "$current_email" != "$target_email" ]; then
        # may want --allow-empty here, if you're allowing empty commits
        # at all, otherwise empty ones don't get the rewrite done
        git commit --amend --author "$target_author <$target_email>" -C HEAD
    fi
fi

Note: you'll need to set this same hook in hooks/post-merge, to get amended merges with the updated name-and-email. And of course you can simplify the hook a bit (don't need an actual can_rewrite variable, just do the two git config get ops with &&s and keep going with another &&). It would also make sense to have more than just user vs user.work, maybe user vs user.ModeA vs user.ModeB, etc., and you can drive it off whatever tests you like (env variables, presence or absence of commands, etc).


OK, per comments, here's a pre-commit hook. Unfortunately it doesn't work with git merge (the pre-commit hook runs and complains and then the merge commit goes in).
#! /bin/sh
fatal()
{
    echo "$@" 1>&2
    exit 1
}
# pick which git config variables to get
if ${SWITCHY-false}; then
    config=user.work
else
    config=user
fi
# tn, te = target author name/email
# an, ae = what git will use for author name/email
tn=$(git config --get $config.name) || exit 0
te=$(git config --get $config.email) || exit 0

an=${GIT_AUTHOR_NAME-$(git config --get user.name)} ||
    fatal "no author name set"
ae=${GIT_AUTHOR_EMAIL-$(git config --get user.email)} ||
    fatal "no author email set"
[ "$an" = "$tn" -a "$ae" = "$te" ] ||
    fatal "git will use author $an <$ae>
but you want them as $tn <$te>
fix your environment variables and try again"

You could combine this pre-commit hook with a post-merge rewrite-the-commit hook (eww :-) ). I did not try a conflicted commit but presumably the pre-commit hook would catch conflicted merges that require you to do your own commit.

(It's also worth noting that this pre-commit hook does not look at the working tree or index, so it does not have the usual "checking the work tree but committing the index" flaw.)

like image 40
torek Avatar answered Dec 19 '22 08:12

torek