| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='fetch --all works correctly' |
| 4 | |
| 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | setup_repository () { |
| 11 | mkdir "$1" && ( |
| 12 | cd "$1" && |
| 13 | git init && |
| 14 | >file && |
| 15 | git add file && |
| 16 | test_tick && |
| 17 | git commit -m "Initial" && |
| 18 | git checkout -b side && |
| 19 | >elif && |
| 20 | git add elif && |
| 21 | test_tick && |
| 22 | git commit -m "Second" && |
| 23 | git checkout main |
| 24 | ) |
| 25 | } |
| 26 | |
| 27 | setup_test_clone () { |
| 28 | test_dir="$1" && |
| 29 | git clone one "$test_dir" && |
| 30 | for r in one two three |
| 31 | do |
| 32 | git -C "$test_dir" remote add "$r" "../$r" || return 1 |
| 33 | done |
| 34 | } |
| 35 | |
| 36 | test_expect_success setup ' |
| 37 | setup_repository one && |
| 38 | setup_repository two && |
| 39 | ( |
| 40 | cd two && git branch another |
| 41 | ) && |
| 42 | git clone --mirror two three && |
| 43 | git clone one test |
| 44 | ' |
| 45 | |
| 46 | cat > test/expect << EOF |
| 47 | one/HEAD -> one/main |
| 48 | one/main |
| 49 | one/side |
| 50 | origin/HEAD -> origin/main |
| 51 | origin/main |
| 52 | origin/side |
| 53 | three/HEAD -> three/main |
| 54 | three/another |
| 55 | three/main |
| 56 | three/side |
| 57 | two/HEAD -> two/main |
| 58 | two/another |
| 59 | two/main |
| 60 | two/side |
| 61 | EOF |
| 62 | |
| 63 | test_expect_success 'git fetch --all' ' |
| 64 | (cd test && |
| 65 | git remote add one ../one && |
| 66 | git remote add two ../two && |
| 67 | git remote add three ../three && |
| 68 | git fetch --all && |
| 69 | git branch -r > output && |
| 70 | test_cmp expect output) |
| 71 | ' |
| 72 | |
| 73 | test_expect_success 'git fetch --all --no-write-fetch-head' ' |
| 74 | (cd test && |
| 75 | rm -f .git/FETCH_HEAD && |
| 76 | git fetch --all --no-write-fetch-head && |
| 77 | test_path_is_missing .git/FETCH_HEAD) |
| 78 | ' |
| 79 | |
| 80 | test_expect_success 'git fetch --all should continue if a remote has errors' ' |
| 81 | (git clone one test2 && |
| 82 | cd test2 && |
| 83 | git remote add bad ../non-existing && |
| 84 | git remote add one ../one && |
| 85 | git remote add two ../two && |
| 86 | git remote add three ../three && |
| 87 | test_must_fail git fetch --all && |
| 88 | git branch -r > output && |
| 89 | test_cmp ../test/expect output) |
| 90 | ' |
| 91 | |
| 92 | test_expect_success 'git fetch --all does not allow non-option arguments' ' |
| 93 | (cd test && |
| 94 | test_must_fail git fetch --all origin && |
| 95 | test_must_fail git fetch --all origin main) |
| 96 | ' |
| 97 | |
| 98 | cat > expect << EOF |
| 99 | origin/HEAD -> origin/main |
| 100 | origin/main |
| 101 | origin/side |
| 102 | three/HEAD -> three/main |
| 103 | three/another |
| 104 | three/main |
| 105 | three/side |
| 106 | EOF |
| 107 | |
| 108 | test_expect_success 'git fetch --multiple (but only one remote)' ' |
| 109 | (git clone one test3 && |
| 110 | cd test3 && |
| 111 | git remote add three ../three && |
| 112 | git fetch --multiple three && |
| 113 | git branch -r > output && |
| 114 | test_cmp ../expect output) |
| 115 | ' |
| 116 | |
| 117 | cat > expect << EOF |
| 118 | one/HEAD -> one/main |
| 119 | one/main |
| 120 | one/side |
| 121 | two/HEAD -> two/main |
| 122 | two/another |
| 123 | two/main |
| 124 | two/side |
| 125 | EOF |
| 126 | |
| 127 | test_expect_success 'git fetch --multiple (two remotes)' ' |
| 128 | (git clone one test4 && |
| 129 | cd test4 && |
| 130 | git remote rm origin && |
| 131 | git remote add one ../one && |
| 132 | git remote add two ../two && |
| 133 | GIT_TRACE=1 git fetch --multiple one two 2>trace && |
| 134 | git branch -r > output && |
| 135 | test_cmp ../expect output && |
| 136 | grep "built-in: git maintenance" trace >gc && |
| 137 | test_line_count = 1 gc |
| 138 | ) |
| 139 | ' |
| 140 | |
| 141 | test_expect_success 'git fetch --multiple (bad remote names)' ' |
| 142 | (cd test4 && |
| 143 | test_must_fail git fetch --multiple four) |
| 144 | ' |
| 145 | |
| 146 | |
| 147 | test_expect_success 'git fetch --all (skipFetchAll)' ' |
| 148 | (cd test4 && |
| 149 | for b in $(git branch -r | grep -v HEAD) |
| 150 | do |
| 151 | git branch -r -d $b || exit 1 |
| 152 | done && |
| 153 | git remote add three ../three && |
| 154 | git config remote.three.skipFetchAll true && |
| 155 | git fetch --all && |
| 156 | git branch -r > output && |
| 157 | test_cmp ../expect output) |
| 158 | ' |
| 159 | |
| 160 | cat > expect << EOF |
| 161 | one/HEAD -> one/main |
| 162 | one/main |
| 163 | one/side |
| 164 | three/HEAD -> three/main |
| 165 | three/another |
| 166 | three/main |
| 167 | three/side |
| 168 | two/HEAD -> two/main |
| 169 | two/another |
| 170 | two/main |
| 171 | two/side |
| 172 | EOF |
| 173 | |
| 174 | test_expect_success 'git fetch --multiple (ignoring skipFetchAll)' ' |
| 175 | (cd test4 && |
| 176 | for b in $(git branch -r | grep -v HEAD) |
| 177 | do |
| 178 | git branch -r -d $b || exit 1 |
| 179 | done && |
| 180 | git fetch --multiple one two three && |
| 181 | git branch -r > output && |
| 182 | test_cmp ../expect output) |
| 183 | ' |
| 184 | |
| 185 | test_expect_success 'git fetch --all --no-tags' ' |
| 186 | git clone one test5 && |
| 187 | git clone test5 test6 && |
| 188 | (cd test5 && git tag test-tag) && |
| 189 | ( |
| 190 | cd test6 && |
| 191 | git fetch --all --no-tags && |
| 192 | git tag >output |
| 193 | ) && |
| 194 | test_must_be_empty test6/output |
| 195 | ' |
| 196 | |
| 197 | test_expect_success 'git fetch --all --tags' ' |
| 198 | echo test-tag >expect && |
| 199 | git clone one test7 && |
| 200 | git clone test7 test8 && |
| 201 | ( |
| 202 | cd test7 && |
| 203 | test_commit test-tag && |
| 204 | git reset --hard HEAD^ |
| 205 | ) && |
| 206 | ( |
| 207 | cd test8 && |
| 208 | git fetch --all --tags && |
| 209 | git tag >output |
| 210 | ) && |
| 211 | test_cmp expect test8/output |
| 212 | ' |
| 213 | |
| 214 | test_expect_success 'parallel' ' |
| 215 | git remote add one ./bogus1 && |
| 216 | git remote add two ./bogus2 && |
| 217 | |
| 218 | test_must_fail env GIT_TRACE="$PWD/trace" \ |
| 219 | git fetch --jobs=2 --multiple one two 2>err && |
| 220 | test_grep "preparing to run up to 2 tasks" trace && |
| 221 | test_grep "could not fetch .one.*128" err && |
| 222 | test_grep "could not fetch .two.*128" err |
| 223 | ' |
| 224 | |
| 225 | test_expect_success 'git fetch --multiple --jobs=0 picks a default' ' |
| 226 | (cd test && |
| 227 | git fetch --multiple --jobs=0) |
| 228 | ' |
| 229 | |
| 230 | create_fetch_all_expect () { |
| 231 | cat >expect <<-\EOF |
| 232 | one/HEAD -> one/main |
| 233 | one/main |
| 234 | one/side |
| 235 | origin/HEAD -> origin/main |
| 236 | origin/main |
| 237 | origin/side |
| 238 | three/HEAD -> three/main |
| 239 | three/another |
| 240 | three/main |
| 241 | three/side |
| 242 | two/HEAD -> two/main |
| 243 | two/another |
| 244 | two/main |
| 245 | two/side |
| 246 | EOF |
| 247 | } |
| 248 | |
| 249 | for fetch_all in true false |
| 250 | do |
| 251 | test_expect_success "git fetch --all (works with fetch.all = $fetch_all)" ' |
| 252 | test_dir="test_fetch_all_$fetch_all" && |
| 253 | setup_test_clone "$test_dir" && |
| 254 | ( |
| 255 | cd "$test_dir" && |
| 256 | git config fetch.all $fetch_all && |
| 257 | git fetch --all && |
| 258 | create_fetch_all_expect && |
| 259 | git branch -r >actual && |
| 260 | test_cmp expect actual |
| 261 | ) |
| 262 | ' |
| 263 | done |
| 264 | |
| 265 | test_expect_success 'git fetch (fetch all remotes with fetch.all = true)' ' |
| 266 | setup_test_clone test9 && |
| 267 | ( |
| 268 | cd test9 && |
| 269 | git config fetch.all true && |
| 270 | git fetch && |
| 271 | git branch -r >actual && |
| 272 | create_fetch_all_expect && |
| 273 | test_cmp expect actual |
| 274 | ) |
| 275 | ' |
| 276 | |
| 277 | create_fetch_one_expect () { |
| 278 | cat >expect <<-\EOF |
| 279 | one/HEAD -> one/main |
| 280 | one/main |
| 281 | one/side |
| 282 | origin/HEAD -> origin/main |
| 283 | origin/main |
| 284 | origin/side |
| 285 | EOF |
| 286 | } |
| 287 | |
| 288 | test_expect_success 'git fetch one (explicit remote overrides fetch.all)' ' |
| 289 | setup_test_clone test10 && |
| 290 | ( |
| 291 | cd test10 && |
| 292 | git config fetch.all true && |
| 293 | git fetch one && |
| 294 | create_fetch_one_expect && |
| 295 | git branch -r >actual && |
| 296 | test_cmp expect actual |
| 297 | ) |
| 298 | ' |
| 299 | |
| 300 | create_fetch_two_as_origin_expect () { |
| 301 | cat >expect <<-\EOF |
| 302 | origin/HEAD -> origin/main |
| 303 | origin/another |
| 304 | origin/main |
| 305 | origin/side |
| 306 | EOF |
| 307 | } |
| 308 | |
| 309 | test_expect_success 'git config fetch.all false (fetch only default remote)' ' |
| 310 | setup_test_clone test11 && |
| 311 | ( |
| 312 | cd test11 && |
| 313 | git config fetch.all false && |
| 314 | git remote set-url origin ../two && |
| 315 | git fetch && |
| 316 | create_fetch_two_as_origin_expect && |
| 317 | git branch -r >actual && |
| 318 | test_cmp expect actual |
| 319 | ) |
| 320 | ' |
| 321 | |
| 322 | for fetch_all in true false |
| 323 | do |
| 324 | test_expect_success "git fetch --no-all (fetch only default remote with fetch.all = $fetch_all)" ' |
| 325 | test_dir="test_no_all_fetch_all_$fetch_all" && |
| 326 | setup_test_clone "$test_dir" && |
| 327 | ( |
| 328 | cd "$test_dir" && |
| 329 | git config fetch.all $fetch_all && |
| 330 | git remote set-url origin ../two && |
| 331 | git fetch --no-all && |
| 332 | create_fetch_two_as_origin_expect && |
| 333 | git branch -r >actual && |
| 334 | test_cmp expect actual |
| 335 | ) |
| 336 | ' |
| 337 | done |
| 338 | |
| 339 | test_expect_success 'git fetch --no-all (fetch only default remote without fetch.all)' ' |
| 340 | setup_test_clone test12 && |
| 341 | ( |
| 342 | cd test12 && |
| 343 | git config --unset-all fetch.all || true && |
| 344 | git remote set-url origin ../two && |
| 345 | git fetch --no-all && |
| 346 | create_fetch_two_as_origin_expect && |
| 347 | git branch -r >actual && |
| 348 | test_cmp expect actual |
| 349 | ) |
| 350 | ' |
| 351 | |
| 352 | test_expect_success 'git fetch --all --no-all (fetch only default remote)' ' |
| 353 | setup_test_clone test13 && |
| 354 | ( |
| 355 | cd test13 && |
| 356 | git remote set-url origin ../two && |
| 357 | git fetch --all --no-all && |
| 358 | create_fetch_two_as_origin_expect && |
| 359 | git branch -r >actual && |
| 360 | test_cmp expect actual |
| 361 | ) |
| 362 | ' |
| 363 | |
| 364 | test_expect_success 'git fetch --no-all one (fetch only explicit remote)' ' |
| 365 | setup_test_clone test14 && |
| 366 | ( |
| 367 | cd test14 && |
| 368 | git fetch --no-all one && |
| 369 | create_fetch_one_expect && |
| 370 | git branch -r >actual && |
| 371 | test_cmp expect actual |
| 372 | ) |
| 373 | ' |
| 374 | |
| 375 | test_expect_success 'git fetch --no-all --all (fetch all remotes)' ' |
| 376 | setup_test_clone test15 && |
| 377 | ( |
| 378 | cd test15 && |
| 379 | git fetch --no-all --all && |
| 380 | create_fetch_all_expect && |
| 381 | git branch -r >actual && |
| 382 | test_cmp expect actual |
| 383 | ) |
| 384 | ' |
| 385 | |
| 386 | test_done |