git-clone.shon commit diffstat rename squashing fix. (cc908b8)
   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 "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
 105no_checkout=
 106upload_pack=
 107bare=
 108reference=
 109origin=
 110origin_override=
 111use_separate_remote=
 112while
 113        case "$#,$1" in
 114        0,*) break ;;
 115        *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
 116        *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
 117          no_checkout=yes ;;
 118        *,--na|*,--nak|*,--nake|*,--naked|\
 119        *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
 120        *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
 121        *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) 
 122          local_shared=yes; use_local=yes ;;
 123        *,-q|*,--quiet) quiet=-q ;;
 124        *,--use-separate-remote)
 125                use_separate_remote=t ;;
 126        1,--reference) usage ;;
 127        *,--reference)
 128                shift; reference="$1" ;;
 129        *,--reference=*)
 130                reference=`expr "$1" : '--reference=\(.*\)'` ;;
 131        *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
 132                case "$2" in
 133                '')
 134                    usage ;;
 135                */*)
 136                    echo >&2 "'$2' is not suitable for an origin name"
 137                    exit 1
 138                esac
 139                git-check-ref-format "heads/$2" || {
 140                    echo >&2 "'$2' is not suitable for a branch name"
 141                    exit 1
 142                }
 143                test -z "$origin_override" || {
 144                    echo >&2 "Do not give more than one --origin options."
 145                    exit 1
 146                }
 147                origin_override=yes
 148                origin="$2"; shift
 149                ;;
 150        1,-u|1,--upload-pack) usage ;;
 151        *,-u|*,--upload-pack)
 152                shift
 153                upload_pack="--exec=$1" ;;
 154        *,-*) usage ;;
 155        *) break ;;
 156        esac
 157do
 158        shift
 159done
 160
 161repo="$1"
 162if test -z "$repo"
 163then
 164    echo >&2 'you must specify a repository to clone.'
 165    exit 1
 166fi
 167
 168# --bare implies --no-checkout
 169if test yes = "$bare"
 170then
 171        if test yes = "$origin_override"
 172        then
 173                echo >&2 '--bare and --origin $origin options are incompatible.'
 174                exit 1
 175        fi
 176        if test t = "$use_separate_remote"
 177        then
 178                echo >&2 '--bare and --use-separate-remote options are incompatible.'
 179                exit 1
 180        fi
 181        no_checkout=yes
 182fi
 183
 184if test -z "$origin"
 185then
 186        origin=origin
 187fi
 188
 189# Turn the source into an absolute path if
 190# it is local
 191if base=$(get_repo_base "$repo"); then
 192        repo="$base"
 193        local=yes
 194fi
 195
 196dir="$2"
 197# Try using "humanish" part of source repo if user didn't specify one
 198[ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 199[ -e "$dir" ] && echo "$dir already exists." && usage
 200mkdir -p "$dir" &&
 201D=$(cd "$dir" && pwd) &&
 202trap 'err=$?; cd ..; rm -r "$D"; exit $err' exit
 203case "$bare" in
 204yes) GIT_DIR="$D" ;;
 205*) GIT_DIR="$D/.git" ;;
 206esac && export GIT_DIR && git-init-db || usage
 207case "$bare" in
 208yes)
 209        GIT_DIR="$D" ;;
 210*)
 211        GIT_DIR="$D/.git" ;;
 212esac
 213
 214if test -n "$reference"
 215then
 216        if test -d "$reference"
 217        then
 218                if test -d "$reference/.git/objects"
 219                then
 220                        reference="$reference/.git"
 221                fi
 222                reference=$(cd "$reference" && pwd)
 223                echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
 224                (cd "$reference" && tar cf - refs) |
 225                (cd "$GIT_DIR/refs" &&
 226                 mkdir reference-tmp &&
 227                 cd reference-tmp &&
 228                 tar xf -)
 229        else
 230                echo >&2 "$reference: not a local directory." && usage
 231        fi
 232fi
 233
 234rm -f "$GIT_DIR/CLONE_HEAD"
 235
 236# We do local magic only when the user tells us to.
 237case "$local,$use_local" in
 238yes,yes)
 239        ( cd "$repo/objects" ) || {
 240                echo >&2 "-l flag seen but $repo is not local."
 241                exit 1
 242        }
 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"
 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"
 298                ;;
 299        http://*)
 300                if test -z "@@NO_CURL@@"
 301                then
 302                        clone_dumb_http "$repo" "$D"
 303                else
 304                        echo >&2 "http transport not supported, rebuild Git with curl support"
 305                        exit 1
 306                fi
 307                ;;
 308        *)
 309                cd "$D" && case "$upload_pack" in
 310                '') git-fetch-pack --all -k $quiet "$repo" ;;
 311                *) git-fetch-pack --all -k $quiet "$upload_pack" "$repo" ;;
 312                esac >"$GIT_DIR/CLONE_HEAD" || {
 313                        echo >&2 "fetch-pack from '$repo' failed."
 314                        exit 1
 315                }
 316                ;;
 317        esac
 318        ;;
 319esac
 320test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
 321
 322if test -f "$GIT_DIR/CLONE_HEAD"
 323then
 324        # Read git-fetch-pack -k output and store the remote branches.
 325        perl -e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin"
 326fi
 327
 328cd "$D" || exit
 329
 330if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
 331then
 332        # Figure out which remote branch HEAD points at.
 333        case "$use_separate_remote" in
 334        '')     remote_top=refs/heads ;;
 335        *)      remote_top="refs/remotes/$origin" ;;
 336        esac
 337
 338        head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
 339        case "$head_sha1" in
 340        'ref: refs/'*)
 341                # Uh-oh, the remote told us (http transport done against
 342                # new style repository with a symref HEAD).
 343                # Ideally we should skip the guesswork but for now
 344                # opt for minimum change.
 345                head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
 346                head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
 347                ;;
 348        esac
 349
 350        # The name under $remote_top the remote HEAD seems to point at.
 351        head_points_at=$(
 352                (
 353                        echo "master"
 354                        cd "$GIT_DIR/$remote_top" &&
 355                        find . -type f -print | sed -e 's/^\.\///'
 356                ) | (
 357                done=f
 358                while read name
 359                do
 360                        test t = $done && continue
 361                        branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
 362                        if test "$head_sha1" = "$branch_tip"
 363                        then
 364                                echo "$name"
 365                                done=t
 366                        fi
 367                done
 368                )
 369        )
 370
 371        # Write out remotes/$origin file, and update our "$head_points_at".
 372        case "$head_points_at" in
 373        ?*)
 374                mkdir -p "$GIT_DIR/remotes" &&
 375                git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
 376                case "$use_separate_remote" in
 377                t)      origin_track="$remote_top/$head_points_at"
 378                        git-update-ref HEAD "$head_sha1" ;;
 379                *)      origin_track="$remote_top/$origin"
 380                        git-update-ref "refs/heads/$origin" "$head_sha1" ;;
 381                esac &&
 382                echo >"$GIT_DIR/remotes/$origin" \
 383                "URL: $repo
 384Pull: refs/heads/$head_points_at:$origin_track" &&
 385                (cd "$GIT_DIR/$remote_top" && find . -type f -print) |
 386                while read dotslref
 387                do
 388                        name=`expr "$dotslref" : './\(.*\)'` &&
 389                        test "$use_separate_remote" = '' && {
 390                                test "$head_points_at" = "$name" ||
 391                                test "$origin" = "$name"
 392                        } ||
 393                        echo "Pull: refs/heads/${name}:$remote_top/${name}"
 394                done >>"$GIT_DIR/remotes/$origin" &&
 395                case "$use_separate_remote" in
 396                t)
 397                        rm -f "refs/remotes/$origin/HEAD"
 398                        git-symbolic-ref "refs/remotes/$origin/HEAD" \
 399                                "refs/remotes/$origin/$head_points_at"
 400                esac
 401        esac
 402
 403        case "$no_checkout" in
 404        '')
 405                git-read-tree -m -u -v HEAD HEAD
 406        esac
 407fi
 408rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
 409
 410trap - exit
 411