fix(diff): detect renamed and deleted files correctly in diff parsing#105
Open
adlternative wants to merge 1 commit into
Open
fix(diff): detect renamed and deleted files correctly in diff parsing#105adlternative wants to merge 1 commit into
adlternative wants to merge 1 commit into
Conversation
When a file was renamed on the target branch, ocr review emitted '[ocr] WARNING: cannot read file <old path> at ref <to>: exit status 128'. Two compounding bugs: 1. The parser required 'a/'/'b/' prefixes when matching '--- /dev/null' / '+++ /dev/null', but git emits these lines without prefixes, so IsNew/IsDeleted were never set and deleted files fell through to a doomed 'git show ref:<old path>'. 2. 'rename from' / 'rename to' extended headers were never parsed, and git diff/show call sites did not force rename detection, so renames degraded to delete+add whenever the user had diff.renames=false. Fixes: - Parse 'rename from'/'rename to', 'new file mode', 'deleted file mode' and unprefixed /dev/null markers in ParseDiffText. - Pass --find-renames to all git diff/show invocations so rename detection no longer depends on user config. - Add IsRenamed to model.Diff (json: is_renamed) and prefer it in diffStatus. - Add parser unit tests and a range-mode rename regression test. Fixes alibaba#99
Author
End-to-End Verification ReportTo confirm the fix actually resolves issue #99 (beyond unit tests), I reproduced the reported scenario and compared the pre-fix and post-fix binaries side by side. Repro setup (mirrors the issue exactly)git init -b master
git config diff.renames false # key: simulates an environment where git does NOT auto-detect renames
seq 1 50 | sed 's/^/code line /' > service.go
git add . && git commit -m 'init on master'
git checkout -b SRQ-12345 # the SRQ-XXXXX branch from the issue
git mv service.go service_renamed.go # rename
sed -i 's/code line 25/code line 25 EDITED/' service_renamed.go # small edit, like the reporter did
git add -A && git commit -m 'rename service.go'
Method
ocr review --from master --to SRQ-12345 --previewResultsBefore fix (reproduces the exact warning from the issue): After fix: Takeaways
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #99 —
ocr review --from master --to BRANCH --format jsonemitted[ocr] WARNING: cannot read file <old path> at ref BRANCH: exit status 128when a file was renamed on the target branch.Root Cause
Two compounding bugs:
/dev/nulldetection (internal/diff/parser.go): git emits--- /dev/null/+++ /dev/nullwithouta//b/prefixes, but the old regexes required them, soIsNew/IsDeletedwere never set. Deleted files fell through togit show ref:<old path>→exit status 128.rename from/rename toextended headers, and the git diff/show call sites did not pass--find-renames, so withdiff.renames=falsea rename degraded to delete(old)+add(new); the delete half hit bug 1 and warned about the old path.Changes
rename from/rename to(authoritative paths, robust for paths with spaces),new file mode,deleted file mode, and unprefixed/dev/nullmarkers inParseDiffText--find-renamesto all 4 git diff/show invocations so rename detection no longer depends on user git configIsRenamedtomodel.Diff(json:is_renamed) and prefer it indiffStatusparser_test.go: rename / pure rename (100% similarity, no hunks) / deleted / new file unit testsTestRangeDiffDetectsRenameintegration test:diff.renames=false+ branch rename + small edit → exactly 1 diff,IsRenamed=true, content read at the new pathTesting
go test -race -count=1 ./...— all packages passgo vet ./...andgo build ./...clean