1#!/bin/sh
2#
3
4USAGE='<fetch-options> <repository> <refspec>...'
5SUBDIRECTORY_OK=Yes
6. git-sh-setup
7set_reflog_action "fetch $*"
8cd_to_toplevel ;# probably unnecessary...
9
10. git-parse-remote
11_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
12_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
13
14LF='
15'
16IFS="$LF"
17
18no_tags=
19tags=
20append=
21force=
22verbose=
23update_head_ok=
24exec=
25keep=
26shallow_depth=
27while case "$#" in 0) break ;; esac
28do
29 case "$1" in
30 -a|--a|--ap|--app|--appe|--appen|--append)
31 append=t
32 ;;
33 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
34 --upload-pa|--upload-pac|--upload-pack)
35 shift
36 exec="--upload-pack=$1"
37 ;;
38 --upl=*|--uplo=*|--uploa=*|--upload=*|\
39 --upload-=*|--upload-p=*|--upload-pa=*|--upload-pac=*|--upload-pack=*)
40 exec=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)')
41 shift
42 ;;
43 -f|--f|--fo|--for|--forc|--force)
44 force=t
45 ;;
46 -t|--t|--ta|--tag|--tags)
47 tags=t
48 ;;
49 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
50 no_tags=t
51 ;;
52 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
53 --update-he|--update-hea|--update-head|--update-head-|\
54 --update-head-o|--update-head-ok)
55 update_head_ok=t
56 ;;
57 -v|--verbose)
58 verbose=Yes
59 ;;
60 -k|--k|--ke|--kee|--keep)
61 keep='-k -k'
62 ;;
63 --depth=*)
64 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
65 ;;
66 --depth)
67 shift
68 shallow_depth="--depth=$1"
69 ;;
70 -*)
71 usage
72 ;;
73 *)
74 break
75 ;;
76 esac
77 shift
78done
79
80case "$#" in
810)
82 origin=$(get_default_remote)
83 test -n "$(get_remote_url ${origin})" ||
84 die "Where do you want to fetch from today?"
85 set x $origin ; shift ;;
86esac
87
88if test -z "$exec"
89then
90 # No command line override and we have configuration for the remote.
91 exec="--upload-pack=$(get_uploadpack $1)"
92fi
93
94remote_nick="$1"
95remote=$(get_remote_url "$@")
96refs=
97rref=
98rsync_slurped_objects=
99
100if test "" = "$append"
101then
102 : >"$GIT_DIR/FETCH_HEAD"
103fi
104
105# Global that is reused later
106ls_remote_result=$(git ls-remote $exec "$remote") ||
107 die "Cannot get the repository state from $remote"
108
109append_fetch_head () {
110 flags=
111 test -n "$verbose" && flags="$flags -v"
112 test -n "$force" && flags="$flags -f"
113 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
114 git-fetch--tool $flags append-fetch-head "$@"
115}
116
117update_local_ref () {
118 flags=
119 test -n "$verbose" && flags="$flags -v"
120 test -n "$force" && flags="$flags -f"
121 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
122 git-fetch--tool $flags update-local-ref "$@"
123}
124
125# updating the current HEAD with git-fetch in a bare
126# repository is always fine.
127if test -z "$update_head_ok" && test $(is_bare_repository) = false
128then
129 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
130fi
131
132# Allow --notags from remote.$1.tagopt
133case "$tags$no_tags" in
134'')
135 case "$(git-config --get "remote.$1.tagopt")" in
136 --no-tags)
137 no_tags=t ;;
138 esac
139esac
140
141# If --tags (and later --heads or --all) is specified, then we are
142# not talking about defaults stored in Pull: line of remotes or
143# branches file, and just fetch those and refspecs explicitly given.
144# Otherwise we do what we always did.
145
146reflist=$(get_remote_refs_for_fetch "$@")
147if test "$tags"
148then
149 taglist=`IFS=' ' &&
150 echo "$ls_remote_result" |
151 git-show-ref --exclude-existing=refs/tags/ |
152 while read sha1 name
153 do
154 echo ".${name}:${name}"
155 done` || exit
156 if test "$#" -gt 1
157 then
158 # remote URL plus explicit refspecs; we need to merge them.
159 reflist="$reflist$LF$taglist"
160 else
161 # No explicit refspecs; fetch tags only.
162 reflist=$taglist
163 fi
164fi
165
166fetch_native () {
167
168 eval=$(echo "$1" | git-fetch--tool parse-reflist "-")
169 eval "$eval"
170
171 ( : subshell because we muck with IFS
172 IFS=" $LF"
173 (
174 git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref ||
175 echo failed "$remote"
176 ) |
177 (
178 flags=
179 test -n "$verbose" && flags="$flags -v"
180 test -n "$force" && flags="$flags -f"
181 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
182 git-fetch--tool $flags native-store \
183 "$remote" "$remote_nick" "$refs"
184 )
185 ) || exit
186
187}
188
189fetch_dumb () {
190 reflist="$1"
191 refs=
192 rref=
193
194 for ref in $reflist
195 do
196 refs="$refs$LF$ref"
197
198 # These are relative path from $GIT_DIR, typically starting at refs/
199 # but may be HEAD
200 if expr "z$ref" : 'z\.' >/dev/null
201 then
202 not_for_merge=t
203 ref=$(expr "z$ref" : 'z\.\(.*\)')
204 else
205 not_for_merge=
206 fi
207 if expr "z$ref" : 'z+' >/dev/null
208 then
209 single_force=t
210 ref=$(expr "z$ref" : 'z+\(.*\)')
211 else
212 single_force=
213 fi
214 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
215 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
216
217 rref="$rref$LF$remote_name"
218
219 # There are transports that can fetch only one head at a time...
220 case "$remote" in
221 http://* | https://* | ftp://*)
222 test -n "$shallow_depth" &&
223 die "shallow clone with http not supported"
224 proto=`expr "$remote" : '\([^:]*\):'`
225 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
226 curl_extra_args="-k"
227 fi
228 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
229 "`git-config --bool http.noEPSV`" = true ]; then
230 noepsv_opt="--disable-epsv"
231 fi
232
233 # Find $remote_name from ls-remote output.
234 head=$(
235 IFS=' '
236 echo "$ls_remote_result" |
237 while read sha1 name
238 do
239 test "z$name" = "z$remote_name" || continue
240 echo "$sha1"
241 break
242 done
243 )
244 expr "z$head" : "z$_x40\$" >/dev/null ||
245 die "No such ref $remote_name at $remote"
246 echo >&2 "Fetching $remote_name from $remote using $proto"
247 git-http-fetch -v -a "$head" "$remote/" || exit
248 ;;
249 rsync://*)
250 test -n "$shallow_depth" &&
251 die "shallow clone with rsync not supported"
252 TMP_HEAD="$GIT_DIR/TMP_HEAD"
253 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
254 head=$(git-rev-parse --verify TMP_HEAD)
255 rm -f "$TMP_HEAD"
256 test "$rsync_slurped_objects" || {
257 rsync -av --ignore-existing --exclude info \
258 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
259
260 # Look at objects/info/alternates for rsync -- http will
261 # support it natively and git native ones will do it on
262 # the remote end. Not having that file is not a crime.
263 rsync -q "$remote/objects/info/alternates" \
264 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
265 rm -f "$GIT_DIR/TMP_ALT"
266 if test -f "$GIT_DIR/TMP_ALT"
267 then
268 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
269 while read alt
270 do
271 case "$alt" in 'bad alternate: '*) die "$alt";; esac
272 echo >&2 "Getting alternate: $alt"
273 rsync -av --ignore-existing --exclude info \
274 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
275 done
276 rm -f "$GIT_DIR/TMP_ALT"
277 fi
278 rsync_slurped_objects=t
279 }
280 ;;
281 esac
282
283 append_fetch_head "$head" "$remote" \
284 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
285
286 done
287
288}
289
290fetch_main () {
291 case "$remote" in
292 http://* | https://* | ftp://* | rsync://* )
293 fetch_dumb "$@"
294 ;;
295 *)
296 fetch_native "$@"
297 ;;
298 esac
299}
300
301fetch_main "$reflist" || exit
302
303# automated tag following
304case "$no_tags$tags" in
305'')
306 case "$reflist" in
307 *:refs/*)
308 # effective only when we are following remote branch
309 # using local tracking branch.
310 taglist=$(IFS=' ' &&
311 echo "$ls_remote_result" |
312 git-show-ref --exclude-existing=refs/tags/ |
313 while read sha1 name
314 do
315 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
316 echo >&2 "Auto-following $name"
317 echo ".${name}:${name}"
318 done)
319 esac
320 case "$taglist" in
321 '') ;;
322 ?*)
323 # do not deepen a shallow tree when following tags
324 shallow_depth=
325 fetch_main "$taglist" || exit ;;
326 esac
327esac
328
329# If the original head was empty (i.e. no "master" yet), or
330# if we were told not to worry, we do not have to check.
331case "$orig_head" in
332'')
333 ;;
334?*)
335 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
336 if test "$curr_head" != "$orig_head"
337 then
338 git-update-ref \
339 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
340 HEAD "$orig_head"
341 die "Cannot fetch into the current branch."
342 fi
343 ;;
344esac