Add a test for getting the global git config excludesfile value

This commit is contained in:
Kovid Goyal 2025-07-09 14:44:57 +05:30
parent f742009c55
commit ecf4ef396b
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 23 additions and 1 deletions

View file

@ -273,11 +273,17 @@ func get_global_gitconfig_excludesfile() (ans string) {
line := strings.TrimSpace(s.Text())
if in_core {
if strings.HasPrefix(line, "[") {
in_core = false
in_core = line == "[core]"
continue
}
if k, rest, found := strings.Cut(line, "="); found && strings.ToLower(strings.TrimSpace(k)) == `excludesfile` {
ans = strings.TrimSpace(rest)
ans = utils.Expanduser(ans)
if !filepath.IsAbs(ans) {
if a, err := filepath.Abs(ans); err != nil {
ans = a
}
}
}
} else if strings.ToLower(line) == "[core]" {
in_core = true

View file

@ -3,6 +3,7 @@ package ignorefiles
import (
"fmt"
"io/fs"
"os"
"strings"
"testing"
@ -142,4 +143,19 @@ bar `: {
}
}
}
os.WriteFile(utils.Expanduser("~/.gitconfig"), []byte(`
[core]
something
[else]
...
[core]
...
[core]
excludesfile = one
[core]
excludesfile = ~/global-gitignore
`), 0600)
if ef := get_global_gitconfig_excludesfile(); ef != utils.Expanduser("~/global-gitignore") {
t.Fatalf("global gitconfig excludes file incorrect: %s != %s", utils.Expanduser("~/global-gitignore"), ef)
}
}