Merge branch 'jc/clone' into next
[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 "Usage: $0 [--use-separate-remote] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [-o <name>] [-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                 echo >&2 "Cannot get remote repository information.
36 Perhaps git-update-server-info needs to be run there?"
37                 exit 1;
38         }
39         while read sha1 refname
40         do
41                 name=`expr "$refname" : 'refs/\(.*\)'` &&
42                 case "$name" in
43                 *^*)    continue;;
44                 esac
45                 if test -n "$use_separate_remote" &&
46                    branch_name=`expr "$name" : 'heads/\(.*\)'`
47                 then
48                         tname="remotes/$origin/$branch_name"
49                 else
50                         tname=$name
51                 fi
52                 git-http-fetch -v -a -w "$tname" "$name" "$1/" || exit 1
53         done <"$clone_tmp/refs"
54         rm -fr "$clone_tmp"
55         http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD"
56 }
57
58 # Read git-fetch-pack -k output and store the remote branches.
59 copy_refs='
60 use File::Path qw(mkpath);
61 use File::Basename qw(dirname);
62 my $git_dir = $ARGV[0];
63 my $use_separate_remote = $ARGV[1];
64 my $origin = $ARGV[2];
65
66 my $branch_top = ($use_separate_remote ? "remotes/$origin" : "heads");
67 my $tag_top = "tags";
68
69 sub store {
70         my ($sha1, $name, $top) = @_;
71         $name = "$git_dir/refs/$top/$name";
72         mkpath(dirname($name));
73         open O, ">", "$name";
74         print O "$sha1\n";
75         close O;
76 }
77
78 open FH, "<", "$git_dir/CLONE_HEAD";
79 while (<FH>) {
80         my ($sha1, $name) = /^([0-9a-f]{40})\s(.*)$/;
81         next if ($name =~ /\^\173/);
82         if ($name eq "HEAD") {
83                 open O, ">", "$git_dir/REMOTE_HEAD";
84                 print O "$sha1\n";
85                 close O;
86                 next;
87         }
88         if ($name =~ s/^refs\/heads\///) {
89                 store($sha1, $name, $branch_top);
90                 next;
91         }
92         if ($name =~ s/^refs\/tags\///) {
93                 store($sha1, $name, $tag_top);
94                 next;
95         }
96 }
97 close FH;
98 '
99
100 quiet=
101 use_local=no
102 local_shared=no
103 no_checkout=
104 upload_pack=
105 bare=
106 reference=
107 origin=
108 origin_override=
109 use_separate_remote=
110 while
111         case "$#,$1" in
112         0,*) break ;;
113         *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
114         *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
115           no_checkout=yes ;;
116         *,--na|*,--nak|*,--nake|*,--naked|\
117         *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
118         *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
119         *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) 
120           local_shared=yes; use_local=yes ;;
121         *,-q|*,--quiet) quiet=-q ;;
122         *,--use-separate-remote)
123                 use_separate_remote=t ;;
124         1,-o) usage;;
125         1,--reference) usage ;;
126         *,--reference)
127                 shift; reference="$1" ;;
128         *,--reference=*)
129                 reference=`expr "$1" : '--reference=\(.*\)'` ;;
130         *,-o)
131                 case "$2" in
132                 */*)
133                     echo >&2 "'$2' is not suitable for an origin name"
134                     exit 1
135                 esac
136                 git-check-ref-format "heads/$2" || {
137                     echo >&2 "'$2' is not suitable for a branch name"
138                     exit 1
139                 }
140                 test -z "$origin_override" || {
141                     echo >&2 "Do not give more than one -o options."
142                     exit 1
143                 }
144                 origin_override=yes
145                 origin="$2"; shift
146                 ;;
147         1,-u|1,--upload-pack) usage ;;
148         *,-u|*,--upload-pack)
149                 shift
150                 upload_pack="--exec=$1" ;;
151         *,-*) usage ;;
152         *) break ;;
153         esac
154 do
155         shift
156 done
157
158 # --bare implies --no-checkout
159 if test yes = "$bare"
160 then
161         if test yes = "$origin_override"
162         then
163                 echo >&2 '--bare and -o $origin options are incompatible.'
164                 exit 1
165         fi
166         if test t = "$use_separate_remote"
167         then
168                 echo >&2 '--bare and --use-separate-remote options are incompatible.'
169                 exit 1
170         fi
171         no_checkout=yes
172 fi
173
174 if test -z "$origin"
175 then
176         origin=origin
177 fi
178
179 # Turn the source into an absolute path if
180 # it is local
181 repo="$1"
182 local=no
183 if base=$(get_repo_base "$repo"); then
184         repo="$base"
185         local=yes
186 fi
187
188 dir="$2"
189 # Try using "humanish" part of source repo if user didn't specify one
190 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
191 [ -e "$dir" ] && echo "$dir already exists." && usage
192 mkdir -p "$dir" &&
193 D=$(cd "$dir" && pwd) &&
194 trap 'err=$?; cd ..; rm -r "$D"; exit $err' exit
195 case "$bare" in
196 yes) GIT_DIR="$D" ;;
197 *) GIT_DIR="$D/.git" ;;
198 esac && export GIT_DIR && git-init-db || usage
199 case "$bare" in
200 yes)
201         GIT_DIR="$D" ;;
202 *)
203         GIT_DIR="$D/.git" ;;
204 esac
205
206 if test -n "$reference"
207 then
208         if test -d "$reference"
209         then
210                 if test -d "$reference/.git/objects"
211                 then
212                         reference="$reference/.git"
213                 fi
214                 reference=$(cd "$reference" && pwd)
215                 echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
216                 (cd "$reference" && tar cf - refs) |
217                 (cd "$GIT_DIR/refs" &&
218                  mkdir reference-tmp &&
219                  cd reference-tmp &&
220                  tar xf -)
221         else
222                 echo >&2 "$reference: not a local directory." && usage
223         fi
224 fi
225
226 rm -f "$GIT_DIR/CLONE_HEAD"
227
228 # We do local magic only when the user tells us to.
229 case "$local,$use_local" in
230 yes,yes)
231         ( cd "$repo/objects" ) || {
232                 echo >&2 "-l flag seen but $repo is not local."
233                 exit 1
234         }
235
236         case "$local_shared" in
237         no)
238             # See if we can hardlink and drop "l" if not.
239             sample_file=$(cd "$repo" && \
240                           find objects -type f -print | sed -e 1q)
241
242             # objects directory should not be empty since we are cloning!
243             test -f "$repo/$sample_file" || exit
244
245             l=
246             if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
247             then
248                     l=l
249             fi &&
250             rm -f "$GIT_DIR/objects/sample" &&
251             cd "$repo" &&
252             find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
253             ;;
254         yes)
255             mkdir -p "$GIT_DIR/objects/info"
256             {
257                 test -f "$repo/objects/info/alternates" &&
258                 cat "$repo/objects/info/alternates";
259                 echo "$repo/objects"
260             } >"$GIT_DIR/objects/info/alternates"
261             ;;
262         esac
263         git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD"
264         ;;
265 *)
266         case "$repo" in
267         rsync://*)
268                 rsync $quiet -av --ignore-existing  \
269                         --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
270                 exit
271                 # Look at objects/info/alternates for rsync -- http will
272                 # support it natively and git native ones will do it on the
273                 # remote end.  Not having that file is not a crime.
274                 rsync -q "$repo/objects/info/alternates" \
275                         "$GIT_DIR/TMP_ALT" 2>/dev/null ||
276                         rm -f "$GIT_DIR/TMP_ALT"
277                 if test -f "$GIT_DIR/TMP_ALT"
278                 then
279                     ( cd "$D" &&
280                       . git-parse-remote &&
281                       resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
282                     while read alt
283                     do
284                         case "$alt" in 'bad alternate: '*) die "$alt";; esac
285                         case "$quiet" in
286                         '')     echo >&2 "Getting alternate: $alt" ;;
287                         esac
288                         rsync $quiet -av --ignore-existing  \
289                             --exclude info "$alt" "$GIT_DIR/objects" || exit
290                     done
291                     rm -f "$GIT_DIR/TMP_ALT"
292                 fi
293                 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD"
294                 ;;
295         http://*)
296                 if test -z "@@NO_CURL@@"
297                 then
298                         clone_dumb_http "$repo" "$D"
299                 else
300                         echo >&2 "http transport not supported, rebuild Git with curl support"
301                         exit 1
302                 fi
303                 ;;
304         *)
305                 cd "$D" && case "$upload_pack" in
306                 '') git-fetch-pack --all -k $quiet "$repo" ;;
307                 *) git-fetch-pack --all -k $quiet "$upload_pack" "$repo" ;;
308                 esac >"$GIT_DIR/CLONE_HEAD" || {
309                         echo >&2 "fetch-pack from '$repo' failed."
310                         exit 1
311                 }
312                 ;;
313         esac
314         ;;
315 esac
316 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
317
318 if test -f "$GIT_DIR/CLONE_HEAD"
319 then
320         # Figure out where the remote HEAD points at.
321         perl -e "$copy_refs" "$GIT_DIR" "$use_separate_remote" "$origin"
322 fi
323
324 cd "$D" || exit
325
326 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
327 then
328         head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
329         # Figure out which remote branch HEAD points at.
330         case "$use_separate_remote" in
331         '')     remote_top=refs/heads ;;
332         *)      remote_top="refs/remotes/$origin" ;;
333         esac
334
335         # What to use to track the remote primary branch
336         if test -n "$use_separate_remote"
337         then
338                 origin_tracking="remotes/$origin/master"
339         else
340                 origin_tracking="heads/$origin"
341         fi
342
343         # The name under $remote_top the remote HEAD seems to point at
344         head_points_at=$(
345                 (
346                         echo "master"
347                         cd "$GIT_DIR/$remote_top" &&
348                         find . -type f -print | sed -e 's/^\.\///'
349                 ) | (
350                 done=f
351                 while read name
352                 do
353                         test t = $done && continue
354                         branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
355                         if test "$head_sha1" = "$branch_tip"
356                         then
357                                 echo "$name"
358                                 done=t
359                         fi
360                 done
361                 )
362         )
363
364         # Write out remotes/$origin file.
365         case "$head_points_at" in
366         ?*)
367                 mkdir -p "$GIT_DIR/remotes" &&
368                 echo >"$GIT_DIR/remotes/$origin" \
369                 "URL: $repo
370 Pull: refs/heads/$head_points_at:refs/$origin_tracking" &&
371                 case "$use_separate_remote" in
372                 t) git-update-ref HEAD "$head_sha1" ;;
373                 *) git-update-ref "refs/heads/$origin" $(git-rev-parse HEAD) ;;
374                 esac &&
375                 (cd "$GIT_DIR/$remote_top" && find . -type f -print) |
376                 while read dotslref
377                 do
378                         name=`expr "$dotslref" : './\(.*\)'` &&
379                         test "$head_points_at" = "$name" ||
380                         test "$origin" = "$name" ||
381                         echo "Pull: refs/heads/${name}:$remote_top/${name}"
382                 done >>"$GIT_DIR/remotes/$origin" &&
383                 case "$use_separate_remote" in
384                 t)
385                         rm -f "refs/remotes/$origin/HEAD"
386                         git-symbolic-ref "refs/remotes/$origin/HEAD" \
387                                 "refs/remotes/$origin/$head_points_at"
388                 esac
389         esac
390
391         case "$no_checkout" in
392         '')
393                 git-read-tree -m -u -v HEAD HEAD
394         esac
395 fi
396 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
397
398 trap - exit
399