Skip to content

Commit bcfaa10

Browse files
committed
feat: refactor and use zstyle for options
1 parent 54eee41 commit bcfaa10

File tree

2 files changed

+90
-88
lines changed

2 files changed

+90
-88
lines changed

git-profiles.plugin.zsh

Lines changed: 0 additions & 88 deletions
This file was deleted.

gitprofiles.plugin.zsh

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
function __gitprofiles_hook() {
7+
## Check if git is installed
8+
if (( ! $+commands[git] )); then
9+
return 1
10+
fi
11+
12+
local -A profile_path_map=()
13+
local -A profile_cfg_map=()
14+
15+
## Get the path to the profile file
16+
zstyle -s ":empresslabs:git:profile" path profile_filepath
17+
18+
## Check if the file exists
19+
if [[ ! -f "${profile_filepath}" ]]; then
20+
return 1
21+
fi
22+
23+
## Load all stored profiles
24+
local profiles=($(grep -o '\[profile [^]]*\]' ${profile_filepath} | tr -d '[]" ' | sed 's/profile//g' | tr '\n' ' '))
25+
26+
## Check if default profile exists
27+
if [[ ! "${profiles}" =~ "default" ]]; then
28+
echo "gitprofiles: 'default' profile not found in '${profile_filepath}'"
29+
return 1
30+
fi
31+
32+
## Iterate over all profiles to get the name, email, signingkey and path
33+
for profile in ${profiles}; do
34+
local -A profile_value_map=()
35+
36+
while read -r key value; do
37+
case "${key}" in
38+
name)
39+
profile_value_map[name]="${value}"
40+
;;
41+
email)
42+
profile_value_map[email]="${value}"
43+
;;
44+
signingkey)
45+
profile_value_map[signingkey]="${value}"
46+
;;
47+
path)
48+
profile_value_map[path]="${value}"
49+
;;
50+
esac
51+
done < <(awk -F ' = ' '/^\[profile/{p=0} /^\[profile "[^"]*'"${profile}"'"/{p=1} p {gsub(/"/, "", $2); print $1,$2}' ${profile_filepath})
52+
53+
profile_path_map[${profile}]="${profile_value_map[path]}"
54+
55+
profile_cfg_map[${profile}.name]="${profile_value_map[name]}"
56+
profile_cfg_map[${profile}.email]="${profile_value_map[email]}"
57+
58+
if [[ -n "${profile[signingkey]}" ]]; then
59+
profile_cfg_map[${profile}.signingkey]="${profile_value_map[signingkey]}"
60+
fi
61+
done
62+
63+
## Get the current directory
64+
local -A current=()
65+
current[dir]=$(pwd)
66+
67+
## Check if the current directory is in one of the profiles paths
68+
for profile in ${(k)profile_path_map}; do
69+
if [[ "${current[dir]}" =~ "${profile_path_map[${profile}]}" ]]; then
70+
local current[profile]="${profile}"
71+
break
72+
fi
73+
done
74+
75+
## If the current directory is not in any profile path, use the default profile
76+
if [[ -z "${current[profile]}" ]]; then
77+
local current[profile]="default"
78+
fi
79+
80+
## Set the current profile name and email
81+
git config --global user.name "${profile_cfg_map[${current[profile]}.name]}"
82+
git config --global user.email "${profile_cfg_map[${current[profile]}.email]}"
83+
84+
## Set the current profile signingkey if it exists
85+
if [[ -n "${profile_cfg_map[${current[profile]}.signingkey]}" ]]; then
86+
git config --global user.signingkey "${profile_cfg_map[${current[profile]}.signingkey]}"
87+
fi
88+
}
89+
90+
add-zsh-hook chpwd __gitprofiles_hook

0 commit comments

Comments
 (0)