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