Skip to content

Commit 8b43fb1

Browse files
peffgitster
authored andcommitted
t5312: test object deletion code paths in a corrupted repository
When we are doing a destructive operation like "git prune", we want to be extra careful that the set of reachable tips we compute is valid. If there is any corruption or oddity, we are better off aborting the operation and letting the user figure things out rather than plowing ahead and possibly deleting some data that cannot be recovered. The tests here include: 1. Pruning objects mentioned only be refs with invalid names. This used to abort prior to d0f810f (refs.c: allow listing and deleting badly named refs, 2014-09-03), but since then we silently ignore the tip. Likewise, we test repacking that can drop objects (either "-ad", which drops anything unreachable, or "-Ad --unpack-unreachable=<time>", which tries to optimize out a loose object write that would be directly pruned). 2. Pruning objects when some refs point to missing objects. We don't know whether any dangling objects would have been reachable from the missing objects. We are better to keep them around, as they are better than nothing for helping the user recover history. 3. Packed refs that point to missing objects can sometimes be dropped. By itself, this is more of an annoyance (you do not have the object anyway; even if you can recover it from elsewhere, all you are losing is a placeholder for your state at the time of corruption). But coupled with (2), if we drop the ref and then go on to prune, we may lose unrecoverable objects. Note that we use test_might_fail for some of the operations. In some cases, it would be appropriate to abort the operation, and in others, it might be acceptable to continue but taking the information into account. The tests don't care either way, and check only for data loss. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bb85775 commit 8b43fb1

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

t/t5312-prune-corruption.sh

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/sh
2+
3+
test_description='
4+
Test pruning of repositories with minor corruptions. The goal
5+
here is that we should always be erring on the side of safety. So
6+
if we see, for example, a ref with a bogus name, it is OK either to
7+
bail out or to proceed using it as a reachable tip, but it is _not_
8+
OK to proceed as if it did not exist. Otherwise we might silently
9+
delete objects that cannot be recovered.
10+
'
11+
. ./test-lib.sh
12+
13+
test_expect_success 'disable reflogs' '
14+
git config core.logallrefupdates false &&
15+
rm -rf .git/logs
16+
'
17+
18+
test_expect_success 'create history reachable only from a bogus-named ref' '
19+
test_tick && git commit --allow-empty -m master &&
20+
base=$(git rev-parse HEAD) &&
21+
test_tick && git commit --allow-empty -m bogus &&
22+
bogus=$(git rev-parse HEAD) &&
23+
git cat-file commit $bogus >saved &&
24+
echo $bogus >.git/refs/heads/bogus..name &&
25+
git reset --hard HEAD^
26+
'
27+
28+
test_expect_failure 'pruning does not drop bogus object' '
29+
test_when_finished "git hash-object -w -t commit saved" &&
30+
test_might_fail git prune --expire=now &&
31+
verbose git cat-file -e $bogus
32+
'
33+
34+
test_expect_success 'put bogus object into pack' '
35+
git tag reachable $bogus &&
36+
git repack -ad &&
37+
git tag -d reachable &&
38+
verbose git cat-file -e $bogus
39+
'
40+
41+
test_expect_failure 'destructive repack keeps packed object' '
42+
test_might_fail git repack -Ad --unpack-unreachable=now &&
43+
verbose git cat-file -e $bogus &&
44+
test_might_fail git repack -ad &&
45+
verbose git cat-file -e $bogus
46+
'
47+
48+
# subsequent tests will have different corruptions
49+
test_expect_success 'clean up bogus ref' '
50+
rm .git/refs/heads/bogus..name
51+
'
52+
53+
# We create two new objects here, "one" and "two". Our
54+
# master branch points to "two", which is deleted,
55+
# corrupting the repository. But we'd like to make sure
56+
# that the otherwise unreachable "one" is not pruned
57+
# (since it is the user's best bet for recovering
58+
# from the corruption).
59+
#
60+
# Note that we also point HEAD somewhere besides "two",
61+
# as we want to make sure we test the case where we
62+
# pick up the reference to "two" by iterating the refs,
63+
# not by resolving HEAD.
64+
test_expect_success 'create history with missing tip commit' '
65+
test_tick && git commit --allow-empty -m one &&
66+
recoverable=$(git rev-parse HEAD) &&
67+
git cat-file commit $recoverable >saved &&
68+
test_tick && git commit --allow-empty -m two &&
69+
missing=$(git rev-parse HEAD) &&
70+
git checkout --detach $base &&
71+
rm .git/objects/$(echo $missing | sed "s,..,&/,") &&
72+
test_must_fail git cat-file -e $missing
73+
'
74+
75+
test_expect_failure 'pruning with a corrupted tip does not drop history' '
76+
test_when_finished "git hash-object -w -t commit saved" &&
77+
test_might_fail git prune --expire=now &&
78+
verbose git cat-file -e $recoverable
79+
'
80+
81+
test_expect_success 'pack-refs does not silently delete broken loose ref' '
82+
git pack-refs --all --prune &&
83+
echo $missing >expect &&
84+
git rev-parse refs/heads/master >actual &&
85+
test_cmp expect actual
86+
'
87+
88+
# we do not want to count on running pack-refs to
89+
# actually pack it, as it is perfectly reasonable to
90+
# skip processing a broken ref
91+
test_expect_success 'create packed-refs file with broken ref' '
92+
rm -f .git/refs/heads/master &&
93+
cat >.git/packed-refs <<-EOF &&
94+
$missing refs/heads/master
95+
$recoverable refs/heads/other
96+
EOF
97+
echo $missing >expect &&
98+
git rev-parse refs/heads/master >actual &&
99+
test_cmp expect actual
100+
'
101+
102+
test_expect_success 'pack-refs does not silently delete broken packed ref' '
103+
git pack-refs --all --prune &&
104+
git rev-parse refs/heads/master >actual &&
105+
test_cmp expect actual
106+
'
107+
108+
test_expect_failure 'pack-refs does not drop broken refs during deletion' '
109+
git update-ref -d refs/heads/other &&
110+
git rev-parse refs/heads/master >actual &&
111+
test_cmp expect actual
112+
'
113+
114+
test_done

0 commit comments

Comments
 (0)