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