blameview: Support browsable functionality to blameview.
[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 die() {
12         echo >&2 "$@"
13         exit 1
14 }
15
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 }
19
20 get_repo_base() {
21         (cd "$1" && (cd .git ; pwd)) 2> /dev/null
22 }
23
24 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
25     curl_extra_args="-k"
26 fi
27
28 http_fetch () {
29         # $1 = Remote, $2 = Local
30         curl -nsfL $curl_extra_args "$1" >"$2"
31 }
32
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-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 }
68
69 quiet=
70 local=no
71 use_local=no
72 local_shared=no
73 unset template
74 no_checkout=
75 upload_pack=
76 bare=
77 reference=
78 origin=
79 origin_override=
80 use_separate_remote=t
81 depth=
82 while
83         case "$#,$1" in
84         0,*) break ;;
85         *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
86         *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
87           no_checkout=yes ;;
88         *,--na|*,--nak|*,--nake|*,--naked|\
89         *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
90         *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
91         *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared) 
92           local_shared=yes; use_local=yes ;;
93         1,--template) usage ;;
94         *,--template)
95                 shift; template="--template=$1" ;;
96         *,--template=*)
97           template="$1" ;;
98         *,-q|*,--quiet) quiet=-q ;;
99         *,--use-separate-remote) ;;
100         *,--no-separate-remote)
101                 die "clones are always made with separate-remote layout" ;;
102         1,--reference) usage ;;
103         *,--reference)
104                 shift; reference="$1" ;;
105         *,--reference=*)
106                 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
107         *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
108                 case "$2" in
109                 '')
110                     usage ;;
111                 */*)
112                     die "'$2' is not suitable for an origin name"
113                 esac
114                 git-check-ref-format "heads/$2" ||
115                     die "'$2' is not suitable for a branch name"
116                 test -z "$origin_override" ||
117                     die "Do not give more than one --origin options."
118                 origin_override=yes
119                 origin="$2"; shift
120                 ;;
121         1,-u|1,--upload-pack) usage ;;
122         *,-u|*,--upload-pack)
123                 shift
124                 upload_pack="--upload-pack=$1" ;;
125         *,--upload-pack=*)
126                 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
127         1,--depth) usage;;
128         *,--depth)
129                 shift
130                 depth="--depth=$1";;
131         *,-*) usage ;;
132         *) break ;;
133         esac
134 do
135         shift
136 done
137
138 repo="$1"
139 test -n "$repo" ||
140     die 'you must specify a repository to clone.'
141
142 # --bare implies --no-checkout and --no-separate-remote
143 if test yes = "$bare"
144 then
145         if test yes = "$origin_override"
146         then
147                 die '--bare and --origin $origin options are incompatible.'
148         fi
149         no_checkout=yes
150         use_separate_remote=
151 fi
152
153 if test -z "$origin"
154 then
155         origin=origin
156 fi
157
158 # Turn the source into an absolute path if
159 # it is local
160 if base=$(get_repo_base "$repo"); then
161         repo="$base"
162         local=yes
163 fi
164
165 dir="$2"
166 # Try using "humanish" part of source repo if user didn't specify one
167 [ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
168 [ -e "$dir" ] && die "destination directory '$dir' already exists."
169 mkdir -p "$dir" &&
170 D=$(cd "$dir" && pwd) &&
171 trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
172 case "$bare" in
173 yes)
174         GIT_DIR="$D" ;;
175 *)
176         GIT_DIR="$D/.git" ;;
177 esac && export GIT_DIR && git-init ${template+"$template"} || usage
178
179 if test -n "$reference"
180 then
181         if test -d "$reference"
182         then
183                 if test -d "$reference/.git/objects"
184                 then
185                         reference="$reference/.git"
186                 fi
187                 reference=$(cd "$reference" && pwd)
188                 echo "$reference/objects" >"$GIT_DIR/objects/info/alternates"
189                 (cd "$reference" && tar cf - refs) |
190                 (cd "$GIT_DIR/refs" &&
191                  mkdir reference-tmp &&
192                  cd reference-tmp &&
193                  tar xf - &&
194                  find refs ! -type d -print |
195                  while read ref
196                  do
197                         if test -h "$ref"
198                         then
199                                 # Old-style symbolic link ref.  Not likely
200                                 # to appear under refs/ but we might as well
201                                 # deal with them.
202                                 :
203                         elif test -f "$ref"
204                         then
205                                 point=$(cat "$ref") &&
206                                         case "$point" in
207                                         'ref: '*) ;;
208                                         *) continue ;;
209                                         esac
210                         fi
211                         # The above makes true ref to 'continue' and
212                         # we will come here when we are looking at
213                         # symbolic link ref or a textual symref (or
214                         # garbage, like fifo).
215                         # The true ref pointed at by it is enough to
216                         # ensure that we do not fetch objects reachable
217                         # from it.
218                         rm -f "$ref"
219                  done
220                 )
221         else
222                 die "reference repository '$reference' is not a local directory."
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                 die "-l flag seen but repository '$repo' is not local."
233
234         case "$local_shared" in
235         no)
236             # See if we can hardlink and drop "l" if not.
237             sample_file=$(cd "$repo" && \
238                           find objects -type f -print | sed -e 1q)
239
240             # objects directory should not be empty since we are cloning!
241             test -f "$repo/$sample_file" || exit
242
243             l=
244             if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
245             then
246                     l=l
247             fi &&
248             rm -f "$GIT_DIR/objects/sample" &&
249             cd "$repo" &&
250             find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
251             ;;
252         yes)
253             mkdir -p "$GIT_DIR/objects/info"
254             echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
255             ;;
256         esac
257         git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
258         ;;
259 *)
260         case "$repo" in
261         rsync://*)
262                 case "$depth" in
263                 "") ;;
264                 *) die "shallow over rsync not supported" ;;
265                 esac
266                 rsync $quiet -av --ignore-existing  \
267                         --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
268                 exit
269                 # Look at objects/info/alternates for rsync -- http will
270                 # support it natively and git native ones will do it on the
271                 # remote end.  Not having that file is not a crime.
272                 rsync -q "$repo/objects/info/alternates" \
273                         "$GIT_DIR/TMP_ALT" 2>/dev/null ||
274                         rm -f "$GIT_DIR/TMP_ALT"
275                 if test -f "$GIT_DIR/TMP_ALT"
276                 then
277                     ( cd "$D" &&
278                       . git-parse-remote &&
279                       resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
280                     while read alt
281                     do
282                         case "$alt" in 'bad alternate: '*) die "$alt";; esac
283                         case "$quiet" in
284                         '')     echo >&2 "Getting alternate: $alt" ;;
285                         esac
286                         rsync $quiet -av --ignore-existing  \
287                             --exclude info "$alt" "$GIT_DIR/objects" || exit
288                     done
289                     rm -f "$GIT_DIR/TMP_ALT"
290                 fi
291                 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
292                 ;;
293         https://*|http://*|ftp://*)
294                 case "$depth" in
295                 "") ;;
296                 *) die "shallow over http or ftp not supported" ;;
297                 esac
298                 if test -z "@@NO_CURL@@"
299                 then
300                         clone_dumb_http "$repo" "$D"
301                 else
302                         die "http transport not supported, rebuild Git with curl support"
303                 fi
304                 ;;
305         *)
306                 case "$upload_pack" in
307                 '') git-fetch-pack --all -k $quiet $depth "$repo" ;;
308                 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth "$repo" ;;
309                 esac >"$GIT_DIR/CLONE_HEAD" ||
310                         die "fetch-pack from '$repo' failed."
311                 ;;
312         esac
313         ;;
314 esac
315 test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
316
317 if test -f "$GIT_DIR/CLONE_HEAD"
318 then
319         # Read git-fetch-pack -k output and store the remote branches.
320         if [ -n "$use_separate_remote" ]
321         then
322                 branch_top="remotes/$origin"
323         else
324                 branch_top="heads"
325         fi
326         tag_top="tags"
327         while read sha1 name
328         do
329                 case "$name" in
330                 *'^{}')
331                         continue ;;
332                 HEAD)
333                         destname="REMOTE_HEAD" ;;
334                 refs/heads/*)
335                         destname="refs/$branch_top/${name#refs/heads/}" ;;
336                 refs/tags/*)
337                         destname="refs/$tag_top/${name#refs/tags/}" ;;
338                 *)
339                         continue ;;
340                 esac
341                 git-update-ref -m "clone: from $repo" "$destname" "$sha1" ""
342         done < "$GIT_DIR/CLONE_HEAD"
343 fi
344
345 cd "$D" || exit
346
347 if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
348 then
349         # a non-bare repository is always in separate-remote layout
350         remote_top="refs/remotes/$origin"
351         head_sha1=`cat "$GIT_DIR/REMOTE_HEAD"`
352         case "$head_sha1" in
353         'ref: refs/'*)
354                 # Uh-oh, the remote told us (http transport done against
355                 # new style repository with a symref HEAD).
356                 # Ideally we should skip the guesswork but for now
357                 # opt for minimum change.
358                 head_sha1=`expr "z$head_sha1" : 'zref: refs/heads/\(.*\)'`
359                 head_sha1=`cat "$GIT_DIR/$remote_top/$head_sha1"`
360                 ;;
361         esac
362
363         # The name under $remote_top the remote HEAD seems to point at.
364         head_points_at=$(
365                 (
366                         test -f "$GIT_DIR/$remote_top/master" && echo "master"
367                         cd "$GIT_DIR/$remote_top" &&
368                         find . -type f -print | sed -e 's/^\.\///'
369                 ) | (
370                 done=f
371                 while read name
372                 do
373                         test t = $done && continue
374                         branch_tip=`cat "$GIT_DIR/$remote_top/$name"`
375                         if test "$head_sha1" = "$branch_tip"
376                         then
377                                 echo "$name"
378                                 done=t
379                         fi
380                 done
381                 )
382         )
383
384         # Write out remote.$origin config, and update our "$head_points_at".
385         case "$head_points_at" in
386         ?*)
387                 # Local default branch
388                 git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
389
390                 # Tracking branch for the primary branch at the remote.
391                 origin_track="$remote_top/$head_points_at" &&
392                 git-update-ref HEAD "$head_sha1" &&
393
394                 # Upstream URL
395                 git-config remote."$origin".url "$repo" &&
396
397                 # Set up the mappings to track the remote branches.
398                 git-config remote."$origin".fetch \
399                         "+refs/heads/*:$remote_top/*" '^$' &&
400                 rm -f "refs/remotes/$origin/HEAD"
401                 git-symbolic-ref "refs/remotes/$origin/HEAD" \
402                         "refs/remotes/$origin/$head_points_at" &&
403
404                 git-config branch."$head_points_at".remote "$origin" &&
405                 git-config branch."$head_points_at".merge "refs/heads/$head_points_at"
406         esac
407
408         case "$no_checkout" in
409         '')
410                 test "z$quiet" = z && v=-v || v=
411                 git-read-tree -m -u $v HEAD HEAD
412         esac
413 fi
414 rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
415
416 trap - 0
417