alibaba/open-code-review · error

load diffs: %w

Error message

load diffs: %w

What it means

The agent Preview method wraps any loadDiffs failure with the 'load diffs:' prefix when producing a DiffPreview (insertions/deletions/file counts) without running a review. Since preview only reads git state, this error means the git diff could not be loaded for the given Args — the wrapped error carries the actual git cause. It is identical in origin to the ResolveIdentity 'load diffs' error but on the preview entry point.

Source

Thrown at internal/agent/preview.go:74

		return ExcludeDefaultPath
	}

	return ExcludeNone
}

// Preview loads diffs and applies the filter algorithm, returning structured
// preview data without dispatching any LLM calls.
//
// It builds none of the review runtime — no session, manifest, or runner — so
// previewing cannot open session persistence. Going through New instead would
// auto-create a session and leave an unfinalized JSONL file under the OCR home.
func Preview(ctx context.Context, args Args) (*DiffPreview, error) {
	return (&Agent{args: args}).preview(ctx)
}

func (a *Agent) preview(ctx context.Context) (*DiffPreview, error) {
	if err := a.loadDiffs(ctx); err != nil {
		return nil, fmt.Errorf("load diffs: %w", err)
	}

	result := &DiffPreview{
		TotalInsertions: a.totalInsertions,
		TotalDeletions:  a.totalDeletions,
		TotalFiles:      len(a.diffs),
		// Non-nil so an empty diff marshals as `"files":[]`, not `"files":null`.
		Entries: make([]DiffPreviewEntry, 0, len(a.diffs)),
	}

	for _, d := range a.diffs {
		path := effectivePath(d)
		entry := DiffPreviewEntry{
			Path:       path,
			Insertions: d.Insertions,
			Deletions:  d.Deletions,
			Status:     diffStatus(d),
		}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Validate RepoDir is a git work tree and the From/To/Commit refs resolve ('git rev-parse --verify') before calling Preview
  2. Read the wrapped error for the exact git failure
  3. Fetch/unshallow if commits are missing in CI
  4. Fix the ref arguments and retry

Example fix

// before
prev, err := agent.Preview(ctx, args) // 'load diffs: ... unknown revision'
// after
if err := checkRef(args.RepoDir, args.From); err != nil { return err }
if err := checkRef(args.RepoDir, args.To); err != nil { return err }
prev, err := agent.Preview(ctx, args)
Defensive patterns

Strategy: validation

Validate before calling

func previewSafe(repoDir, from, to string) error {
    if out, err := execGit(repoDir, "rev-parse", "--is-inside-work-tree"); err != nil || strings.TrimSpace(out) != "true" {
        return fmt.Errorf("%s is not a git work tree", repoDir)
    }
    for _, r := range []string{from, to} {
        if r != "" && execGit(repoDir, "rev-parse", "--verify", r+"^{commit}") != nil {
            return fmt.Errorf("ref %q missing", r)
        }
    }
    return nil
}

Try / catch

prev, err := agent.Preview(ctx, args)
if err != nil {
    if strings.Contains(err.Error(), "load diffs:") {
        log.Warnf("preview unavailable for %v: %v", args, err)
        return emptyPreview(), nil
    }
    return err
}

Prevention

When it happens

Trigger: agent.Preview (or Agent.preview) called with a RepoDir that is not a git repo, refs that do not resolve, or when the underlying git diff command fails (corrupt index, missing objects, runner error).

Common situations: Running preview from the wrong working directory; typo'd --from/--to; shallow CI clones lacking the target commit; detached repos after history rewrites.

Related errors


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