Code

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