8e7150127aff222059f3549a8b7f647b8f792c7d
[git.git] / git-clone.sh
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.
9 unset CDPATH
10
11 usage() {
12         echo >&2 "* git clone [-l [-s]] [-q] [-u <upload-pack>] [-n] <repo> <dir>"
13         exit 1
14 }
15
16 get_repo_base() {
17         (cd "$1" && (cd .git ; pwd)) 2> /dev/null
18 }
19
20 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
21     curl_extra_args="-k"
22 fi
23
24 http_fetch () {
25         # $1 = Remote, $2 = Local
26         curl -nsfL $curl_extra_args "$1" >"$2"
27 }
28
29 clone_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.
37 Perhaps 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
65 quiet=
66 use_local=no
67 local_shared=no
68 no_checkout=
69 upload_pack=
70 while
71         case "$#,$1" in
72         0,*) break ;;
73         *,-n) no_checkout=yes ;;
74         *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
75         *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) 
76           local_shared=yes ;;
77         *,-q|*,--quiet) quiet=-q ;;
78         1,-u|1,--upload-pack) usage ;;
79         *,-u|*,--upload-pack)
80                 shift
81                 upload_pack="--exec=$1" ;;
82         *,-*) usage ;;
83         *) break ;;
84         esac
85 do
86         shift
87 done
88
89 # Turn the source into an absolute path if
90 # it is local
91 repo="$1"
92 local=no
93 if base=$(get_repo_base "$repo"); then
94         repo="$base"
95         local=yes
96 fi
97
98 dir="$2"
99 # Try using "humanish" part of source repo if user didn't specify one
100 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*/||g')
101 mkdir "$dir" &&
102 D=$(
103         (cd "$dir" && git-init-db && pwd)
104 ) &&
105 test -d "$D" || usage
106
107 # We do local magic only when the user tells us to.
108 case "$local,$use_local" in
109 yes,yes)
110         ( cd "$repo/objects" ) || {
111                 echo >&2 "-l flag seen but $repo is not local."
112                 exit 1
113         }
114
115         case "$local_shared" in
116         no)
117             # See if we can hardlink and drop "l" if not.
118             sample_file=$(cd "$repo" && \
119                           find objects -type f -print | sed -e 1q)
120
121             # objects directory should not be empty since we are cloning!
122             test -f "$repo/$sample_file" || exit
123
124             l=
125             if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null
126             then
127                     l=l
128             fi &&
129             rm -f "$D/.git/objects/sample" &&
130             cd "$repo" &&
131             find objects -depth -print | cpio -puamd$l "$D/.git/" || exit 1
132             ;;
133         yes)
134             mkdir -p "$D/.git/objects/info"
135             {
136                 test -f "$repo/objects/info/alternates" &&
137                 cat "$repo/objects/info/alternates";
138                 echo "$repo/objects"
139             } >"$D/.git/objects/info/alternates"
140             ;;
141         esac
142
143         # Make a duplicate of refs and HEAD pointer
144         HEAD=
145         if test -f "$repo/HEAD"
146         then
147                 HEAD=HEAD
148         fi
149         (cd "$repo" && tar cf - refs $HEAD) |
150         (cd "$D/.git" && tar xf -) || exit 1
151         ;;
152 *)
153         case "$repo" in
154         rsync://*)
155                 rsync $quiet -av --ignore-existing  \
156                         --exclude info "$repo/objects/" "$D/.git/objects/" &&
157                 rsync $quiet -av --ignore-existing  \
158                         --exclude info "$repo/refs/" "$D/.git/refs/" || exit
159
160                 # Look at objects/info/alternates for rsync -- http will
161                 # support it natively and git native ones will do it on the
162                 # remote end.  Not having that file is not a crime.
163                 rsync -q "$repo/objects/info/alternates" \
164                         "$D/.git/TMP_ALT" 2>/dev/null ||
165                         rm -f "$D/.git/TMP_ALT"
166                 if test -f "$D/.git/TMP_ALT"
167                 then
168                     ( cd "$D" &&
169                       . git-parse-remote &&
170                       resolve_alternates "$repo" <"./.git/TMP_ALT" ) |
171                     while read alt
172                     do
173                         case "$alt" in 'bad alternate: '*) die "$alt";; esac
174                         case "$quiet" in
175                         '')     echo >&2 "Getting alternate: $alt" ;;
176                         esac
177                         rsync $quiet -av --ignore-existing  \
178                             --exclude info "$alt" "$D/.git/objects" || exit
179                     done
180                     rm -f "$D/.git/TMP_ALT"
181                 fi
182                 ;;
183         http://*)
184                 clone_dumb_http "$repo" "$D"
185                 ;;
186         *)
187                 cd "$D" && case "$upload_pack" in
188                 '') git-clone-pack $quiet "$repo" ;;
189                 *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
190                 esac
191                 ;;
192         esac
193         ;;
194 esac
195
196 cd "$D" || exit
197
198 if test -f ".git/HEAD"
199 then
200         head_points_at=`git-symbolic-ref HEAD`
201         case "$head_points_at" in
202         refs/heads/*)
203                 head_points_at=`expr "$head_points_at" : 'refs/heads/\(.*\)'`
204                 mkdir -p .git/remotes &&
205                 echo >.git/remotes/origin \
206                 "URL: $repo
207 Pull: $head_points_at:origin" &&
208                 cp ".git/refs/heads/$head_points_at" .git/refs/heads/origin &&
209                 find .git/refs/heads -type f -print |
210                 while read ref
211                 do
212                         head=`expr "$ref" : '.git/refs/heads/\(.*\)'` &&
213                         test "$head_points_at" = "$head" ||
214                         test "origin" = "$head" ||
215                         echo "Pull: ${head}:${head}"
216                 done >>.git/remotes/origin
217         esac
218
219         case "$no_checkout" in
220         '')
221                 git checkout
222         esac
223 fi