Repository navigation
Closed
Description
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 --cachedPitmaster 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
pitmaster/src/Diff/TreeDiff.php
Lines 92 to 101 in 5beb97d
| 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; |
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.
Metadata
Metadata
Assignees
Labels
No labels
Activity