Skip to content

Commit

Permalink
diffcore-delta: avoid ignoring final 'line' of file
Browse files Browse the repository at this point in the history
hash_chars() would hash lines to integers, and store them in a spanhash,
but cut lines at 64 characters.  Thus, whenever it reached 64 characters
or a newline, it would create a new spanhash.  The problem is, the final
part of the file might not end 64 characters after the previous 'line'
and might not end with a newline.  This could, for example, cause an
85-byte file with 12 lines and only the first character in the file
differing to appear merely 23% similar rather than the expected 97%.
Ensure the last line is included, and add a testcase that would have
caught this problem.

Signed-off-by: Elijah Newren <newren@gmail.com>
  • Loading branch information
newren committed Jan 13, 2024
1 parent 055bb6e commit e0223bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions diffcore-delta.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ static struct spanhash_top *hash_chars(struct repository *r,
n = 0;
accum1 = accum2 = 0;
}
if (n > 0) {
hashval = (accum1 + accum2 * 0x61) % HASHBASE;
hash = add_spanhash(hash, hashval, n);
}
QSORT(hash->data, (size_t)1ul << hash->alloc_log2, spanhash_cmp);
return hash;
}
Expand Down
24 changes: 24 additions & 0 deletions t/t4001-diff-rename.sh
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,28 @@ test_expect_success 'basename similarity vs best similarity' '
test_cmp expected actual
'

test_expect_success 'last line matters too' '
{
test_write_lines a 0 1 2 3 4 5 6 7 8 9 &&
printf "git ignores final up to 63 characters if not newline terminated"
} >no-final-lf &&
git add no-final-lf &&
git commit -m "original version of file with no final newline" &&
# Change ONLY the first character of the whole file
{
test_write_lines b 0 1 2 3 4 5 6 7 8 9 &&
printf "git ignores final up to 63 characters if not newline terminated"
} >no-final-lf &&
git add no-final-lf &&
git mv no-final-lf still-absent-final-lf &&
git commit -a -m "rename no-final-lf -> still-absent-final-lf" &&
git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
sed -e "s/^R[0-9]* /R /" actual >actual.munged &&
cat >expected <<-\EOF &&
R no-final-lf still-absent-final-lf
EOF
test_cmp expected actual.munged
'

test_done

0 comments on commit e0223bb

Please sign in to comment.