|
4 | 4 | # vim: set ts=4 sw=4 tw=0 et :
|
5 | 5 | #!/usr/bin/env zsh
|
6 | 6 |
|
| 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 | + |
7 | 16 | 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 |
10 | 19 | return 1
|
11 | 20 | fi
|
12 | 21 |
|
@@ -36,7 +45,7 @@ function __gitprofiles_hook() {
|
36 | 45 | if [[ "$line" =~ '^\[profile[[:space:]]+"([^"]+)"\]' ]]; then
|
37 | 46 | current_section="${match[1]}"
|
38 | 47 | profiles+=("$current_section")
|
39 |
| - [[ -n "${GP_DEBUG}" ]] && print -u2 "Found profile: ${current_section}" |
| 48 | + # [[ -n "${GP_DEBUG}" ]] && print -u2 "Found profile: ${current_section}" |
40 | 49 | fi
|
41 | 50 | done < "${profile_filepath}"
|
42 | 51 |
|
@@ -161,21 +170,38 @@ function __gitprofiles_hook() {
|
161 | 170 |
|
162 | 171 | [[ -n "${GP_DEBUG}" ]] && print -u2 "Current directory: ${current_dir}"
|
163 | 172 |
|
164 |
| - # Check if current directory matches any profile paths |
| 173 | + # First pass: Check for exact matches |
165 | 174 | 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}" |
167 | 176 |
|
168 | 177 | local paths=(${=profile_paths_map[$profile]}) # Convert to array
|
169 | 178 | 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}" |
173 | 182 | matched_profile="${profile}"
|
174 | 183 | break 2
|
175 | 184 | fi
|
176 | 185 | done
|
177 | 186 | done
|
178 | 187 |
|
| 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 | + |
179 | 205 | ## Set the current profile name and email
|
180 | 206 | git config --global user.name "${profile_cfg_map[${matched_profile}.name]}"
|
181 | 207 | git config --global user.email "${profile_cfg_map[${matched_profile}.email]}"
|
|
0 commit comments