Resolve source operations from the Git checkout root
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user