Skip to content

Commit 7a781d8

Browse files
committed
Issue #25: Added support for git worktrees
1 parent c78fe9c commit 7a781d8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

cmd/common/git_config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"os"
77
"path/filepath"
8+
"strings"
89

910
"github.com/gildas/go-errors"
1011
"github.com/gildas/go-logger"
@@ -21,6 +22,29 @@ func OpenGitConfig(context context.Context) (io.ReadCloser, error) {
2122
last := folder + "dummy"
2223

2324
for {
25+
// If .git is a filem (e.g. worktree), read the actual git dir from there (field gitdir)
26+
gitPath := filepath.Join(folder, ".git")
27+
info, err := os.Stat(gitPath)
28+
if err == nil && !info.IsDir() {
29+
log.Debugf(".git is a file, reading gitdir from there")
30+
content, err := os.ReadFile(gitPath)
31+
if err == nil {
32+
lines := string(content)
33+
const prefix = "gitdir: "
34+
for line := range strings.SplitSeq(lines, "\n") {
35+
if len(line) > len(prefix) && line[:len(prefix)] == prefix {
36+
gitDir := line[len(prefix):]
37+
log.Debugf("found gitdir: %s", gitDir)
38+
if !filepath.IsAbs(gitDir) {
39+
folder = filepath.Join(folder, gitDir)
40+
} else {
41+
folder = gitDir
42+
}
43+
break
44+
}
45+
}
46+
}
47+
}
2448
filename := filepath.Join(folder, ".git/config")
2549
if folder == last {
2650
return nil, errors.NotFound.With("file", filename)

0 commit comments

Comments
 (0)