Code

fetch-pack: check parse_commit/object results
[git.git] / git-clone.sh
index 4cbf60f554cee4560559cc3aa4e7d6fbb90e68ed..e98112277839349a47c26e69ce30982604d12d2c 100755 (executable)
@@ -8,15 +8,36 @@
 # See git-sh-setup why.
 unset CDPATH
 
+OPTIONS_SPEC="\
+git-clone [options] [--] <repo> [<dir>]
+--
+n,no-checkout        don't create a checkout
+bare                 create a bare repository
+naked                create a bare repository
+l,local              to clone from a local repository
+no-hardlinks         don't use local hardlinks, always copy
+s,shared             setup as a shared repository
+template=            path to the template directory
+q,quiet              be quiet
+reference=           reference repository
+o,origin=            use <name> instead of 'origin' to track upstream
+u,upload-pack=       path to git-upload-pack on the remote
+depth=               create a shallow clone of that depth
+
+use-separate-remote  compatibility, do not use
+no-separate-remote   compatibility, do not use"
+
 die() {
        echo >&2 "$@"
        exit 1
 }
 
 usage() {
-       die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
+       exec "$0" -h
 }
 
+eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
+
 get_repo_base() {
        (
                cd "`/bin/pwd`" &&
@@ -28,13 +49,19 @@ get_repo_base() {
        ) 2>/dev/null
 }
 
-if [ -n "$GIT_SSL_NO_VERIFY" ]; then
+if [ -n "$GIT_SSL_NO_VERIFY" -o \
+       "`git config --bool http.sslVerify`" = false ]; then
     curl_extra_args="-k"
 fi
 
 http_fetch () {
        # $1 = Remote, $2 = Local
        curl -nsfL $curl_extra_args "$1" >"$2"
+       curl_exit_status=$?
+       case $curl_exit_status in
+       126|127) exit ;;
+       *)       return $curl_exit_status ;;
+       esac
 }
 
 clone_dumb_http () {
@@ -43,7 +70,7 @@ clone_dumb_http () {
        clone_tmp="$GIT_DIR/clone-tmp" &&
        mkdir -p "$clone_tmp" || exit 1
        if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
-               "`git-config --bool http.noEPSV`" = true ]; then
+               "`git config --bool http.noEPSV`" = true ]; then
                curl_extra_args="${curl_extra_args} --disable-epsv"
        fi
        http_fetch "$1/info/refs" "$clone_tmp/refs" ||
@@ -87,7 +114,7 @@ Perhaps git-update-server-info needs to be run there?"
 
 quiet=
 local=no
-use_local=no
+use_local_hardlink=yes
 local_shared=no
 unset template
 no_checkout=
@@ -99,60 +126,59 @@ origin_override=
 use_separate_remote=t
 depth=
 no_progress=
+local_explicitly_asked_for=
 test -t 1 || no_progress=--no-progress
-while
-       case "$#,$1" in
-       0,*) break ;;
-       *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
-       *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
-         no_checkout=yes ;;
-       *,--na|*,--nak|*,--nake|*,--naked|\
-       *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
-       *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
-        *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
-          local_shared=yes; use_local=yes ;;
-       1,--template) usage ;;
-       *,--template)
+
+while test $# != 0
+do
+       case "$1" in
+       -n|--no-checkout)
+               no_checkout=yes ;;
+       --naked|--bare)
+               bare=yes ;;
+       -l|--local)
+               local_explicitly_asked_for=yes
+               use_local_hardlink=yes
+               ;;
+       --no-hardlinks)
+               use_local_hardlink=no ;;
+       -s|--shared)
+               local_shared=yes ;;
+       --template)
                shift; template="--template=$1" ;;
-       *,--template=*)
-         template="$1" ;;
-       *,-q|*,--quiet) quiet=-q ;;
-       *,--use-separate-remote) ;;
-       *,--no-separate-remote)
+       -q|--quiet)
+               quiet=-q ;;
+       --use-separate-remote|--no-separate-remote)
                die "clones are always made with separate-remote layout" ;;
-       1,--reference) usage ;;
-       *,--reference)
+       --reference)
                shift; reference="$1" ;;
-       *,--reference=*)
-               reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
-       *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
-               case "$2" in
+       -o|--origin)
+               shift;
+               case "$1" in
                '')
                    usage ;;
                */*)
-                   die "'$2' is not suitable for an origin name"
+                   die "'$1' is not suitable for an origin name"
                esac
-               git-check-ref-format "heads/$2" ||
-                   die "'$2' is not suitable for a branch name"
+               git check-ref-format "heads/$1" ||
+                   die "'$1' is not suitable for a branch name"
                test -z "$origin_override" ||
                    die "Do not give more than one --origin options."
                origin_override=yes
-               origin="$2"; shift
+               origin="$1"
                ;;
-       1,-u|1,--upload-pack) usage ;;
-       *,-u|*,--upload-pack)
+       -u|--upload-pack)
                shift
                upload_pack="--upload-pack=$1" ;;
-       *,--upload-pack=*)
-               upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
-       1,--depth) usage;;
-       *,--depth)
+       --depth)
+               shift
+               depth="--depth=$1" ;;
+       --)
                shift
-               depth="--depth=$1";;
-       *,-*) usage ;;
-       *) break ;;
+               break ;;
+       *)
+               usage ;;
        esac
-do
        shift
 done
 
@@ -180,22 +206,63 @@ fi
 # it is local
 if base=$(get_repo_base "$repo"); then
        repo="$base"
-       local=yes
+       if test -z "$depth"
+       then
+               local=yes
+       fi
+elif test -f "$repo"
+then
+       case "$repo" in /*) ;; *) repo="$PWD/$repo" ;; esac
+fi
+
+# Decide the directory name of the new repository
+if test -n "$2"
+then
+       dir="$2"
+else
+       # Derive one from the repository name
+       # Try using "humanish" part of source repo if user didn't specify one
+       if test -f "$repo"
+       then
+               # Cloning from a bundle
+               dir=$(echo "$repo" | sed -e 's|/*\.bundle$||' -e 's|.*/||g')
+       else
+               dir=$(echo "$repo" |
+                       sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
+       fi
 fi
 
-dir="$2"
-# Try using "humanish" part of source repo if user didn't specify one
-[ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 [ -e "$dir" ] && die "destination directory '$dir' already exists."
-mkdir -p "$dir" &&
-D=$(cd "$dir" && pwd) &&
-trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
-case "$bare" in
-yes)
-       GIT_DIR="$D" ;;
-*)
-       GIT_DIR="$D/.git" ;;
-esac && export GIT_DIR && git-init ${template+"$template"} || usage
+[ yes = "$bare" ] && unset GIT_WORK_TREE
+[ -n "$GIT_WORK_TREE" ] && [ -e "$GIT_WORK_TREE" ] &&
+die "working tree '$GIT_WORK_TREE' already exists."
+D=
+W=
+cleanup() {
+       err=$?
+       test -z "$D" && rm -rf "$dir"
+       test -z "$W" && test -n "$GIT_WORK_TREE" && rm -rf "$GIT_WORK_TREE"
+       cd ..
+       test -n "$D" && rm -rf "$D"
+       test -n "$W" && rm -rf "$W"
+       exit $err
+}
+trap cleanup 0
+mkdir -p "$dir" && D=$(cd "$dir" && pwd) || usage
+test -n "$GIT_WORK_TREE" && mkdir -p "$GIT_WORK_TREE" &&
+W=$(cd "$GIT_WORK_TREE" && pwd) && GIT_WORK_TREE="$W" && export GIT_WORK_TREE
+if test yes = "$bare" || test -n "$GIT_WORK_TREE"; then
+       GIT_DIR="$D"
+else
+       GIT_DIR="$D/.git"
+fi &&
+export GIT_DIR &&
+GIT_CONFIG="$GIT_DIR/config" git-init $quiet ${template+"$template"} || usage
+
+if test -n "$bare"
+then
+       GIT_CONFIG="$GIT_DIR/config" git config core.bare true
+fi
 
 if test -n "$reference"
 then
@@ -233,34 +300,38 @@ fi
 rm -f "$GIT_DIR/CLONE_HEAD"
 
 # We do local magic only when the user tells us to.
-case "$local,$use_local" in
-yes,yes)
+case "$local" in
+yes)
        ( cd "$repo/objects" ) ||
-               die "-l flag seen but repository '$repo' is not local."
-
-       case "$local_shared" in
-       no)
-           # See if we can hardlink and drop "l" if not.
-           sample_file=$(cd "$repo" && \
-                         find objects -type f -print | sed -e 1q)
-
-           # objects directory should not be empty since we are cloning!
-           test -f "$repo/$sample_file" || exit
-
-           l=
-           if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
-           then
-                   l=l
-           fi &&
-           rm -f "$GIT_DIR/objects/sample" &&
-           cd "$repo" &&
-           find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
-           ;;
-       yes)
-           mkdir -p "$GIT_DIR/objects/info"
-           echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
-           ;;
-       esac
+               die "cannot chdir to local '$repo/objects'."
+
+       if test "$local_shared" = yes
+       then
+               mkdir -p "$GIT_DIR/objects/info"
+               echo "$repo/objects" >>"$GIT_DIR/objects/info/alternates"
+       else
+               l= &&
+               if test "$use_local_hardlink" = yes
+               then
+                       # See if we can hardlink and drop "l" if not.
+                       sample_file=$(cd "$repo" && \
+                                     find objects -type f -print | sed -e 1q)
+                       # objects directory should not be empty because
+                       # we are cloning!
+                       test -f "$repo/$sample_file" ||
+                               die "fatal: cannot clone empty repository"
+                       if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
+                       then
+                               rm -f "$GIT_DIR/objects/sample"
+                               l=l
+                       elif test -n "$local_explicitly_asked_for"
+                       then
+                               echo >&2 "Warning: -l asked but cannot hardlink to $repo"
+                       fi
+               fi &&
+               cd "$repo" &&
+               find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
+       fi
        git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
        ;;
 *)
@@ -310,11 +381,17 @@ yes,yes)
                fi
                ;;
        *)
-               case "$upload_pack" in
-               '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
-               *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
-               esac >"$GIT_DIR/CLONE_HEAD" ||
+               if [ -f "$repo" ] ; then
+                       git bundle unbundle "$repo" > "$GIT_DIR/CLONE_HEAD" ||
+                       die "unbundle from '$repo' failed."
+               else
+                       case "$upload_pack" in
+                       '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
+                       *) git-fetch-pack --all -k \
+                               $quiet "$upload_pack" $depth $no_progress "$repo" ;;
+                       esac >"$GIT_DIR/CLONE_HEAD" ||
                        die "fetch-pack from '$repo' failed."
+               fi
                ;;
        esac
        ;;
@@ -345,17 +422,22 @@ then
                *)
                        continue ;;
                esac
-               git-update-ref -m "clone: from $repo" "$destname" "$sha1" ""
+               git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
        done < "$GIT_DIR/CLONE_HEAD"
 fi
 
-cd "$D" || exit
+if test -n "$W"; then
+       cd "$W" || exit
+else
+       cd "$D" || exit
+fi
 
-if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
+if test -z "$bare"
 then
        # a non-bare repository is always in separate-remote layout
        remote_top="refs/remotes/$origin"
-       head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
+       head_sha1=
+       test ! -r "$GIT_DIR/REMOTE_HEAD" || head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
        case "$head_sha1" in
        'ref: refs/'*)
                # Uh-oh, the remote told us (http transport done against
@@ -389,39 +471,46 @@ then
        )
 
        # Upstream URL
-       git-config remote."$origin".url "$repo" &&
+       git config remote."$origin".url "$repo" &&
 
        # Set up the mappings to track the remote branches.
-       git-config remote."$origin".fetch \
+       git config remote."$origin".fetch \
                "+refs/heads/*:$remote_top/*" '^$' &&
 
        # Write out remote.$origin config, and update our "$head_points_at".
        case "$head_points_at" in
        ?*)
                # Local default branch
-               git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
+               git symbolic-ref HEAD "refs/heads/$head_points_at" &&
 
                # Tracking branch for the primary branch at the remote.
-               git-update-ref HEAD "$head_sha1" &&
+               git update-ref HEAD "$head_sha1" &&
 
                rm -f "refs/remotes/$origin/HEAD"
-               git-symbolic-ref "refs/remotes/$origin/HEAD" \
+               git symbolic-ref "refs/remotes/$origin/HEAD" \
                        "refs/remotes/$origin/$head_points_at" &&
 
-               git-config branch."$head_points_at".remote "$origin" &&
-               git-config branch."$head_points_at".merge "refs/heads/$head_points_at"
+               git config branch."$head_points_at".remote "$origin" &&
+               git config branch."$head_points_at".merge "refs/heads/$head_points_at"
                ;;
        '')
-               # Source had detached HEAD pointing nowhere
-               git-update-ref --no-deref HEAD "$head_sha1" &&
-               rm -f "refs/remotes/$origin/HEAD"
+               if test -z "$head_sha1"
+               then
+                       # Source had nonexistent ref in HEAD
+                       echo >&2 "Warning: Remote HEAD refers to nonexistent ref, unable to checkout."
+                       no_checkout=t
+               else
+                       # Source had detached HEAD pointing nowhere
+                       git update-ref --no-deref HEAD "$head_sha1" &&
+                       rm -f "refs/remotes/$origin/HEAD"
+               fi
                ;;
        esac
 
        case "$no_checkout" in
        '')
                test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
-               git-read-tree -m -u $v HEAD HEAD
+               git read-tree -m -u $v HEAD HEAD
        esac
 fi
 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"