Code

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