-
Notifications
You must be signed in to change notification settings - Fork 33
/
post-checkout
executable file
·54 lines (42 loc) · 1.37 KB
/
post-checkout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
#checkout hook to locally set user name and email based on user defined patterns
#The patterns are matched against the clone url.
#
#Based on http://www.dvratil.cz/2015/12/git-trick-628-automatically-set-commit-author-based-on-repo-url/
function warn {
echo -e "\n$1 Email and author not initialized in local config!"
}
email="$(git config --local user.email)"
name="$(git config --local user.name)"
if [[ $1 != "0000000000000000000000000000000000000000" || -n $email || -n $name ]]; then
exit 0
fi
#get remote name:
# only one: take it
# more: take "origin", or fail
remote="$([[ $(git remote | wc -l) -eq 1 ]] && git remote || git remote | grep "^origin$")"
if [[ -z $remote ]]; then
warn "Failed to detect remote."
exit 0
fi
url="$(git config --local remote.${remote}.url)"
configpath="${XDG_CONFIG_HOME:-${HOME}/.config}/git/templates/git-clone-init"
if [[ ! -f "$configpath" ]]; then
cat << INPUT > "$configpath"
#!/bin/bash
case "\$url" in
*@github.com:* ) email=""; name="";;
*//github.com/* ) email=""; name="";;
esac
INPUT
warn "\nMissing file $configpath. Template created..."
exit 0
fi
. "$configpath"
if [[ -z $name || -z $email ]]; then
warn "Failed to detect identity using $configpath."
exit 0
fi
git config --local user.email "$email"
git config --local user.name "$name"
echo -e "\nLocal identity for ${PWD##*/} set to \"$name <$email>\""