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