git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / t / t7426-submodule-get-default-remote.sh
| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git submodule--helper get-default-remote' |
| 4 | |
| 5 | TEST_NO_CREATE_REPO=1 |
| 6 | . ./test-lib.sh |
| 7 | |
| 8 | test_expect_success 'setup' ' |
| 9 | git config --global protocol.file.allow always |
| 10 | ' |
| 11 | |
| 12 | test_expect_success 'setup repositories' ' |
| 13 | # Create a repository to be used as submodule |
| 14 | git init sub && |
| 15 | test_commit --no-tag -C sub "initial commit in sub" file.txt "sub content" && |
| 16 | |
| 17 | # Create main repository |
| 18 | git init super && |
| 19 | ( |
| 20 | cd super && |
| 21 | mkdir subdir && |
| 22 | test_commit --no-tag -C subdir "initial commit in super" main.txt "super content" && |
| 23 | git submodule add ../sub subpath && |
| 24 | git commit -m "add submodule 'sub' at subpath" |
| 25 | ) |
| 26 | ' |
| 27 | |
| 28 | test_expect_success 'get-default-remote returns origin for initialized submodule' ' |
| 29 | ( |
| 30 | cd super && |
| 31 | git submodule update --init && |
| 32 | echo "origin" >expect && |
| 33 | git submodule--helper get-default-remote subpath >actual && |
| 34 | test_cmp expect actual |
| 35 | ) |
| 36 | ' |
| 37 | |
| 38 | test_expect_success 'get-default-remote works from subdirectory' ' |
| 39 | ( |
| 40 | cd super/subdir && |
| 41 | echo "origin" >expect && |
| 42 | git submodule--helper get-default-remote ../subpath >actual && |
| 43 | test_cmp expect actual |
| 44 | ) |
| 45 | ' |
| 46 | |
| 47 | test_expect_success 'get-default-remote fails with non-existent path' ' |
| 48 | ( |
| 49 | cd super && |
| 50 | test_must_fail git submodule--helper get-default-remote nonexistent 2>err && |
| 51 | test_grep "could not get a repository handle" err |
| 52 | ) |
| 53 | ' |
| 54 | |
| 55 | test_expect_success 'get-default-remote fails with non-submodule path' ' |
| 56 | ( |
| 57 | cd super && |
| 58 | test_must_fail git submodule--helper get-default-remote subdir 2>err && |
| 59 | test_grep "could not get a repository handle" err |
| 60 | ) |
| 61 | ' |
| 62 | |
| 63 | test_expect_success 'get-default-remote fails with uninitialized submodule' ' |
| 64 | test_when_finished " |
| 65 | git -C super config -f .gitmodules --remove-section submodule.uninitialized && |
| 66 | git -C super update-index --force-remove uninitialized |
| 67 | " && |
| 68 | ( |
| 69 | cd super && |
| 70 | git config -f .gitmodules submodule.uninitialized.path uninitialized && |
| 71 | git config -f .gitmodules submodule.uninitialized.url ../sub && |
| 72 | head=$(git -C ../sub rev-parse HEAD) && |
| 73 | git update-index --add --cacheinfo 160000,$head,uninitialized && |
| 74 | test_must_fail git submodule--helper get-default-remote \ |
| 75 | uninitialized 2>err && |
| 76 | test_grep "could not get a repository handle" err |
| 77 | ) |
| 78 | ' |
| 79 | |
| 80 | test_expect_success 'get-default-remote fails without path argument' ' |
| 81 | ( |
| 82 | cd super && |
| 83 | test_must_fail git submodule--helper get-default-remote 2>err && |
| 84 | test_grep "usage:" err |
| 85 | ) |
| 86 | ' |
| 87 | |
| 88 | test_expect_success 'get-default-remote fails with too many arguments' ' |
| 89 | ( |
| 90 | cd super && |
| 91 | test_must_fail git submodule--helper get-default-remote subpath subdir 2>err && |
| 92 | test_grep "usage:" err |
| 93 | ) |
| 94 | ' |
| 95 | |
| 96 | test_expect_success 'setup submodule with non-origin default remote name' ' |
| 97 | # Create another submodule path with a different remote name |
| 98 | ( |
| 99 | cd super && |
| 100 | git submodule add ../sub upstream-subpath && |
| 101 | git commit -m "add second submodule in upstream-subpath" && |
| 102 | git submodule update --init upstream-subpath && |
| 103 | |
| 104 | # Change the remote name in the submodule |
| 105 | cd upstream-subpath && |
| 106 | git remote rename origin upstream |
| 107 | ) |
| 108 | ' |
| 109 | |
| 110 | test_expect_success 'get-default-remote returns non-origin remote name' ' |
| 111 | ( |
| 112 | cd super && |
| 113 | echo "upstream" >expect && |
| 114 | git submodule--helper get-default-remote upstream-subpath >actual && |
| 115 | test_cmp expect actual |
| 116 | ) |
| 117 | ' |
| 118 | |
| 119 | test_expect_success 'get-default-remote handles submodule with multiple remotes' ' |
| 120 | ( |
| 121 | cd super/subpath && |
| 122 | git remote add other-upstream ../../sub && |
| 123 | git remote add myfork ../../sub |
| 124 | ) && |
| 125 | |
| 126 | ( |
| 127 | cd super && |
| 128 | echo "origin" >expect && |
| 129 | git submodule--helper get-default-remote subpath >actual && |
| 130 | test_cmp expect actual |
| 131 | ) |
| 132 | ' |
| 133 | |
| 134 | test_expect_success 'get-default-remote handles submodule with multiple remotes and none are origin' ' |
| 135 | ( |
| 136 | cd super/upstream-subpath && |
| 137 | git remote add yet-another-upstream ../../sub && |
| 138 | git remote add yourfork ../../sub |
| 139 | ) && |
| 140 | |
| 141 | ( |
| 142 | cd super && |
| 143 | echo "upstream" >expect && |
| 144 | git submodule--helper get-default-remote upstream-subpath >actual && |
| 145 | test_cmp expect actual |
| 146 | ) |
| 147 | ' |
| 148 | |
| 149 | test_expect_success 'setup nested submodule with non-origin remote' ' |
| 150 | git init innersub && |
| 151 | test_commit --no-tag -C innersub "initial commit in innersub" inner.txt "innersub content" && |
| 152 | |
| 153 | ( |
| 154 | cd sub && |
| 155 | git submodule add ../innersub innersubpath && |
| 156 | git commit -m "add nested submodule at innersubpath" |
| 157 | ) && |
| 158 | |
| 159 | ( |
| 160 | cd super/upstream-subpath && |
| 161 | git pull upstream && |
| 162 | git submodule update --init --recursive . && |
| 163 | ( |
| 164 | cd innersubpath && |
| 165 | git remote rename origin another_upstream |
| 166 | ) |
| 167 | ) |
| 168 | ' |
| 169 | |
| 170 | test_expect_success 'get-default-remote works with nested submodule' ' |
| 171 | ( |
| 172 | cd super && |
| 173 | echo "another_upstream" >expect && |
| 174 | git submodule--helper get-default-remote upstream-subpath/innersubpath >actual && |
| 175 | test_cmp expect actual |
| 176 | ) |
| 177 | ' |
| 178 | |
| 179 | test_expect_success 'get-default-remote works with submodule that has no remotes' ' |
| 180 | # Create a submodule directory manually without remotes |
| 181 | ( |
| 182 | cd super && |
| 183 | git init no-remote-sub && |
| 184 | test_commit --no-tag -C no-remote-sub "local commit" local.txt "local content" |
| 185 | ) && |
| 186 | |
| 187 | # Add it as a submodule |
| 188 | ( |
| 189 | cd super && |
| 190 | git submodule add ./no-remote-sub && |
| 191 | git commit -m "add local submodule 'no-remote-sub'" |
| 192 | ) && |
| 193 | |
| 194 | ( |
| 195 | cd super && |
| 196 | # Should fall back to "origin" remote name when no remotes exist |
| 197 | echo "origin" >expect && |
| 198 | git submodule--helper get-default-remote no-remote-sub >actual && |
| 199 | test_cmp expect actual |
| 200 | ) |
| 201 | ' |
| 202 | |
| 203 | test_done |