Skip to content

Commit

Permalink
traverse: fix git
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 3, 2023
1 parent c6b8ff3 commit de5b463
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
9 changes: 0 additions & 9 deletions example/cmd/modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,6 @@ func TestChdirF(t *testing.T) {
Tag("files").
Usage("ChdirF()"))

s.Env("GIT_DIR", "subdirA/") // TODO should also work for subdirA
s.Run("modifier", "--chdirf", "").Expect(
carapace.ActionValues(
"file2.txt",
).StyleF(style.ForPath).
NoSpace('/').
Tag("files").
Usage("ChdirF()"))

s.Env("GIT_WORK_TREE", "subdirB/") // TODO should also work for subdirB
s.Run("modifier", "--chdirf", "").Expect(
carapace.ActionValues(
Expand Down
9 changes: 6 additions & 3 deletions pkg/traverse/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ func GitDir(tc Context) (string, error) {
if dir, ok := tc.LookupEnv("GIT_DIR"); ok {
return filepath.ToSlash(dir), nil
}
return Parent(".git")(tc)
dir, err := GitWorkTree(tc)
if err == nil {
dir += "/.git"
}
return dir, err
}

// GitWorkTree returns the location of the root of the working directory for a non-bare repository.
func GitWorkTree(tc Context) (string, error) {
if dir, ok := tc.LookupEnv("GIT_WORK_TREE"); ok {
return filepath.ToSlash(dir), nil
}

return GitDir(tc)
return Parent(".git")(tc)
}
2 changes: 1 addition & 1 deletion pkg/traverse/traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Parent(names ...string) func(tc Context) (string, error) {
func traverse(path string, name string) (target string, err error) {
var absPath string
if absPath, err = filepath.Abs(path); err == nil {
target = filepath.ToSlash(absPath + "/" + name)
target = filepath.ToSlash(absPath + "/" + strings.TrimSuffix(name, "/"))
if _, err = os.Stat(target); err != nil {
parent := filepath.Dir(absPath)
if parent != path {
Expand Down

0 comments on commit de5b463

Please sign in to comment.