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