xdl_merge(): fix a segmentation fault when refining conflicts
[git/mingw/j6t.git] / git-fetch.sh
blob8bd11f8b60c0ffec8a67f59bc7e12e1f63adfc91
1 #!/bin/sh
4 USAGE='<fetch-options> <repository> <refspec>...'
5 SUBDIRECTORY_OK=Yes
6 . git-sh-setup
7 set_reflog_action "fetch $*"
9 TOP=$(git-rev-parse --show-cdup)
10 if test ! -z "$TOP"
11 then
12 cd "$TOP"
14 . git-parse-remote
15 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
16 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
18 LF='
20 IFS="$LF"
22 no_tags=
23 tags=
24 append=
25 force=
26 verbose=
27 update_head_ok=
28 exec=
29 upload_pack=
30 keep=
31 shallow_depth=
32 while case "$#" in 0) break ;; esac
34 case "$1" in
35 -a|--a|--ap|--app|--appe|--appen|--append)
36 append=t
38 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
39 --upload-pa|--upload-pac|--upload-pack)
40 shift
41 exec="--exec=$1"
42 upload_pack="-u $1"
44 -f|--f|--fo|--for|--forc|--force)
45 force=t
47 -t|--t|--ta|--tag|--tags)
48 tags=t
50 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
51 no_tags=t
53 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
54 --update-he|--update-hea|--update-head|--update-head-|\
55 --update-head-o|--update-head-ok)
56 update_head_ok=t
58 -v|--verbose)
59 verbose=Yes
61 -k|--k|--ke|--kee|--keep)
62 keep='-k -k'
64 --depth=*)
65 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
67 --depth)
68 shift
69 shallow_depth="--depth=$1"
71 -*)
72 usage
75 break
77 esac
78 shift
79 done
81 case "$#" in
83 origin=$(get_default_remote)
84 test -n "$(get_remote_url ${origin})" ||
85 die "Where do you want to fetch from today?"
86 set x $origin ; shift ;;
87 esac
89 remote_nick="$1"
90 remote=$(get_remote_url "$@")
91 refs=
92 rref=
93 rsync_slurped_objects=
95 if test "" = "$append"
96 then
97 : >"$GIT_DIR/FETCH_HEAD"
100 # Global that is reused later
101 ls_remote_result=$(git ls-remote $upload_pack "$remote") ||
102 die "Cannot get the repository state from $remote"
104 append_fetch_head () {
105 head_="$1"
106 remote_="$2"
107 remote_name_="$3"
108 remote_nick_="$4"
109 local_name_="$5"
110 case "$6" in
111 t) not_for_merge_='not-for-merge' ;;
112 '') not_for_merge_= ;;
113 esac
115 # remote-nick is the URL given on the command line (or a shorthand)
116 # remote-name is the $GIT_DIR relative refs/ path we computed
117 # for this refspec.
119 # the $note_ variable will be fed to git-fmt-merge-msg for further
120 # processing.
121 case "$remote_name_" in
122 HEAD)
123 note_= ;;
124 refs/heads/*)
125 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
126 note_="branch '$note_' of " ;;
127 refs/tags/*)
128 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
129 note_="tag '$note_' of " ;;
130 refs/remotes/*)
131 note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
132 note_="remote branch '$note_' of " ;;
134 note_="$remote_name of " ;;
135 esac
136 remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
137 remote_="$remote_1_"
138 note_="$note_$remote_"
140 # 2.6.11-tree tag would not be happy to be fed to resolve.
141 if git-cat-file commit "$head_" >/dev/null 2>&1
142 then
143 headc_=$(git-rev-parse --verify "$head_^0") || exit
144 echo "$headc_ $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
145 else
146 echo "$head_ not-for-merge $note_" >>"$GIT_DIR/FETCH_HEAD"
149 update_local_ref "$local_name_" "$head_" "$note_"
152 update_local_ref () {
153 # If we are storing the head locally make sure that it is
154 # a fast forward (aka "reverse push").
156 label_=$(git-cat-file -t $2)
157 newshort_=$(git-rev-parse --short $2)
158 if test -z "$1" ; then
159 [ "$verbose" ] && echo >&2 "* fetched $3"
160 [ "$verbose" ] && echo >&2 " $label_: $newshort_"
161 return 0
163 oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
165 case "$1" in
166 refs/tags/*)
167 # Tags need not be pointing at commits so there
168 # is no way to guarantee "fast-forward" anyway.
169 if test -n "$oldshort_"
170 then
171 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
172 then
173 [ "$verbose" ] && echo >&2 "* $1: same as $3"
174 [ "$verbose" ] && echo >&2 " $label_: $newshort_" ||:
175 else
176 echo >&2 "* $1: updating with $3"
177 echo >&2 " $label_: $newshort_"
178 git-update-ref -m "$GIT_REFLOG_ACTION: updating tag" "$1" "$2"
180 else
181 echo >&2 "* $1: storing $3"
182 echo >&2 " $label_: $newshort_"
183 git-update-ref -m "$GIT_REFLOG_ACTION: storing tag" "$1" "$2"
187 refs/heads/* | refs/remotes/*)
188 # $1 is the ref being updated.
189 # $2 is the new value for the ref.
190 local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
191 if test "$local"
192 then
193 # Require fast-forward.
194 mb=$(git-merge-base "$local" "$2") &&
195 case "$2,$mb" in
196 $local,*)
197 if test -n "$verbose"
198 then
199 echo >&2 "* $1: same as $3"
200 echo >&2 " $label_: $newshort_"
203 *,$local)
204 echo >&2 "* $1: fast forward to $3"
205 echo >&2 " old..new: $oldshort_..$newshort_"
206 git-update-ref -m "$GIT_REFLOG_ACTION: fast-forward" "$1" "$2" "$local"
209 false
211 esac || {
212 case ",$force,$single_force," in
213 *,t,*)
214 echo >&2 "* $1: forcing update to non-fast forward $3"
215 echo >&2 " old...new: $oldshort_...$newshort_"
216 git-update-ref -m "$GIT_REFLOG_ACTION: forced-update" "$1" "$2" "$local"
219 echo >&2 "* $1: not updating to non-fast forward $3"
220 echo >&2 " old...new: $oldshort_...$newshort_"
221 exit 1
223 esac
225 else
226 echo >&2 "* $1: storing $3"
227 echo >&2 " $label_: $newshort_"
228 git-update-ref -m "$GIT_REFLOG_ACTION: storing head" "$1" "$2"
231 esac
234 case "$update_head_ok" in
236 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
238 esac
240 # If --tags (and later --heads or --all) is specified, then we are
241 # not talking about defaults stored in Pull: line of remotes or
242 # branches file, and just fetch those and refspecs explicitly given.
243 # Otherwise we do what we always did.
245 reflist=$(get_remote_refs_for_fetch "$@")
246 if test "$tags"
247 then
248 taglist=`IFS=' ' &&
249 echo "$ls_remote_result" |
250 while read sha1 name
252 case "$sha1" in
253 fail)
254 exit 1
255 esac
256 case "$name" in
257 *^*) continue ;;
258 refs/tags/*) ;;
259 *) continue ;;
260 esac
261 if git-check-ref-format "$name"
262 then
263 echo ".${name}:${name}"
264 else
265 echo >&2 "warning: tag ${name} ignored"
267 done` || exit
268 if test "$#" -gt 1
269 then
270 # remote URL plus explicit refspecs; we need to merge them.
271 reflist="$reflist$LF$taglist"
272 else
273 # No explicit refspecs; fetch tags only.
274 reflist=$taglist
278 fetch_main () {
279 reflist="$1"
280 refs=
281 rref=
283 for ref in $reflist
285 refs="$refs$LF$ref"
287 # These are relative path from $GIT_DIR, typically starting at refs/
288 # but may be HEAD
289 if expr "z$ref" : 'z\.' >/dev/null
290 then
291 not_for_merge=t
292 ref=$(expr "z$ref" : 'z\.\(.*\)')
293 else
294 not_for_merge=
296 if expr "z$ref" : 'z+' >/dev/null
297 then
298 single_force=t
299 ref=$(expr "z$ref" : 'z+\(.*\)')
300 else
301 single_force=
303 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
304 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
306 rref="$rref$LF$remote_name"
308 # There are transports that can fetch only one head at a time...
309 case "$remote" in
310 http://* | https://* | ftp://*)
311 test -n "$shallow_depth" &&
312 die "shallow clone with http not supported"
313 proto=`expr "$remote" : '\([^:]*\):'`
314 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
315 curl_extra_args="-k"
317 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
318 "`git-repo-config --bool http.noEPSV`" = true ]; then
319 noepsv_opt="--disable-epsv"
322 # Find $remote_name from ls-remote output.
323 head=$(
324 IFS=' '
325 echo "$ls_remote_result" |
326 while read sha1 name
328 test "z$name" = "z$remote_name" || continue
329 echo "$sha1"
330 break
331 done
333 expr "z$head" : "z$_x40\$" >/dev/null ||
334 die "No such ref $remote_name at $remote"
335 echo >&2 "Fetching $remote_name from $remote using $proto"
336 git-http-fetch -v -a "$head" "$remote/" || exit
338 rsync://*)
339 test -n "$shallow_depth" &&
340 die "shallow clone with rsync not supported"
341 TMP_HEAD="$GIT_DIR/TMP_HEAD"
342 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
343 head=$(git-rev-parse --verify TMP_HEAD)
344 rm -f "$TMP_HEAD"
345 test "$rsync_slurped_objects" || {
346 rsync -av --ignore-existing --exclude info \
347 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
349 # Look at objects/info/alternates for rsync -- http will
350 # support it natively and git native ones will do it on
351 # the remote end. Not having that file is not a crime.
352 rsync -q "$remote/objects/info/alternates" \
353 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
354 rm -f "$GIT_DIR/TMP_ALT"
355 if test -f "$GIT_DIR/TMP_ALT"
356 then
357 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
358 while read alt
360 case "$alt" in 'bad alternate: '*) die "$alt";; esac
361 echo >&2 "Getting alternate: $alt"
362 rsync -av --ignore-existing --exclude info \
363 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
364 done
365 rm -f "$GIT_DIR/TMP_ALT"
367 rsync_slurped_objects=t
371 # We will do git native transport with just one call later.
372 continue ;;
373 esac
375 append_fetch_head "$head" "$remote" \
376 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
378 done
380 case "$remote" in
381 http://* | https://* | ftp://* | rsync://* )
382 ;; # we are already done.
384 ( : subshell because we muck with IFS
385 pack_lockfile=
386 IFS=" $LF"
388 git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref || echo failed "$remote"
390 while read sha1 remote_name
392 case "$sha1" in
393 failed)
394 echo >&2 "Fetch failure: $remote"
395 exit 1 ;;
396 # special line coming from index-pack with the pack name
397 pack)
398 continue ;;
399 keep)
400 pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
401 continue ;;
402 esac
403 found=
404 single_force=
405 for ref in $refs
407 case "$ref" in
408 +$remote_name:*)
409 single_force=t
410 not_for_merge=
411 found="$ref"
412 break ;;
413 .+$remote_name:*)
414 single_force=t
415 not_for_merge=t
416 found="$ref"
417 break ;;
418 .$remote_name:*)
419 not_for_merge=t
420 found="$ref"
421 break ;;
422 $remote_name:*)
423 not_for_merge=
424 found="$ref"
425 break ;;
426 esac
427 done
428 local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
429 append_fetch_head "$sha1" "$remote" \
430 "$remote_name" "$remote_nick" "$local_name" \
431 "$not_for_merge" || exit
432 done &&
433 if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
434 ) || exit ;;
435 esac
439 fetch_main "$reflist" || exit
441 # automated tag following
442 case "$no_tags$tags" in
444 case "$reflist" in
445 *:refs/*)
446 # effective only when we are following remote branch
447 # using local tracking branch.
448 taglist=$(IFS=' ' &&
449 echo "$ls_remote_result" |
450 git-show-ref --exclude-existing=refs/tags/ |
451 while read sha1 name
453 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
454 echo >&2 "Auto-following $name"
455 echo ".${name}:${name}"
456 done)
457 esac
458 case "$taglist" in
459 '') ;;
461 # do not deepen a shallow tree when following tags
462 shallow_depth=
463 fetch_main "$taglist" || exit ;;
464 esac
465 esac
467 # If the original head was empty (i.e. no "master" yet), or
468 # if we were told not to worry, we do not have to check.
469 case "$orig_head" in
473 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
474 if test "$curr_head" != "$orig_head"
475 then
476 git-update-ref \
477 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
478 HEAD "$orig_head"
479 die "Cannot fetch into the current branch."
482 esac