Skip to content

Diff output differs from git for new, deleted, binary, renamed and mode-changed files #3

Closed

Description

@dnnsjsk

The docs and README say diff output is byte-exact with git diff (

Pitmaster computes diffs that are byte-exact with `git diff` output. The diff engine supports multiple algorithms, unified diff formatting, binary file detection, and tree-level recursive comparison.
,
- Compute diffs (Myers O(ND) algorithm, byte-exact with `git diff`)
). That holds for modified text files with mode 100644. Several cases differ from git 2.43:

Case git Pitmaster (DiffResult::format() and pitmaster diff)
New file new file mode 100644, index 0000000..<new>, --- /dev/null, @@ -0,0 +1 @@ no mode line, no index line, --- a/<path>, @@ -1,0 +1 @@
Deleted file deleted file mode 100644, index <old>..0000000, +++ /dev/null, @@ -1 +0,0 @@ no mode line, no index line, +++ b/<path>
Mode the real mode (100755, 120000, ...) always 100644 in the index line
Mode-only change old mode / new mode nothing (TreeDiff skips entries with equal hashes)
Binary index a..b 100644 + Binary files a/x and b/x differ Binary files differ, no index line
Rename similarity index, rename from, rename to plain diff --git a/old b/new with a content diff
CLI output to a pipe no colour unless asked always ANSI-coloured (commandDiff colorizes unconditionally)

Repro

git init -q t && cd t && git config user.name T && git config user.email t@x
printf 'one\ntwo\n' > m.txt && printf 'gone\n' > d.txt && printf '\x00\x01' > bin.dat
git add . && git commit -qm init
printf 'one\nTWO\n' > m.txt && git rm -q d.txt && printf '\x00\x02' > bin.dat && git add -A
pitmaster diff --cached | cat -v
git diff --cached

A staged new file alone shows the header difference:

# pitmaster diff --cached          # git diff --cached
diff --git a/b.txt b/b.txt         diff --git a/b.txt b/b.txt
--- a/b.txt                        new file mode 100644
+++ b/b.txt                        index 0000000..3e75765
@@ -1,0 +1 @@                      --- /dev/null
+new                               +++ b/b.txt
                                   @@ -0,0 +1 @@
                                   +new

Source

  • public function format(): string
    {
    if ($this->binary) {
    return "diff --git a/{$this->oldPath} b/{$this->newPath}\n"
    . "Binary files differ\n";
    }
    if ($this->hunks === []) {
    return '';
    }
    $lines = [];
    $lines[] = "diff --git a/{$this->oldPath} b/{$this->newPath}";
    if ($this->oldHash !== null && $this->newHash !== null) {
    $lines[] = "index " . substr($this->oldHash, 0, 7) . ".." . substr($this->newHash, 0, 7) . " 100644";
    }
    $lines[] = "--- a/{$this->oldPath}";
    $lines[] = "+++ b/{$this->newPath}";
    : no new/deleted/mode headers, /dev/null never used, mode hard-coded to 100644 (L48), binary message at L37.
  • public function header(): string
    {
    $old = $this->oldCount === 1 ? "-{$this->oldStart}" : "-{$this->oldStart},{$this->oldCount}";
    $new = $this->newCount === 1 ? "+{$this->newStart}" : "+{$this->newStart},{$this->newCount}";
    $section = $this->section !== null && $this->section !== '' ? ' ' . $this->section : '';
    return "@@ {$old} {$new} @@{$section}";
    }
    : an empty side prints -1,0 instead of git's -0,0.
  • if ($oldEntry['hash'] === $newEntry['hash']) {
    continue;
    }
    : equal hashes are skipped, so mode-only changes vanish; modes are not passed to DiffResult.
  • $other[] = new DiffResult(
    $del->oldPath,
    $add->newPath,
    $hunks,
    false,
    $del->oldHash,
    $add->newHash,
    );
    : rename results carry no rename metadata.
  • pitmaster/bin/pitmaster

    Lines 433 to 435 in 5beb97d

    foreach ($results as $diff) {
    fwrite(STDOUT, ColorDiff::colorize($diff->format()));
    }
    : ColorDiff::colorize() is applied even when stdout is not a terminal (show checks stream_isatty(), diff does not).

Expected

Either output matching git for these cases, or docs that do not claim byte-exactness. The docs are being updated to describe the current output.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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