git-clone.shon commit Merge branch 'mk/color' (dc46fa3)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005, Linus Torvalds
   4# Copyright (c) 2005, Junio C Hamano
   5#
   6# Clone a repository into a different directory that does not yet exist.
   7
   8# See git-sh-setup why.
   9unset CDPATH
  10
  11OPTIONS_SPEC="\
  12git-clone [options] [--] <repo> [<dir>]
  13--
  14n,no-checkout        don't create a checkout
  15bare                 create a bare repository
  16naked                create a bare repository
  17l,local              to clone from a local repository
  18no-hardlinks         don't use local hardlinks, always copy
  19s,shared             setup as a shared repository
  20template=            path to the template directory
  21q,quiet              be quiet
  22reference=           reference repository
  23o,origin=            use <name> instead of 'origin' to track upstream
  24u,upload-pack=       path to git-upload-pack on the remote
  25depth=               create a shallow clone of that depth
  26
  27use-separate-remote  compatibility, do not use
  28no-separate-remote   compatibility, do not use"
  29
  30die() {
  31        echo >&2 "$@"
  32        exit 1
  33}
  34
  35usage() {
  36        exec "$0" -h
  37}
  38
  39eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
  40
  41get_repo_base() {
  42        (
  43                cd "`/bin/pwd`" &&
  44                cd "$1" || cd "$1.git" &&
  45                {
  46                        cd .git
  47                        pwd
  48                }
  49        ) 2>/dev/null
  50}
  51
  52if [ -n "$GIT_SSL_NO_VERIFY" -o \
  53        "`git config --bool http.sslVerify`" = false ]; then
  54    curl_extra_args="-k"
  55fi
  56
  57http_fetch () {
  58        # $1 = Remote, $2 = Local
  59        curl -nsfL $curl_extra_args "$1" >"$2"
  60        curl_exit_status=$?
  61        case $curl_exit_status in
  62        126|127) exit ;;
  63        *)       return $curl_exit_status ;;
  64        esac
  65}
  66
  67clone_dumb_http () {
  68        # $1 - remote, $2 - local
  69        cd "$2" &&
  70        clone_tmp="$GIT_DIR/clone-tmp" &&
  71        mkdir -p "$clone_tmp" || exit 1
  72        if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
  73                "`git config --bool http.noEPSV`" = true ]; then
  74                curl_extra_args="${curl_extra_args} --disable-epsv"
  75        fi
  76        http_fetch "$1/info/refs" "$clone_tmp/refs" ||
  77                die "Cannot get remote repository information.
  78Perhaps git-update-server-info needs to be run there?"
  79        test "z$quiet" = z && v=-v || v=
  80        while read sha1 refname
  81        do
  82                name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
  83                case "$name" in
  84                *^*)    continue;;
  85                esac
  86                case "$bare,$name" in
  87                yes,* | ,heads/* | ,tags/*) ;;
  88                *)      continue ;;
  89                esac
  90                if test -n "$use_separate_remote" &&
  91                   branch_name=`expr "z$name" : 'zheads/\(.*\)'`
  92                then
  93                        tname="remotes/$origin/$branch_name"
  94                else
  95                        tname=$name
  96                fi
  97                git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
  98        done <"$clone_tmp/refs"
  99        rm -fr "$clone_tmp"
 100        http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
 101        rm -f "$GIT_DIR/REMOTE_HEAD"
 102        if test -f "$GIT_DIR/REMOTE_HEAD"; then
 103                head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
 104                case "$head_sha1" in
 105                'ref: refs/'*)
 106                        ;;
 107                *)
 108                        git-http-fetch $v -a "$head_sha1" "$1" ||
 109                        rm -f "$GIT_DIR/REMOTE_HEAD"
 110                        ;;
 111                esac
 112        fi
 113}
 114
 115quiet=
 116local=no
 117use_local_hardlink=yes
 118local_shared=no
 119unset template
 120no_checkout=
 121upload_pack=
 122bare=
 123reference=
 124origin=
 125origin_override=
 126use_separate_remote=t
 127depth=
 128no_progress=
 129local_explicitly_asked_for=
 130test -t 1 || no_progress=--no-progress
 131
 132while test $# != 0
 133do
 134        case "$1" in
 135        -n|--no-checkout)
 136                no_checkout=yes ;;
 137        --naked|--bare)
 138                bare=yes ;;
 139        -l|--local)
 140                local_explicitly_asked_for=yes
 141                use_local_hardlink=yes
 142                ;;
 143        --no-hardlinks)
 144                use_local_hardlink=no ;;
 145        -s|--shared)
 146                local_shared=yes ;;
 147        --template)
 148                shift; template="--template=$1" ;;
 149        -q|--quiet)
 150                quiet=-q ;;
 151        --use-separate-remote|--no-separate-remote)
 152                die "clones are always made with separate-remote layout" ;;
 153        --reference)
 154                shift; reference="$1" ;;
 155        -o|--origin)
 156                shift;
 157                case "$1" in
 158                '')
 159                    usage ;;
 160                */*)
 161                    die "'$1' is not suitable for an origin name"
 162                esac
 163                git check-ref-format "heads/$1" ||
 164                    die "'$1' is not suitable for a branch name"
 165                test -z "$origin_override" ||
 166                    die "Do not give more than one --origin options."
 167                origin_override=yes
 168                origin="$1"
 169                ;;
 170        -u|--upload-pack)
 171                shift
 172                upload_pack="--upload-pack=$1" ;;
 173        --depth)
 174                shift
 175                depth="--depth=$1" ;;
 176        --)
 177                shift
 178                break ;;
 179        *)
 180                usage ;;
 181        esac
 182        shift
 183done
 184
 185repo="$1"
 186test -n "$repo" ||
 187    die 'you must specify a repository to clone.'
 188
 189# --bare implies --no-checkout and --no-separate-remote
 190if test yes = "$bare"
 191then
 192        if test yes = "$origin_override"
 193        then
 194                die '--bare and --origin $origin options are incompatible.'
 195        fi
 196        no_checkout=yes
 197        use_separate_remote=
 198fi
 199
 200if test -z "$origin"
 201then
 202        origin=origin
 203fi
 204
 205# Turn the source into an absolute path if
 206# it is local
 207if base=$(get_repo_base "$repo"); then
 208        repo="$base"
 209        if test -z "$depth"
 210        then
 211                local=yes
 212        fi
 213elif test -f "$repo"
 214then
 215        case "$repo" in /*) ;; *) repo="$PWD/$repo" ;; esac
 216fi
 217
 218# Decide the directory name of the new repository
 219if test -n "$2"
 220then
 221        dir="$2"
 222else
 223        # Derive one from the repository name
 224        # Try using "humanish" part of source repo if user didn't specify one
 225        if test -f "$repo"
 226        then
 227                # Cloning from a bundle
 228                dir=$(echo "$repo" | sed -e 's|/*\.bundle$||' -e 's|.*/||g')
 229        else
 230                dir=$(echo "$repo" |
 231                        sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 232        fi
 233fi
 234
 235[ -e "$dir" ] && die "destination directory '$dir' already exists."
 236[ yes = "$bare" ] && unset GIT_WORK_TREE
 237[ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
 238die "working tree '$GIT_WORK_TREE' already exists."
 239D=
 240W=
 241cleanup() {
 242        err=$?
 243        test -z "$D" && rm -rf "$dir"
 244        test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
 245        cd ..
 246        test -n "$D" && rm -rf "$D"
 247        test -n "$W" && rm -rf "$W"
 248        exit $err
 249}
 250trap cleanup 0
 251mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
 252test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
 253W=$(cd "$GIT_WORK_TREE" && pwd) && GIT_WORK_TREE="$W" && export GIT_WORK_TREE
 254if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
 255        GIT_DIR="$D"
 256else
 257        GIT_DIR="$D/.git"
 258fi &&
 259export GIT_DIR &&
 260GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
 261
 262if test -n "$bare"
 263then
 264        GIT_CONFIG="$GIT_DIR/config" git config core.bare true
 265fi
 266
 267if test -n "$reference"
 268then
 269        ref_git=
 270        if test -d "$reference"
 271        then
 272                if test -d "$reference/.git/objects"
 273                then
 274                        ref_git="$reference/.git"
 275                elif test -d "$reference/objects"
 276                then
 277                        ref_git="$reference"
 278                fi
 279        fi
 280        if test -n "$ref_git"
 281        then
 282                ref_git=$(cd "$ref_git" && pwd)
 283                echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
 284                (
 285                        GIT_DIR="$ref_git" git for-each-ref \
 286                                --format='%(objectname) %(*objectname)'
 287                ) |
 288                while read a b
 289                do
 290                        test -z "$a" ||
 291                        git update-ref "refs/reference-tmp/$a" "$a"
 292                        test -z "$b" ||
 293                        git update-ref "refs/reference-tmp/$b" "$b"
 294                done
 295        else
 296                die "reference repository '$reference' is not a local directory."
 297        fi
 298fi
 299
 300rm -f "$GIT_DIR/CLONE_HEAD"
 301
 302# We do local magic only when the user tells us to.
 303case "$local" in
 304yes)
 305        ( cd "$repo/objects" ) ||
 306                die "cannot chdir to local '$repo/objects'."
 307
 308        if test "$local_shared" = yes
 309        then
 310                mkdir -p "$GIT_DIR/objects/info"
 311                echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
 312        else
 313                cpio_quiet_flag=""
 314                cpio --help 2>&1 | grep -- --quiet >/dev/null && \
 315                        cpio_quiet_flag=--quiet
 316                l= &&
 317                if test "$use_local_hardlink" = yes
 318                then
 319                        # See if we can hardlink and drop "l" if not.
 320                        sample_file=$(cd "$repo" && \
 321                                      find objects -type f -print | sed -e 1q)
 322                        # objects directory should not be empty because
 323                        # we are cloning!
 324                        test -f "$repo/$sample_file" ||
 325                                die "fatal: cannot clone empty repository"
 326                        if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
 327                        then
 328                                rm -f "$GIT_DIR/objects/sample"
 329                                l=l
 330                        elif test -n "$local_explicitly_asked_for"
 331                        then
 332                                echo >&2 "Warning: -l asked but cannot hardlink to $repo"
 333                        fi
 334                fi &&
 335                cd "$repo" &&
 336                find objects -depth -print | cpio $cpio_quiet_flag -pumd$l "$GIT_DIR/" || \
 337                        exit 1
 338        fi
 339        git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
 340        ;;
 341*)
 342        case "$repo" in
 343        rsync://*)
 344                case "$depth" in
 345                "") ;;
 346                *) die "shallow over rsync not supported" ;;
 347                esac
 348                rsync $quiet -av --ignore-existing  \
 349                        --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
 350                exit
 351                # Look at objects/info/alternates for rsync -- http will
 352                # support it natively and git native ones will do it on the
 353                # remote end.  Not having that file is not a crime.
 354                rsync -q "$repo/objects/info/alternates" \
 355                        "$GIT_DIR/TMP_ALT" 2>/dev/null ||
 356                        rm -f "$GIT_DIR/TMP_ALT"
 357                if test -f "$GIT_DIR/TMP_ALT"
 358                then
 359                    ( cd "$D" &&
 360                      . git-parse-remote &&
 361                      resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
 362                    while read alt
 363                    do
 364                        case "$alt" in 'bad alternate: '*) die "$alt";; esac
 365                        case "$quiet" in
 366                        '')     echo >&2 "Getting alternate: $alt" ;;
 367                        esac
 368                        rsync $quiet -av --ignore-existing  \
 369                            --exclude info "$alt" "$GIT_DIR/objects" || exit
 370                    done
 371                    rm -f "$GIT_DIR/TMP_ALT"
 372                fi
 373                git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
 374                ;;
 375        https://*|http://*|ftp://*)
 376                case "$depth" in
 377                "") ;;
 378                *) die "shallow over http or ftp not supported" ;;
 379                esac
 380                if test -z "@@NO_CURL@@"
 381                then
 382                        clone_dumb_http "$repo" "$D"
 383                else
 384                        die "http transport not supported, rebuild Git with curl support"
 385                fi
 386                ;;
 387        *)
 388                if [ -f "$repo" ] ; then
 389                        git bundle unbundle "$repo" > "$GIT_DIR/CLONE_HEAD" ||
 390                        die "unbundle from '$repo' failed."
 391                else
 392                        case "$upload_pack" in
 393                        '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
 394                        *) git-fetch-pack --all -k \
 395                                $quiet "$upload_pack" $depth $no_progress "$repo" ;;
 396                        esac >"$GIT_DIR/CLONE_HEAD" ||
 397                        die "fetch-pack from '$repo' failed."
 398                fi
 399                ;;
 400        esac
 401        ;;
 402esac
 403test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
 404
 405if test -f "$GIT_DIR/CLONE_HEAD"
 406then
 407        # Read git-fetch-pack -k output and store the remote branches.
 408        if [ -n "$use_separate_remote" ]
 409        then
 410                branch_top="remotes/$origin"
 411        else
 412                branch_top="heads"
 413        fi
 414        tag_top="tags"
 415        while read sha1 name
 416        do
 417                case "$name" in
 418                *'^{}')
 419                        continue ;;
 420                HEAD)
 421                        destname="REMOTE_HEAD" ;;
 422                refs/heads/*)
 423                        destname="refs/$branch_top/${name#refs/heads/}" ;;
 424                refs/tags/*)
 425                        destname="refs/$tag_top/${name#refs/tags/}" ;;
 426                *)
 427                        continue ;;
 428                esac
 429                git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
 430        done < "$GIT_DIR/CLONE_HEAD"
 431fi
 432
 433if test -n "$W"; then
 434        cd "$W" || exit
 435else
 436        cd "$D" || exit
 437fi
 438
 439if test -z "$bare"
 440then
 441        # a non-bare repository is always in separate-remote layout
 442        remote_top="refs/remotes/$origin"
 443        head_sha1=
 444        test ! -r "$GIT_DIR/REMOTE_HEAD" || head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
 445        case "$head_sha1" in
 446        'ref: refs/'*)
 447                # Uh-oh, the remote told us (http transport done against
 448                # new style repository with a symref HEAD).
 449                # Ideally we should skip the guesswork but for now
 450                # opt for minimum change.
 451                head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
 452                head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
 453                ;;
 454        esac
 455
 456        # The name under $remote_top the remote HEAD seems to point at.
 457        head_points_at=$(
 458                (
 459                        test -f "$GIT_DIR/$remote_top/master" && echo "master"
 460                        cd "$GIT_DIR/$remote_top" &&
 461                        find . -type f -print | sed -e 's/^\.\///'
 462                ) | (
 463                done=f
 464                while read name
 465                do
 466                        test t = $done && continue
 467                        branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
 468                        if test "$head_sha1" = "$branch_tip"
 469                        then
 470                                echo "$name"
 471                                done=t
 472                        fi
 473                done
 474                )
 475        )
 476
 477        # Upstream URL
 478        git config remote."$origin".url "$repo" &&
 479
 480        # Set up the mappings to track the remote branches.
 481        git config remote."$origin".fetch \
 482                "+refs/heads/*:$remote_top/*" '^$' &&
 483
 484        # Write out remote.$origin config, and update our "$head_points_at".
 485        case "$head_points_at" in
 486        ?*)
 487                # Local default branch
 488                git symbolic-ref HEAD "refs/heads/$head_points_at" &&
 489
 490                # Tracking branch for the primary branch at the remote.
 491                git update-ref HEAD "$head_sha1" &&
 492
 493                rm -f "refs/remotes/$origin/HEAD"
 494                git symbolic-ref "refs/remotes/$origin/HEAD" \
 495                        "refs/remotes/$origin/$head_points_at" &&
 496
 497                git config branch."$head_points_at".remote "$origin" &&
 498                git config branch."$head_points_at".merge "refs/heads/$head_points_at"
 499                ;;
 500        '')
 501                if test -z "$head_sha1"
 502                then
 503                        # Source had nonexistent ref in HEAD
 504                        echo >&2 "Warning: Remote HEAD refers to nonexistent ref, unable to checkout."
 505                        no_checkout=t
 506                else
 507                        # Source had detached HEAD pointing nowhere
 508                        git update-ref --no-deref HEAD "$head_sha1" &&
 509                        rm -f "refs/remotes/$origin/HEAD"
 510                fi
 511                ;;
 512        esac
 513
 514        case "$no_checkout" in
 515        '')
 516                test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
 517                git read-tree -m -u $v HEAD HEAD
 518        esac
 519fi
 520rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
 521
 522trap - 0