]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5312-prune-corruption.sh
tests: mark tests relying on the current default for `init.defaultBranch`
[thirdparty/git.git] / t / t5312-prune-corruption.sh
CommitLineData
8b43fb18
JK
1#!/bin/sh
2
3test_description='
4Test pruning of repositories with minor corruptions. The goal
5here is that we should always be erring on the side of safety. So
6if we see, for example, a ref with a bogus name, it is OK either to
7bail out or to proceed using it as a reachable tip, but it is _not_
8OK to proceed as if it did not exist. Otherwise we might silently
9delete objects that cannot be recovered.
10'
334afbc7
JS
11GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
12export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
13
8b43fb18
JK
14. ./test-lib.sh
15
16test_expect_success 'disable reflogs' '
17 git config core.logallrefupdates false &&
d0ab0584 18 git reflog expire --expire=all --all
8b43fb18
JK
19'
20
21test_expect_success 'create history reachable only from a bogus-named ref' '
22 test_tick && git commit --allow-empty -m master &&
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
ff4056bb 31test_expect_success 'pruning does not drop bogus object' '
8b43fb18
JK
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
37test_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
8d422993 44test_expect_success 'destructive repack keeps packed object' '
8b43fb18
JK
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
52test_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# master 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.
67test_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
ff4056bb 78test_expect_success 'pruning with a corrupted tip does not drop history' '
8b43fb18
JK
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
84test_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/master >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
94test_expect_success 'create packed-refs file with broken ref' '
95 rm -f .git/refs/heads/master &&
96 cat >.git/packed-refs <<-EOF &&
97 $missing refs/heads/master
98 $recoverable refs/heads/other
99 EOF
100 echo $missing >expect &&
101 git rev-parse refs/heads/master >actual &&
102 test_cmp expect actual
103'
104
105test_expect_success 'pack-refs does not silently delete broken packed ref' '
106 git pack-refs --all --prune &&
107 git rev-parse refs/heads/master >actual &&
108 test_cmp expect actual
109'
110
ea56c4e0 111test_expect_success 'pack-refs does not drop broken refs during deletion' '
8b43fb18
JK
112 git update-ref -d refs/heads/other &&
113 git rev-parse refs/heads/master >actual &&
114 test_cmp expect actual
115'
116
117test_done