Skip to content

Conversation

@elwynelwyn
Copy link

@elwynelwyn elwynelwyn commented Oct 29, 2025

Fix for #16

When creating a diff file where old / new content is identical, currently there is a crash - because comparing the same content will have no hunks, and code always tries to create hunks.

This PR changes diff-parse from a do/while loop, to a while loop, to ensure peek() is called initially before trying to create hunks.

For the case content is identical, empty hunks array will be created instead of Expected hunk header but reached end of diff exception.

Test case to reproduce the bug / demonstrate the fix
#!/usr/bin/env node

// build package first
import { generateDiffFile } from "./packages/file/index.js";

const identicalContent = `hello world
line 2
line 3`;

try {
  const diffFile = generateDiffFile(
    "test.txt",
    identicalContent,
    "test.txt",
    identicalContent,
    "txt",
    "txt"
  );

  diffFile.init();

  diffFile.buildSplitDiffLines();

  diffFile.buildUnifiedDiffLines();

  console.log(`diff file built:
- Additions: ${diffFile.additionLength}
- Deletions: ${diffFile.deletionLength}
- Split lines: ${diffFile.splitLineLength}
- Unified lines: ${diffFile.unifiedLineLength}`);
} catch (error) {
  console.error(`oh no: ${error.message}`, error);
}

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.

1 participant