git-fetch.sh 11.6 KB
Newer Older
1 2
#!/bin/sh
#
3 4

USAGE='<fetch-options> <repository> <refspec>...'
5
SUBDIRECTORY_OK=Yes
6
. git-sh-setup
7
set_reflog_action "fetch $*"
8
cd_to_toplevel ;# probably unnecessary...
9

J
Junio C Hamano 已提交
10
. git-parse-remote
J
Junio C Hamano 已提交
11 12 13
_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"

14 15 16 17
LF='
'
IFS="$LF"

J
Junio C Hamano 已提交
18
no_tags=
L
Linus Torvalds 已提交
19
tags=
J
Junio C Hamano 已提交
20
append=
J
Junio C Hamano 已提交
21
force=
22
verbose=
23
update_head_ok=
24
exec=
25
keep=
26
shallow_depth=
J
Junio C Hamano 已提交
27 28 29 30 31 32
while case "$#" in 0) break ;; esac
do
	case "$1" in
	-a|--a|--ap|--app|--appe|--appen|--append)
		append=t
		;;
33 34
	--upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
	--upload-pa|--upload-pac|--upload-pack)
35
		shift
36 37 38 39 40 41
		exec="--upload-pack=$1"
		;;
	--upl=*|--uplo=*|--uploa=*|--upload=*|\
	--upload-=*|--upload-p=*|--upload-pa=*|--upload-pac=*|--upload-pack=*)
		exec=--upload-pack=$(expr "$1" : '-[^=]*=\(.*\)')
		shift
42
		;;
J
Junio C Hamano 已提交
43 44
	-f|--f|--fo|--for|--forc|--force)
		force=t
45
		;;
L
Linus Torvalds 已提交
46 47 48
	-t|--t|--ta|--tag|--tags)
		tags=t
		;;
J
Junio C Hamano 已提交
49 50 51
	-n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
		no_tags=t
		;;
52 53 54 55
	-u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
	--update-he|--update-hea|--update-head|--update-head-|\
	--update-head-o|--update-head-ok)
		update_head_ok=t
J
Junio C Hamano 已提交
56
		;;
57 58 59
	-v|--verbose)
		verbose=Yes
		;;
60
	-k|--k|--ke|--kee|--keep)
61
		keep='-k -k'
62
		;;
63 64 65 66 67 68 69
	--depth=*)
		shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
		;;
	--depth)
		shift
		shallow_depth="--depth=$1"
		;;
70 71 72
	-*)
		usage
		;;
J
Junio C Hamano 已提交
73 74 75 76
	*)
		break
		;;
	esac
77
	shift
J
Junio C Hamano 已提交
78 79
done

J
Junio C Hamano 已提交
80 81
case "$#" in
0)
82 83 84 85
	origin=$(get_default_remote)
	test -n "$(get_remote_url ${origin})" ||
		die "Where do you want to fetch from today?"
	set x $origin ; shift ;;
J
Junio C Hamano 已提交
86
esac
J
Junio C Hamano 已提交
87

88 89 90 91 92 93
if test -z "$exec"
then
	# No command line override and we have configuration for the remote.
	exec="--upload-pack=$(get_uploadpack $1)"
fi

J
Junio C Hamano 已提交
94 95 96 97 98 99 100 101
remote_nick="$1"
remote=$(get_remote_url "$@")
refs=
rref=
rsync_slurped_objects=

if test "" = "$append"
then
S
Santi_Béjar 已提交
102
	: >"$GIT_DIR/FETCH_HEAD"
J
Junio C Hamano 已提交
103 104
fi

105
# Global that is reused later
106
ls_remote_result=$(git ls-remote $exec "$remote") ||
107
	die "Cannot get the repository state from $remote"
108

J
Junio C Hamano 已提交
109 110 111 112 113 114
append_fetch_head () {
    head_="$1"
    remote_="$2"
    remote_name_="$3"
    remote_nick_="$4"
    local_name_="$5"
115 116 117 118
    case "$6" in
    t) not_for_merge_='not-for-merge' ;;
    '') not_for_merge_= ;;
    esac
J
Junio C Hamano 已提交
119

J
Junio C Hamano 已提交
120 121 122
    # remote-nick is the URL given on the command line (or a shorthand)
    # remote-name is the $GIT_DIR relative refs/ path we computed
    # for this refspec.
123 124 125

    # the $note_ variable will be fed to git-fmt-merge-msg for further
    # processing.
J
Junio C Hamano 已提交
126 127 128 129 130
    case "$remote_name_" in
    HEAD)
	note_= ;;
    refs/heads/*)
	note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
131
	note_="branch '$note_' of " ;;
J
Junio C Hamano 已提交
132 133
    refs/tags/*)
	note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
134
	note_="tag '$note_' of " ;;
135 136 137
    refs/remotes/*)
	note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
	note_="remote branch '$note_' of " ;;
J
Junio C Hamano 已提交
138
    *)
139
	note_="$remote_name of " ;;
J
Junio C Hamano 已提交
140
    esac
141
    remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
142 143
	remote_="$remote_1_"
    note_="$note_$remote_"
J
Junio C Hamano 已提交
144

J
Junio C Hamano 已提交
145 146 147
    # 2.6.11-tree tag would not be happy to be fed to resolve.
    if git-cat-file commit "$head_" >/dev/null 2>&1
    then
J
Junio C Hamano 已提交
148
	headc_=$(git-rev-parse --verify "$head_^0") || exit
S
Santi_Béjar 已提交
149
	echo "$headc_	$not_for_merge_	$note_" >>"$GIT_DIR/FETCH_HEAD"
J
Junio C Hamano 已提交
150
    else
S
Santi_Béjar 已提交
151
	echo "$head_	not-for-merge	$note_" >>"$GIT_DIR/FETCH_HEAD"
J
Junio C Hamano 已提交
152
    fi
S
Santi B,Ai(Bjar 已提交
153 154

    update_local_ref "$local_name_" "$head_" "$note_"
J
Junio C Hamano 已提交
155 156
}

S
Santi B,Ai(Bjar 已提交
157 158 159 160 161 162 163 164 165 166 167
update_local_ref () {
    # If we are storing the head locally make sure that it is
    # a fast forward (aka "reverse push").

    label_=$(git-cat-file -t $2)
    newshort_=$(git-rev-parse --short $2)
    if test -z "$1" ; then
	[ "$verbose" ] && echo >&2 "* fetched $3"
	[ "$verbose" ] && echo >&2 "  $label_: $newshort_"
	return 0
    fi
J
Junio C Hamano 已提交
168 169
    oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)

J
Junio C Hamano 已提交
170 171 172 173
    case "$1" in
    refs/tags/*)
	# Tags need not be pointing at commits so there
	# is no way to guarantee "fast-forward" anyway.
J
Junio C Hamano 已提交
174
	if test -n "$oldshort_"
J
Junio C Hamano 已提交
175
	then
J
Junio C Hamano 已提交
176
		if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
177
		then
S
Santi B,Ai(Bjar 已提交
178 179
			[ "$verbose" ] && echo >&2 "* $1: same as $3"
			[ "$verbose" ] && echo >&2 "  $label_: $newshort_" ||:
180 181
		else
			echo >&2 "* $1: updating with $3"
S
Santi B,Ai(Bjar 已提交
182
			echo >&2 "  $label_: $newshort_"
183
			git-update-ref -m "$GIT_REFLOG_ACTION: updating tag" "$1" "$2"
184
		fi
J
Junio C Hamano 已提交
185
	else
J
Junio C Hamano 已提交
186
		echo >&2 "* $1: storing $3"
S
Santi B,Ai(Bjar 已提交
187
		echo >&2 "  $label_: $newshort_"
188
		git-update-ref -m "$GIT_REFLOG_ACTION: storing tag" "$1" "$2"
J
Junio C Hamano 已提交
189
	fi
J
Junio C Hamano 已提交
190
	;;
J
Junio C Hamano 已提交
191

192
    refs/heads/* | refs/remotes/*)
J
Junio C Hamano 已提交
193 194 195 196
	# $1 is the ref being updated.
	# $2 is the new value for the ref.
	local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
	if test "$local"
197
	then
J
Junio C Hamano 已提交
198
	    # Require fast-forward.
J
Junio C Hamano 已提交
199 200 201
	    mb=$(git-merge-base "$local" "$2") &&
	    case "$2,$mb" in
	    $local,*)
202 203 204
	        if test -n "$verbose"
		then
			echo >&2 "* $1: same as $3"
S
Santi B,Ai(Bjar 已提交
205
			echo >&2 "  $label_: $newshort_"
206
		fi
J
Junio C Hamano 已提交
207 208
		;;
	    *,$local)
J
Junio C Hamano 已提交
209
		echo >&2 "* $1: fast forward to $3"
S
Santi B,Ai(Bjar 已提交
210
		echo >&2 "  old..new: $oldshort_..$newshort_"
211
		git-update-ref -m "$GIT_REFLOG_ACTION: fast-forward" "$1" "$2" "$local"
J
Junio C Hamano 已提交
212 213 214 215 216
		;;
	    *)
		false
		;;
	    esac || {
J
Junio C Hamano 已提交
217 218
		case ",$force,$single_force," in
		*,t,*)
S
Santi B,Ai(Bjar 已提交
219 220
			echo >&2 "* $1: forcing update to non-fast forward $3"
			echo >&2 "  old...new: $oldshort_...$newshort_"
221
			git-update-ref -m "$GIT_REFLOG_ACTION: forced-update" "$1" "$2" "$local"
J
Junio C Hamano 已提交
222 223
			;;
		*)
S
Santi B,Ai(Bjar 已提交
224 225
			echo >&2 "* $1: not updating to non-fast forward $3"
			echo >&2 "  old...new: $oldshort_...$newshort_"
226
			exit 1
J
Junio C Hamano 已提交
227 228
			;;
		esac
J
Junio C Hamano 已提交
229 230
	    }
	else
J
Junio C Hamano 已提交
231
	    echo >&2 "* $1: storing $3"
S
Santi B,Ai(Bjar 已提交
232
	    echo >&2 "  $label_: $newshort_"
233
	    git-update-ref -m "$GIT_REFLOG_ACTION: storing head" "$1" "$2"
234
	fi
235
	;;
J
Junio C Hamano 已提交
236 237 238
    esac
}

239 240 241 242
# updating the current HEAD with git-fetch in a bare
# repository is always fine.
if test -z "$update_head_ok" && test $(is_bare_repository) = false
then
J
Junio C Hamano 已提交
243
	orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
244
fi
245

L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253
# If --tags (and later --heads or --all) is specified, then we are
# not talking about defaults stored in Pull: line of remotes or
# branches file, and just fetch those and refspecs explicitly given.
# Otherwise we do what we always did.

reflist=$(get_remote_refs_for_fetch "$@")
if test "$tags"
then
254
	taglist=`IFS='	' &&
255
		  echo "$ls_remote_result" |
256 257
	          while read sha1 name
		  do
258 259 260 261
			case "$sha1" in
			fail)
				exit 1
			esac
262
			case "$name" in
263
			*^*) continue ;;
264 265
			refs/tags/*) ;;
			*) continue ;;
266 267 268 269 270 271 272
			esac
		  	if git-check-ref-format "$name"
			then
			    echo ".${name}:${name}"
			else
			    echo >&2 "warning: tag ${name} ignored"
			fi
273
		  done` || exit
L
Linus Torvalds 已提交
274 275 276
	if test "$#" -gt 1
	then
		# remote URL plus explicit refspecs; we need to merge them.
277
		reflist="$reflist$LF$taglist"
L
Linus Torvalds 已提交
278 279 280 281 282 283
	else
		# No explicit refspecs; fetch tags only.
		reflist=$taglist
	fi
fi

J
Junio C Hamano 已提交
284 285 286
fetch_main () {
  reflist="$1"
  refs=
287
  rref=
288

J
Junio C Hamano 已提交
289 290 291
  for ref in $reflist
  do
      refs="$refs$LF$ref"
292

J
Junio C Hamano 已提交
293 294
      # These are relative path from $GIT_DIR, typically starting at refs/
      # but may be HEAD
295
      if expr "z$ref" : 'z\.' >/dev/null
J
Junio C Hamano 已提交
296 297
      then
	  not_for_merge=t
298
	  ref=$(expr "z$ref" : 'z\.\(.*\)')
J
Junio C Hamano 已提交
299 300 301
      else
	  not_for_merge=
      fi
J
Junio C Hamano 已提交
302
      if expr "z$ref" : 'z+' >/dev/null
J
Junio C Hamano 已提交
303 304
      then
	  single_force=t
J
Junio C Hamano 已提交
305
	  ref=$(expr "z$ref" : 'z+\(.*\)')
J
Junio C Hamano 已提交
306 307 308
      else
	  single_force=
      fi
309 310
      remote_name=$(expr "z$ref" : 'z\([^:]*\):')
      local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
J
Junio C Hamano 已提交
311

J
Junio C Hamano 已提交
312
      rref="$rref$LF$remote_name"
313

J
Junio C Hamano 已提交
314 315
      # There are transports that can fetch only one head at a time...
      case "$remote" in
316
      http://* | https://* | ftp://*)
317 318
	  test -n "$shallow_depth" &&
		die "shallow clone with http not supported"
T
Tuncer Ayaz 已提交
319
	  proto=`expr "$remote" : '\([^:]*\):'`
J
Junio C Hamano 已提交
320 321 322
	  if [ -n "$GIT_SSL_NO_VERIFY" ]; then
	      curl_extra_args="-k"
	  fi
323 324 325 326
	  if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
		"`git-repo-config --bool http.noEPSV`" = true ]; then
	      noepsv_opt="--disable-epsv"
	  fi
327 328 329 330 331 332 333 334 335 336 337 338

	  # Find $remote_name from ls-remote output.
	  head=$(
		IFS='	'
		echo "$ls_remote_result" |
		while read sha1 name
		do
			test "z$name" = "z$remote_name" || continue
			echo "$sha1"
			break
		done
	  )
339
	  expr "z$head" : "z$_x40\$" >/dev/null ||
340
		die "No such ref $remote_name at $remote"
T
Tuncer Ayaz 已提交
341
	  echo >&2 "Fetching $remote_name from $remote using $proto"
J
Junio C Hamano 已提交
342 343 344
	  git-http-fetch -v -a "$head" "$remote/" || exit
	  ;;
      rsync://*)
345 346
	  test -n "$shallow_depth" &&
		die "shallow clone with rsync not supported"
J
Junio C Hamano 已提交
347 348 349 350 351 352 353
	  TMP_HEAD="$GIT_DIR/TMP_HEAD"
	  rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
	  head=$(git-rev-parse --verify TMP_HEAD)
	  rm -f "$TMP_HEAD"
	  test "$rsync_slurped_objects" || {
	      rsync -av --ignore-existing --exclude info \
		  "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
J
Junio C Hamano 已提交
354

J
Junio C Hamano 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	      # Look at objects/info/alternates for rsync -- http will
	      # support it natively and git native ones will do it on
	      # the remote end.  Not having that file is not a crime.
	      rsync -q "$remote/objects/info/alternates" \
		  "$GIT_DIR/TMP_ALT" 2>/dev/null ||
		  rm -f "$GIT_DIR/TMP_ALT"
	      if test -f "$GIT_DIR/TMP_ALT"
	      then
		  resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
		  while read alt
		  do
		      case "$alt" in 'bad alternate: '*) die "$alt";; esac
		      echo >&2 "Getting alternate: $alt"
		      rsync -av --ignore-existing --exclude info \
		      "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
		  done
		  rm -f "$GIT_DIR/TMP_ALT"
	      fi
	      rsync_slurped_objects=t
	  }
	  ;;
      *)
	  # We will do git native transport with just one call later.
	  continue ;;
      esac
380

J
Junio C Hamano 已提交
381
      append_fetch_head "$head" "$remote" \
382
	  "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
J
Junio C Hamano 已提交
383

J
Junio C Hamano 已提交
384 385 386
  done

  case "$remote" in
387
  http://* | https://* | ftp://* | rsync://* )
J
Junio C Hamano 已提交
388 389 390 391 392
      ;; # we are already done.
  *)
    ( : subshell because we muck with IFS
      IFS=" 	$LF"
      (
393 394
	  git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref ||
	  echo failed "$remote"
J
Junio C Hamano 已提交
395
      ) |
396 397 398 399 400 401 402 403 404 405 406
      (
	trap '
		if test -n "$keepfile" && test -f "$keepfile"
		then
			rm -f "$keepfile"
		fi
	' 0

        keepfile=
	while read sha1 remote_name
	do
J
Junio C Hamano 已提交
407 408 409 410
	  case "$sha1" in
	  failed)
		  echo >&2 "Fetch failure: $remote"
		  exit 1 ;;
411 412 413 414
	  # special line coming from index-pack with the pack name
	  pack)
		  continue ;;
	  keep)
415
		  keepfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
416
		  continue ;;
J
Junio C Hamano 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	  esac
	  found=
	  single_force=
	  for ref in $refs
	  do
	      case "$ref" in
	      +$remote_name:*)
		  single_force=t
		  not_for_merge=
		  found="$ref"
		  break ;;
	      .+$remote_name:*)
		  single_force=t
		  not_for_merge=t
		  found="$ref"
		  break ;;
	      .$remote_name:*)
		  not_for_merge=t
		  found="$ref"
		  break ;;
	      $remote_name:*)
		  not_for_merge=
		  found="$ref"
		  break ;;
	      esac
	  done
443
	  local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
J
Junio C Hamano 已提交
444
	  append_fetch_head "$sha1" "$remote" \
445 446
		  "$remote_name" "$remote_nick" "$local_name" \
		  "$not_for_merge" || exit
447 448
        done
      )
J
Junio C Hamano 已提交
449 450 451 452 453
    ) || exit ;;
  esac

}

454
fetch_main "$reflist" || exit
J
Junio C Hamano 已提交
455 456 457 458

# automated tag following
case "$no_tags$tags" in
'')
459 460 461 462
	case "$reflist" in
	*:refs/*)
		# effective only when we are following remote branch
		# using local tracking branch.
463
		taglist=$(IFS='	' &&
464
		echo "$ls_remote_result" |
465
		git-show-ref --exclude-existing=refs/tags/ |
466 467 468 469 470 471 472
		while read sha1 name
		do
			git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
			echo >&2 "Auto-following $name"
			echo ".${name}:${name}"
		done)
	esac
J
Junio C Hamano 已提交
473 474 475
	case "$taglist" in
	'') ;;
	?*)
476 477
		# do not deepen a shallow tree when following tags
		shallow_depth=
478
		fetch_main "$taglist" || exit ;;
J
Junio C Hamano 已提交
479
	esac
J
Junio C Hamano 已提交
480
esac
481 482 483

# If the original head was empty (i.e. no "master" yet), or
# if we were told not to worry, we do not have to check.
484 485
case "$orig_head" in
'')
486
	;;
487
?*)
J
Junio C Hamano 已提交
488
	curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
489 490
	if test "$curr_head" != "$orig_head"
	then
491
	    git-update-ref \
492
			-m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
493
			HEAD "$orig_head"
494 495 496 497
		die "Cannot fetch into the current branch."
	fi
	;;
esac