git-clone.shon commit ls-tree: chomp leading directories when run from a subdirectory (a69dd58)
   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 [-l [-s]] [-q] [-u <upload-pack>] [-o <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        http_fetch "$1/objects/info/packs" "$clone_tmp/packs" || {
  36                echo >&2 "Cannot get remote repository information.
  37Perhaps git-update-server-info needs to be run there?"
  38                exit 1;
  39        }
  40        while read type name
  41        do
  42                case "$type" in
  43                P) ;;
  44                *) continue ;;
  45                esac &&
  46
  47                idx=`expr "$name" : '\(.*\)\.pack'`.idx
  48                http_fetch "$1/objects/pack/$name" ".git/objects/pack/$name" &&
  49                http_fetch "$1/objects/pack/$idx" ".git/objects/pack/$idx" &&
  50                git-verify-pack ".git/objects/pack/$idx" || exit 1
  51        done <"$clone_tmp/packs"
  52
  53        while read sha1 refname
  54        do
  55                name=`expr "$refname" : 'refs/\(.*\)'` &&
  56                case "$name" in
  57                *^*)    ;;
  58                *)
  59                        git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
  60                esac
  61        done <"$clone_tmp/refs"
  62        rm -fr "$clone_tmp"
  63}
  64
  65quiet=
  66use_local=no
  67local_shared=no
  68no_checkout=
  69upload_pack=
  70origin=origin
  71while
  72        case "$#,$1" in
  73        0,*) break ;;
  74        *,-n) no_checkout=yes ;;
  75        *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
  76        *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) 
  77          local_shared=yes; use_local=yes ;;
  78        *,-q|*,--quiet) quiet=-q ;;
  79        1,-o) usage;;
  80        *,-o)
  81                git-check-ref-format "$2" || {
  82                    echo >&2 "'$2' is not suitable for a branch name"
  83                    exit 1
  84                }
  85                origin="$2"; shift
  86                ;;
  87        1,-u|1,--upload-pack) usage ;;
  88        *,-u|*,--upload-pack)
  89                shift
  90                upload_pack="--exec=$1" ;;
  91        *,-*) usage ;;
  92        *) break ;;
  93        esac
  94do
  95        shift
  96done
  97
  98# Turn the source into an absolute path if
  99# it is local
 100repo="$1"
 101local=no
 102if base=$(get_repo_base "$repo"); then
 103        repo="$base"
 104        local=yes
 105fi
 106
 107dir="$2"
 108# Try using "humanish" part of source repo if user didn't specify one
 109[ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*/||g')
 110[ -e "$dir" ] && echo "$dir already exists." && usage
 111mkdir -p "$dir" &&
 112D=$(
 113        (cd "$dir" && git-init-db && pwd)
 114) &&
 115test -d "$D" || usage
 116
 117# We do local magic only when the user tells us to.
 118case "$local,$use_local" in
 119yes,yes)
 120        ( cd "$repo/objects" ) || {
 121                echo >&2 "-l flag seen but $repo is not local."
 122                exit 1
 123        }
 124
 125        case "$local_shared" in
 126        no)
 127            # See if we can hardlink and drop "l" if not.
 128            sample_file=$(cd "$repo" && \
 129                          find objects -type f -print | sed -e 1q)
 130
 131            # objects directory should not be empty since we are cloning!
 132            test -f "$repo/$sample_file" || exit
 133
 134            l=
 135            if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null
 136            then
 137                    l=l
 138            fi &&
 139            rm -f "$D/.git/objects/sample" &&
 140            cd "$repo" &&
 141            find objects -depth -print | cpio -puamd$l "$D/.git/" || exit 1
 142            ;;
 143        yes)
 144            mkdir -p "$D/.git/objects/info"
 145            {
 146                test -f "$repo/objects/info/alternates" &&
 147                cat "$repo/objects/info/alternates";
 148                echo "$repo/objects"
 149            } >"$D/.git/objects/info/alternates"
 150            ;;
 151        esac
 152
 153        # Make a duplicate of refs and HEAD pointer
 154        HEAD=
 155        if test -f "$repo/HEAD"
 156        then
 157                HEAD=HEAD
 158        fi
 159        (cd "$repo" && tar cf - refs $HEAD) |
 160        (cd "$D/.git" && tar xf -) || exit 1
 161        ;;
 162*)
 163        case "$repo" in
 164        rsync://*)
 165                rsync $quiet -av --ignore-existing  \
 166                        --exclude info "$repo/objects/" "$D/.git/objects/" &&
 167                rsync $quiet -av --ignore-existing  \
 168                        --exclude info "$repo/refs/" "$D/.git/refs/" || exit
 169
 170                # Look at objects/info/alternates for rsync -- http will
 171                # support it natively and git native ones will do it on the
 172                # remote end.  Not having that file is not a crime.
 173                rsync -q "$repo/objects/info/alternates" \
 174                        "$D/.git/TMP_ALT" 2>/dev/null ||
 175                        rm -f "$D/.git/TMP_ALT"
 176                if test -f "$D/.git/TMP_ALT"
 177                then
 178                    ( cd "$D" &&
 179                      . git-parse-remote &&
 180                      resolve_alternates "$repo" <"./.git/TMP_ALT" ) |
 181                    while read alt
 182                    do
 183                        case "$alt" in 'bad alternate: '*) die "$alt";; esac
 184                        case "$quiet" in
 185                        '')     echo >&2 "Getting alternate: $alt" ;;
 186                        esac
 187                        rsync $quiet -av --ignore-existing  \
 188                            --exclude info "$alt" "$D/.git/objects" || exit
 189                    done
 190                    rm -f "$D/.git/TMP_ALT"
 191                fi
 192                ;;
 193        http://*)
 194                clone_dumb_http "$repo" "$D"
 195                ;;
 196        *)
 197                cd "$D" && case "$upload_pack" in
 198                '') git-clone-pack $quiet "$repo" ;;
 199                *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
 200                esac || {
 201                        echo >&2 "clone-pack from '$repo' failed."
 202                        exit 1
 203                }
 204                ;;
 205        esac
 206        ;;
 207esac
 208
 209cd "$D" || exit
 210
 211if test -f ".git/HEAD"
 212then
 213        head_points_at=`git-symbolic-ref HEAD`
 214        case "$head_points_at" in
 215        refs/heads/*)
 216                head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
 217                mkdir -p .git/remotes &&
 218                echo >.git/remotes/origin \
 219                "URL: $repo
 220Pull: $head_points_at:$origin" &&
 221                git-update-ref "refs/heads/$origin" $(git-rev-parse HEAD) &&
 222                find .git/refs/heads -type f -print |
 223                while read ref
 224                do
 225                        head=`expr "$ref" : '.git/refs/heads/\(.*\)'` &&
 226                        test "$head_points_at" = "$head" ||
 227                        test "$origin" = "$head" ||
 228                        echo "Pull: ${head}:${head}"
 229                done >>.git/remotes/origin
 230        esac
 231
 232        case "$no_checkout" in
 233        '')
 234                git checkout
 235        esac
 236fi