Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

traverse: fix git #909

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading