git-clone.shon commit git-clone: print an error message when trying to clone empty repo (ef4cffd)
   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
  11OPTIONS_SPEC="\
  12git-clone [options] [--] <repo> [<dir>]
  13--
  14n,no-checkout        don't create a checkout
  15bare                 create a bare repository
  16naked                create a bare repository
  17l,local              to clone from a local repository
  18no-hardlinks         don't use local hardlinks, always copy
  19s,shared             setup as a shared repository
  20template=            path to the template directory
  21q,quiet              be quiet
  22reference=           reference repository
  23o,origin=            use <name> instead of 'origin' to track upstream
  24u,upload-pack=       path to git-upload-pack on the remote
  25depth=               create a shallow clone of that depth
  26
  27use-separate-remote  compatibility, do not use
  28no-separate-remote   compatibility, do not use"
  29
  30die() {
  31        echo >&2 "$@"
  32        exit 1
  33}
  34
  35usage() {
  36        exec "$0" -h
  37}
  38
  39eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
  40
  41get_repo_base() {
  42        (
  43                cd "`/bin/pwd`" &&
  44                cd "$1" || cd "$1.git" &&
  45                {
  46                        cd .git
  47                        pwd
  48                }
  49        ) 2>/dev/null
  50}
  51
  52if [ -n "$GIT_SSL_NO_VERIFY" -o \
  53        "`git config --bool http.sslVerify`" = false ]; then
  54    curl_extra_args="-k"
  55fi
  56
  57http_fetch () {
  58        # $1 = Remote, $2 = Local
  59        curl -nsfL $curl_extra_args "$1" >"$2" ||
  60                case $? in
  61                126|127) exit ;;
  62                *)       return $? ;;
  63                esac
  64}
  65
  66clone_dumb_http () {
  67        # $1 - remote, $2 - local
  68        cd "$2" &&
  69        clone_tmp="$GIT_DIR/clone-tmp" &&
  70        mkdir -p "$clone_tmp" || exit 1
  71        if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
  72                "`git config --bool http.noEPSV`" = true ]; then
  73                curl_extra_args="${curl_extra_args} --disable-epsv"
  74        fi
  75        http_fetch "$1/info/refs" "$clone_tmp/refs" ||
  76                die "Cannot get remote repository information.
  77Perhaps git-update-server-info needs to be run there?"
  78        test "z$quiet" = z && v=-v || v=
  79        while read sha1 refname
  80        do
  81                name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
  82                case "$name" in
  83                *^*)    continue;;
  84                esac
  85                case "$bare,$name" in
  86                yes,* | ,heads/* | ,tags/*) ;;
  87                *)      continue ;;
  88                esac
  89                if test -n "$use_separate_remote" &&
  90                   branch_name=`expr "z$name" : 'zheads/\(.*\)'`
  91                then
  92                        tname="remotes/$origin/$branch_name"
  93                else
  94                        tname=$name
  95                fi
  96                git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
  97        done <"$clone_tmp/refs"
  98        rm -fr "$clone_tmp"
  99        http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
 100        rm -f "$GIT_DIR/REMOTE_HEAD"
 101        if test -f "$GIT_DIR/REMOTE_HEAD"; then
 102                head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
 103                case "$head_sha1" in
 104                'ref: refs/'*)
 105                        ;;
 106                *)
 107                        git-http-fetch $v -a "$head_sha1" "$1" ||
 108                        rm -f "$GIT_DIR/REMOTE_HEAD"
 109                        ;;
 110                esac
 111        fi
 112}
 113
 114quiet=
 115local=no
 116use_local_hardlink=yes
 117local_shared=no
 118unset template
 119no_checkout=
 120upload_pack=
 121bare=
 122reference=
 123origin=
 124origin_override=
 125use_separate_remote=t
 126depth=
 127no_progress=
 128local_explicitly_asked_for=
 129test -t 1 || no_progress=--no-progress
 130
 131while test $# != 0
 132do
 133        case "$1" in
 134        -n|--no-checkout)
 135                no_checkout=yes ;;
 136        --naked|--bare)
 137                bare=yes ;;
 138        -l|--local)
 139                local_explicitly_asked_for=yes
 140                use_local_hardlink=yes
 141                ;;
 142        --no-hardlinks)
 143                use_local_hardlink=no ;;
 144        -s|--shared)
 145                local_shared=yes ;;
 146        --template)
 147                shift; template="--template=$1" ;;
 148        -q|--quiet)
 149                quiet=-q ;;
 150        --use-separate-remote|--no-separate-remote)
 151                die "clones are always made with separate-remote layout" ;;
 152        --reference)
 153                shift; reference="$1" ;;
 154        -o,--origin)
 155                shift;
 156                case "$1" in
 157                '')
 158                    usage ;;
 159                */*)
 160                    die "'$1' is not suitable for an origin name"
 161                esac
 162                git check-ref-format "heads/$1" ||
 163                    die "'$1' is not suitable for a branch name"
 164                test -z "$origin_override" ||
 165                    die "Do not give more than one --origin options."
 166                origin_override=yes
 167                origin="$1"
 168                ;;
 169        -u|--upload-pack)
 170                shift
 171                upload_pack="--upload-pack=$1" ;;
 172        --depth)
 173                shift
 174                depth="--depth=$1" ;;
 175        --)
 176                shift
 177                break ;;
 178        *)
 179                usage ;;
 180        esac
 181        shift
 182done
 183
 184repo="$1"
 185test -n "$repo" ||
 186    die 'you must specify a repository to clone.'
 187
 188# --bare implies --no-checkout and --no-separate-remote
 189if test yes = "$bare"
 190then
 191        if test yes = "$origin_override"
 192        then
 193                die '--bare and --origin $origin options are incompatible.'
 194        fi
 195        no_checkout=yes
 196        use_separate_remote=
 197fi
 198
 199if test -z "$origin"
 200then
 201        origin=origin
 202fi
 203
 204# Turn the source into an absolute path if
 205# it is local
 206if base=$(get_repo_base "$repo"); then
 207        repo="$base"
 208        local=yes
 209fi
 210
 211dir="$2"
 212# Try using "humanish" part of source repo if user didn't specify one
 213[ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 214[ -e "$dir" ] && die "destination directory '$dir' already exists."
 215[ yes = "$bare" ] && unset GIT_WORK_TREE
 216[ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
 217die "working tree '$GIT_WORK_TREE' already exists."
 218D=
 219W=
 220cleanup() {
 221        err=$?
 222        test -z "$D" && rm -rf "$dir"
 223        test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
 224        cd ..
 225        test -n "$D" && rm -rf "$D"
 226        test -n "$W" && rm -rf "$W"
 227        exit $err
 228}
 229trap cleanup 0
 230mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
 231test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
 232W=$(cd "$GIT_WORK_TREE" && pwd) && GIT_WORK_TREE="$W" && export GIT_WORK_TREE
 233if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
 234        GIT_DIR="$D"
 235else
 236        GIT_DIR="$D/.git"
 237fi &&
 238export GIT_DIR &&
 239GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
 240
 241if test -n "$bare"
 242then
 243        GIT_CONFIG="$GIT_DIR/config" git config core.bare true
 244fi
 245
 246if test -n "$reference"
 247then
 248        ref_git=
 249        if test -d "$reference"
 250        then
 251                if test -d "$reference/.git/objects"
 252                then
 253                        ref_git="$reference/.git"
 254                elif test -d "$reference/objects"
 255                then
 256                        ref_git="$reference"
 257                fi
 258        fi
 259        if test -n "$ref_git"
 260        then
 261                ref_git=$(cd "$ref_git" && pwd)
 262                echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
 263                (
 264                        GIT_DIR="$ref_git" git for-each-ref \
 265                                --format='%(objectname) %(*objectname)'
 266                ) |
 267                while read a b
 268                do
 269                        test -z "$a" ||
 270                        git update-ref "refs/reference-tmp/$a" "$a"
 271                        test -z "$b" ||
 272                        git update-ref "refs/reference-tmp/$b" "$b"
 273                done
 274        else
 275                die "reference repository '$reference' is not a local directory."
 276        fi
 277fi
 278
 279rm -f "$GIT_DIR/CLONE_HEAD"
 280
 281# We do local magic only when the user tells us to.
 282case "$local" in
 283yes)
 284        ( cd "$repo/objects" ) ||
 285                die "cannot chdir to local '$repo/objects'."
 286
 287        if test "$local_shared" = yes
 288        then
 289                mkdir -p "$GIT_DIR/objects/info"
 290                echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
 291        else
 292                l= &&
 293                if test "$use_local_hardlink" = yes
 294                then
 295                        # See if we can hardlink and drop "l" if not.
 296                        sample_file=$(cd "$repo" && \
 297                                      find objects -type f -print | sed -e 1q)
 298                        # objects directory should not be empty because
 299                        # we are cloning!
 300                        test -f "$repo/$sample_file" ||
 301                                die "fatal: cannot clone empty repository"
 302                        if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
 303                        then
 304                                rm -f "$GIT_DIR/objects/sample"
 305                                l=l
 306                        elif test -n "$local_explicitly_asked_for"
 307                        then
 308                                echo >&2 "Warning: -l asked but cannot hardlink to $repo"
 309                        fi
 310                fi &&
 311                cd "$repo" &&
 312                find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
 313        fi
 314        git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
 315        ;;
 316*)
 317        case "$repo" in
 318        rsync://*)
 319                case "$depth" in
 320                "") ;;
 321                *) die "shallow over rsync not supported" ;;
 322                esac
 323                rsync $quiet -av --ignore-existing  \
 324                        --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
 325                exit
 326                # Look at objects/info/alternates for rsync -- http will
 327                # support it natively and git native ones will do it on the
 328                # remote end.  Not having that file is not a crime.
 329                rsync -q "$repo/objects/info/alternates" \
 330                        "$GIT_DIR/TMP_ALT" 2>/dev/null ||
 331                        rm -f "$GIT_DIR/TMP_ALT"
 332                if test -f "$GIT_DIR/TMP_ALT"
 333                then
 334                    ( cd "$D" &&
 335                      . git-parse-remote &&
 336                      resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
 337                    while read alt
 338                    do
 339                        case "$alt" in 'bad alternate: '*) die "$alt";; esac
 340                        case "$quiet" in
 341                        '')     echo >&2 "Getting alternate: $alt" ;;
 342                        esac
 343                        rsync $quiet -av --ignore-existing  \
 344                            --exclude info "$alt" "$GIT_DIR/objects" || exit
 345                    done
 346                    rm -f "$GIT_DIR/TMP_ALT"
 347                fi
 348                git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
 349                ;;
 350        https://*|http://*|ftp://*)
 351                case "$depth" in
 352                "") ;;
 353                *) die "shallow over http or ftp not supported" ;;
 354                esac
 355                if test -z "@@NO_CURL@@"
 356                then
 357                        clone_dumb_http "$repo" "$D"
 358                else
 359                        die "http transport not supported, rebuild Git with curl support"
 360                fi
 361                ;;
 362        *)
 363                case "$upload_pack" in
 364                '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
 365                *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
 366                esac >"$GIT_DIR/CLONE_HEAD" ||
 367                        die "fetch-pack from '$repo' failed."
 368                ;;
 369        esac
 370        ;;
 371esac
 372test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
 373
 374if test -f "$GIT_DIR/CLONE_HEAD"
 375then
 376        # Read git-fetch-pack -k output and store the remote branches.
 377        if [ -n "$use_separate_remote" ]
 378        then
 379                branch_top="remotes/$origin"
 380        else
 381                branch_top="heads"
 382        fi
 383        tag_top="tags"
 384        while read sha1 name
 385        do
 386                case "$name" in
 387                *'^{}')
 388                        continue ;;
 389                HEAD)
 390                        destname="REMOTE_HEAD" ;;
 391                refs/heads/*)
 392                        destname="refs/$branch_top/${name#refs/heads/}" ;;
 393                refs/tags/*)
 394                        destname="refs/$tag_top/${name#refs/tags/}" ;;
 395                *)
 396                        continue ;;
 397                esac
 398                git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
 399        done < "$GIT_DIR/CLONE_HEAD"
 400fi
 401
 402if test -n "$W"; then
 403        cd "$W" || exit
 404else
 405        cd "$D" || exit
 406fi
 407
 408if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
 409then
 410        # a non-bare repository is always in separate-remote layout
 411        remote_top="refs/remotes/$origin"
 412        head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
 413        case "$head_sha1" in
 414        'ref: refs/'*)
 415                # Uh-oh, the remote told us (http transport done against
 416                # new style repository with a symref HEAD).
 417                # Ideally we should skip the guesswork but for now
 418                # opt for minimum change.
 419                head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
 420                head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
 421                ;;
 422        esac
 423
 424        # The name under $remote_top the remote HEAD seems to point at.
 425        head_points_at=$(
 426                (
 427                        test -f "$GIT_DIR/$remote_top/master" && echo "master"
 428                        cd "$GIT_DIR/$remote_top" &&
 429                        find . -type f -print | sed -e 's/^\.\///'
 430                ) | (
 431                done=f
 432                while read name
 433                do
 434                        test t = $done && continue
 435                        branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
 436                        if test "$head_sha1" = "$branch_tip"
 437                        then
 438                                echo "$name"
 439                                done=t
 440                        fi
 441                done
 442                )
 443        )
 444
 445        # Upstream URL
 446        git config remote."$origin".url "$repo" &&
 447
 448        # Set up the mappings to track the remote branches.
 449        git config remote."$origin".fetch \
 450                "+refs/heads/*:$remote_top/*" '^$' &&
 451
 452        # Write out remote.$origin config, and update our "$head_points_at".
 453        case "$head_points_at" in
 454        ?*)
 455                # Local default branch
 456                git symbolic-ref HEAD "refs/heads/$head_points_at" &&
 457
 458                # Tracking branch for the primary branch at the remote.
 459                git update-ref HEAD "$head_sha1" &&
 460
 461                rm -f "refs/remotes/$origin/HEAD"
 462                git symbolic-ref "refs/remotes/$origin/HEAD" \
 463                        "refs/remotes/$origin/$head_points_at" &&
 464
 465                git config branch."$head_points_at".remote "$origin" &&
 466                git config branch."$head_points_at".merge "refs/heads/$head_points_at"
 467                ;;
 468        '')
 469                # Source had detached HEAD pointing nowhere
 470                git update-ref --no-deref HEAD "$head_sha1" &&
 471                rm -f "refs/remotes/$origin/HEAD"
 472                ;;
 473        esac
 474
 475        case "$no_checkout" in
 476        '')
 477                test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
 478                git read-tree -m -u $v HEAD HEAD
 479        esac
 480fi
 481rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
 482
 483trap - 0