FindGitRoot finds the root directory of the git repository. Uses pure Go filesystem traversal to avoid requiring the git executable, which can fail when the binary runs under Rosetta 2 on macOS ARM64 or in environments where git is not on PATH. Returns an error if not in a git repository.
()
| 83 | // environments where git is not on PATH. |
| 84 | // Returns an error if not in a git repository. |
| 85 | func FindGitRoot() (string, error) { |
| 86 | gitutilLog.Print("Finding git root directory") |
| 87 | |
| 88 | dir, err := os.Getwd() |
| 89 | if err != nil { |
| 90 | gitutilLog.Printf("Failed to get current directory: %v", err) |
| 91 | return "", fmt.Errorf("failed to get current directory: %w", err) |
| 92 | } |
| 93 | |
| 94 | root, err := FindGitRootFrom(dir) |
| 95 | if err != nil { |
| 96 | gitutilLog.Printf("Failed to find git root: %v", err) |
| 97 | return "", err |
| 98 | } |
| 99 | |
| 100 | gitutilLog.Printf("Found git root: %s", root) |
| 101 | return root, nil |
| 102 | } |
| 103 | |
| 104 | // FindGitRootFrom finds the root directory of the git repository starting from |
| 105 | // the given directory. It traverses upward until it finds a .git entry (file or |