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