t / t5702-protocol-v2.shon commit fetch: send "refs/tags/" prefix upon CLI refspecs (2b55435)
   1#!/bin/sh
   2
   3test_description='test git wire-protocol version 2'
   4
   5TEST_NO_CREATE_REPO=1
   6
   7. ./test-lib.sh
   8
   9# Test protocol v2 with 'git://' transport
  10#
  11. "$TEST_DIRECTORY"/lib-git-daemon.sh
  12start_git_daemon --export-all --enable=receive-pack
  13daemon_parent=$GIT_DAEMON_DOCUMENT_ROOT_PATH/parent
  14
  15test_expect_success 'create repo to be served by git-daemon' '
  16        git init "$daemon_parent" &&
  17        test_commit -C "$daemon_parent" one
  18'
  19
  20test_expect_success 'list refs with git:// using protocol v2' '
  21        test_when_finished "rm -f log" &&
  22
  23        GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
  24                ls-remote --symref "$GIT_DAEMON_URL/parent" >actual &&
  25
  26        # Client requested to use protocol v2
  27        grep "git> .*\\\0\\\0version=2\\\0$" log &&
  28        # Server responded using protocol v2
  29        grep "git< version 2" log &&
  30
  31        git ls-remote --symref "$GIT_DAEMON_URL/parent" >expect &&
  32        test_cmp actual expect
  33'
  34
  35test_expect_success 'ref advertisment is filtered with ls-remote using protocol v2' '
  36        test_when_finished "rm -f log" &&
  37
  38        GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
  39                ls-remote "$GIT_DAEMON_URL/parent" master >actual &&
  40
  41        cat >expect <<-EOF &&
  42        $(git -C "$daemon_parent" rev-parse refs/heads/master)$(printf "\t")refs/heads/master
  43        EOF
  44
  45        test_cmp actual expect
  46'
  47
  48test_expect_success 'clone with git:// using protocol v2' '
  49        test_when_finished "rm -f log" &&
  50
  51        GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
  52                clone "$GIT_DAEMON_URL/parent" daemon_child &&
  53
  54        git -C daemon_child log -1 --format=%s >actual &&
  55        git -C "$daemon_parent" log -1 --format=%s >expect &&
  56        test_cmp expect actual &&
  57
  58        # Client requested to use protocol v2
  59        grep "clone> .*\\\0\\\0version=2\\\0$" log &&
  60        # Server responded using protocol v2
  61        grep "clone< version 2" log
  62'
  63
  64test_expect_success 'fetch with git:// using protocol v2' '
  65        test_when_finished "rm -f log" &&
  66
  67        test_commit -C "$daemon_parent" two &&
  68
  69        GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
  70                fetch &&
  71
  72        git -C daemon_child log -1 --format=%s origin/master >actual &&
  73        git -C "$daemon_parent" log -1 --format=%s >expect &&
  74        test_cmp expect actual &&
  75
  76        # Client requested to use protocol v2
  77        grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
  78        # Server responded using protocol v2
  79        grep "fetch< version 2" log
  80'
  81
  82test_expect_success 'pull with git:// using protocol v2' '
  83        test_when_finished "rm -f log" &&
  84
  85        GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
  86                pull &&
  87
  88        git -C daemon_child log -1 --format=%s >actual &&
  89        git -C "$daemon_parent" log -1 --format=%s >expect &&
  90        test_cmp expect actual &&
  91
  92        # Client requested to use protocol v2
  93        grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
  94        # Server responded using protocol v2
  95        grep "fetch< version 2" log
  96'
  97
  98test_expect_success 'push with git:// and a config of v2 does not request v2' '
  99        test_when_finished "rm -f log" &&
 100
 101        # Till v2 for push is designed, make sure that if a client has
 102        # protocol.version configured to use v2, that the client instead falls
 103        # back and uses v0.
 104
 105        test_commit -C daemon_child three &&
 106
 107        # Push to another branch, as the target repository has the
 108        # master branch checked out and we cannot push into it.
 109        GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
 110                push origin HEAD:client_branch &&
 111
 112        git -C daemon_child log -1 --format=%s >actual &&
 113        git -C "$daemon_parent" log -1 --format=%s client_branch >expect &&
 114        test_cmp expect actual &&
 115
 116        # Client requested to use protocol v2
 117        ! grep "push> .*\\\0\\\0version=2\\\0$" log &&
 118        # Server responded using protocol v2
 119        ! grep "push< version 2" log
 120'
 121
 122stop_git_daemon
 123
 124# Test protocol v2 with 'file://' transport
 125#
 126test_expect_success 'create repo to be served by file:// transport' '
 127        git init file_parent &&
 128        test_commit -C file_parent one
 129'
 130
 131test_expect_success 'list refs with file:// using protocol v2' '
 132        test_when_finished "rm -f log" &&
 133
 134        GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
 135                ls-remote --symref "file://$(pwd)/file_parent" >actual &&
 136
 137        # Server responded using protocol v2
 138        grep "git< version 2" log &&
 139
 140        git ls-remote --symref "file://$(pwd)/file_parent" >expect &&
 141        test_cmp actual expect
 142'
 143
 144test_expect_success 'ref advertisment is filtered with ls-remote using protocol v2' '
 145        test_when_finished "rm -f log" &&
 146
 147        GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
 148                ls-remote "file://$(pwd)/file_parent" master >actual &&
 149
 150        cat >expect <<-EOF &&
 151        $(git -C file_parent rev-parse refs/heads/master)$(printf "\t")refs/heads/master
 152        EOF
 153
 154        test_cmp actual expect
 155'
 156
 157test_expect_success 'server-options are sent when using ls-remote' '
 158        test_when_finished "rm -f log" &&
 159
 160        GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
 161                ls-remote -o hello -o world "file://$(pwd)/file_parent" master >actual &&
 162
 163        cat >expect <<-EOF &&
 164        $(git -C file_parent rev-parse refs/heads/master)$(printf "\t")refs/heads/master
 165        EOF
 166
 167        test_cmp actual expect &&
 168        grep "server-option=hello" log &&
 169        grep "server-option=world" log
 170'
 171
 172
 173test_expect_success 'clone with file:// using protocol v2' '
 174        test_when_finished "rm -f log" &&
 175
 176        GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
 177                clone "file://$(pwd)/file_parent" file_child &&
 178
 179        git -C file_child log -1 --format=%s >actual &&
 180        git -C file_parent log -1 --format=%s >expect &&
 181        test_cmp expect actual &&
 182
 183        # Server responded using protocol v2
 184        grep "clone< version 2" log
 185'
 186
 187test_expect_success 'fetch with file:// using protocol v2' '
 188        test_when_finished "rm -f log" &&
 189
 190        test_commit -C file_parent two &&
 191
 192        GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
 193                fetch origin &&
 194
 195        git -C file_child log -1 --format=%s origin/master >actual &&
 196        git -C file_parent log -1 --format=%s >expect &&
 197        test_cmp expect actual &&
 198
 199        # Server responded using protocol v2
 200        grep "fetch< version 2" log
 201'
 202
 203test_expect_success 'ref advertisment is filtered during fetch using protocol v2' '
 204        test_when_finished "rm -f log" &&
 205
 206        test_commit -C file_parent three &&
 207        git -C file_parent branch unwanted-branch three &&
 208
 209        GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
 210                fetch origin master &&
 211
 212        git -C file_child log -1 --format=%s origin/master >actual &&
 213        git -C file_parent log -1 --format=%s >expect &&
 214        test_cmp expect actual &&
 215
 216        grep "refs/heads/master" log &&
 217        ! grep "refs/heads/unwanted-branch" log
 218'
 219
 220test_expect_success 'server-options are sent when fetching' '
 221        test_when_finished "rm -f log" &&
 222
 223        test_commit -C file_parent four &&
 224
 225        GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
 226                fetch -o hello -o world origin master &&
 227
 228        git -C file_child log -1 --format=%s origin/master >actual &&
 229        git -C file_parent log -1 --format=%s >expect &&
 230        test_cmp expect actual &&
 231
 232        grep "server-option=hello" log &&
 233        grep "server-option=world" log
 234'
 235
 236test_expect_success 'upload-pack respects config using protocol v2' '
 237        git init server &&
 238        write_script server/.git/hook <<-\EOF &&
 239                touch hookout
 240                "$@"
 241        EOF
 242        test_commit -C server one &&
 243
 244        test_config_global uploadpack.packobjectshook ./hook &&
 245        test_path_is_missing server/.git/hookout &&
 246        git -c protocol.version=2 clone "file://$(pwd)/server" client &&
 247        test_path_is_file server/.git/hookout
 248'
 249
 250test_expect_success 'setup filter tests' '
 251        rm -rf server client &&
 252        git init server &&
 253
 254        # 1 commit to create a file, and 1 commit to modify it
 255        test_commit -C server message1 a.txt &&
 256        test_commit -C server message2 a.txt &&
 257        git -C server config protocol.version 2 &&
 258        git -C server config uploadpack.allowfilter 1 &&
 259        git -C server config uploadpack.allowanysha1inwant 1 &&
 260        git -C server config protocol.version 2
 261'
 262
 263test_expect_success 'partial clone' '
 264        GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
 265                clone --filter=blob:none "file://$(pwd)/server" client &&
 266        grep "version 2" trace &&
 267
 268        # Ensure that the old version of the file is missing
 269        git -C client rev-list master --quiet --objects --missing=print \
 270                >observed.oids &&
 271        grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
 272
 273        # Ensure that client passes fsck
 274        git -C client fsck
 275'
 276
 277test_expect_success 'dynamically fetch missing object' '
 278        rm "$(pwd)/trace" &&
 279        GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
 280                cat-file -p $(git -C server rev-parse message1:a.txt) &&
 281        grep "version 2" trace
 282'
 283
 284test_expect_success 'partial fetch' '
 285        rm -rf client "$(pwd)/trace" &&
 286        git init client &&
 287        SERVER="file://$(pwd)/server" &&
 288        test_config -C client extensions.partialClone "$SERVER" &&
 289
 290        GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
 291                fetch --filter=blob:none "$SERVER" master:refs/heads/other &&
 292        grep "version 2" trace &&
 293
 294        # Ensure that the old version of the file is missing
 295        git -C client rev-list other --quiet --objects --missing=print \
 296                >observed.oids &&
 297        grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
 298
 299        # Ensure that client passes fsck
 300        git -C client fsck
 301'
 302
 303test_expect_success 'do not advertise filter if not configured to do so' '
 304        SERVER="file://$(pwd)/server" &&
 305
 306        rm "$(pwd)/trace" &&
 307        git -C server config uploadpack.allowfilter 1 &&
 308        GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
 309                ls-remote "$SERVER" &&
 310        grep "fetch=.*filter" trace &&
 311
 312        rm "$(pwd)/trace" &&
 313        git -C server config uploadpack.allowfilter 0 &&
 314        GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
 315                ls-remote "$SERVER" &&
 316        grep "fetch=" trace >fetch_capabilities &&
 317        ! grep filter fetch_capabilities
 318'
 319
 320test_expect_success 'partial clone warns if filter is not advertised' '
 321        rm -rf client &&
 322        git -C server config uploadpack.allowfilter 0 &&
 323        git -c protocol.version=2 \
 324                clone --filter=blob:none "file://$(pwd)/server" client 2>err &&
 325        test_i18ngrep "filtering not recognized by server, ignoring" err
 326'
 327
 328test_expect_success 'even with handcrafted request, filter does not work if not advertised' '
 329        git -C server config uploadpack.allowfilter 0 &&
 330
 331        # Custom request that tries to filter even though it is not advertised.
 332        test-pkt-line pack >in <<-EOF &&
 333        command=fetch
 334        0001
 335        want $(git -C server rev-parse master)
 336        filter blob:none
 337        0000
 338        EOF
 339
 340        test_must_fail git -C server serve --stateless-rpc <in >/dev/null 2>err &&
 341        grep "unexpected line: .filter blob:none." err &&
 342
 343        # Exercise to ensure that if advertised, filter works
 344        git -C server config uploadpack.allowfilter 1 &&
 345        git -C server serve --stateless-rpc <in >/dev/null
 346'
 347
 348test_expect_success 'default refspec is used to filter ref when fetchcing' '
 349        test_when_finished "rm -f log" &&
 350
 351        GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
 352                fetch origin &&
 353
 354        git -C file_child log -1 --format=%s three >actual &&
 355        git -C file_parent log -1 --format=%s three >expect &&
 356        test_cmp expect actual &&
 357
 358        grep "ref-prefix refs/heads/" log &&
 359        grep "ref-prefix refs/tags/" log
 360'
 361
 362test_expect_success 'fetch supports various ways of have lines' '
 363        rm -rf server client trace &&
 364        git init server &&
 365        test_commit -C server dwim &&
 366        TREE=$(git -C server rev-parse HEAD^{tree}) &&
 367        git -C server tag exact \
 368                $(git -C server commit-tree -m a "$TREE") &&
 369        git -C server tag dwim-unwanted \
 370                $(git -C server commit-tree -m b "$TREE") &&
 371        git -C server tag exact-unwanted \
 372                $(git -C server commit-tree -m c "$TREE") &&
 373        git -C server tag prefix1 \
 374                $(git -C server commit-tree -m d "$TREE") &&
 375        git -C server tag prefix2 \
 376                $(git -C server commit-tree -m e "$TREE") &&
 377        git -C server tag fetch-by-sha1 \
 378                $(git -C server commit-tree -m f "$TREE") &&
 379        git -C server tag completely-unrelated \
 380                $(git -C server commit-tree -m g "$TREE") &&
 381
 382        git init client &&
 383        GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
 384                fetch "file://$(pwd)/server" \
 385                dwim \
 386                refs/tags/exact \
 387                refs/tags/prefix*:refs/tags/prefix* \
 388                "$(git -C server rev-parse fetch-by-sha1)" &&
 389
 390        # Ensure that the appropriate prefixes are sent (using a sample)
 391        grep "fetch> ref-prefix dwim" trace &&
 392        grep "fetch> ref-prefix refs/heads/dwim" trace &&
 393        grep "fetch> ref-prefix refs/tags/prefix" trace &&
 394
 395        # Ensure that the correct objects are returned
 396        git -C client cat-file -e $(git -C server rev-parse dwim) &&
 397        git -C client cat-file -e $(git -C server rev-parse exact) &&
 398        git -C client cat-file -e $(git -C server rev-parse prefix1) &&
 399        git -C client cat-file -e $(git -C server rev-parse prefix2) &&
 400        git -C client cat-file -e $(git -C server rev-parse fetch-by-sha1) &&
 401        test_must_fail git -C client cat-file -e \
 402                $(git -C server rev-parse dwim-unwanted) &&
 403        test_must_fail git -C client cat-file -e \
 404                $(git -C server rev-parse exact-unwanted) &&
 405        test_must_fail git -C client cat-file -e \
 406                $(git -C server rev-parse completely-unrelated)
 407'
 408
 409test_expect_success 'fetch supports include-tag and tag following' '
 410        rm -rf server client trace &&
 411        git init server &&
 412
 413        test_commit -C server to_fetch &&
 414        git -C server tag -a annotated_tag -m message &&
 415
 416        git init client &&
 417        GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
 418                fetch "$(pwd)/server" to_fetch:to_fetch &&
 419
 420        grep "fetch> ref-prefix to_fetch" trace &&
 421        grep "fetch> ref-prefix refs/tags/" trace &&
 422        grep "fetch> include-tag" trace &&
 423
 424        git -C client cat-file -e $(git -C client rev-parse annotated_tag)
 425'
 426
 427# Test protocol v2 with 'http://' transport
 428#
 429. "$TEST_DIRECTORY"/lib-httpd.sh
 430start_httpd
 431
 432test_expect_success 'create repo to be served by http:// transport' '
 433        git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
 434        git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config http.receivepack true &&
 435        test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one
 436'
 437
 438test_expect_success 'clone with http:// using protocol v2' '
 439        test_when_finished "rm -f log" &&
 440
 441        GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
 442                clone "$HTTPD_URL/smart/http_parent" http_child &&
 443
 444        git -C http_child log -1 --format=%s >actual &&
 445        git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
 446        test_cmp expect actual &&
 447
 448        # Client requested to use protocol v2
 449        grep "Git-Protocol: version=2" log &&
 450        # Server responded using protocol v2
 451        grep "git< version 2" log
 452'
 453
 454test_expect_success 'fetch with http:// using protocol v2' '
 455        test_when_finished "rm -f log" &&
 456
 457        test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
 458
 459        GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
 460                fetch &&
 461
 462        git -C http_child log -1 --format=%s origin/master >actual &&
 463        git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
 464        test_cmp expect actual &&
 465
 466        # Server responded using protocol v2
 467        grep "git< version 2" log
 468'
 469
 470test_expect_success 'push with http:// and a config of v2 does not request v2' '
 471        test_when_finished "rm -f log" &&
 472        # Till v2 for push is designed, make sure that if a client has
 473        # protocol.version configured to use v2, that the client instead falls
 474        # back and uses v0.
 475
 476        test_commit -C http_child three &&
 477
 478        # Push to another branch, as the target repository has the
 479        # master branch checked out and we cannot push into it.
 480        GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
 481                push origin HEAD:client_branch &&
 482
 483        git -C http_child log -1 --format=%s >actual &&
 484        git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s client_branch >expect &&
 485        test_cmp expect actual &&
 486
 487        # Client didnt request to use protocol v2
 488        ! grep "Git-Protocol: version=2" log &&
 489        # Server didnt respond using protocol v2
 490        ! grep "git< version 2" log
 491'
 492
 493
 494stop_httpd
 495
 496test_done