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