Skip to content

Commit 1e36865

Browse files
committed
check we're in a git repo, else there isnt much use in processing anything else
1 parent 6162e72 commit 1e36865

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

gitprofiles.plugin.zsh

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44
# vim: set ts=4 sw=4 tw=0 et :
55
#!/usr/bin/env zsh
66

7+
function __is_git_repo() {
8+
# if git isnt installes, this will also return false
9+
if [[ -n "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]]; then
10+
return 0
11+
else
12+
return 1
13+
fi
14+
}
15+
716
function __gitprofiles_hook() {
8-
## Check if git is installed
9-
if (( ! $+commands[git] )); then
17+
# make sure we're in a git repo
18+
if ! __is_git_repo; then
1019
return 1
1120
fi
1221

@@ -36,7 +45,7 @@ function __gitprofiles_hook() {
3645
if [[ "$line" =~ '^\[profile[[:space:]]+"([^"]+)"\]' ]]; then
3746
current_section="${match[1]}"
3847
profiles+=("$current_section")
39-
[[ -n "${GP_DEBUG}" ]] && print -u2 "Found profile: ${current_section}"
48+
# [[ -n "${GP_DEBUG}" ]] && print -u2 "Found profile: ${current_section}"
4049
fi
4150
done < "${profile_filepath}"
4251

@@ -161,21 +170,38 @@ function __gitprofiles_hook() {
161170

162171
[[ -n "${GP_DEBUG}" ]] && print -u2 "Current directory: ${current_dir}"
163172

164-
# Check if current directory matches any profile paths
173+
# First pass: Check for exact matches
165174
for profile in ${(k)profile_paths_map}; do
166-
[[ -n "${GP_DEBUG}" ]] && print -u2 "Testing Profile: ${profile}"
175+
[[ -n "${GP_DEBUG}" ]] && print -u2 "Testing Profile (exact): ${profile}"
167176

168177
local paths=(${=profile_paths_map[$profile]}) # Convert to array
169178
for path_pattern in $paths; do
170-
[[ -n "${GP_DEBUG}" ]] && print -u2 "Testing path pattern: ${path_pattern}"
171-
172-
if [[ "${current_dir}" =~ "${path_pattern}" ]]; then
179+
# Only do exact match if pattern doesn't contain a wildcard
180+
if [[ ! $path_pattern =~ '[*?]' ]] && [[ "${current_dir}" = "${path_pattern}" ]]; then
181+
[[ -n "${GP_DEBUG}" ]] && print -u2 "Matched (exact) path: ${path_pattern}"
173182
matched_profile="${profile}"
174183
break 2
175184
fi
176185
done
177186
done
178187

188+
# Second pass: Check for wildcard matches (only if no exact match found)
189+
if [[ "${matched_profile}" = "default" ]]; then
190+
for profile in ${(k)profile_paths_map}; do
191+
[[ -n "${GP_DEBUG}" ]] && print -u2 "Testing Profile (wildcard): ${profile}"
192+
193+
local paths=(${=profile_paths_map[$profile]}) # Convert to array
194+
for path_pattern in $paths; do
195+
# Only do regex match if we have a wildcard
196+
if [[ $path_pattern =~ '[*?]' ]] && [[ "${current_dir}" =~ "${path_pattern}" ]]; then
197+
[[ -n "${GP_DEBUG}" ]] && print -u2 "Matched (*) path: ${path_pattern}"
198+
matched_profile="${profile}"
199+
break 2
200+
fi
201+
done
202+
done
203+
fi
204+
179205
## Set the current profile name and email
180206
git config --global user.name "${profile_cfg_map[${matched_profile}.name]}"
181207
git config --global user.email "${profile_cfg_map[${matched_profile}.email]}"

0 commit comments

Comments
 (0)