-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost-commit
71 lines (56 loc) · 2 KB
/
post-commit
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/sh
script_root_dir=$(dirname $0)
# requirements
. "${script_root_dir}/vendor/ini-parser.sh"
# read `~/.gitconfig` or a custom config file (`~/.gitconfig` by default)
custom_config_file_location=$1
config_file_location=""
if [[ $custom_config_file_location != "" ]]; then
config_file_location=$custom_config_file_location
else
config_file_location="${HOME}/.gitconfig"
fi
# read config
read_ini "${config_file_location}"
# transform the ${INI__ALL_VARS} to an array if it begins with 'INI__emails'
emails_array=()
IFS=' ' ini_variables=(${INI__ALL_VARS})
for variable in "${ini_variables[@]}"; do
# check if the variable begins with 'INI__user__emails__'
if [[ $variable == INI__emails__* ]]; then
emails_array+=("${variable:13}=${!variable}") # 13 indicates the start of the folder name
fi
done
if (( ${#emails_array[@]} == 0 )); then
# exit if no emails were found in the configuration
exit 0
fi
# get the current git config user.email
current_name=$(git config --get user.name)
current_email=$(git config --get user.email)
# figure out which email address to use
new_email=$current_email
# loop forwards (ending at the current directory) and check if any parent folders match a folder/name in a config file
for item in "${emails_array[@]}"; do
IFS='=' keyvalue=(${item})
folder_name=${keyvalue[0]}
email=${keyvalue[1]}
# break the current path string into an array
currentPath=$(pwd)
IFS='/' path_items=(${currentPath})
for i in ${path_items[@]}; do
if [[ $i == $folder_name ]]; then
new_email=$email
break 2
fi
done
done
if [[ $new_email != $current_email ]]; then
echo "Current email (${current_email}) does not match intended email (${new_email})."
echo "Updating email to ${new_email}. Retrying..."
# update email for future commits
git config user.email "${new_email}"
# then amend the previous commit
git commit --amend --author="${current_name} <${new_email}>" --no-edit
fi
exit 0