Skip to content

fix(diff): split a bare "diff --git" header at its separator - #1691

Merged
lizhengfeng101 merged 3 commits into
alibaba:mainfrom
JasonBuildAI:fix/bare-diff-header-separator
Nov 3, 2026
Merged

lizhengfeng101 merged 3 commits into
alibaba:mainfrom
JasonBuildAI:fix/bare-diff-header-separator

Conversation

@JasonBuildAI

@JasonBuildAI JasonBuildAI commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

ParseDiffText takes its pathnames from diffHeaderRe (^diff --git a/(.+?) b/(.+)$). The first group is lazy, so it stops at the first b/ on the line, and for an unquoted header that is not necessarily the separator: git writes a/P b/P and does not quote a space. A pathname under a directory whose name ends in " b" is therefore split inside the path, and the file is read from a path that does not exist.

$ git status --short
 M "x b/y.go"

$ git diff HEAD      (the provider's own arguments)
diff --git a/x b/y.go b/x b/y.go
index 789ca71..aa33d82 100644
--- a/x b/y.go
+++ b/x b/y.go
@@ -1 +1,2 @@
 package y
+var Changed = true

$ ocr review --preview                      # before
[ocr] WARNING: cannot read file y.go b/x b/y.go for review: resolve parent path for "y.go b/x b/y.go": GetFileAttributesEx ...\y.go b: The system cannot find the file specified.

Preview: 1 file(s) changed  |  +1  -0

Will review (1):
  [R]  y.go b/x b/y.go      +1    -0

The one modified file was reported as a rename to y.go b/x b/y.go, its content was never read, and a comment filed against it would name a file that does not exist. --commit mode failed the same way (cannot read file y.go b/x b/y.go at ref HEAD: exit status 128); --from/--to runs the same parser.

Change

A header that is not a rename or a copy names the same path on both sides, so the split whose two sides agree is the one git wrote. parseDiffHeaderLine now takes that split for a bare header and falls back to diffHeaderRe unchanged. A rename has no equal split, and its rename from/rename to lines stay authoritative, so rename handling is untouched.

An equal split can only be the separator: at a b/ inside the pathname of a same-path header the two sides differ by an odd number of bytes (the header is a/ + P + + b/ + P), so they never match.

After:

$ ocr review --preview                      # after
Preview: 1 file(s) changed  |  +1  -0

Will review (1):
  [M]  x b/y.go             +1    -0

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

How Has This Been Tested?

  • make test passes locally (replicated on a machine without make; see the raw commands below)
  • Manual testing (described below)

internal/diff/parser_test.go adds two tests. TestParseDiffText_SamePathHeaderWithDelimiterInPath fails on the unpatched parser:

=== RUN   TestParseDiffText_SamePathHeaderWithDelimiterInPath
[ocr] WARNING: cannot read file y.go b/x b/y.go for review: ...
    parser_test.go:117: paths = "x" / "y.go b/x b/y.go", want "x b/y.go" on both sides
--- FAIL: TestParseDiffText_SamePathHeaderWithDelimiterInPath (0.01s)
FAIL	github.com/alibaba/open-code-review/internal/diff	0.090s

and passes with it. TestParseDiffText_AmbiguousRenameHeaderStillUsesRenameLines pins the boundary: a rename whose two paths both contain b/ keeps taking its paths from the rename from/rename to lines.

Raw check output on this machine (Go 1.25.9, windows/amd64, base fabbdb2):

go vet (24 packages, /extensions/ excluded)            exit 0
gofmt -s -l .                                          (empty)
go mod tidy                                            (no diff)
go test -race -count=1 <24 pkgs>                       ok, all packages, exit 0
go test -count=1 -coverprofile <24 pkgs>               ok, all packages, exit 0
go tool cover -func=coverage.out | grep total:         total: 90.6%   (internal/diff 92.9%)
go run scripts/verify-english-only.go                  No unapproved non-English text in 648 scanned source files.

One of several -race runs here intermittently failed two timing-sensitive tests in packages this change does not touch (internal/llm cancel-during-backoff, internal/mcp CloseAll timeout) under heavy parallel load; both pass in isolation and the other -race runs passed all 24 packages.

make check, make test and make coverage could not be invoked literally: make and bash are not installed on this Windows machine, so license-check/license-add and the Makefile's shell line were run by hand as the commands above. ocr review --audience agent reports no valid LLM endpoint configured here, so self-review used the project's no-LLM path instead (ocr delegate preview / ocr delegate rule internal/diff/parser.go), which selected internal/diff/parser.go (+36/-1) against the Go rule group; I found nothing to change against those rules.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable) - internal parser change, no user-facing surface
  • I have signed the CLA
  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

AI/LLM disclosure: drafted with AI assistance (Codex CLI) at my request, under my direction and review. The scope, the decision to fix this rather than a claimed or architectural topic, and the checks to run were set by me; I reproduced the failure on a scratch repository, read every changed line, and the reasoning above is my own. The CLA is signed (the license/cla check passed).

Related Issues

Closes #1690

@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

@NanaseInori NanaseInori left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The equal-path split fixes unquoted non-rename headers containing b/ inside the filename. Headers without that equal split retain the existing parser, and explicit rename headers still supply the final paths. The regression cases cover both behaviors. No blocking issue found in the code review.

@JasonBuildAI

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

@JasonBuildAI

Copy link
Copy Markdown
Contributor Author

@NanaseInori Thanks for the approval on this one — the change is unchanged since your review and CI is green. I believe it is ready to merge whenever you have a moment; happy to make any further adjustments if something else is needed.

@lizhengfeng101

Copy link
Copy Markdown
Contributor

I followed up as a maintainer by fixing the copy-path parsing regression and adding 40 real-Git regression checks with Codex CLI assistance.

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 98f1dfe into alibaba:main Nov 3, 2026
12 checks passed
kall added a commit to kall/open-code-review that referenced this pull request Nov 3, 2026
No text conflicts and no silent breaks. Upstream added no new calls to
the helpers the branch moved into internal/model and left the filter
algorithms untouched. The internal/diff fixes reach the core commands
through the shared code: `ocr core relocate` calls diff.ResolveComment,
so it gains the blank-line-free new-side retry (alibaba#1777), and `ocr core
diff` goes through GetDiff, so it keeps tracked gitignored files in
workspace mode (alibaba#1837) and splits bare "diff --git" headers (alibaba#1691).
New file types (.jsonc, HAML/Slim/ERB, EJS/Liquid/Nunjucks) and Scala
rules are shared rule data. The branch delta against v1.13.5 is
unchanged. Build, vet, make check and the full -race test suite pass;
coverage is 91.2%.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bare "diff --git" header is split inside the pathname when it contains " b/"

3 participants