alibaba/open-code-review · error

git ls-files (untracked): %w

Error message

git ls-files (untracked): %w

What it means

The second git call in listFilesViaGit lists untracked-but-not-ignored files via `git ls-files -z --others --exclude-standard`. A failure here is wrapped as 'git ls-files (untracked): ...'. Tracked enumeration already succeeded, so the repo is basically healthy but the --others query failed.

Source

Thrown at internal/scan/provider.go:174

		return p.listFilesViaGit(ctx)
	}
	return p.listFilesViaWalk(ctx)
}

// isGitRepo reports whether p.repoDir is inside a git working tree.
func (p *Provider) isGitRepo(ctx context.Context) bool {
	cmd := exec.CommandContext(ctx, "git", "-C", p.repoDir, "rev-parse", "--git-dir")
	return cmd.Run() == nil
}

func (p *Provider) listFilesViaGit(ctx context.Context) ([]string, error) {
	tracked, err := p.gitLs(ctx, "-z")
	if err != nil {
		return nil, fmt.Errorf("git ls-files (tracked): %w", err)
	}
	untracked, err := p.gitLs(ctx, "-z", "--others", "--exclude-standard")
	if err != nil {
		return nil, fmt.Errorf("git ls-files (untracked): %w", err)
	}

	seen := make(map[string]struct{}, len(tracked)+len(untracked))
	all := make([]string, 0, len(tracked)+len(untracked))
	for _, f := range append(tracked, untracked...) {
		if f == "" {
			continue
		}
		if _, ok := seen[f]; ok {
			continue
		}
		seen[f] = struct{}{}
		all = append(all, f)
	}
	return all, nil
}

// listFilesViaWalk recursively walks p.repoDir collecting regular files.

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Run `git ls-files --others --exclude-standard` manually to reproduce the git error.
  2. Clear a stale .git/index.lock if present.
  3. Ensure global git config and core.excludesFile are readable.
  4. Increase timeout / retry if the context was canceled mid-run.
Defensive patterns

Strategy: try-catch

Validate before calling

cmd := exec.Command("git", "-C", repoDir, "ls-files", "--others", "--exclude-standard")
if err := cmd.Run(); err != nil {
    return fmt.Errorf("untracked listing will fail: %w", err)
}

Type guard

func isGitLsUntrackedError(err error) bool {
    return err != nil && strings.Contains(err.Error(), "git ls-files (untracked)")
}

Try / catch

files, err := provider.Enumerate(ctx)
if err != nil {
    if strings.Contains(err.Error(), "untracked") {
        // tracked listing worked; safe to retry with backoff
        time.Sleep(time.Second)
        return provider.Enumerate(ctx)
    }
    return err
}

Prevention

When it happens

Trigger: p.gitLs(ctx, "-z", "--others", "--exclude-standard") returns an error after the tracked listing succeeded — typically context cancellation between the two calls, or an unusable temp/config state for git.

Common situations: Context deadline hit between the two git invocations on large repos; global gitignore or config unreadable (permissions, HOME changed); concurrent git index.lock contention.

Understand the failure class

Background: "git command failed": what it means when a tool shells out to git and git exits non-zero — this error's family across 21 libraries.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/e4116d78bcf61433. Report an issue: GitHub.