Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (C) 2018 Antonio Ospite <ao2@ao2.it> |
| 4 | # |
| 5 | |
| 6 | test_description='Test reading/writing .gitmodules when not in the working tree |
| 7 | |
| 8 | This test verifies that, when .gitmodules is in the current branch but is not |
| 9 | in the working tree reading from it still works but writing to it does not. |
| 10 | |
| 11 | The test setup uses a sparse checkout, however the same scenario can be set up |
| 12 | also by committing .gitmodules and then just removing it from the filesystem. |
| 13 | ' |
| 14 | |
Jonathan Tan | 13a2f62 | 2021-10-08 14:08:19 -0700 | [diff] [blame] | 15 | GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1 |
| 16 | export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB |
| 17 | |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 18 | . ./test-lib.sh |
| 19 | |
Taylor Blau | 0d3beb7 | 2022-07-29 15:21:40 -0400 | [diff] [blame] | 20 | test_expect_success 'setup' ' |
| 21 | git config --global protocol.file.allow always |
| 22 | ' |
| 23 | |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 24 | test_expect_success 'sparse checkout setup which hides .gitmodules' ' |
| 25 | git init upstream && |
| 26 | git init submodule && |
| 27 | (cd submodule && |
| 28 | echo file >file && |
| 29 | git add file && |
| 30 | test_tick && |
| 31 | git commit -m "Add file" |
| 32 | ) && |
| 33 | (cd upstream && |
| 34 | git submodule add ../submodule && |
| 35 | test_tick && |
| 36 | git commit -m "Add submodule" |
| 37 | ) && |
Ævar Arnfjörð Bjarmason | ead7460 | 2022-06-03 13:15:09 +0200 | [diff] [blame] | 38 | git clone --template= upstream super && |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 39 | (cd super && |
Ævar Arnfjörð Bjarmason | ead7460 | 2022-06-03 13:15:09 +0200 | [diff] [blame] | 40 | mkdir .git/info && |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 41 | cat >.git/info/sparse-checkout <<-\EOF && |
| 42 | /* |
| 43 | !/.gitmodules |
| 44 | EOF |
| 45 | git config core.sparsecheckout true && |
| 46 | git read-tree -m -u HEAD && |
| 47 | test_path_is_missing .gitmodules |
| 48 | ) |
| 49 | ' |
| 50 | |
| 51 | test_expect_success 'reading gitmodules config file when it is not checked out' ' |
| 52 | echo "../submodule" >expect && |
Ævar Arnfjörð Bjarmason | cc74a4a | 2022-11-08 15:10:32 +0100 | [diff] [blame] | 53 | test-tool -C super submodule config-list submodule.submodule.url >actual && |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 54 | test_cmp expect actual |
| 55 | ' |
| 56 | |
| 57 | test_expect_success 'not writing gitmodules config file when it is not checked out' ' |
Ævar Arnfjörð Bjarmason | cc74a4a | 2022-11-08 15:10:32 +0100 | [diff] [blame] | 58 | test_must_fail test-tool -C super submodule config-set submodule.submodule.url newurl && |
Antonio Ospite | 76e9bdc | 2018-10-25 18:18:12 +0200 | [diff] [blame] | 59 | test_path_is_missing super/.gitmodules |
| 60 | ' |
| 61 | |
| 62 | test_expect_success 'initialising submodule when the gitmodules config is not checked out' ' |
| 63 | test_must_fail git -C super config submodule.submodule.url && |
| 64 | git -C super submodule init && |
| 65 | git -C super config submodule.submodule.url >actual && |
| 66 | echo "$(pwd)/submodule" >expect && |
| 67 | test_cmp expect actual |
| 68 | ' |
| 69 | |
| 70 | test_expect_success 'updating submodule when the gitmodules config is not checked out' ' |
| 71 | test_path_is_missing super/submodule/file && |
| 72 | git -C super submodule update && |
| 73 | test_cmp submodule/file super/submodule/file |
| 74 | ' |
| 75 | |
| 76 | ORIG_SUBMODULE=$(git -C submodule rev-parse HEAD) |
| 77 | ORIG_UPSTREAM=$(git -C upstream rev-parse HEAD) |
| 78 | ORIG_SUPER=$(git -C super rev-parse HEAD) |
| 79 | |
| 80 | test_expect_success 're-updating submodule when the gitmodules config is not checked out' ' |
| 81 | test_when_finished "git -C submodule reset --hard $ORIG_SUBMODULE; |
| 82 | git -C upstream reset --hard $ORIG_UPSTREAM; |
| 83 | git -C super reset --hard $ORIG_SUPER; |
| 84 | git -C upstream submodule update --remote; |
| 85 | git -C super pull; |
| 86 | git -C super submodule update --remote" && |
| 87 | (cd submodule && |
| 88 | echo file2 >file2 && |
| 89 | git add file2 && |
| 90 | test_tick && |
| 91 | git commit -m "Add file2 to submodule" |
| 92 | ) && |
| 93 | (cd upstream && |
| 94 | git submodule update --remote && |
| 95 | git add submodule && |
| 96 | test_tick && |
| 97 | git commit -m "Update submodule" |
| 98 | ) && |
| 99 | git -C super pull && |
| 100 | # The --for-status options reads the gitmodules config |
| 101 | git -C super submodule summary --for-status >actual && |
| 102 | rev1=$(git -C submodule rev-parse --short HEAD) && |
| 103 | rev2=$(git -C submodule rev-parse --short HEAD^) && |
| 104 | cat >expect <<-EOF && |
| 105 | * submodule ${rev1}...${rev2} (1): |
| 106 | < Add file2 to submodule |
| 107 | |
| 108 | EOF |
| 109 | test_cmp expect actual && |
| 110 | # Test that the update actually succeeds |
| 111 | test_path_is_missing super/submodule/file2 && |
| 112 | git -C super submodule update && |
| 113 | test_cmp submodule/file2 super/submodule/file2 && |
| 114 | git -C super status --short >output && |
| 115 | test_must_be_empty output |
| 116 | ' |
| 117 | |
| 118 | test_expect_success 'not adding submodules when the gitmodules config is not checked out' ' |
| 119 | git clone submodule new_submodule && |
| 120 | test_must_fail git -C super submodule add ../new_submodule && |
| 121 | test_path_is_missing .gitmodules |
| 122 | ' |
| 123 | |
| 124 | # This test checks that the previous "git submodule add" did not leave the |
| 125 | # repository in a spurious state when it failed. |
| 126 | test_expect_success 'init submodule still works even after the previous add failed' ' |
| 127 | git -C super submodule init |
| 128 | ' |
| 129 | |
| 130 | test_done |