Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 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. |
Jeff King | 5b062e1 | 2021-09-24 14:37:11 -0400 | [diff] [blame] | 10 | |
| 11 | Note that we do assert command failure in these cases, because that is |
| 12 | what currently happens. If that changes, these tests should be revisited. |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 13 | ' |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 14 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
Johannes Schindelin | 334afbc | 2020-11-18 23:44:19 +0000 | [diff] [blame] | 15 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 16 | |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 17 | . ./test-lib.sh |
| 18 | |
| 19 | test_expect_success 'disable reflogs' ' |
| 20 | git config core.logallrefupdates false && |
David Turner | d0ab058 | 2015-07-27 18:57:08 -0400 | [diff] [blame] | 21 | git reflog expire --expire=all --all |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 22 | ' |
| 23 | |
Jeff King | f805844 | 2021-09-24 14:36:18 -0400 | [diff] [blame] | 24 | create_bogus_ref () { |
| 25 | test_when_finished 'rm -f .git/refs/heads/bogus..name' && |
| 26 | echo $bogus >.git/refs/heads/bogus..name |
| 27 | } |
| 28 | |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 29 | test_expect_success 'create history reachable only from a bogus-named ref' ' |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 30 | test_tick && git commit --allow-empty -m main && |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 31 | base=$(git rev-parse HEAD) && |
| 32 | test_tick && git commit --allow-empty -m bogus && |
| 33 | bogus=$(git rev-parse HEAD) && |
| 34 | git cat-file commit $bogus >saved && |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 35 | git reset --hard HEAD^ |
| 36 | ' |
| 37 | |
Jeff King | ff4056b | 2015-03-20 14:43:09 -0400 | [diff] [blame] | 38 | test_expect_success 'pruning does not drop bogus object' ' |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 39 | test_when_finished "git hash-object -w -t commit saved" && |
Jeff King | f805844 | 2021-09-24 14:36:18 -0400 | [diff] [blame] | 40 | create_bogus_ref && |
Jeff King | 5b062e1 | 2021-09-24 14:37:11 -0400 | [diff] [blame] | 41 | test_must_fail git prune --expire=now && |
Jeff King | 2ac0cbc | 2021-09-24 14:35:29 -0400 | [diff] [blame] | 42 | git cat-file -e $bogus |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 43 | ' |
| 44 | |
| 45 | test_expect_success 'put bogus object into pack' ' |
| 46 | git tag reachable $bogus && |
| 47 | git repack -ad && |
| 48 | git tag -d reachable && |
Jeff King | 2ac0cbc | 2021-09-24 14:35:29 -0400 | [diff] [blame] | 49 | git cat-file -e $bogus |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 50 | ' |
| 51 | |
Jeff King | 968f12f | 2021-09-24 14:46:13 -0400 | [diff] [blame] | 52 | test_expect_success 'non-destructive repack bails on bogus ref' ' |
Jeff King | 078eecb | 2021-09-24 14:36:42 -0400 | [diff] [blame] | 53 | create_bogus_ref && |
Jeff King | 968f12f | 2021-09-24 14:46:13 -0400 | [diff] [blame] | 54 | test_must_fail git repack -adk |
Jeff King | 078eecb | 2021-09-24 14:36:42 -0400 | [diff] [blame] | 55 | ' |
| 56 | |
Jeff King | 968f12f | 2021-09-24 14:46:13 -0400 | [diff] [blame] | 57 | test_expect_success 'GIT_REF_PARANOIA=0 overrides safety' ' |
| 58 | create_bogus_ref && |
| 59 | GIT_REF_PARANOIA=0 git repack -adk |
| 60 | ' |
| 61 | |
| 62 | |
Jeff King | 8d42299 | 2015-03-20 14:43:13 -0400 | [diff] [blame] | 63 | test_expect_success 'destructive repack keeps packed object' ' |
Jeff King | f805844 | 2021-09-24 14:36:18 -0400 | [diff] [blame] | 64 | create_bogus_ref && |
Jeff King | 5b062e1 | 2021-09-24 14:37:11 -0400 | [diff] [blame] | 65 | test_must_fail git repack -Ad --unpack-unreachable=now && |
Jeff King | 2ac0cbc | 2021-09-24 14:35:29 -0400 | [diff] [blame] | 66 | git cat-file -e $bogus && |
Jeff King | 5b062e1 | 2021-09-24 14:37:11 -0400 | [diff] [blame] | 67 | test_must_fail git repack -ad && |
Jeff King | 2ac0cbc | 2021-09-24 14:35:29 -0400 | [diff] [blame] | 68 | git cat-file -e $bogus |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 69 | ' |
| 70 | |
Jeff King | 6d751be | 2021-09-24 14:42:38 -0400 | [diff] [blame] | 71 | test_expect_success 'destructive repack not confused by dangling symref' ' |
| 72 | test_when_finished "git symbolic-ref -d refs/heads/dangling" && |
| 73 | git symbolic-ref refs/heads/dangling refs/heads/does-not-exist && |
| 74 | git repack -ad && |
| 75 | test_must_fail git cat-file -e $bogus |
| 76 | ' |
| 77 | |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 78 | # We create two new objects here, "one" and "two". Our |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 79 | # main branch points to "two", which is deleted, |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 80 | # corrupting the repository. But we'd like to make sure |
| 81 | # that the otherwise unreachable "one" is not pruned |
| 82 | # (since it is the user's best bet for recovering |
| 83 | # from the corruption). |
| 84 | # |
| 85 | # Note that we also point HEAD somewhere besides "two", |
| 86 | # as we want to make sure we test the case where we |
| 87 | # pick up the reference to "two" by iterating the refs, |
| 88 | # not by resolving HEAD. |
| 89 | test_expect_success 'create history with missing tip commit' ' |
| 90 | test_tick && git commit --allow-empty -m one && |
| 91 | recoverable=$(git rev-parse HEAD) && |
| 92 | git cat-file commit $recoverable >saved && |
| 93 | test_tick && git commit --allow-empty -m two && |
| 94 | missing=$(git rev-parse HEAD) && |
| 95 | git checkout --detach $base && |
| 96 | rm .git/objects/$(echo $missing | sed "s,..,&/,") && |
| 97 | test_must_fail git cat-file -e $missing |
| 98 | ' |
| 99 | |
Jeff King | ff4056b | 2015-03-20 14:43:09 -0400 | [diff] [blame] | 100 | test_expect_success 'pruning with a corrupted tip does not drop history' ' |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 101 | test_when_finished "git hash-object -w -t commit saved" && |
Jeff King | 5b062e1 | 2021-09-24 14:37:11 -0400 | [diff] [blame] | 102 | test_must_fail git prune --expire=now && |
Jeff King | 2ac0cbc | 2021-09-24 14:35:29 -0400 | [diff] [blame] | 103 | git cat-file -e $recoverable |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 104 | ' |
| 105 | |
| 106 | test_expect_success 'pack-refs does not silently delete broken loose ref' ' |
| 107 | git pack-refs --all --prune && |
| 108 | echo $missing >expect && |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 109 | git rev-parse refs/heads/main >actual && |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 110 | test_cmp expect actual |
| 111 | ' |
| 112 | |
| 113 | # we do not want to count on running pack-refs to |
| 114 | # actually pack it, as it is perfectly reasonable to |
| 115 | # skip processing a broken ref |
| 116 | test_expect_success 'create packed-refs file with broken ref' ' |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 117 | rm -f .git/refs/heads/main && |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 118 | cat >.git/packed-refs <<-EOF && |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 119 | $missing refs/heads/main |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 120 | $recoverable refs/heads/other |
| 121 | EOF |
| 122 | echo $missing >expect && |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 123 | git rev-parse refs/heads/main >actual && |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 124 | test_cmp expect actual |
| 125 | ' |
| 126 | |
| 127 | test_expect_success 'pack-refs does not silently delete broken packed ref' ' |
| 128 | git pack-refs --all --prune && |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 129 | git rev-parse refs/heads/main >actual && |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 130 | test_cmp expect actual |
| 131 | ' |
| 132 | |
Jeff King | ea56c4e | 2015-03-20 14:43:17 -0400 | [diff] [blame] | 133 | test_expect_success 'pack-refs does not drop broken refs during deletion' ' |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 134 | git update-ref -d refs/heads/other && |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 135 | git rev-parse refs/heads/main >actual && |
Jeff King | 8b43fb1 | 2015-03-20 14:43:02 -0400 | [diff] [blame] | 136 | test_cmp expect actual |
| 137 | ' |
| 138 | |
| 139 | test_done |