Skip to content

Commit 30b1373

Browse files
committed
Merge branch 'feature/plugin' into develop
2 parents af8746b + e3a8a47 commit 30b1373

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

git-profiles.plugin.zsh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Copyright (c) Bruno Sales <[email protected]>. Licensed under the MIT License.
2+
# See the LICENSE file in the project root for full license information.
3+
4+
#!/usr/bin/env zsh
5+
6+
## File to store the profiles
7+
export GIT_PROFILES_FILE="${GIT_PROFILES_FILE:-${HOME}/.config/git/profiles}"
8+
export GIT_PROFILES_FILE_FALLBACK="${HOME}/.git-profiles"
9+
10+
function __git_profiles_hook() {
11+
## Check if git is installed
12+
if (( ! $+commands[git] )); then
13+
return 1
14+
fi
15+
16+
## Check if the file exists
17+
if [[ ! -f "${GIT_PROFILES_FILE}" ]]; then
18+
export GIT_PROFILES_FILE="${GIT_PROFILES_FILE_FALLBACK}"
19+
fi
20+
21+
## Load all stored profiles
22+
local profiles=($(grep -o '\[profile [^]]*\]' ${GIT_PROFILES_FILE} | tr -d '[]" ' | sed 's/profile//g' | tr '\n' ' '))
23+
24+
local -A profiles_paths=()
25+
local -A profiles_configs=()
26+
27+
## Iterate over all profiles to get the name, email, signingkey and path
28+
for profile in ${profiles}; do
29+
while read -r key value; do
30+
case "${key}" in
31+
name)
32+
profile_name="${value}"
33+
;;
34+
email)
35+
profile_email="${value}"
36+
;;
37+
signingkey)
38+
profile_signingkey="${value}"
39+
;;
40+
path)
41+
profile_path="${value}"
42+
;;
43+
esac
44+
done < <(awk -F ' = ' '/^\[profile/{p=0} /^\[profile "[^"]*'"${profile}"'"/{p=1} p {gsub(/"/, "", $2); print $1,$2}' ${GIT_PROFILES_FILE})
45+
46+
profiles_paths[${profile}]="${profile_path}"
47+
48+
profiles_configs[${profile}.name]="${profile_name}"
49+
profiles_configs[${profile}.email]="${profile_email}"
50+
51+
if [[ -n "${profile_signingkey}" ]]; then
52+
profiles_configs[${profile}.signingkey]="${profile_signingkey}"
53+
fi
54+
done
55+
56+
## Get the current directory
57+
local current_dir=$(pwd)
58+
59+
## Check if the current directory is in one of the profiles paths
60+
for profile in ${(k)profiles_paths}; do
61+
if [[ "${current_dir}" =~ "${profiles_paths[${profile}]}" ]]; then
62+
local current_profile="${profile}"
63+
break
64+
fi
65+
done
66+
67+
## If the current directory is not in any profile path, use the default profile
68+
if [[ -z "${current_profile}" ]]; then
69+
local current_profile="default"
70+
fi
71+
72+
## Define the current profile name and email
73+
local current_profile_name="${profiles_configs[${current_profile}.name]}"
74+
local current_profile_email="${profiles_configs[${current_profile}.email]}"
75+
76+
## Set the current profile name and email
77+
git config --global user.name "${current_profile_name}"
78+
git config --global user.email "${current_profile_email}"
79+
80+
## Set the current profile signingkey if it exists
81+
if [[ -n "${profiles_configs[${current_profile}.signingkey]}" ]]; then
82+
local current_profile_signingkey="${profiles_configs[${current_profile}.signingkey]}"
83+
84+
git config --global user.signingkey "${current_profile_signingkey}"
85+
fi
86+
}
87+
88+
add-zsh-hook chpwd __git_profiles_hook

0 commit comments

Comments
 (0)