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