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