golangci/golangci-lint · error

can't prepare diff by revgrep: %w

Error message

can't prepare diff by revgrep: %w

What it means

The diff processor asked the revgrep checker to prepare the diff (against the base revision or merge base) and revgrep failed. This wraps the underlying git/diff error so the developer sees why the changed-lines computation could not be produced. Without a prepared diff, patch-based filtering of issues cannot proceed.

Source

Thrown at pkg/result/processors/diff.go:79

			return nil, fmt.Errorf("can't read from patch file %s: %w", p.patchFilePath, err)
		}

		patchReader = bytes.NewReader(patch)

	case p.patch != "":
		patchReader = strings.NewReader(p.patch)
	}

	checker := revgrep.Checker{
		Patch:        patchReader,
		RevisionFrom: p.fromRev,
		MergeBase:    p.fromMergeBase,
		WholeFiles:   p.wholeFiles,
	}

	err := checker.Prepare(context.Background())
	if err != nil {
		return nil, fmt.Errorf("can't prepare diff by revgrep: %w", err)
	}

	return transformIssues(issues, func(issue *result.Issue) *result.Issue {
		if issue.FromLinter == typeCheckName {
			// Never hide typechecking errors.
			return issue
		}

		hunkPos, isNew := checker.IsNew(issue.WorkingDirectoryRelativePath, issue.Line())
		if !isNew {
			return nil
		}

		newIssue := *issue
		newIssue.HunkPos = hunkPos

		return &newIssue
	}), nil

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Ensure the base revision exists: run in a full (non-shallow) clone or add a fetch-depth/fetch of the target branch
  2. Verify you are running inside a git repository
  3. Check the --new-from-rev value matches a real commit/branch (e.g. origin/main)
  4. Test the diff manually: git diff <rev>

Example fix

# before (GitHub Actions)
- uses: actions/checkout@v4
- run: golangci-lint run --new-from-rev=main
# after
- uses: actions/checkout@v4
  with:
    fetch-depth: 0
- run: golangci-lint run --new-from-rev=origin/main
Defensive patterns

Strategy: validation

Validate before calling

# shell: verify base revision exists before running
if ! git cat-file -e "$BASE_REV^{commit}" 2>/dev/null; then
  echo "base revision $BASE_REV not found"; exit 1
fi

Try / catch

if err != nil {
    if strings.Contains(err.Error(), "can't prepare diff by revgrep") {
        log.Fatalf("git/diff setup problem: %v — is the repo a full clone?", err)
    }
    return err
}

Prevention

When it happens

Trigger: Process is called with a from-revision (e.g. --new-from-rev) or fromMergeBase and the revgrep checker's Prepare fails — typically because the revision does not exist in the repository or git operations fail.

Common situations: --new-from-rev references a commit not present in a shallow CI clone; running outside a git repository; merge-base computation fails on detached HEAD or unrelated histories; wrong branch name in CI configuration.

Related errors


AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02). Data as JSON: /api/errors/ce4be9c65500fcc6. Report an issue: GitHub.