-
Notifications
You must be signed in to change notification settings - Fork 702
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
Support git rev-parse --show-toplevel
#74
Comments
Code in question looks like this:
Currently go-git's |
I believe this doesn't detect properly bare repositories. This is what I'm doing: func detectGitPath(path string) (string, error) {
// normalize the path
path, err := filepath.Abs(path)
if err != nil {
return "", err
}
for {
fi, err := os.Stat(stdpath.Join(path, ".git"))
if err == nil {
if !fi.IsDir() {
return "", fmt.Errorf(".git exist but is not a directory")
}
return stdpath.Join(path, ".git"), nil
}
if !os.IsNotExist(err) {
// unknown error
return "", err
}
// detect bare repo
ok, err := isGitDir(path)
if err != nil {
return "", err
}
if ok {
return path, nil
}
if parent := filepath.Dir(path); parent == path {
return "", fmt.Errorf(".git not found")
} else {
path = parent
}
}
}
func isGitDir(path string) (bool, error) {
markers := []string{"HEAD", "objects", "refs"}
for _, marker := range markers {
_, err := os.Stat(stdpath.Join(path, marker))
if err == nil {
continue
}
if !os.IsNotExist(err) {
// unknown error
return false, err
} else {
return false, nil
}
}
return true, nil
} It's based on https://github.com/git/git/blob/3bab5d56259722843359702bc27111475437ad2a/setup.c#L328-L338 |
To help us keep things tidy and focus on the active tasks, we've introduced a stale bot to spot issues/PRs that haven't had any activity in a while. This particular issue hasn't had any updates or activity in the past 90 days, so it's been labeled as 'stale'. If it remains inactive for the next 30 days, it'll be automatically closed. We understand everyone's busy, but if this issue is still important to you, please feel free to add a comment or make an update to keep it active. Thanks for your understanding and cooperation! |
This has 11 upvotes, I think it is not yet stale. |
Hey, I was thinking of adding native Also, I am new to this, can anyone give any suggestion on where to start so I can pick up this feature? |
I believe this is already supported if you use func main() {
CheckArgs("<path>")
path := os.Args[1]
wt := osfs.New(path, osfs.WithBoundOS())
dot, err := wt.Chroot(git.GitDirName)
CheckIfError(err)
storer := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
r, err := git.Open(storer, wt)
CheckIfError(err)
w, err := r.Worktree()
CheckIfError(err)
Info("git rev-parse --show-toplevel")
d, err := filepath.Abs(w.Filesystem.Root())
CheckIfError(err)
fmt.Println(d)
} Please note that the worktree filesystem is abstracted by With BoundOS you can see the actual directory - as per above. |
If I have a repository at
~/foo
, I can rungit rev-parse --show-toplevel
in~/foo/bar
without a problem to find the root of the current repository (~/foo
).This was mentioned way back in src-d/go-git#765
However, the fine folks at Google worked around this issue by the code here:
GoogleContainerTools/skaffold#275
And I think it would be handy to include this commonly needed functionality in go-git itself so people didn't need to rely on go-findroot and the presence of the git binary to achieve this.
The text was updated successfully, but these errors were encountered: