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>
Jeff King committed
Nov 27, 2012 at 21:27 UTC
5d34a4359d51b44483a1e62dc8a1f6ec686c8b6f
2 files changed
+26
fsck.c
+10
@@ -142,6 +142,8 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
142
int has_null_sha1 = 0;
143
int has_full_path = 0;
144
int has_empty_name = 0;
145
+ int has_dot = 0;
146
+ int has_dotdot = 0;
147
int has_zero_pad = 0;
148
int has_bad_modes = 0;
149
int has_dup_entries = 0;
@@ -168,6 +170,10 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
170
has_full_path = 1;
171
if (!*name)
172
has_empty_name = 1;
173
+ if (!strcmp(name, "."))
174
+ has_dot = 1;
175
+ if (!strcmp(name, ".."))
176
+ has_dotdot = 1;
177
has_zero_pad |= *(char *)desc.buffer == '0';
178
update_tree_entry(&desc);
179
@@ -217,6 +223,10 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
223
retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");
224
if (has_empty_name)
225
retval += error_func(&item->object, FSCK_WARN, "contains empty pathname");
226
+ if (has_dot)
227
+ retval += error_func(&item->object, FSCK_WARN, "contains '.'");
228
+ if (has_dotdot)
229
+ retval += error_func(&item->object, FSCK_WARN, "contains '..'");
230
if (has_zero_pad)
231
retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes");
232
if (has_bad_modes)
t/t1450-fsck.sh
+16
@@ -237,4 +237,20 @@ test_expect_success 'fsck notices submodule entry pointing to null sha1' '
237
)
238
'
239
240
+test_expect_success 'fsck notices "." and ".." in trees' '
241
+ (
242
+ git init dots &&
243
+ cd dots &&
244
+ blob=$(echo foo | git hash-object -w --stdin) &&
245
+ tab=$(printf "\\t") &&
246
+ git mktree <<-EOF &&
247
+ 100644 blob $blob$tab.
248
+ 100644 blob $blob$tab..
249
+ EOF
250
+ git fsck 2>out &&
251
+ cat out &&
252
+ grep "warning.*\\." out
253
+ )
254
+'
255
+
256
test_done