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