t / t5601-clone.shon commit Merge branch 'ep/shell-command-substitution-style' (e572fef)
   1#!/bin/sh
   2
   3test_description=clone
   4
   5. ./test-lib.sh
   6
   7test_expect_success setup '
   8
   9        rm -fr .git &&
  10        test_create_repo src &&
  11        (
  12                cd src &&
  13                >file &&
  14                git add file &&
  15                git commit -m initial &&
  16                echo 1 >file &&
  17                git add file &&
  18                git commit -m updated
  19        )
  20
  21'
  22
  23test_expect_success 'clone with excess parameters (1)' '
  24
  25        rm -fr dst &&
  26        test_must_fail git clone -n src dst junk
  27
  28'
  29
  30test_expect_success 'clone with excess parameters (2)' '
  31
  32        rm -fr dst &&
  33        test_must_fail git clone -n "file://$(pwd)/src" dst junk
  34
  35'
  36
  37test_expect_success C_LOCALE_OUTPUT 'output from clone' '
  38        rm -fr dst &&
  39        git clone -n "file://$(pwd)/src" dst >output 2>&1 &&
  40        test $(grep Clon output | wc -l) = 1
  41'
  42
  43test_expect_success 'clone does not keep pack' '
  44
  45        rm -fr dst &&
  46        git clone -n "file://$(pwd)/src" dst &&
  47        ! test -f dst/file &&
  48        ! (echo dst/.git/objects/pack/pack-* | grep "\.keep")
  49
  50'
  51
  52test_expect_success 'clone checks out files' '
  53
  54        rm -fr dst &&
  55        git clone src dst &&
  56        test -f dst/file
  57
  58'
  59
  60test_expect_success 'clone respects GIT_WORK_TREE' '
  61
  62        GIT_WORK_TREE=worktree git clone src bare &&
  63        test -f bare/config &&
  64        test -f worktree/file
  65
  66'
  67
  68test_expect_success 'clone from hooks' '
  69
  70        test_create_repo r0 &&
  71        cd r0 &&
  72        test_commit initial &&
  73        cd .. &&
  74        git init r1 &&
  75        cd r1 &&
  76        cat >.git/hooks/pre-commit <<-\EOF &&
  77        #!/bin/sh
  78        git clone ../r0 ../r2
  79        exit 1
  80        EOF
  81        chmod u+x .git/hooks/pre-commit &&
  82        : >file &&
  83        git add file &&
  84        test_must_fail git commit -m invoke-hook &&
  85        cd .. &&
  86        test_cmp r0/.git/HEAD r2/.git/HEAD &&
  87        test_cmp r0/initial.t r2/initial.t
  88
  89'
  90
  91test_expect_success 'clone creates intermediate directories' '
  92
  93        git clone src long/path/to/dst &&
  94        test -f long/path/to/dst/file
  95
  96'
  97
  98test_expect_success 'clone creates intermediate directories for bare repo' '
  99
 100        git clone --bare src long/path/to/bare/dst &&
 101        test -f long/path/to/bare/dst/config
 102
 103'
 104
 105test_expect_success 'clone --mirror' '
 106
 107        git clone --mirror src mirror &&
 108        test -f mirror/HEAD &&
 109        test ! -f mirror/file &&
 110        FETCH="$(cd mirror && git config remote.origin.fetch)" &&
 111        test "+refs/*:refs/*" = "$FETCH" &&
 112        MIRROR="$(cd mirror && git config --bool remote.origin.mirror)" &&
 113        test "$MIRROR" = true
 114
 115'
 116
 117test_expect_success 'clone --mirror with detached HEAD' '
 118
 119        ( cd src && git checkout HEAD^ && git rev-parse HEAD >../expected ) &&
 120        git clone --mirror src mirror.detached &&
 121        ( cd src && git checkout - ) &&
 122        GIT_DIR=mirror.detached git rev-parse HEAD >actual &&
 123        test_cmp expected actual
 124
 125'
 126
 127test_expect_success 'clone --bare with detached HEAD' '
 128
 129        ( cd src && git checkout HEAD^ && git rev-parse HEAD >../expected ) &&
 130        git clone --bare src bare.detached &&
 131        ( cd src && git checkout - ) &&
 132        GIT_DIR=bare.detached git rev-parse HEAD >actual &&
 133        test_cmp expected actual
 134
 135'
 136
 137test_expect_success 'clone --bare names the local repository <name>.git' '
 138
 139        git clone --bare src &&
 140        test -d src.git
 141
 142'
 143
 144test_expect_success 'clone --mirror does not repeat tags' '
 145
 146        (cd src &&
 147         git tag some-tag HEAD) &&
 148        git clone --mirror src mirror2 &&
 149        (cd mirror2 &&
 150         git show-ref 2> clone.err > clone.out) &&
 151        test_must_fail grep Duplicate mirror2/clone.err &&
 152        grep some-tag mirror2/clone.out
 153
 154'
 155
 156test_expect_success 'clone to destination with trailing /' '
 157
 158        git clone src target-1/ &&
 159        T=$( cd target-1 && git rev-parse HEAD ) &&
 160        S=$( cd src && git rev-parse HEAD ) &&
 161        test "$T" = "$S"
 162
 163'
 164
 165test_expect_success 'clone to destination with extra trailing /' '
 166
 167        git clone src target-2/// &&
 168        T=$( cd target-2 && git rev-parse HEAD ) &&
 169        S=$( cd src && git rev-parse HEAD ) &&
 170        test "$T" = "$S"
 171
 172'
 173
 174test_expect_success 'clone to an existing empty directory' '
 175        mkdir target-3 &&
 176        git clone src target-3 &&
 177        T=$( cd target-3 && git rev-parse HEAD ) &&
 178        S=$( cd src && git rev-parse HEAD ) &&
 179        test "$T" = "$S"
 180'
 181
 182test_expect_success 'clone to an existing non-empty directory' '
 183        mkdir target-4 &&
 184        >target-4/Fakefile &&
 185        test_must_fail git clone src target-4
 186'
 187
 188test_expect_success 'clone to an existing path' '
 189        >target-5 &&
 190        test_must_fail git clone src target-5
 191'
 192
 193test_expect_success 'clone a void' '
 194        mkdir src-0 &&
 195        (
 196                cd src-0 && git init
 197        ) &&
 198        git clone "file://$(pwd)/src-0" target-6 2>err-6 &&
 199        ! grep "fatal:" err-6 &&
 200        (
 201                cd src-0 && test_commit A
 202        ) &&
 203        git clone "file://$(pwd)/src-0" target-7 2>err-7 &&
 204        ! grep "fatal:" err-7 &&
 205        # There is no reason to insist they are bit-for-bit
 206        # identical, but this test should suffice for now.
 207        test_cmp target-6/.git/config target-7/.git/config
 208'
 209
 210test_expect_success 'clone respects global branch.autosetuprebase' '
 211        (
 212                test_config="$HOME/.gitconfig" &&
 213                git config -f "$test_config" branch.autosetuprebase remote &&
 214                rm -fr dst &&
 215                git clone src dst &&
 216                cd dst &&
 217                actual="z$(git config branch.master.rebase)" &&
 218                test ztrue = $actual
 219        )
 220'
 221
 222test_expect_success 'respect url-encoding of file://' '
 223        git init x+y &&
 224        git clone "file://$PWD/x+y" xy-url-1 &&
 225        git clone "file://$PWD/x%2By" xy-url-2
 226'
 227
 228test_expect_success 'do not query-string-decode + in URLs' '
 229        rm -rf x+y &&
 230        git init "x y" &&
 231        test_must_fail git clone "file://$PWD/x+y" xy-no-plus
 232'
 233
 234test_expect_success 'do not respect url-encoding of non-url path' '
 235        git init x+y &&
 236        test_must_fail git clone x%2By xy-regular &&
 237        git clone x+y xy-regular
 238'
 239
 240test_expect_success 'clone separate gitdir' '
 241        rm -rf dst &&
 242        git clone --separate-git-dir realgitdir src dst &&
 243        test -d realgitdir/refs
 244'
 245
 246test_expect_success 'clone separate gitdir: output' '
 247        echo "gitdir: $(pwd)/realgitdir" >expected &&
 248        test_cmp expected dst/.git
 249'
 250
 251test_expect_success 'clone from .git file' '
 252        git clone dst/.git dst2
 253'
 254
 255test_expect_success 'fetch from .git gitfile' '
 256        (
 257                cd dst2 &&
 258                git fetch ../dst/.git
 259        )
 260'
 261
 262test_expect_success 'fetch from gitfile parent' '
 263        (
 264                cd dst2 &&
 265                git fetch ../dst
 266        )
 267'
 268
 269test_expect_success 'clone separate gitdir where target already exists' '
 270        rm -rf dst &&
 271        test_must_fail git clone --separate-git-dir realgitdir src dst
 272'
 273
 274test_expect_success 'clone --reference from original' '
 275        git clone --shared --bare src src-1 &&
 276        git clone --bare src src-2 &&
 277        git clone --reference=src-2 --bare src-1 target-8 &&
 278        grep /src-2/ target-8/objects/info/alternates
 279'
 280
 281test_expect_success 'clone with more than one --reference' '
 282        git clone --bare src src-3 &&
 283        git clone --bare src src-4 &&
 284        git clone --reference=src-3 --reference=src-4 src target-9 &&
 285        grep /src-3/ target-9/.git/objects/info/alternates &&
 286        grep /src-4/ target-9/.git/objects/info/alternates
 287'
 288
 289test_expect_success 'clone from original with relative alternate' '
 290        mkdir nest &&
 291        git clone --bare src nest/src-5 &&
 292        echo ../../../src/.git/objects >nest/src-5/objects/info/alternates &&
 293        git clone --bare nest/src-5 target-10 &&
 294        grep /src/\\.git/objects target-10/objects/info/alternates
 295'
 296
 297test_expect_success 'clone checking out a tag' '
 298        git clone --branch=some-tag src dst.tag &&
 299        GIT_DIR=src/.git git rev-parse some-tag >expected &&
 300        test_cmp expected dst.tag/.git/HEAD &&
 301        GIT_DIR=dst.tag/.git git config remote.origin.fetch >fetch.actual &&
 302        echo "+refs/heads/*:refs/remotes/origin/*" >fetch.expected &&
 303        test_cmp fetch.expected fetch.actual
 304'
 305
 306setup_ssh_wrapper () {
 307        test_expect_success 'setup ssh wrapper' '
 308                write_script "$TRASH_DIRECTORY/ssh-wrapper" <<-\EOF &&
 309                echo >>"$TRASH_DIRECTORY/ssh-output" "ssh: $*" &&
 310                # throw away all but the last argument, which should be the
 311                # command
 312                while test $# -gt 1; do shift; done
 313                eval "$1"
 314                EOF
 315                GIT_SSH="$TRASH_DIRECTORY/ssh-wrapper" &&
 316                export GIT_SSH &&
 317                export TRASH_DIRECTORY &&
 318                >"$TRASH_DIRECTORY"/ssh-output
 319        '
 320}
 321
 322copy_ssh_wrapper_as () {
 323        cp "$TRASH_DIRECTORY/ssh-wrapper" "$1" &&
 324        GIT_SSH="$1" &&
 325        export GIT_SSH
 326}
 327
 328expect_ssh () {
 329        test_when_finished '
 330                (cd "$TRASH_DIRECTORY" && rm -f ssh-expect && >ssh-output)
 331        ' &&
 332        {
 333                case "$#" in
 334                1)
 335                        ;;
 336                2)
 337                        echo "ssh: $1 git-upload-pack '$2'"
 338                        ;;
 339                3)
 340                        echo "ssh: $1 $2 git-upload-pack '$3'"
 341                        ;;
 342                *)
 343                        echo "ssh: $1 $2 git-upload-pack '$3' $4"
 344                esac
 345        } >"$TRASH_DIRECTORY/ssh-expect" &&
 346        (cd "$TRASH_DIRECTORY" && test_cmp ssh-expect ssh-output)
 347}
 348
 349setup_ssh_wrapper
 350
 351test_expect_success 'clone myhost:src uses ssh' '
 352        git clone myhost:src ssh-clone &&
 353        expect_ssh myhost src
 354'
 355
 356test_expect_success !MINGW,!CYGWIN 'clone local path foo:bar' '
 357        cp -R src "foo:bar" &&
 358        git clone "foo:bar" foobar &&
 359        expect_ssh none
 360'
 361
 362test_expect_success 'bracketed hostnames are still ssh' '
 363        git clone "[myhost:123]:src" ssh-bracket-clone &&
 364        expect_ssh "-p 123" myhost src
 365'
 366
 367test_expect_success 'uplink is not treated as putty' '
 368        copy_ssh_wrapper_as "$TRASH_DIRECTORY/uplink" &&
 369        git clone "[myhost:123]:src" ssh-bracket-clone-uplink &&
 370        expect_ssh "-p 123" myhost src
 371'
 372
 373test_expect_success 'plink is treated specially (as putty)' '
 374        copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
 375        git clone "[myhost:123]:src" ssh-bracket-clone-plink-0 &&
 376        expect_ssh "-P 123" myhost src
 377'
 378
 379test_expect_success 'plink.exe is treated specially (as putty)' '
 380        copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
 381        git clone "[myhost:123]:src" ssh-bracket-clone-plink-1 &&
 382        expect_ssh "-P 123" myhost src
 383'
 384
 385test_expect_success 'tortoiseplink is like putty, with extra arguments' '
 386        copy_ssh_wrapper_as "$TRASH_DIRECTORY/tortoiseplink" &&
 387        git clone "[myhost:123]:src" ssh-bracket-clone-plink-2 &&
 388        expect_ssh "-batch -P 123" myhost src
 389'
 390
 391# Reset the GIT_SSH environment variable for clone tests.
 392setup_ssh_wrapper
 393
 394counter=0
 395# $1 url
 396# $2 none|host
 397# $3 path
 398test_clone_url () {
 399        counter=$(($counter + 1))
 400        test_might_fail git clone "$1" tmp$counter &&
 401        shift &&
 402        expect_ssh "$@"
 403}
 404
 405test_expect_success !MINGW 'clone c:temp is ssl' '
 406        test_clone_url c:temp c temp
 407'
 408
 409test_expect_success MINGW 'clone c:temp is dos drive' '
 410        test_clone_url c:temp none
 411'
 412
 413#ip v4
 414for repo in rep rep/home/project 123
 415do
 416        test_expect_success "clone host:$repo" '
 417                test_clone_url host:$repo host $repo
 418        '
 419done
 420
 421#ipv6
 422for repo in rep rep/home/project 123
 423do
 424        test_expect_success "clone [::1]:$repo" '
 425                test_clone_url [::1]:$repo ::1 "$repo"
 426        '
 427done
 428#home directory
 429test_expect_success "clone host:/~repo" '
 430        test_clone_url host:/~repo host "~repo"
 431'
 432
 433test_expect_success "clone [::1]:/~repo" '
 434        test_clone_url [::1]:/~repo ::1 "~repo"
 435'
 436
 437# Corner cases
 438for url in foo/bar:baz [foo]bar/baz:qux [foo/bar]:baz
 439do
 440        test_expect_success "clone $url is not ssh" '
 441                test_clone_url $url none
 442        '
 443done
 444
 445#with ssh:// scheme
 446#ignore trailing colon
 447for tcol in "" :
 448do
 449        test_expect_success "clone ssh://host.xz$tcol/home/user/repo" '
 450                test_clone_url "ssh://host.xz$tcol/home/user/repo" host.xz /home/user/repo
 451        '
 452        # from home directory
 453        test_expect_success "clone ssh://host.xz$tcol/~repo" '
 454        test_clone_url "ssh://host.xz$tcol/~repo" host.xz "~repo"
 455'
 456done
 457
 458# with port number
 459test_expect_success 'clone ssh://host.xz:22/home/user/repo' '
 460        test_clone_url "ssh://host.xz:22/home/user/repo" "-p 22 host.xz" "/home/user/repo"
 461'
 462
 463# from home directory with port number
 464test_expect_success 'clone ssh://host.xz:22/~repo' '
 465        test_clone_url "ssh://host.xz:22/~repo" "-p 22 host.xz" "~repo"
 466'
 467
 468#IPv6
 469for tuah in ::1 [::1] [::1]: user@::1 user@[::1] user@[::1]: [user@::1] [user@::1]:
 470do
 471        ehost=$(echo $tuah | sed -e "s/1]:/1]/ "| tr -d "[]")
 472        test_expect_success "clone ssh://$tuah/home/user/repo" "
 473          test_clone_url ssh://$tuah/home/user/repo $ehost /home/user/repo
 474        "
 475done
 476
 477#IPv6 from home directory
 478for tuah in ::1 [::1] user@::1 user@[::1] [user@::1]
 479do
 480        euah=$(echo $tuah | tr -d "[]")
 481        test_expect_success "clone ssh://$tuah/~repo" "
 482          test_clone_url ssh://$tuah/~repo $euah '~repo'
 483        "
 484done
 485
 486#IPv6 with port number
 487for tuah in [::1] user@[::1] [user@::1]
 488do
 489        euah=$(echo $tuah | tr -d "[]")
 490        test_expect_success "clone ssh://$tuah:22/home/user/repo" "
 491          test_clone_url ssh://$tuah:22/home/user/repo '-p 22' $euah /home/user/repo
 492        "
 493done
 494
 495#IPv6 from home directory with port number
 496for tuah in [::1] user@[::1] [user@::1]
 497do
 498        euah=$(echo $tuah | tr -d "[]")
 499        test_expect_success "clone ssh://$tuah:22/~repo" "
 500          test_clone_url ssh://$tuah:22/~repo '-p 22' $euah '~repo'
 501        "
 502done
 503
 504test_expect_success 'clone from a repository with two identical branches' '
 505
 506        (
 507                cd src &&
 508                git checkout -b another master
 509        ) &&
 510        git clone src target-11 &&
 511        test "z$( cd target-11 && git symbolic-ref HEAD )" = zrefs/heads/another
 512
 513'
 514
 515test_expect_success 'shallow clone locally' '
 516        git clone --depth=1 --no-local src ssrrcc &&
 517        git clone ssrrcc ddsstt &&
 518        test_cmp ssrrcc/.git/shallow ddsstt/.git/shallow &&
 519        ( cd ddsstt && git fsck )
 520'
 521
 522test_expect_success 'GIT_TRACE_PACKFILE produces a usable pack' '
 523        rm -rf dst.git &&
 524        GIT_TRACE_PACKFILE=$PWD/tmp.pack git clone --no-local --bare src dst.git &&
 525        git init --bare replay.git &&
 526        git -C replay.git index-pack -v --stdin <tmp.pack
 527'
 528
 529test_done