Skip to content

diffStaged() ignores its pathspec and returns nothing before the first commit #5

Closed

Description

@dnnsjsk

Two problems in Repository::diffStaged() (

pitmaster/src/Repository.php

Lines 2214 to 2235 in 5beb97d

public function diffStaged(?string $pathspec = null, string $algorithm = 'myers'): array
{
$headId = $this->refs->resolveHead();
if ($headId === null) {
return [];
}
$commit = $this->objects->read($headId);
if (!$commit instanceof Commit) {
return [];
}
$index = $this->index();
$treeDiff = new TreeDiff($this->objects, DiffAlgorithmName::normalize($algorithm));
// Build a tree from the index
$indexTreeId = $this->buildTreeFromIndex($index);
return $treeDiff->diff($commit->tree, $indexTreeId);
}
):

  1. The $pathspec argument is accepted and documented ($repo->diffStaged('src/specific-file.php') in docs/operations/diff.md) but never used, so every staged file is returned.
  2. Before the first commit (no HEAD) it returns [] (L2218-L2220). git shows every staged file as a new file in that state (git diff --cached), so pitmaster diff --cached prints nothing for a fresh repository.

Repro

// repo with a.txt and b.txt committed, then both modified and staged
count($repo->diffStaged('a.txt')); // 2, expected 1
pitmaster init && printf 'x\n' > a.txt && pitmaster add a.txt
pitmaster diff --cached   # prints nothing; git diff --cached shows a.txt as a new file

Expected

Only entries whose old or new path equals the pathspec are returned, and an unborn HEAD is compared against the empty tree.

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