Preserve discovery failures for damaged checkouts

This commit is contained in:
2026-11-01 21:53:40 +00:00
parent 5d5653bfc3
commit 66f5884ab4
5 changed files with 176 additions and 12 deletions

View File

@@ -55,7 +55,7 @@ func TestDiscoverRepositoryRoot(t *testing.T) {
}
}
func TestNoRepositoryDiagnostic(t *testing.T) {
func TestNoRepositoryBoundary(t *testing.T) {
for _, tc := range []struct {
message string
want bool
@@ -65,12 +65,63 @@ func TestNoRepositoryDiagnostic(t *testing.T) {
{"fatal: not a git repository: '/bad'", false},
{"fatal: detected dubious ownership in repository", false},
} {
if got := noRepositoryDiagnostic(tc.message); got != tc.want {
if _, got := noRepositoryBoundary(tc.message); got != tc.want {
t.Errorf("diagnostic %q = %t", tc.message, got)
}
}
}
func TestDiscoverRepositoryRootHonorsDiscoveryBoundaries(t *testing.T) {
repo := newRemoteFixture(t)
child := filepath.Join(repo.work, "child")
if err := os.Mkdir(child, 0o755); err != nil {
t.Fatal(err)
}
alias := filepath.Join(t.TempDir(), "alias")
if err := os.Symlink(repo.work, alias); err != nil {
t.Fatal(err)
}
if err := os.Remove(filepath.Join(repo.work, ".git", "HEAD")); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name, directory, ceiling string
standalone bool
}{
{"damaged ancestor", child, "", false},
{"excluded ancestor", child, repo.work, true},
{"physical ceiling alias", child, alias, true},
{"literal ceiling alias", child, string(os.PathListSeparator) + alias, false},
{"ceiling includes current directory", repo.work, repo.work, false},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("GIT_CEILING_DIRECTORIES", tc.ceiling)
_, found, err := DiscoverRepositoryRoot(t.Context(), tc.directory)
if tc.standalone {
if err != nil || found {
t.Fatalf("excluded checkout: found %t, error %v", found, err)
}
} else {
var exitErr *exec.ExitError
if found || !errors.As(err, &exitErr) || exitErr.ExitCode() != 128 {
t.Fatalf("damaged checkout: found %t, error %v", found, err)
}
}
})
}
bin := t.TempDir()
script := "#!/bin/sh\nprintf 'fatal: not a git repository (or any parent up to mount point %s)\\nStopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).\\n' \"$DISCOVERY_BOUNDARY\" >&2\nexit 128\n"
if err := os.WriteFile(filepath.Join(bin, "git"), []byte(script), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin)
t.Setenv("GIT_CEILING_DIRECTORIES", "")
t.Setenv("DISCOVERY_BOUNDARY", repo.work)
if _, found, err := DiscoverRepositoryRoot(t.Context(), child); err != nil || found {
t.Fatalf("filesystem boundary: found %t, error %v", found, err)
}
}
func TestDiscoverRepositoryRootFailures(t *testing.T) {
dir := t.TempDir()
bin := t.TempDir()

View File

@@ -80,8 +80,20 @@ func DiscoverRepositoryRoot(ctx context.Context, directory string) (string, bool
}
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && exitErr.ExitCode() == 128 && noRepositoryDiagnostic(stderr.String()) {
return "", false, nil
if errors.As(err, &exitErr) && exitErr.ExitCode() == 128 {
if boundary, noRepository := noRepositoryBoundary(stderr.String()); noRepository {
boundaries := discoveryCeilings()
if boundary != "" {
boundaries = append(boundaries, boundary)
}
standalone, inspectErr := standaloneDirectory(directory, boundaries)
if inspectErr != nil {
return "", false, errors.Join(inspectErr, err)
}
if standalone {
return "", false, nil
}
}
}
if message := strings.TrimSpace(stderr.String()); message != "" {
return "", false, fmt.Errorf("resolve Git repository root: %s: %w", message, err)
@@ -89,14 +101,41 @@ func DiscoverRepositoryRoot(ctx context.Context, directory string) (string, bool
return "", false, fmt.Errorf("resolve Git repository root: %w", err)
}
func noRepositoryDiagnostic(message string) bool {
func noRepositoryBoundary(message string) (string, bool) {
message = strings.TrimSpace(message)
if message == "fatal: not a git repository (or any of the parent directories): .git" {
return true
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)
if strings.HasPrefix(message, prefix) && strings.HasSuffix(message, suffix) && len(message) > len(prefix)+len(suffix) {
return filepath.Clean(message[len(prefix) : len(message)-len(suffix)]), true
}
return "", false
}
func discoveryCeilings() []string {
var ceilings []string
resolveLinks := true
for _, entry := range filepath.SplitList(os.Getenv("GIT_CEILING_DIRECTORIES")) {
// Git uses an empty entry to mark subsequent paths as already physical.
if entry == "" {
resolveLinks = false
continue
}
if !filepath.IsAbs(entry) {
continue
}
if resolveLinks {
resolved, err := filepath.EvalSymlinks(entry)
if err != nil {
continue // Git ignores ceiling entries it cannot resolve.
}
entry = resolved
}
ceilings = append(ceilings, filepath.Clean(entry))
}
return ceilings
}
func standaloneWithoutGit(directory string) (bool, error) {
@@ -105,6 +144,12 @@ func standaloneWithoutGit(directory string) (bool, error) {
if gitDirSet || workTreeSet {
return false, nil
}
return standaloneDirectory(directory, nil)
}
// standaloneDirectory checks only locations Git could have searched. Boundaries
// are excluded parents; the supplied directory itself is always inspected.
func standaloneDirectory(directory string, boundaries []string) (bool, error) {
abs, err := filepath.Abs(directory)
if err != nil {
return false, fmt.Errorf("inspect repository directory: %w", err)
@@ -128,9 +173,15 @@ func standaloneWithoutGit(directory string) (bool, error) {
if !errors.Is(err, os.ErrNotExist) {
return false, fmt.Errorf("inspect Git marker in %s: %w", dir, err)
}
if filepath.Dir(dir) == dir {
parent := filepath.Dir(dir)
if parent == dir {
return true, nil
}
for _, boundary := range boundaries {
if parent == boundary {
return true, nil
}
}
}
}