-
-
Save and1truong/d421838a9c94432f439d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
:<<'=cut' | |
=head1 NAME | |
git-wrapper - avoid committing as you@invalid | |
=head1 SYNOPSIS | |
alias git=git-wrapper | |
=head1 DESCRIPTION | |
Use this instead of the real B<git> | |
to protect yourself against committing stuff | |
with the wrong identity. | |
This requires that you do not configure a global | |
C<user.email> value at all. | |
Unless your user name and host name | |
happen to be a really unlucky combination | |
which is equivalent to what you actually | |
want to use, | |
the default value will be unacceptable, | |
which this wrapper traps before it | |
chains to the real B<git> | |
for anything except F<git config> | |
(so you can set up your C<user.email> | |
to what you want it to be in each | |
individual check-out) | |
and F<git clone> | |
(so you can clone :-) | |
and F<git init>. | |
=head1 INSTALLATION | |
alias git=git-wrapper | |
Add this to your F<.bash_profile> or similar to use globally. | |
=head1 HISTORY | |
I like to use different identities on different Git projects. | |
Inevitably, I ended up committing stuff with the wrong identity | |
more often than not (and needing to run the script I created | |
out of L<http://stackoverflow.com/a/870367/874188> all the time), | |
so I felt I had to come up with a fix. I played around with | |
Git aliases for a bit, but in the end, this turned out to be | |
the simplest and most usable solution. | |
=head1 AUTHOR | |
era eriksson | |
L<[email protected]> | |
=head1 LICENSE | |
Creative Commons CC-SA | |
=cut | |
compare_emails () { | |
local global # sic | |
local local # sac | |
global=$(cd; git config user.email) | |
local=$(git config user.email) | |
case $global in $local) | |
echo "$0: user.email '$global' is also local -- abort" >&2 | |
return 55 ;; | |
esac | |
return 0 | |
} | |
case $1 in | |
config | clone | init | --help | -h ) ;; | |
*) compare_emails || exit $?;; | |
esac | |
exec git "$@"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment