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