Repository navigation
fix(gitlab): count diff lines whose content begins with -- or ++ - #12133
Conversation
|
/track-community-pr |
Greptile SummaryThis PR fixes a silent line-count bug in
Confidence Score: 5/5Safe to merge — the change is a minimal, well-tested parser fix with no side effects outside The fix is a four-line state-machine addition to a pure string-parsing function. The single call site ( Files Needing Attention: No files require special attention.
|
| Filename | Overview |
|---|---|
| src/main/gitlab/work-item-details.ts | Adds inHunk state to countDiffLines, correctly distinguishing file headers from hunk content; function is now exported @internal for isolated testing. Logic is correct and minimal. |
| src/main/gitlab/work-item-details.test.ts | Adds a 12-test describe('countDiffLines') block covering both collision cases, multi-hunk accumulation, binary/empty inputs, the no-newline marker, and @@-prefixed content lines. Good mutation coverage documented in the PR description. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["diff.split() iterate lines"] --> B{"line.startsWith('@@')?"}
B -- Yes --> C["inHunk = true, continue"]
B -- No --> D{"inHunk?"}
D -- No --> E["skip (file header region)"]
D -- Yes --> F{"line.startsWith('+')?"}
F -- Yes --> G["additions++"]
F -- No --> H{"line.startsWith('-')?"}
H -- Yes --> I["deletions++"]
H -- No --> J["skip (context or marker line)"]
C --> A
E --> A
G --> A
I --> A
J --> A
A --> K["return additions and deletions"]
Reviews (4): Last reviewed commit: "fix(gitlab): validate countDiffLines wit..." | Re-trigger Greptile
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
5fc6d8d to
9bd968f
Compare
countDiffLines skipped every line starting with ---/+++ as a file header, but a removed line whose original text began with -- (SQL/Lua/Haskell `-- comment`) becomes a diff line `---<content>`, colliding with the `--- a/file` header — so its deletion was silently dropped from the +N/-N shown in the GitLab MR dialog. Same collision for an added line whose content began with ++ (+++ flag). Track hunk state: ---/+++ are file headers only before the first @@; inside a hunk every +/- is content, matching the unified-diff rule git itself uses to disambiguate headers from content. Co-authored-by: Claude <[email protected]>
9bd968f to
43304b1
Compare
GitLab's /diffs endpoint returns json_safe_diff starting at @@ without file headers. Add comprehensive test coverage validating the collision fix correctly handles this format: content lines beginning with -- or ++ are counted as additions/deletions. Tests cover binary files, empty diffs, no-newline markers, and content beginning with @@ or C-style ++. Clarify function contract: requires hunk headers to distinguish headers from content lines.
diffFromText colourizes agent-tool unified-diff output, skipping the
`+++ b/file`/`--- a/file` headers via `!startsWith('+++')`/`('---')`.
That guard was too broad: a removed line whose original text began with
`--` (SQL/Lua/Haskell `-- comment`) is emitted by git as `---<content>`
and was misclassified as context (and could drop the whole diff via the
`added+removed<2` gate). Symmetric for added `++` content.
Treat `---`/`+++` as headers only as the canonical consecutive pair, so
content lines are counted correctly. Sibling of stablyai#12133 (countDiffLines);
uses the pair rule instead of `@@` hunk-state because agent-tool output
may be header-less.
Co-Authored-By: Claude <[email protected]>
…blyai#12133) * fix(gitlab): count diff lines whose content begins with -- or ++ countDiffLines skipped every line starting with ---/+++ as a file header, but a removed line whose original text began with -- (SQL/Lua/Haskell `-- comment`) becomes a diff line `---<content>`, colliding with the `--- a/file` header — so its deletion was silently dropped from the +N/-N shown in the GitLab MR dialog. Same collision for an added line whose content began with ++ (+++ flag). Track hunk state: ---/+++ are file headers only before the first @@; inside a hunk every +/- is content, matching the unified-diff rule git itself uses to disambiguate headers from content. Co-authored-by: Claude <[email protected]> * fix(gitlab): validate countDiffLines with actual diff format GitLab's /diffs endpoint returns json_safe_diff starting at @@ without file headers. Add comprehensive test coverage validating the collision fix correctly handles this format: content lines beginning with -- or ++ are counted as additions/deletions. Tests cover binary files, empty diffs, no-newline markers, and content beginning with @@ or C-style ++. Clarify function contract: requires hunk headers to distinguish headers from content lines. --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Jinjing <[email protected]>
Summary
countDiffLinesinsrc/main/gitlab/work-item-details.tsparses a GitLab MR file's unified diff to compute the+N/-Nshown in the MR dialog. It skipped every line starting with---/+++as a file header — but a removed content line whose original text began with--(SQL/Lua/Haskell-- comment) is rendered by git as a diff line---<content>, which is indistinguishable from the--- a/fileheader under a prefix check, so its deletion was silently dropped. The symmetric collision affects an added line whose content began with++(+++ flag).Concretely, a diff like:
returned
{ additions: 1, deletions: 0 }instead of{ additions: 1, deletions: 1 }— the-- old commentremoval was swallowed as a header.Fix
Track hunk state:
---/+++file headers only ever appear before the first@@hunk header; once inside a hunk, every+/-line is content. This is the standard unified-diff disambiguation (how git itself separates headers from content) and is byte-equivalent to the old behaviour for header-only / no-hunk diffs (binary, renamed, mode-only →{0,0}).Screenshots
No visual change beyond the corrected +N/-N counts.
Testing
pnpm lintpnpm typecheckpnpm testPassed:
describe('countDiffLines')block inwork-item-details.test.ts— 8 direct unit tests (countDiffLinesis now@internal-exported for isolated testing, matching the existing_resetProjectRefCacheconvention in this module's sibling).--removed-line and++added-line collisions each drop a count to 0); the normal-old/+newcase and the header-only{0,0}case stay green.additions/deletionscounters → exactly 2 tests fail (the asymmetric{2,1}and multi-hunk{3,2}cases — the first round's asserts were all symmetric{1,1}/{0,0}, which a swap would not catch; these two close that hole).pnpm typecheckgreen across all three tsconfigs (node, tc.cli, tc.web).pnpm exec oxlintclean on both files (exit 0).'@@ -1 +1 @@\n-old\n+new'→{additions:1, deletions:1}) stays green — no regression.AI Review Report
Security Audit
Pure string-parsing fix on an already-fetched API payload. No new input handling, IPC, auth, path, or dependency surface. No untrusted-data path beyond the existing GitLab API diff (already trusted upstream).
Notes
countDiffLinesfeedsmapMRFile→GitLabMRFile.additions/.deletions.countDiffLinesIntoMultisetinsrc/renderer/src/components/editor/diff-line-stats.tsis a distinct function in a different file and is unaffected.