Preserve discovery failures for damaged checkouts
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user