fsck: warn about '.' and '..' in trees

A tree with meta-paths like '.' or '..' does not work well
with git; the index will refuse to load it or check it out
to the filesystem (and even if we did not have that safety,
it would look like we were overwriting an untracked
directory). For the same reason, it is difficult to create
such a tree with regular git.

Let's warn about these dubious entries during fsck, just in
case somebody has created a bogus tree (and this also lets
us prevent them from propagating when transfer.fsckObjects
is set).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2012-11-27 21:27:37 -05:00 committed by Junio C Hamano
parent 7e2010537e
commit 5d34a4359d
2 changed files with 26 additions and 0 deletions

10
fsck.c
View file

@ -142,6 +142,8 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
int has_null_sha1 = 0;
int has_full_path = 0;
int has_empty_name = 0;
int has_dot = 0;
int has_dotdot = 0;
int has_zero_pad = 0;
int has_bad_modes = 0;
int has_dup_entries = 0;
@ -168,6 +170,10 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
has_full_path = 1;
if (!*name)
has_empty_name = 1;
if (!strcmp(name, "."))
has_dot = 1;
if (!strcmp(name, ".."))
has_dotdot = 1;
has_zero_pad |= *(char *)desc.buffer == '0';
update_tree_entry(&desc);
@ -217,6 +223,10 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");
if (has_empty_name)
retval += error_func(&item->object, FSCK_WARN, "contains empty pathname");
if (has_dot)
retval += error_func(&item->object, FSCK_WARN, "contains '.'");
if (has_dotdot)
retval += error_func(&item->object, FSCK_WARN, "contains '..'");
if (has_zero_pad)
retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes");
if (has_bad_modes)

View file

@ -237,4 +237,20 @@ test_expect_success 'fsck notices submodule entry pointing to null sha1' '
)
'
test_expect_success 'fsck notices "." and ".." in trees' '
(
git init dots &&
cd dots &&
blob=$(echo foo | git hash-object -w --stdin) &&
tab=$(printf "\\t") &&
git mktree <<-EOF &&
100644 blob $blob$tab.
100644 blob $blob$tab..
EOF
git fsck 2>out &&
cat out &&
grep "warning.*\\." out
)
'
test_done