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