Merge branch 'jk/complete-branch-force-delete'
[git] / t / t5312-prune-corruption.sh
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 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
12 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
13
14 . ./test-lib.sh
15
16 test_expect_success 'disable reflogs' '
17         git config core.logallrefupdates false &&
18         git reflog expire --expire=all --all
19 '
20
21 test_expect_success 'create history reachable only from a bogus-named ref' '
22         test_tick && git commit --allow-empty -m main &&
23         base=$(git rev-parse HEAD) &&
24         test_tick && git commit --allow-empty -m bogus &&
25         bogus=$(git rev-parse HEAD) &&
26         git cat-file commit $bogus >saved &&
27         echo $bogus >.git/refs/heads/bogus..name &&
28         git reset --hard HEAD^
29 '
30
31 test_expect_success 'pruning does not drop bogus object' '
32         test_when_finished "git hash-object -w -t commit saved" &&
33         test_might_fail git prune --expire=now &&
34         verbose git cat-file -e $bogus
35 '
36
37 test_expect_success 'put bogus object into pack' '
38         git tag reachable $bogus &&
39         git repack -ad &&
40         git tag -d reachable &&
41         verbose git cat-file -e $bogus
42 '
43
44 test_expect_success 'destructive repack keeps packed object' '
45         test_might_fail git repack -Ad --unpack-unreachable=now &&
46         verbose git cat-file -e $bogus &&
47         test_might_fail git repack -ad &&
48         verbose git cat-file -e $bogus
49 '
50
51 # subsequent tests will have different corruptions
52 test_expect_success 'clean up bogus ref' '
53         rm .git/refs/heads/bogus..name
54 '
55
56 # We create two new objects here, "one" and "two". Our
57 # main branch points to "two", which is deleted,
58 # corrupting the repository. But we'd like to make sure
59 # that the otherwise unreachable "one" is not pruned
60 # (since it is the user's best bet for recovering
61 # from the corruption).
62 #
63 # Note that we also point HEAD somewhere besides "two",
64 # as we want to make sure we test the case where we
65 # pick up the reference to "two" by iterating the refs,
66 # not by resolving HEAD.
67 test_expect_success 'create history with missing tip commit' '
68         test_tick && git commit --allow-empty -m one &&
69         recoverable=$(git rev-parse HEAD) &&
70         git cat-file commit $recoverable >saved &&
71         test_tick && git commit --allow-empty -m two &&
72         missing=$(git rev-parse HEAD) &&
73         git checkout --detach $base &&
74         rm .git/objects/$(echo $missing | sed "s,..,&/,") &&
75         test_must_fail git cat-file -e $missing
76 '
77
78 test_expect_success 'pruning with a corrupted tip does not drop history' '
79         test_when_finished "git hash-object -w -t commit saved" &&
80         test_might_fail git prune --expire=now &&
81         verbose git cat-file -e $recoverable
82 '
83
84 test_expect_success 'pack-refs does not silently delete broken loose ref' '
85         git pack-refs --all --prune &&
86         echo $missing >expect &&
87         git rev-parse refs/heads/main >actual &&
88         test_cmp expect actual
89 '
90
91 # we do not want to count on running pack-refs to
92 # actually pack it, as it is perfectly reasonable to
93 # skip processing a broken ref
94 test_expect_success 'create packed-refs file with broken ref' '
95         rm -f .git/refs/heads/main &&
96         cat >.git/packed-refs <<-EOF &&
97         $missing refs/heads/main
98         $recoverable refs/heads/other
99         EOF
100         echo $missing >expect &&
101         git rev-parse refs/heads/main >actual &&
102         test_cmp expect actual
103 '
104
105 test_expect_success 'pack-refs does not silently delete broken packed ref' '
106         git pack-refs --all --prune &&
107         git rev-parse refs/heads/main >actual &&
108         test_cmp expect actual
109 '
110
111 test_expect_success 'pack-refs does not drop broken refs during deletion' '
112         git update-ref -d refs/heads/other &&
113         git rev-parse refs/heads/main >actual &&
114         test_cmp expect actual
115 '
116
117 test_done