]> git.ipfire.org Git - thirdparty/git.git/blame - git-clone.sh
Fix documentation of tag in git-fast-import.txt
[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() {
37818d7d 17 die "Usage: $0 [--template=<template_directory>] [--reference <reference-repo>] [--bare] [-l [-s]] [-q] [-u <upload-pack>] [--origin <name>] [--depth <n>] [-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 38 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
e0d10e1c 39 "`git-config --bool http.noEPSV`" = true ]; then
3ea099d4
SK
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?"
7e8c8255 45 test "z$quiet" = z && v=-v || v=
0516de30
JH
46 while read sha1 refname
47 do
f327dbce 48 name=`expr "z$refname" : 'zrefs/\(.*\)'` &&
cdb39508 49 case "$name" in
dfeff66e 50 *^*) continue;;
cdb39508 51 esac
983d2ee2
JH
52 case "$bare,$name" in
53 yes,* | ,heads/* | ,tags/*) ;;
54 *) continue ;;
55 esac
dfeff66e 56 if test -n "$use_separate_remote" &&
f327dbce 57 branch_name=`expr "z$name" : 'zheads/\(.*\)'`
dfeff66e 58 then
5ceb05f8 59 tname="remotes/$origin/$branch_name"
dfeff66e
JH
60 else
61 tname=$name
62 fi
928c210a 63 git-http-fetch $v -a -w "$tname" "$sha1" "$1" || exit 1
0516de30
JH
64 done <"$clone_tmp/refs"
65 rm -fr "$clone_tmp"
c72112e6
JH
66 http_fetch "$1/HEAD" "$GIT_DIR/REMOTE_HEAD" ||
67 rm -f "$GIT_DIR/REMOTE_HEAD"
dfeff66e
JH
68}
69
167a4a33 70quiet=
ef5b4eab 71local=no
e95ab1ed 72use_local=no
aae4f42c 73local_shared=no
a57c8bac 74unset template
036a72d8 75no_checkout=
6ec311da 76upload_pack=
87e80c4b 77bare=
dfeff66e
JH
78reference=
79origin=
e6489a1b 80origin_override=
71821351 81use_separate_remote=t
016e6ccb 82depth=
83a5ad61
JS
83no_progress=
84test -t 1 || no_progress=--no-progress
e95ab1ed
JH
85while
86 case "$#,$1" in
87 0,*) break ;;
8a1a120c
JH
88 *,-n|*,--no|*,--no-|*,--no-c|*,--no-ch|*,--no-che|*,--no-chec|\
89 *,--no-check|*,--no-checko|*,--no-checkou|*,--no-checkout)
90 no_checkout=yes ;;
87e80c4b
JH
91 *,--na|*,--nak|*,--nake|*,--naked|\
92 *,-b|*,--b|*,--ba|*,--bar|*,--bare) bare=yes ;;
1cadb5a2 93 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
aae4f42c 94 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
2c4ed386 95 local_shared=yes; use_local=yes ;;
a57c8bac
JH
96 1,--template) usage ;;
97 *,--template)
98 shift; template="--template=$1" ;;
99 *,--template=*)
100 template="$1" ;;
167a4a33 101 *,-q|*,--quiet) quiet=-q ;;
63085fab 102 *,--use-separate-remote) ;;
b360cca0 103 *,--no-separate-remote)
63085fab 104 die "clones are always made with separate-remote layout" ;;
dfeff66e
JH
105 1,--reference) usage ;;
106 *,--reference)
107 shift; reference="$1" ;;
108 *,--reference=*)
8096fae7 109 reference=`expr "z$1" : 'z--reference=\(.*\)'` ;;
98a4fef3 110 *,-o|*,--or|*,--ori|*,--orig|*,--origi|*,--origin)
47874d6d 111 case "$2" in
98a4fef3
YS
112 '')
113 usage ;;
47874d6d 114 */*)
87b787ac 115 die "'$2' is not suitable for an origin name"
47874d6d 116 esac
87b787ac
DL
117 git-check-ref-format "heads/$2" ||
118 die "'$2' is not suitable for a branch name"
119 test -z "$origin_override" ||
120 die "Do not give more than one --origin options."
e6489a1b 121 origin_override=yes
e6c310fd
JS
122 origin="$2"; shift
123 ;;
1cadb5a2 124 1,-u|1,--upload-pack) usage ;;
6ec311da
JH
125 *,-u|*,--upload-pack)
126 shift
ae1dffcb
JH
127 upload_pack="--upload-pack=$1" ;;
128 *,--upload-pack=*)
4a91a1f3 129 upload_pack=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
016e6ccb
JS
130 1,--depth) usage;;
131 *,--depth)
132 shift
133 depth="--depth=$1";;
e95ab1ed
JH
134 *,-*) usage ;;
135 *) break ;;
136 esac
137do
138 shift
139done
140
ef5b4eab 141repo="$1"
87b787ac
DL
142test -n "$repo" ||
143 die 'you must specify a repository to clone.'
ef5b4eab 144
b360cca0 145# --bare implies --no-checkout and --no-separate-remote
e6489a1b
JH
146if test yes = "$bare"
147then
148 if test yes = "$origin_override"
149 then
87b787ac 150 die '--bare and --origin $origin options are incompatible.'
e6489a1b
JH
151 fi
152 no_checkout=yes
71821351 153 use_separate_remote=
e6489a1b 154fi
8a1a120c 155
47874d6d 156if test -z "$origin"
dfeff66e 157then
47874d6d 158 origin=origin
dfeff66e
JH
159fi
160
ba375acf
LT
161# Turn the source into an absolute path if
162# it is local
ba375acf
LT
163if base=$(get_repo_base "$repo"); then
164 repo="$base"
165 local=yes
166fi
167
3f571e0b 168dir="$2"
0879aa28 169# Try using "humanish" part of source repo if user didn't specify one
e7555785 170[ -z "$dir" ] && dir=$(echo "$repo" | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
87b787ac 171[ -e "$dir" ] && die "destination directory '$dir' already exists."
7f10f7c4 172mkdir -p "$dir" &&
8a1a120c 173D=$(cd "$dir" && pwd) &&
6ebdee5a 174trap 'err=$?; cd ..; rm -rf "$D"; exit $err' 0
87e80c4b 175case "$bare" in
8a1a120c
JH
176yes)
177 GIT_DIR="$D" ;;
178*)
179 GIT_DIR="$D/.git" ;;
5c94f87e 180esac && export GIT_DIR && git-init ${template+"$template"} || usage
e95ab1ed 181
dfeff66e
JH
182if test -n "$reference"
183then
099c7837 184 ref_git=
dfeff66e
JH
185 if test -d "$reference"
186 then
187 if test -d "$reference/.git/objects"
188 then
099c7837
JH
189 ref_git="$reference/.git"
190 elif test -d "$reference/objects"
191 then
192 ref_git="$reference"
dfeff66e 193 fi
099c7837
JH
194 fi
195 if test -n "$ref_git"
196 then
197 ref_git=$(cd "$ref_git" && pwd)
198 echo "$ref_git/objects" >"$GIT_DIR/objects/info/alternates"
199 (
200 GIT_DIR="$ref_git" git for-each-ref \
201 --format='%(objectname) %(*objectname)'
202 ) |
203 while read a b
204 do
205 test -z "$a" ||
206 git update-ref "refs/reference-tmp/$a" "$a"
207 test -z "$b" ||
208 git update-ref "refs/reference-tmp/$b" "$b"
209 done
dfeff66e 210 else
87b787ac 211 die "reference repository '$reference' is not a local directory."
dfeff66e
JH
212 fi
213fi
214
215rm -f "$GIT_DIR/CLONE_HEAD"
216
e95ab1ed 217# We do local magic only when the user tells us to.
ba375acf
LT
218case "$local,$use_local" in
219yes,yes)
87b787ac
DL
220 ( cd "$repo/objects" ) ||
221 die "-l flag seen but repository '$repo' is not local."
e95ab1ed 222
aae4f42c
JH
223 case "$local_shared" in
224 no)
225 # See if we can hardlink and drop "l" if not.
226 sample_file=$(cd "$repo" && \
227 find objects -type f -print | sed -e 1q)
e95ab1ed 228
aae4f42c
JH
229 # objects directory should not be empty since we are cloning!
230 test -f "$repo/$sample_file" || exit
e95ab1ed 231
aae4f42c 232 l=
8a1a120c 233 if ln "$repo/$sample_file" "$GIT_DIR/objects/sample" 2>/dev/null
aae4f42c
JH
234 then
235 l=l
236 fi &&
8a1a120c 237 rm -f "$GIT_DIR/objects/sample" &&
aae4f42c 238 cd "$repo" &&
8e1618f9 239 find objects -depth -print | cpio -pumd$l "$GIT_DIR/" || exit 1
aae4f42c
JH
240 ;;
241 yes)
8a1a120c 242 mkdir -p "$GIT_DIR/objects/info"
04384022 243 echo "$repo/objects" >> "$GIT_DIR/objects/info/alternates"
aae4f42c
JH
244 ;;
245 esac
57a39690 246 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
7558ef89
LT
247 ;;
248*)
1cadb5a2
JH
249 case "$repo" in
250 rsync://*)
016e6ccb
JS
251 case "$depth" in
252 "") ;;
253 *) die "shallow over rsync not supported" ;;
254 esac
4447badc 255 rsync $quiet -av --ignore-existing \
dfeff66e
JH
256 --exclude info "$repo/objects/" "$GIT_DIR/objects/" ||
257 exit
4447badc
JH
258 # Look at objects/info/alternates for rsync -- http will
259 # support it natively and git native ones will do it on the
260 # remote end. Not having that file is not a crime.
89d844d0 261 rsync -q "$repo/objects/info/alternates" \
8a1a120c
JH
262 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
263 rm -f "$GIT_DIR/TMP_ALT"
264 if test -f "$GIT_DIR/TMP_ALT"
4447badc 265 then
0e9ab02d 266 ( cd "$D" &&
4447badc 267 . git-parse-remote &&
8a1a120c 268 resolve_alternates "$repo" <"$GIT_DIR/TMP_ALT" ) |
4447badc
JH
269 while read alt
270 do
271 case "$alt" in 'bad alternate: '*) die "$alt";; esac
272 case "$quiet" in
273 '') echo >&2 "Getting alternate: $alt" ;;
274 esac
275 rsync $quiet -av --ignore-existing \
8a1a120c 276 --exclude info "$alt" "$GIT_DIR/objects" || exit
4447badc 277 done
8a1a120c 278 rm -f "$GIT_DIR/TMP_ALT"
4447badc 279 fi
57a39690 280 git-ls-remote "$repo" >"$GIT_DIR/CLONE_HEAD" || exit 1
1cadb5a2 281 ;;
38529e28 282 https://*|http://*|ftp://*)
016e6ccb
JS
283 case "$depth" in
284 "") ;;
285 *) die "shallow over http or ftp not supported" ;;
286 esac
6c5c62f3
FP
287 if test -z "@@NO_CURL@@"
288 then
289 clone_dumb_http "$repo" "$D"
290 else
87b787ac 291 die "http transport not supported, rebuild Git with curl support"
6c5c62f3 292 fi
1cadb5a2
JH
293 ;;
294 *)
ced78b39 295 case "$upload_pack" in
83a5ad61
JS
296 '') git-fetch-pack --all -k $quiet $depth $no_progress "$repo";;
297 *) git-fetch-pack --all -k $quiet "$upload_pack" $depth $no_progress "$repo" ;;
87b787ac
DL
298 esac >"$GIT_DIR/CLONE_HEAD" ||
299 die "fetch-pack from '$repo' failed."
1cadb5a2 300 ;;
6ec311da 301 esac
7558ef89
LT
302 ;;
303esac
dfeff66e
JH
304test -d "$GIT_DIR/refs/reference-tmp" && rm -fr "$GIT_DIR/refs/reference-tmp"
305
306if test -f "$GIT_DIR/CLONE_HEAD"
307then
c72112e6 308 # Read git-fetch-pack -k output and store the remote branches.
def2747d
SS
309 if [ -n "$use_separate_remote" ]
310 then
311 branch_top="remotes/$origin"
312 else
313 branch_top="heads"
314 fi
315 tag_top="tags"
316 while read sha1 name
317 do
318 case "$name" in
319 *'^{}')
320 continue ;;
321 HEAD)
322 destname="REMOTE_HEAD" ;;
323 refs/heads/*)
324 destname="refs/$branch_top/${name#refs/heads/}" ;;
325 refs/tags/*)
326 destname="refs/$tag_top/${name#refs/tags/}" ;;
327 *)
328 continue ;;
329 esac
330 git-update-ref -m "clone: from $repo" "$destname" "$sha1" ""
331 done < "$GIT_DIR/CLONE_HEAD"
dfeff66e 332fi
1cadb5a2 333
0e9ab02d 334cd "$D" || exit
036a72d8 335
dfeff66e 336if test -z "$bare" && test -f "$GIT_DIR/REMOTE_HEAD"
036a72d8 337then
63085fab
JH
338 # a non-bare repository is always in separate-remote layout
339 remote_top="refs/remotes/$origin"
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 (
3fe71f3a 355 test -f "$GIT_DIR/$remote_top/master" && echo "master"
dfeff66e
JH
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
61dde8f9 373 # Write out remote.$origin config, and update our "$head_points_at".
e125c1a7 374 case "$head_points_at" in
dfeff66e 375 ?*)
61dde8f9 376 # Local default branch
c72112e6 377 git-symbolic-ref HEAD "refs/heads/$head_points_at" &&
61dde8f9
JH
378
379 # Tracking branch for the primary branch at the remote.
63085fab
JH
380 origin_track="$remote_top/$head_points_at" &&
381 git-update-ref HEAD "$head_sha1" &&
61dde8f9 382
3dd3d5b0 383 # Upstream URL
e0d10e1c 384 git-config remote."$origin".url "$repo" &&
61dde8f9 385
3dd3d5b0 386 # Set up the mappings to track the remote branches.
e0d10e1c 387 git-config remote."$origin".fetch \
013672bc 388 "+refs/heads/*:$remote_top/*" '^$' &&
63085fab
JH
389 rm -f "refs/remotes/$origin/HEAD"
390 git-symbolic-ref "refs/remotes/$origin/HEAD" \
391 "refs/remotes/$origin/$head_points_at" &&
61dde8f9 392
e0d10e1c
TP
393 git-config branch."$head_points_at".remote "$origin" &&
394 git-config branch."$head_points_at".merge "refs/heads/$head_points_at"
e125c1a7
JH
395 esac
396
036a72d8
JH
397 case "$no_checkout" in
398 '')
b0e90897 399 test "z$quiet" = z -a "z$no_progress" = z && v=-v || v=
810799ec 400 git-read-tree -m -u $v HEAD HEAD
036a72d8
JH
401 esac
402fi
dfeff66e 403rm -f "$GIT_DIR/CLONE_HEAD" "$GIT_DIR/REMOTE_HEAD"
41ff7a10 404
f803eec5 405trap - 0
41ff7a10 406