filter-branch documentation: some more touch-ups.
[gitweb.git] / git-clone.sh
index 490f3e48db02d01cb480047c011aa7b670c431bc..48dafa21cf9efef957739408c78ba4ab47494f13 100755 (executable)
@@ -2,7 +2,7 @@
 #
 # Copyright (c) 2005, Linus Torvalds
 # Copyright (c) 2005, Junio C Hamano
-# 
+#
 # Clone a repository into a different directory that does not yet exist.
 
 # See git-sh-setup why.
@@ -14,11 +14,18 @@ die() {
 }
 
 usage() {
-       die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [-n] <repo> [<dir>]"
+       die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-n] <repo> [<dir>]"
 }
 
 get_repo_base() {
-       (cd "$1" && (cd .git ; pwd)) 2> /dev/null
+       (
+               cd "`/bin/pwd`" &&
+               cd "$1" || cd "$1.git" &&
+               {
+                       cd .git
+                       pwd
+               }
+       ) 2>/dev/null
 }
 
 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
@@ -36,12 +43,13 @@ clone_dumb_http () {
        clone_tmp="$GIT_DIR/clone-tmp" &&
        mkdir -p "$clone_tmp" || exit 1
        if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
-               "`git-repo-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" ||
                die "Cannot get remote repository information.
 Perhaps git-update-server-info needs to be run there?"
+       test "z$quiet" = z && v=-v || v=
        while read sha1 refname
        do
                name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
@@ -59,55 +67,24 @@ Perhaps git-update-server-info needs to be run there?"
                else
                        tname=$name
                fi
-               git-http-fetch -v -a -w "$tname" "$name" "$1/" || exit 1
+               git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
        done <"$clone_tmp/refs"
        rm -fr "$clone_tmp"
        http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
        rm -f "$GIT_DIR/REMOTE_HEAD"
+       if test -f "$GIT_DIR/REMOTE_HEAD"; then
+               head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
+               case "$head_sha1" in
+               'ref: refs/'*)
+                       ;;
+               *)
+                       git-http-fetch $v -a "$head_sha1" "$1" ||
+                       rm -f "$GIT_DIR/REMOTE_HEAD"
+                       ;;
+               esac
+       fi
 }
 
-# Read git-fetch-pack -k output and store the remote branches.
-copy_refs='
-use File::Path qw(mkpath);
-use File::Basename qw(dirname);
-my $git_dir = $ARGV[0];
-my $use_separate_remote = $ARGV[1];
-my $origin = $ARGV[2];
-
-my $branch_top = ($use_separate_remote ? "remotes/$origin" : "heads");
-my $tag_top = "tags";
-
-sub store {
-       my ($sha1, $name, $top) = @_;
-       $name = "$git_dir/refs/$top/$name";
-       mkpath(dirname($name));
-       open O, ">", "$name";
-       print O "$sha1\n";
-       close O;
-}
-
-open FH, "<", "$git_dir/CLONE_HEAD";
-while (<FH>) {
-       my ($sha1, $name) = /^([0-9a-f]{40})\s(.*)$/;
-       next if ($name =~ /\^\173/);
-       if ($name eq "HEAD") {
-               open O, ">", "$git_dir/REMOTE_HEAD";
-               print O "$sha1\n";
-               close O;
-               next;
-       }
-       if ($name =~ s/^refs\/heads\///) {
-               store($sha1, $name, $branch_top);
-               next;
-       }
-       if ($name =~ s/^refs\/tags\///) {
-               store($sha1, $name, $tag_top);
-               next;
-       }
-}
-close FH;
-'
-
 quiet=
 local=no
 use_local=no
@@ -120,6 +97,9 @@ reference=
 origin=
 origin_override=
 use_separate_remote=t
+depth=
+no_progress=
+test -t 1 || no_progress=--no-progress
 while
        case "$#,$1" in
        0,*) break ;;
@@ -129,7 +109,7 @@ while
        *,--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) 
+        *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
           local_shared=yes; use_local=yes ;;
        1,--template) usage ;;
        *,--template)
@@ -152,7 +132,7 @@ while
                */*)
                    die "'$2' is not suitable for an origin name"
                esac
-               git-check-ref-format "heads/$2" ||
+               git check-ref-format "heads/$2" ||
                    die "'$2' is not suitable for a branch name"
                test -z "$origin_override" ||
                    die "Do not give more than one --origin options."
@@ -162,7 +142,13 @@ while
        1,-u|1,--upload-pack) usage ;;
        *,-u|*,--upload-pack)
                shift
-               upload_pack="--exec=$1" ;;
+               upload_pack="--upload-pack=$1" ;;
+       *,--upload-pack=*)
+               upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
+       1,--depth) usage;;
+       *,--depth)
+               shift
+               depth="--depth=$1";;
        *,-*) usage ;;
        *) break ;;
        esac
@@ -209,23 +195,36 @@ yes)
        GIT_DIR="$D" ;;
 *)
        GIT_DIR="$D/.git" ;;
-esac && export GIT_DIR && git-init-db ${template+"$template"} || usage
+esac && export GIT_DIR && git init $quiet ${template+"$template"} || usage
 
 if test -n "$reference"
 then
+       ref_git=
        if test -d "$reference"
        then
                if test -d "$reference/.git/objects"
                then
-                       reference="$reference/.git"
+                       ref_git="$reference/.git"
+               elif test -d "$reference/objects"
+               then
+                       ref_git="$reference"
                fi
-               reference=$(cd "$reference" && pwd)
-               echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
-               (cd "$reference" && tar cf - refs) |
-               (cd "$GIT_DIR/refs" &&
-                mkdir reference-tmp &&
-                cd reference-tmp &&
-                tar xf -)
+       fi
+       if test -n "$ref_git"
+       then
+               ref_git=$(cd "$ref_git" && pwd)
+               echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
+               (
+                       GIT_DIR="$ref_git" git for-each-ref \
+                               --format='%(objectname) %(*objectname)'
+               ) |
+               while read a b
+               do
+                       test -z "$a" ||
+                       git update-ref "refs/reference-tmp/$a" "$a"
+                       test -z "$b" ||
+                       git update-ref "refs/reference-tmp/$b" "$b"
+               done
        else
                die "reference repository '$reference' is not a local directory."
        fi
@@ -267,6 +266,10 @@ yes,yes)
 *)
        case "$repo" in
        rsync://*)
+               case "$depth" in
+               "") ;;
+               *) die "shallow over rsync not supported" ;;
+               esac
                rsync $quiet -av --ignore-existing  \
                        --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
                exit
@@ -295,6 +298,10 @@ yes,yes)
                git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
                ;;
        https://*|http://*|ftp://*)
+               case "$depth" in
+               "") ;;
+               *) die "shallow over http or ftp not supported" ;;
+               esac
                if test -z "@@NO_CURL@@"
                then
                        clone_dumb_http "$repo" "$D"
@@ -304,8 +311,8 @@ yes,yes)
                ;;
        *)
                case "$upload_pack" in
-               '') git-fetch-pack --all -k $quiet "$repo" ;;
-               *) git-fetch-pack --all -k $quiet "$upload_pack" "$repo" ;;
+               '') 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."
                ;;
@@ -317,8 +324,29 @@ test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
 if test -f "$GIT_DIR/CLONE_HEAD"
 then
        # Read git-fetch-pack -k output and store the remote branches.
-       @@PERL@@ -e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin" ||
-       exit
+       if [ -n "$use_separate_remote" ]
+       then
+               branch_top="remotes/$origin"
+       else
+               branch_top="heads"
+       fi
+       tag_top="tags"
+       while read sha1 name
+       do
+               case "$name" in
+               *'^{}')
+                       continue ;;
+               HEAD)
+                       destname="REMOTE_HEAD" ;;
+               refs/heads/*)
+                       destname="refs/$branch_top/${name#refs/heads/}" ;;
+               refs/tags/*)
+                       destname="refs/$tag_top/${name#refs/tags/}" ;;
+               *)
+                       continue ;;
+               esac
+               git update-ref -m "clone: from $repo" "$destname" "$sha1" ""
+       done < "$GIT_DIR/CLONE_HEAD"
 fi
 
 cd "$D" || exit
@@ -342,7 +370,7 @@ then
        # The name under $remote_top the remote HEAD seems to point at.
        head_points_at=$(
                (
-                       echo "master"
+                       test -f "$GIT_DIR/$remote_top/master" && echo "master"
                        cd "$GIT_DIR/$remote_top" &&
                        find . -type f -print | sed -e 's/^\.\///'
                ) | (
@@ -360,37 +388,42 @@ then
                )
        )
 
+       # Upstream URL
+       git config remote."$origin".url "$repo" &&
+
+       # Set up the mappings to track the remote branches.
+       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.
-               origin_track="$remote_top/$head_points_at" &&
-               git-update-ref HEAD "$head_sha1" &&
+               git update-ref HEAD "$head_sha1" &&
 
-               # Upstream URL
-               git-repo-config remote."$origin".url "$repo" &&
-
-               # Set up the mappings to track the remote branches.
-               git-repo-config remote."$origin".fetch \
-                       "refs/heads/*:$remote_top/*" '^$' &&
                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-repo-config branch."$head_points_at".remote "$origin" &&
-               git-repo-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"
+               ;;
        esac
 
        case "$no_checkout" in
        '')
-               test "z$quiet" = z && v=-v || v=
-               git-read-tree -m -u $v HEAD HEAD
+               test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
+               git read-tree -m -u $v HEAD HEAD
        esac
 fi
 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
 
 trap - 0
-