Resolve source operations from the Git checkout root

This commit is contained in:
2026-10-10 14:03:57 +00:00
parent 750632f80e
commit 7555d3e6c9
11 changed files with 164 additions and 13 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"path"
"path/filepath"
"strconv"
"strings"
@@ -30,6 +31,24 @@ type TaggedSource struct {
TagObjectType string
}
// RepositoryRoot resolves the containing checkout, including independent
// worktrees and directories reached through filesystem aliases.
func RepositoryRoot(ctx context.Context, directory string) (string, error) {
if directory == "" {
directory = "."
}
root, err := run(ctx, directory, nil, "rev-parse", "--show-toplevel")
if err != nil {
return "", fmt.Errorf("resolve Git repository root: %w", err)
}
root = strings.TrimSuffix(root, "\n")
resolved, err := filepath.EvalSymlinks(root)
if err != nil {
return "", fmt.Errorf("resolve Git repository directory: %w", err)
}
return resolved, nil
}
// CaptureHead records the checkout commit before identity derived from files
// in that commit is inspected.
func CaptureHead(ctx context.Context, repositoryDir string) (string, error) {