Discover checkout roots for optional preparation
This commit is contained in:
@@ -3,8 +3,11 @@ package git
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -41,6 +44,10 @@ func RepositoryRoot(ctx context.Context, directory string) (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve Git repository root: %w", err)
|
||||
}
|
||||
return normalizeRepositoryRoot(root)
|
||||
}
|
||||
|
||||
func normalizeRepositoryRoot(root string) (string, error) {
|
||||
root = strings.TrimSuffix(root, "\n")
|
||||
resolved, err := filepath.EvalSymlinks(root)
|
||||
if err != nil {
|
||||
@@ -49,6 +56,84 @@ func RepositoryRoot(ctx context.Context, directory string) (string, error) {
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
// DiscoverRepositoryRoot finds a containing checkout or confirms standalone use.
|
||||
func DiscoverRepositoryRoot(ctx context.Context, directory string) (string, bool, error) {
|
||||
if directory == "" {
|
||||
directory = "."
|
||||
}
|
||||
var stdout, stderr strings.Builder
|
||||
err := process.Run(ctx, process.Invocation{
|
||||
Args: []string{"git", "rev-parse", "--show-toplevel"}, Dir: directory,
|
||||
Env: map[string]string{"LC_ALL": "C"}, Stdout: &stdout, Stderr: &stderr,
|
||||
})
|
||||
if err == nil {
|
||||
root, err := normalizeRepositoryRoot(stdout.String())
|
||||
return root, err == nil, err
|
||||
}
|
||||
if errors.Is(err, exec.ErrNotFound) {
|
||||
standalone, inspectErr := standaloneWithoutGit(directory)
|
||||
if inspectErr != nil {
|
||||
return "", false, inspectErr
|
||||
}
|
||||
if standalone {
|
||||
return "", false, nil
|
||||
}
|
||||
}
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) && exitErr.ExitCode() == 128 && noRepositoryDiagnostic(stderr.String()) {
|
||||
return "", false, nil
|
||||
}
|
||||
if message := strings.TrimSpace(stderr.String()); message != "" {
|
||||
return "", false, fmt.Errorf("resolve Git repository root: %s: %w", message, err)
|
||||
}
|
||||
return "", false, fmt.Errorf("resolve Git repository root: %w", err)
|
||||
}
|
||||
|
||||
func noRepositoryDiagnostic(message string) bool {
|
||||
message = strings.TrimSpace(message)
|
||||
if message == "fatal: not a git repository (or any of the parent directories): .git" {
|
||||
return true
|
||||
}
|
||||
const prefix = "fatal: not a git repository (or any parent up to mount point "
|
||||
const suffix = ")\nStopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."
|
||||
return strings.HasPrefix(message, prefix) && strings.HasSuffix(message, suffix) && len(message) > len(prefix)+len(suffix)
|
||||
}
|
||||
|
||||
func standaloneWithoutGit(directory string) (bool, error) {
|
||||
_, gitDirSet := os.LookupEnv("GIT_DIR")
|
||||
_, workTreeSet := os.LookupEnv("GIT_WORK_TREE")
|
||||
if gitDirSet || workTreeSet {
|
||||
return false, nil
|
||||
}
|
||||
abs, err := filepath.Abs(directory)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("inspect repository directory: %w", err)
|
||||
}
|
||||
physical, err := filepath.EvalSymlinks(abs)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("inspect repository directory: %w", err)
|
||||
}
|
||||
info, err := os.Stat(physical)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("inspect repository directory: %w", err)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return false, fmt.Errorf("inspect repository directory %q: not a directory", directory)
|
||||
}
|
||||
for dir := physical; ; dir = filepath.Dir(dir) {
|
||||
_, err := os.Lstat(filepath.Join(dir, ".git"))
|
||||
if err == nil {
|
||||
return false, nil
|
||||
}
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return false, fmt.Errorf("inspect Git marker in %s: %w", dir, err)
|
||||
}
|
||||
if filepath.Dir(dir) == dir {
|
||||
return true, 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