Skip to content

diff.print({ format: "Patch" }) strips +/-/space line origin markers #212

Description

@monotykamary

What happened

diff.print({ format: "Patch" }) returns patch output that has correct ---/+++/@@ headers but is missing the +/-/ (space) prefixes on the body lines inside hunks.

What I expected

Proper unified diff output, identical to what git diff --patch produces, where every line inside a hunk is prefixed with + (addition), - (deletion), or (space, context).

Reproduction

const { Repository } = require("@es-git/repository");
const repo = await Repository.open("/path/to/dirty-repo");
const diff = await repo.diffIndexToWorkdir();
const patch = diff.print({ format: "Patch" });
console.log(patch);

Compare the output with git diff --patch in the same repo. The hunk bodies will look like this:

es-git output (broken):

@@ -1,3 +1,3 @@
 alpha
beta
 gamma

git diff output (correct):

@@ -1,3 +1,3 @@
  alpha
-beta
+MORE
  gamma

The +/-/ prefixes are completely absent from the es-git output.

Root cause

In src/diff.rs, the print method does:

let _ = self.inner.print(format.into(), |_delta, _hunk, line| {
    if let Ok(content) = std::str::from_utf8(line.content()) {
        lines.push(content.to_string());
    }
    true
});

It uses line.content() which returns the line body without the origin character. The origin (+, -, ) is available via line.origin(). It needs to be prepended:

let _ = self.inner.print(format.into(), |_delta, _hunk, line| {
    if let Ok(content) = std::str::from_utf8(line.content()) {
        lines.push(format!("{}{}", line.origin(), content));
    }
    true
});

This affects all DiffFormat variants that go through print, not just Patch, since the callback is the same.

Impact

Consumers that parse the patch output (e.g. to render diffs in a UI) cannot distinguish additions, deletions, and context lines without the origin markers. This makes the print() method unusable for any PATCH-format output that needs to be parsed downstream.

Environment

  • es-git: latest (as of 2026-06-14)
  • Node.js: v22.x
  • OS: macOS 15

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions