Skip to content

Rename detection pairs unrelated single-line files #4

Closed

Description

@dnnsjsk

Rename detection in TreeDiff pairs a deleted file with an added file even when they share no content, as long as both are one line long. diffStaged(), diffTree(), show() and pitmaster diff --cached then print one combined diff --git a/old b/new entry instead of a deletion and an addition.

Repro

git init -q t && cd t && git config user.name T && git config user.email t@x
printf 'gone\n' > d.txt && git add . && git commit -qm init
git rm -q d.txt && printf 'new\n' > n.txt && git add n.txt
pitmaster diff --cached

Pitmaster prints diff --git a/d.txt b/n.txt with -gone / +new. git reports d.txt deleted and n.txt added (no rename; the files are 0% similar).

Cause

if ($oldContent !== '' && $newContent !== '') {
$oldLines = explode("\n", $oldContent);
$newLines = explode("\n", $newContent);
$common = count(array_intersect($oldLines, $newLines));
$total = max(count($oldLines), count($newLines));
$score = (int) (($common / $total) * 100);
if ($score > $bestScore && $score >= 50) {
$bestScore = $score;
$bestIdx = $ai;
splits both blobs with explode("\n", ...). Content ending in a newline yields a trailing empty string, which array_intersect() counts as a shared line. For two single-line files that is 1 of 2 lines, a score of 50, which passes the >= 50 threshold. Larger files get the same one-line bonus, and array_intersect() also counts repeated lines (blank lines, braces) multiple times, so the score is generally higher than git's similarity index.

Expected

Files with no common content are reported as a deletion and an addition; the similarity measure ignores the trailing empty element.

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