]> git.ipfire.org Git - thirdparty/git.git/blame - git-clone.sh
[PATCH] Return proper error valud from "parse_date()"
[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() {
aae4f42c 12 echo >&2 "* git clone [-l [-s]] [-q] [-u <upload-pack>] <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
26 curl -nsf $curl_extra_args "$1" >"$2"
27}
28
29clone_dumb_http () {
30 # $1 - remote, $2 - local
31 cd "$2" &&
32 clone_tmp='.git/clone-tmp' &&
33 mkdir -p "$clone_tmp" || exit 1
34 http_fetch "$1/info/refs" "$clone_tmp/refs" &&
35 http_fetch "$1/objects/info/packs" "$clone_tmp/packs" || {
36 echo >&2 "Cannot get remote repository information.
37Perhaps git-update-server-info needs to be run there?"
38 exit 1;
39 }
40 while read type name
41 do
42 case "$type" in
43 P) ;;
44 *) continue ;;
45 esac &&
46
47 idx=`expr "$name" : '\(.*\)\.pack'`.idx
48 http_fetch "$1/objects/pack/$name" ".git/objects/pack/$name" &&
49 http_fetch "$1/objects/pack/$idx" ".git/objects/pack/$idx" &&
50 git-verify-pack ".git/objects/pack/$idx" || exit 1
51 done <"$clone_tmp/packs"
52
53 while read sha1 refname
54 do
55 name=`expr "$refname" : 'refs/\(.*\)'` &&
215a7ad1 56 git-http-fetch -v -a -w "$name" "$name" "$1/" || exit 1
0516de30
JH
57 done <"$clone_tmp/refs"
58 rm -fr "$clone_tmp"
59}
60
167a4a33 61quiet=
e95ab1ed 62use_local=no
aae4f42c 63local_shared=no
6ec311da 64upload_pack=
e95ab1ed
JH
65while
66 case "$#,$1" in
67 0,*) break ;;
1cadb5a2 68 *,-l|*,--l|*,--lo|*,--loc|*,--loca|*,--local) use_local=yes ;;
aae4f42c
JH
69 *,-s|*,--s|*,--sh|*,--sha|*,--shar|*,--share|*,--shared)
70 local_shared=yes ;;
167a4a33 71 *,-q|*,--quiet) quiet=-q ;;
1cadb5a2 72 1,-u|1,--upload-pack) usage ;;
6ec311da
JH
73 *,-u|*,--upload-pack)
74 shift
1cadb5a2 75 upload_pack="--exec=$1" ;;
e95ab1ed
JH
76 *,-*) usage ;;
77 *) break ;;
78 esac
79do
80 shift
81done
82
ba375acf
LT
83# Turn the source into an absolute path if
84# it is local
3f571e0b 85repo="$1"
ba375acf
LT
86local=no
87if base=$(get_repo_base "$repo"); then
88 repo="$base"
89 local=yes
90fi
91
3f571e0b 92dir="$2"
e95ab1ed
JH
93mkdir "$dir" &&
94D=$(
95 (cd "$dir" && git-init-db && pwd)
96) &&
97test -d "$D" || usage
98
99# We do local magic only when the user tells us to.
ba375acf
LT
100case "$local,$use_local" in
101yes,yes)
e95ab1ed 102 ( cd "$repo/objects" ) || {
ab6625e0
JH
103 echo >&2 "-l flag seen but $repo is not local."
104 exit 1
e95ab1ed
JH
105 }
106
aae4f42c
JH
107 case "$local_shared" in
108 no)
109 # See if we can hardlink and drop "l" if not.
110 sample_file=$(cd "$repo" && \
111 find objects -type f -print | sed -e 1q)
e95ab1ed 112
aae4f42c
JH
113 # objects directory should not be empty since we are cloning!
114 test -f "$repo/$sample_file" || exit
e95ab1ed 115
aae4f42c
JH
116 l=
117 if ln "$repo/$sample_file" "$D/.git/objects/sample" 2>/dev/null
118 then
119 l=l
120 fi &&
121 rm -f "$D/.git/objects/sample" &&
122 cd "$repo" &&
123 find objects -type f -print |
124 cpio -puamd$l "$D/.git/" || exit 1
125 ;;
126 yes)
127 mkdir -p "$D/.git/objects/info"
0f87f893
JH
128 {
129 test -f "$repo/objects/info/alternates" &&
130 cat "$repo/objects/info/alternates";
131 echo "$repo/objects"
132 } >"$D/.git/objects/info/alternates"
aae4f42c
JH
133 ;;
134 esac
e95ab1ed
JH
135
136 # Make a duplicate of refs and HEAD pointer
137 HEAD=
138 if test -f "$repo/HEAD"
139 then
140 HEAD=HEAD
141 fi
142 tar Ccf "$repo" - refs $HEAD | tar Cxf "$D/.git" - || exit 1
7558ef89
LT
143 ;;
144*)
1cadb5a2
JH
145 case "$repo" in
146 rsync://*)
4447badc
JH
147 rsync $quiet -av --ignore-existing \
148 --exclude info "$repo/objects/" "$D/.git/objects/" &&
149 rsync $quiet -av --ignore-existing \
150 --exclude info "$repo/refs/" "$D/.git/refs/" || exit
151
152 # Look at objects/info/alternates for rsync -- http will
153 # support it natively and git native ones will do it on the
154 # remote end. Not having that file is not a crime.
155 rsync -q "$repo/objects/info/alternates" "$D/.git/TMP_ALT" ||
156 rm -f "$D/.git/TMP_ALT"
157 if test -f "$D/.git/TMP_ALT"
158 then
159 ( cd $D &&
160 . git-parse-remote &&
161 resolve_alternates "$repo" <"./.git/TMP_ALT" ) |
162 while read alt
163 do
164 case "$alt" in 'bad alternate: '*) die "$alt";; esac
165 case "$quiet" in
166 '') echo >&2 "Getting alternate: $alt" ;;
167 esac
168 rsync $quiet -av --ignore-existing \
169 --exclude info "$alt" "$D/.git/objects" || exit
170 done
171 rm -f "$D/.git/TMP_ALT"
172 fi
1cadb5a2
JH
173 ;;
174 http://*)
0516de30 175 clone_dumb_http "$repo" "$D"
1cadb5a2
JH
176 ;;
177 *)
178 cd "$D" && case "$upload_pack" in
179 '') git-clone-pack $quiet "$repo" ;;
180 *) git-clone-pack $quiet "$upload_pack" "$repo" ;;
181 esac
182 ;;
6ec311da 183 esac
7558ef89
LT
184 ;;
185esac
1cadb5a2
JH
186
187# Update origin.
6687f8fe
JH
188mkdir -p "$D/.git/remotes/" &&
189rm -f "$D/.git/remotes/origin" &&
190echo >"$D/.git/remotes/origin" \
191"URL: $repo
192Pull: master:origin"