Skip to content

fix(gitlab): count diff lines whose content begins with -- or ++ - #12133

Merged
AmethystLiang merged 2 commits into
stablyai:mainfrom
YuriNachos:fix-gitlab-diff-line-counter
Aug 3, 2026
Merged

AmethystLiang merged 2 commits into
stablyai:mainfrom
YuriNachos:fix-gitlab-diff-line-counter

Conversation

@YuriNachos

@YuriNachos YuriNachos commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

countDiffLines in src/main/gitlab/work-item-details.ts parses a GitLab MR file's unified diff to compute the +N/-N shown 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/file header 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:

--- a/db.sql
+++ b/db.sql
@@ -1,2 +1 @@
 ctx
--- old comment
+new

returned { additions: 1, deletions: 0 } instead of { additions: 1, deletions: 1 } — the -- old comment removal 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 lint
  • pnpm typecheck
  • pnpm test
  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

Passed:

  • New describe('countDiffLines') block in work-item-details.test.ts — 8 direct unit tests (countDiffLines is now @internal-exported for isolated testing, matching the existing _resetProjectRefCache convention in this module's sibling).
  • Mutation-checked (RED-before-GREEN), each isolated:
    • reverting to the old header-only skip → exactly 2 tests fail (the -- removed-line and ++ added-line collisions each drop a count to 0); the normal -old/+new case and the header-only {0,0} case stay green.
    • swapping the additions/deletions counters → 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).
    • reverted: 8/8 green.
  • pnpm typecheck green across all three tsconfigs (node, tc.cli, tc.web).
  • pnpm exec oxlint clean on both files (exit 0).
  • Existing integration test (diff '@@ -1 +1 @@\n-old\n+new' → {additions:1, deletions:1}) stays green — no regression.

AI Review Report

  • Cross-platform (macOS/Linux/Windows): the SUT is pure string parsing of a GitLab API diff payload; no platform assumptions. Runs identically everywhere.
  • SSH/remote/local + folder-workspace: N/A — parses a diff string already fetched; no git binary / SSH / folder-workspace path involved.
  • Provider/agent neutrality: GitLab-specific MR detail parsing.
  • Performance / hot path: none — runs once per MR file on detail load.
  • UI quality: corrects the +N/-N line counts shown in the MR file list.

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

  • Single call site: countDiffLines feeds mapMRFile → GitLabMRFile.additions/.deletions.
  • The renderer-side countDiffLinesIntoMultiset in src/renderer/src/components/editor/diff-line-stats.ts is a distinct function in a different file and is unaffected.
  • No open or closed PR touches this counter.
  • No linked issue: this fixes a parser bug found through code review (no external report), not a tracked issue.
  • A maintainer may need to approve-and-run the CI workflows for this fork PR; let me know if anything else is needed.

@YuriNachos

Copy link
Copy Markdown
Contributor Author

/track-community-pr

@greptile-apps

greptile-apps Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

Greptile Summary

This PR fixes a silent line-count bug in countDiffLines where removed lines whose original content began with -- (e.g., SQL/Lua/Haskell -- comment) were being silently dropped because the diff encoding ---<content> was indistinguishable from the --- a/file file-header under a plain startsWith check. The symmetric case (+++ content vs +++ b/file) was equally affected.

  • Root cause & fix: The old code globally skipped every line starting with --- or +++. The fix introduces a boolean inHunk flag that is set to true on the first @@ hunk-header line; only then are +/- prefix lines counted as content. This is standard unified-diff disambiguation and is byte-equivalent to the old behaviour for header-only or hunk-less diffs (binary, rename-only → {0, 0}).
  • Testing: countDiffLines is now @internal-exported (matching existing conventions in this module), and a new describe('countDiffLines') block adds 12 unit tests covering the two collisions, asymmetric hunks, multi-hunk accumulation, binary/empty diffs, and several edge-case content lines.

Confidence Score: 5/5

Safe to merge — the change is a minimal, well-tested parser fix with no side effects outside countDiffLines.

The fix is a four-line state-machine addition to a pure string-parsing function. The single call site (mapMRFile) passes the result through unchanged, and the new inHunk guard is byte-equivalent to the old behaviour for all inputs that contain no hunk. The 12-unit test suite covers both collision cases, the real GitLab header-less payload shape, multi-hunk accumulation, and relevant edge-case content. No auth, I/O, or shared state is touched.

Files Needing Attention: No files require special attention.

Important Files Changed

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"]
Loading

Reviews (4): Last reviewed commit: "fix(gitlab): validate countDiffLines wit..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3075b35b-7097-4748-91fd-924d60225bb0

📥 Commits

Reviewing files that changed from the base of the PR and between 43304b1 and 669443c.

📒 Files selected for processing (2)
  • src/main/gitlab/work-item-details.test.ts
  • src/main/gitlab/work-item-details.ts

📝 Walkthrough

Walkthrough

countDiffLines is now exported and tracks whether parsing has entered a diff hunk. It skips file headers and counts removed lines that begin with repeated - characters. Tests cover standard, repeated-prefix, header-only, asymmetric, and multi-hunk diffs.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the GitLab diff-counting fix for content beginning with -- or ++.
Description check ✅ Passed The description covers the summary, testing, screenshots, AI review, security audit, and notes with specific implementation and validation details.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@YuriNachos
YuriNachos force-pushed the fix-gitlab-diff-line-counter branch from 5fc6d8d to 9bd968f Compare August 2, 2026 20:01
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]>
@YuriNachos
YuriNachos force-pushed the fix-gitlab-diff-line-counter branch from 9bd968f to 43304b1 Compare August 2, 2026 20:35
@AmethystLiang
AmethystLiang self-requested a review August 2, 2026 23:24
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.
@AmethystLiang
AmethystLiang merged commit d426e35 into stablyai:main Aug 3, 2026
1 check passed
YuriNachos added a commit to YuriNachos/orca that referenced this pull request Aug 3, 2026
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]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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]>
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.

2 participants