Skip to content

Count unchanged lines in Text Comparison - #22

Open
SakarPaisen wants to merge 1 commit into
github-community-gitam:mainfrom
SakarPaisen:feat/diff-unchanged-lines-5
Open

SakarPaisen wants to merge 1 commit into
github-community-gitam:mainfrom
SakarPaisen:feat/diff-unchanged-lines-5

Conversation

@SakarPaisen

Copy link
Copy Markdown
Contributor

What

Closes #5.

compare_text now returns unchanged_lines, counted from the equal opcodes of the alignment it already builds. The unified diff string and the added_lines / removed_lines counts are untouched.

>>> {k: v for k, v in compare_text("hello\nold", "hello\nnew").items() if k != "diff"}
{'added_lines': 1, 'removed_lines': 1, 'unchanged_lines': 1}

Scope

  • src/mcp_tools_lab/tools/diff.py — one counter for tag == "equal", using old_end - old_start, plus a docstring line.
  • tests/test_diff.py — five new tests.
  • docs/tool-guide.md — describes the field and the added_lines + unchanged_lines == lines in after invariant.

Tests added

  • Documented example: "hello\nold" vs "hello\nnew" → unchanged_lines=1, added_lines=1, removed_lines=1.
  • Identical two-line input → 2; two empty inputs → 0.
  • Entirely different inputs → 0.
  • "same\r\n" vs "same" → 1, so the existing newline normalization applies to the count too.
  • Repeated lines "a\nb\nb\nc" vs "a\nb\nc\nc" → unchanged_lines=3, added_lines=1, removed_lines=1. I checked this against the real SequenceMatcher alignment instead of assuming where the repeated b/c pair up.

Checks

  • python -m pytest tests/test_diff.py -q — 7 passed
  • python -m pytest -q — 67 passed
  • python .github/scripts/check_rules.py — Scanned 11 files under src/ for print() and network imports: OK

Python: 3.13 on Windows 11.

Open item for the maintainers: if the case-insensitive comparison change (#13) adds an ignore_case option later, this count should follow the same comparison mode; the brief for #13 already says so. I left that out of scope here.

(Full disclosure: I used an AI coding assistant for this change, and I reviewed and verified everything submitted here.)

compare_text now reports unchanged_lines, the size of the equal opcodes in the alignment it already builds, so the diff string and the added/removed counts are unchanged. Tests cover the documented replacement example, identical and empty input, entirely different input, line-ending normalization, and repeated lines where the alignment is ambiguous.
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.

Count unchanged lines in Text Comparison

1 participant