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