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