1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-10-30 05:47:53 +01:00
git/t/t4054-diff-bogus-tree.sh
Jeff King e54501004a diff: do not use null sha1 as a sentinel value
The diff code represents paths using the diff_filespec
struct. This struct has a sha1 to represent the sha1 of the
content at that path, as well as a sha1_valid member which
indicates whether its sha1 field is actually useful. If
sha1_valid is not true, then the filespec represents a
working tree file (e.g., for the no-index case, or for when
the index is not up-to-date).

The diff_filespec is only used internally, though. At the
interfaces to the diff subsystem, callers feed the sha1
directly, and we create a diff_filespec from it. It's at
that point that we look at the sha1 and decide whether it is
valid or not; callers may pass the null sha1 as a sentinel
value to indicate that it is not.

We should not typically see the null sha1 coming from any
other source (e.g., in the index itself, or from a tree).
However, a corrupt tree might have a null sha1, which would
cause "diff --patch" to accidentally diff the working tree
version of a file instead of treating it as a blob.

This patch extends the edges of the diff interface to accept
a "sha1_valid" flag whenever we accept a sha1, and to use
that flag when creating a filespec. In some cases, this
means passing the flag through several layers, making the
code change larger than would be desirable.

One alternative would be to simply die() upon seeing
corrupted trees with null sha1s. However, this fix more
directly addresses the problem (while bogus sha1s in a tree
are probably a bad thing, it is really the sentinel
confusion sending us down the wrong code path that is what
makes it devastating). And it means that git is more capable
of examining and debugging these corrupted trees. For
example, you can still "diff --raw" such a tree to find out
when the bogus entry was introduced; you just cannot do a
"--patch" diff (just as you could not with any other
corrupted tree, as we do not have any content to diff).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-07-29 15:04:32 -07:00

83 lines
2.3 KiB
Bash
Executable file

#!/bin/sh
test_description='test diff with a bogus tree containing the null sha1'
. ./test-lib.sh
empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
test_expect_success 'create bogus tree' '
bogus_tree=$(
printf "100644 fooQQQQQQQQQQQQQQQQQQQQQ" |
q_to_nul |
git hash-object -w --stdin -t tree
)
'
test_expect_success 'create tree with matching file' '
echo bar >foo &&
git add foo &&
good_tree=$(git write-tree)
blob=$(git rev-parse :foo)
'
test_expect_success 'raw diff shows null sha1 (addition)' '
echo ":000000 100644 $_z40 $_z40 A foo" >expect &&
git diff-tree $empty_tree $bogus_tree >actual &&
test_cmp expect actual
'
test_expect_success 'raw diff shows null sha1 (removal)' '
echo ":100644 000000 $_z40 $_z40 D foo" >expect &&
git diff-tree $bogus_tree $empty_tree >actual &&
test_cmp expect actual
'
test_expect_success 'raw diff shows null sha1 (modification)' '
echo ":100644 100644 $blob $_z40 M foo" >expect &&
git diff-tree $good_tree $bogus_tree >actual &&
test_cmp expect actual
'
test_expect_success 'raw diff shows null sha1 (other direction)' '
echo ":100644 100644 $_z40 $blob M foo" >expect &&
git diff-tree $bogus_tree $good_tree >actual &&
test_cmp expect actual
'
test_expect_success 'raw diff shows null sha1 (reverse)' '
echo ":100644 100644 $_z40 $blob M foo" >expect &&
git diff-tree -R $good_tree $bogus_tree >actual &&
test_cmp expect actual
'
test_expect_success 'raw diff shows null sha1 (index)' '
echo ":100644 100644 $_z40 $blob M foo" >expect &&
git diff-index $bogus_tree >actual &&
test_cmp expect actual
'
test_expect_success 'patch fails due to bogus sha1 (addition)' '
test_must_fail git diff-tree -p $empty_tree $bogus_tree
'
test_expect_success 'patch fails due to bogus sha1 (removal)' '
test_must_fail git diff-tree -p $bogus_tree $empty_tree
'
test_expect_success 'patch fails due to bogus sha1 (modification)' '
test_must_fail git diff-tree -p $good_tree $bogus_tree
'
test_expect_success 'patch fails due to bogus sha1 (other direction)' '
test_must_fail git diff-tree -p $bogus_tree $good_tree
'
test_expect_success 'patch fails due to bogus sha1 (reverse)' '
test_must_fail git diff-tree -R -p $good_tree $bogus_tree
'
test_expect_success 'patch fails due to bogus sha1 (index)' '
test_must_fail git diff-index -p $bogus_tree
'
test_done