-
Notifications
You must be signed in to change notification settings - Fork 2
/
finder.go
39 lines (33 loc) · 1.04 KB
/
finder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package git_tools
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func FindGitRepoRootUsingGit(dir string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
cmd.Dir = dir
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
// FindGitRepoRootByTraversal finds the Git repository root for the given directory by manually checking for a .git directory.
func FindGitRepoRootByTraversal(dir string) (string, error) {
currentPath, err := filepath.Abs(dir)
if err != nil {
return "", fmt.Errorf("failed to resolve absolute path for %s: %v", dir, err)
}
// Check up to the filesystem root
for currentPath != filepath.Dir(currentPath) { // Continue until the root directory is reached
if _, err := os.Stat(filepath.Join(currentPath, ".git")); err == nil {
return currentPath, nil
}
// Move up one directory level
currentPath = filepath.Dir(currentPath)
}
return "", fmt.Errorf("no Git repository found starting from directory %s", dir)
}