git-submodule.sh 21.9 KB
Newer Older
L
Lars Hjemli 已提交
1 2
#!/bin/sh
#
3
# git-submodules.sh: add, init, update or list git submodules
L
Lars Hjemli 已提交
4 5 6
#
# Copyright (c) 2007 Lars Hjemli

7
dashless=$(basename "$0" | sed -e 's/-/ /')
8
USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>]
9
   or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10
   or: $dashless [--quiet] init [--] [<path>...]
11
   or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
12
   or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
13
   or: $dashless [--quiet] foreach [--recursive] <command>
14
   or: $dashless [--quiet] sync [--] [<path>...]"
15
OPTIONS_SPEC=
L
Lars Hjemli 已提交
16
. git-sh-setup
17
. git-sh-i18n
18
. git-parse-remote
L
Lars Hjemli 已提交
19 20
require_work_tree

21
command=
22
branch=
23
force=
24
reference=
L
Lars Hjemli 已提交
25
cached=
26 27
recursive=
init=
28
files=
29
nofetch=
30
update=
31
prefix=
L
Lars Hjemli 已提交
32

33 34 35
# Resolve relative url by appending to parent's url
resolve_relative_url ()
{
36
	remote=$(get_default_remote)
37
	remoteurl=$(git config "remote.$remote.url") ||
38
		remoteurl=$(pwd) # the repository is its own authoritative upstream
39
	url="$1"
40
	remoteurl=${remoteurl%/}
41
	sep=/
42 43 44 45 46
	while test -n "$url"
	do
		case "$url" in
		../*)
			url="${url#../}"
47 48 49 50 51 52 53 54 55
			case "$remoteurl" in
			*/*)
				remoteurl="${remoteurl%/*}"
				;;
			*:*)
				remoteurl="${remoteurl%:*}"
				sep=:
				;;
			*)
56
				die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
57 58
				;;
			esac
59 60 61 62 63 64 65 66
			;;
		./*)
			url="${url#./}"
			;;
		*)
			break;;
		esac
	done
67
	echo "$remoteurl$sep${url%/}"
68 69
}

70 71 72 73 74 75
#
# Get submodule info for registered submodules
# $@ = path to limit submodule list
#
module_list()
{
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	git ls-files --error-unmatch --stage -- "$@" |
	perl -e '
	my %unmerged = ();
	my ($null_sha1) = ("0" x 40);
	while (<STDIN>) {
		chomp;
		my ($mode, $sha1, $stage, $path) =
			/^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
		next unless $mode eq "160000";
		if ($stage ne "0") {
			if (!$unmerged{$path}++) {
				print "$mode $null_sha1 U\t$path\n";
			}
			next;
		}
		print "$_\n";
	}
	'
94 95
}

96 97 98 99 100 101 102
#
# Map submodule path to submodule name
#
# $1 = path
#
module_name()
{
103
	# Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
104
	sm_path="$1"
C
Chris Ridd 已提交
105
	re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
106
	name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
107
		sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
T
Tay Ray Chuan 已提交
108
	test -z "$name" &&
109
	die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
T
Tay Ray Chuan 已提交
110
	echo "$name"
111
}
112 113 114 115

#
# Clone a submodule
#
116
# Prior to calling, cmd_update checks that a possibly existing
117
# path is not a git repository.
118
# Likewise, cmd_add checks that path does not exist at all,
119 120
# since it is the location of a new submodule.
#
121 122
module_clone()
{
123
	sm_path=$1
124
	url=$2
125
	reference="$3"
126 127 128 129 130
	quiet=
	if test -n "$GIT_QUIET"
	then
		quiet=-q
	fi
131

F
Fredrik Gustafsson 已提交
132 133
	gitdir=
	gitdir_base=
134 135
	name=$(module_name "$sm_path" 2>/dev/null)
	test -n "$name" || name="$sm_path"
136
	base_name=$(dirname "$name")
137

F
Fredrik Gustafsson 已提交
138
	gitdir=$(git rev-parse --git-dir)
139 140
	gitdir_base="$gitdir/modules/$base_name"
	gitdir="$gitdir/modules/$name"
F
Fredrik Gustafsson 已提交
141 142

	if test -d "$gitdir"
143
	then
144
		mkdir -p "$sm_path"
F
Fredrik Gustafsson 已提交
145
		rm -f "$gitdir/index"
146
	else
F
Fredrik Gustafsson 已提交
147
		mkdir -p "$gitdir_base"
148
		git clone $quiet -n ${reference:+"$reference"} \
149 150
			--separate-git-dir "$gitdir" "$url" "$sm_path" ||
		die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
F
Fredrik Gustafsson 已提交
151
	fi
152

153
	a=$(cd "$gitdir" && pwd)/
154
	b=$(cd "$sm_path" && pwd)/
155 156 157
	# normalize Windows-style absolute paths to POSIX-style absolute paths
	case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
	case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
158 159 160 161 162 163 164 165 166 167 168 169 170
	# Remove all common leading directories after a sanity check
	if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
		die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
	fi
	while test "${a%%/*}" = "${b%%/*}"
	do
		a=${a#*/}
		b=${b#*/}
	done
	# Now chop off the trailing '/'s that were added in the beginning
	a=${a%/}
	b=${b%/}

171 172
	# Turn each leading "*/" component into "../"
	rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
173
	echo "gitdir: $rel/$a" >"$sm_path/.git"
174

175
	rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
176
	(clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
177 178
}

179 180 181
#
# Add a new submodule to the working tree, .gitmodules and the index
#
182
# $@ = repo path
183 184 185
#
# optional branch is stored in global branch variable
#
186
cmd_add()
187
{
188 189 190 191 192 193 194 195 196
	# parse $args after "submodule ... add".
	while test $# -ne 0
	do
		case "$1" in
		-b | --branch)
			case "$2" in '') usage ;; esac
			branch=$2
			shift
			;;
197 198 199
		-f | --force)
			force=$1
			;;
200
		-q|--quiet)
201
			GIT_QUIET=1
202
			;;
203 204 205 206 207 208 209 210 211
		--reference)
			case "$2" in '') usage ;; esac
			reference="--reference=$2"
			shift
			;;
		--reference=*)
			reference="$1"
			shift
			;;
212 213 214 215 216 217 218 219 220 221 222 223 224 225
		--)
			shift
			break
			;;
		-*)
			usage
			;;
		*)
			break
			;;
		esac
		shift
	done

226
	repo=$1
227
	sm_path=$2
228

229 230
	if test -z "$sm_path"; then
		sm_path=$(echo "$repo" |
231 232 233
			sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
	fi

234
	if test -z "$repo" -o -z "$sm_path"; then
235 236 237
		usage
	fi

238 239 240 241 242 243 244 245 246 247 248
	# assure repo is absolute or relative to parent
	case "$repo" in
	./*|../*)
		# dereference source url relative to parent's url
		realrepo=$(resolve_relative_url "$repo") || exit
		;;
	*:*|/*)
		# absolute url
		realrepo=$repo
		;;
	*)
249
		die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
250 251 252
	;;
	esac

253 254
	# normalize path:
	# multiple //; leading ./; /./; /../; trailing /
255
	sm_path=$(printf '%s/\n' "$sm_path" |
256 257 258 259 260 261 262 263 264
		sed -e '
			s|//*|/|g
			s|^\(\./\)*||
			s|/\./|/|g
			:start
			s|\([^/]*\)/\.\./||
			tstart
			s|/*$||
		')
265 266
	git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
	die "$(eval_gettext "'\$sm_path' already exists in the index")"
267

268
	if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
269
	then
270
		eval_gettextln "The following path is ignored by one of your .gitignore files:
271
\$sm_path
272
Use -f if you really want to add it." >&2
273 274 275
		exit 1
	fi

276
	# perhaps the path exists and is already a git repo, else clone it
277
	if test -e "$sm_path"
278
	then
279
		if test -d "$sm_path"/.git -o -f "$sm_path"/.git
280
		then
281
			eval_gettextln "Adding existing repo at '\$sm_path' to the index"
282
		else
283
			die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
284
		fi
285

286 287
	else

288
		module_clone "$sm_path" "$realrepo" "$reference" || exit
289
		(
290
			clear_local_git_env
291
			cd "$sm_path" &&
292 293 294
			# ash fails to wordsplit ${branch:+-b "$branch"...}
			case "$branch" in
			'') git checkout -f -q ;;
295
			?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
296
			esac
297
		) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
298
	fi
299
	git config submodule."$sm_path".url "$realrepo"
300

301 302
	git add $force "$sm_path" ||
	die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
303

304 305
	git config -f .gitmodules submodule."$sm_path".path "$sm_path" &&
	git config -f .gitmodules submodule."$sm_path".url "$repo" &&
306
	git add --force .gitmodules ||
307
	die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
308 309
}

310 311 312 313 314 315 316 317
#
# Execute an arbitrary command sequence in each checked out
# submodule
#
# $@ = command to execute
#
cmd_foreach()
{
318 319 320 321 322 323 324
	# parse $args after "submodule ... foreach".
	while test $# -ne 0
	do
		case "$1" in
		-q|--quiet)
			GIT_QUIET=1
			;;
325 326 327
		--recursive)
			recursive=1
			;;
328 329 330 331 332 333 334 335 336 337
		-*)
			usage
			;;
		*)
			break
			;;
		esac
		shift
	done

338 339
	toplevel=$(pwd)

340 341 342 343
	# dup stdin so that it can be restored when running the external
	# command in the subshell (and a recursive call to this function)
	exec 3<&0

344
	module_list |
345
	while read mode sha1 stage sm_path
346
	do
347
		if test -e "$sm_path"/.git
348
		then
349 350
			say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
			name=$(module_name "$sm_path")
351
			(
352
				prefix="$prefix$sm_path/"
353
				clear_local_git_env
354 355 356
				# we make $path available to scripts ...
				path=$sm_path
				cd "$sm_path" &&
357 358 359 360 361
				eval "$@" &&
				if test -n "$recursive"
				then
					cmd_foreach "--recursive" "$@"
				fi
362
			) <&3 3<&- ||
363
			die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
364 365 366 367
		fi
	done
}

L
Lars Hjemli 已提交
368
#
369
# Register submodules in .git/config
L
Lars Hjemli 已提交
370 371 372
#
# $@ = requested paths (default to all)
#
373
cmd_init()
L
Lars Hjemli 已提交
374
{
375 376 377 378 379
	# parse $args after "submodule ... init".
	while test $# -ne 0
	do
		case "$1" in
		-q|--quiet)
380
			GIT_QUIET=1
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
			;;
		--)
			shift
			break
			;;
		-*)
			usage
			;;
		*)
			break
			;;
		esac
		shift
	done

396
	module_list "$@" |
397
	while read mode sha1 stage sm_path
L
Lars Hjemli 已提交
398
	do
399
		name=$(module_name "$sm_path") || exit
400 401

		# Copy url setting when it is not set yet
402 403 404 405
		if test -z "$(git config "submodule.$name.url")"
		then
			url=$(git config -f .gitmodules submodule."$name".url)
			test -z "$url" &&
406
			die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
407 408 409 410 411 412 413 414

			# Possibly a url relative to parent
			case "$url" in
			./*|../*)
				url=$(resolve_relative_url "$url") || exit
				;;
			esac
			git config submodule."$name".url "$url" ||
415
			die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
416 417

			say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
418
		fi
L
Lars Hjemli 已提交
419

420
		# Copy "update" setting when it is not set yet
421 422
		upd="$(git config -f .gitmodules submodule."$name".update)"
		test -z "$upd" ||
423
		test -n "$(git config submodule."$name".update)" ||
424
		git config submodule."$name".update "$upd" ||
425
		die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
L
Lars Hjemli 已提交
426 427 428 429
	done
}

#
430
# Update each submodule path to correct revision, using clone and checkout as needed
L
Lars Hjemli 已提交
431 432 433
#
# $@ = requested paths (default to all)
#
434
cmd_update()
L
Lars Hjemli 已提交
435
{
436
	# parse $args after "submodule ... update".
437
	orig_flags=
438 439 440 441
	while test $# -ne 0
	do
		case "$1" in
		-q|--quiet)
442
			GIT_QUIET=1
443
			;;
444
		-i|--init)
445
			init=1
446
			;;
447 448 449
		-N|--no-fetch)
			nofetch=1
			;;
450 451 452
		-f|--force)
			force=$1
			;;
453
		-r|--rebase)
454
			update="rebase"
455
			;;
456 457 458
		--reference)
			case "$2" in '') usage ;; esac
			reference="--reference=$2"
459 460
			orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
			shift
461 462 463 464
			;;
		--reference=*)
			reference="$1"
			;;
465 466 467
		-m|--merge)
			update="merge"
			;;
468 469 470
		--recursive)
			recursive=1
			;;
471 472 473
		--checkout)
			update="checkout"
			;;
474 475 476 477 478 479 480 481 482 483 484
		--)
			shift
			break
			;;
		-*)
			usage
			;;
		*)
			break
			;;
		esac
485 486
		orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
		shift
487 488
	done

489 490 491 492 493
	if test -n "$init"
	then
		cmd_init "--" "$@" || return
	fi

494
	cloned_modules=
495 496
	module_list "$@" | {
	err=
497
	while read mode sha1 stage sm_path
L
Lars Hjemli 已提交
498
	do
499 500
		if test "$stage" = U
		then
501
			echo >&2 "Skipping unmerged submodule $sm_path"
502 503
			continue
		fi
504
		name=$(module_name "$sm_path") || exit
505
		url=$(git config submodule."$name".url)
506 507 508 509 510 511 512 513 514
		if ! test -z "$update"
		then
			update_module=$update
		else
			update_module=$(git config submodule."$name".update)
		fi

		if test "$update_module" = "none"
		then
515
			echo "Skipping submodule '$sm_path'"
516 517 518
			continue
		fi

519
		if test -z "$url"
L
Lars Hjemli 已提交
520 521 522 523
		then
			# Only mention uninitialized submodules when its
			# path have been specified
			test "$#" != "0" &&
524
			say "$(eval_gettext "Submodule path '\$sm_path' not initialized
525
Maybe you want to use 'update --init'?")"
526 527 528
			continue
		fi

529
		if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
530
		then
531
			module_clone "$sm_path" "$url" "$reference"|| exit
532
			cloned_modules="$cloned_modules;$name"
533 534
			subsha1=
		else
535
			subsha1=$(clear_local_git_env; cd "$sm_path" &&
536
				git rev-parse --verify HEAD) ||
537
			die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
L
Lars Hjemli 已提交
538
		fi
539

L
Lars Hjemli 已提交
540 541
		if test "$subsha1" != "$sha1"
		then
542 543 544
			subforce=$force
			# If we don't already have a -f flag and the submodule has never been checked out
			if test -z "$subsha1" -a -z "$force"
545
			then
546
				subforce="-f"
547
			fi
548 549 550

			if test -z "$nofetch"
			then
551 552
				# Run fetch only if $sha1 isn't present or it
				# is not reachable from a ref.
553
				(clear_local_git_env; cd "$sm_path" &&
554
					( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
555
					 test -z "$rev") || git-fetch)) ||
556
				die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
557 558
			fi

559 560 561 562 563 564 565
			# Is this something we just cloned?
			case ";$cloned_modules;" in
			*";$name;"*)
				# then there is no local change to integrate
				update_module= ;;
			esac

566
			must_die_on_failure=
567 568 569
			case "$update_module" in
			rebase)
				command="git rebase"
570 571
				die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
				say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
572
				must_die_on_failure=yes
573
				;;
574 575
			merge)
				command="git merge"
576 577
				die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
				say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
578
				must_die_on_failure=yes
579
				;;
580
			*)
581
				command="git checkout $subforce -q"
582 583
				die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
				say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
584 585
				;;
			esac
L
Lars Hjemli 已提交
586

587
			if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
588
			then
589
				say "$say_msg"
590 591
			elif test -n "$must_die_on_failure"
			then
592
				die_with_status 2 "$die_msg"
593
			else
594
				err="${err};$die_msg"
595
				continue
596
			fi
L
Lars Hjemli 已提交
597
		fi
598 599 600

		if test -n "$recursive"
		then
601
			(clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
602 603 604
			res=$?
			if test $res -gt 0
			then
605
				die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
606 607
				if test $res -eq 1
				then
608
					err="${err};$die_msg"
609 610
					continue
				else
611
					die_with_status $res "$die_msg"
612 613
				fi
			fi
614
		fi
L
Lars Hjemli 已提交
615
	done
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

	if test -n "$err"
	then
		OIFS=$IFS
		IFS=';'
		for e in $err
		do
			if test -n "$e"
			then
				echo >&2 "$e"
			fi
		done
		IFS=$OIFS
		exit 1
	fi
	}
L
Lars Hjemli 已提交
632 633
}

634 635
set_name_rev () {
	revname=$( (
636
		clear_local_git_env
637
		cd "$1" && {
638 639
			git describe "$2" 2>/dev/null ||
			git describe --tags "$2" 2>/dev/null ||
640 641
			git describe --contains "$2" 2>/dev/null ||
			git describe --all --always "$2"
642 643 644 645
		}
	) )
	test -z "$revname" || revname=" ($revname)"
}
P
Ping Yin 已提交
646 647 648 649 650 651 652 653 654
#
# Show commit summary for submodules in index or working tree
#
# If '--cached' is given, show summary between index and given commit,
# or between working tree and given commit
#
# $@ = [commit (default 'HEAD'),] requested paths (default all)
#
cmd_summary() {
655
	summary_limit=-1
656
	for_status=
657
	diff_cmd=diff-index
658

P
Ping Yin 已提交
659 660 661 662 663 664 665
	# parse $args after "submodule ... summary".
	while test $# -ne 0
	do
		case "$1" in
		--cached)
			cached="$1"
			;;
666 667 668
		--files)
			files="$1"
			;;
669 670 671
		--for-status)
			for_status="$1"
			;;
672 673 674 675 676 677 678 679 680
		-n|--summary-limit)
			if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
			then
				:
			else
				usage
			fi
			shift
			;;
P
Ping Yin 已提交
681 682 683 684 685 686 687 688 689 690 691 692 693
		--)
			shift
			break
			;;
		-*)
			usage
			;;
		*)
			break
			;;
		esac
		shift
	done
694

695 696
	test $summary_limit = 0 && return

697
	if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
P
Ping Yin 已提交
698 699
	then
		head=$rev
700
		test $# = 0 || shift
701 702
	elif test -z "$1" -o "$1" = "HEAD"
	then
703 704
		# before the first commit: compare with an empty tree
		head=$(git hash-object -w -t tree --stdin </dev/null)
705
		test -z "$1" || shift
P
Ping Yin 已提交
706
	else
707
		head="HEAD"
P
Ping Yin 已提交
708 709
	fi

710 711 712
	if [ -n "$files" ]
	then
		test -n "$cached" &&
713
		die "$(gettext -- "--cached cannot be used with --files")"
714 715 716 717
		diff_cmd=diff-files
		head=
	fi

P
Ping Yin 已提交
718 719
	cd_to_toplevel
	# Get modified modules cared by user
720
	modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
721
		sane_egrep '^:([0-7]* )?160000' |
P
Ping Yin 已提交
722 723 724 725 726 727 728 729 730
		while read mod_src mod_dst sha1_src sha1_dst status name
		do
			# Always show modules deleted or type-changed (blob<->module)
			test $status = D -o $status = T && echo "$name" && continue
			# Also show added or modified modules which are checked out
			GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
			echo "$name"
		done
	)
731

732 733
	test -z "$modules" && return

734
	git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
735
	sane_egrep '^:([0-7]* )?160000' |
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
	cut -c2- |
	while read mod_src mod_dst sha1_src sha1_dst status name
	do
		if test -z "$cached" &&
			test $sha1_dst = 0000000000000000000000000000000000000000
		then
			case "$mod_dst" in
			160000)
				sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
				;;
			100644 | 100755 | 120000)
				sha1_dst=$(git hash-object $name)
				;;
			000000)
				;; # removed
			*)
				# unexpected type
753
				eval_gettextln "unexpected mode \$mod_dst" >&2
754 755 756 757 758 759 760
				continue ;;
			esac
		fi
		missing_src=
		missing_dst=

		test $mod_src = 160000 &&
M
Miklos Vajna 已提交
761
		! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
762 763 764
		missing_src=t

		test $mod_dst = 160000 &&
M
Miklos Vajna 已提交
765
		! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
766
		missing_dst=t
767

768 769 770
		total_commits=
		case "$missing_src,$missing_dst" in
		t,)
771
			errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_src")"
772 773
			;;
		,t)
774
			errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_dst")"
775 776
			;;
		t,t)
777
			errmsg="$(eval_gettext "  Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
778 779 780 781 782 783 784 785 786 787 788 789 790 791
			;;
		*)
			errmsg=
			total_commits=$(
			if test $mod_src = 160000 -a $mod_dst = 160000
			then
				range="$sha1_src...$sha1_dst"
			elif test $mod_src = 160000
			then
				range=$sha1_src
			else
				range=$sha1_dst
			fi
			GIT_DIR="$name/.git" \
J
Jeff King 已提交
792
			git rev-list --first-parent $range -- | wc -l
793
			)
794
			total_commits=" ($(($total_commits + 0)))"
795 796 797 798 799 800 801
			;;
		esac

		sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
		sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
		if test $status = T
		then
802 803
			blob="$(gettext "blob")"
			submodule="$(gettext "submodule")"
804 805
			if test $mod_dst = 160000
			then
806
				echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
807
			else
808
				echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
809 810 811 812 813 814 815 816 817 818 819 820
			fi
		else
			echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
		fi
		if test -n "$errmsg"
		then
			# Don't give error msg for modification whose dst is not submodule
			# i.e. deleted or changed to blob
			test $mod_dst = 160000 && echo "$errmsg"
		else
			if test $mod_src = 160000 -a $mod_dst = 160000
			then
821 822
				limit=
				test $summary_limit -gt 0 && limit="-$summary_limit"
823
				GIT_DIR="$name/.git" \
824
				git log $limit --pretty='format:  %m %s' \
825 826 827 828 829 830 831 832 833 834 835 836
				--first-parent $sha1_src...$sha1_dst
			elif test $mod_dst = 160000
			then
				GIT_DIR="$name/.git" \
				git log --pretty='format:  > %s' -1 $sha1_dst
			else
				GIT_DIR="$name/.git" \
				git log --pretty='format:  < %s' -1 $sha1_src
			fi
			echo
		fi
		echo
837 838
	done |
	if test -n "$for_status"; then
839
		if [ -n "$files" ]; then
840
			gettextln "# Submodules changed but not updated:"
841
		else
842
			gettextln "# Submodule changes to be committed:"
843
		fi
844 845 846 847 848
		echo "#"
		sed -e 's|^|# |' -e 's|^# $|#|'
	else
		cat
	fi
P
Ping Yin 已提交
849
}
L
Lars Hjemli 已提交
850
#
851
# List all submodules, prefixed with:
L
Lars Hjemli 已提交
852 853 854 855 856 857 858 859
#  - submodule not initialized
#  + different revision checked out
#
# If --cached was specified the revision in the index will be printed
# instead of the currently checked out revision.
#
# $@ = requested paths (default to all)
#
860
cmd_status()
L
Lars Hjemli 已提交
861
{
862
	# parse $args after "submodule ... status".
863
	orig_flags=
864 865 866 867
	while test $# -ne 0
	do
		case "$1" in
		-q|--quiet)
868
			GIT_QUIET=1
869 870 871 872
			;;
		--cached)
			cached=1
			;;
873 874 875
		--recursive)
			recursive=1
			;;
876 877 878 879 880 881 882 883 884 885 886
		--)
			shift
			break
			;;
		-*)
			usage
			;;
		*)
			break
			;;
		esac
887
		orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
888 889 890
		shift
	done

891
	module_list "$@" |
892
	while read mode sha1 stage sm_path
L
Lars Hjemli 已提交
893
	do
894
		name=$(module_name "$sm_path") || exit
895
		url=$(git config submodule."$name".url)
896
		displaypath="$prefix$sm_path"
897 898 899 900 901
		if test "$stage" = U
		then
			say "U$sha1 $displaypath"
			continue
		fi
902
		if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
L
Lars Hjemli 已提交
903
		then
904
			say "-$sha1 $displaypath"
L
Lars Hjemli 已提交
905 906
			continue;
		fi
907 908
		set_name_rev "$sm_path" "$sha1"
		if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
L
Lars Hjemli 已提交
909
		then
910
			say " $sha1 $displaypath$revname"
L
Lars Hjemli 已提交
911 912 913
		else
			if test -z "$cached"
			then
914 915
				sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
				set_name_rev "$sm_path" "$sha1"
L
Lars Hjemli 已提交
916
			fi
917 918 919 920 921 922 923
			say "+$sha1 $displaypath$revname"
		fi

		if test -n "$recursive"
		then
			(
				prefix="$displaypath/"
924
				clear_local_git_env
925
				cd "$sm_path" &&
926
				eval cmd_status "$orig_args"
927
			) ||
928
			die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
L
Lars Hjemli 已提交
929 930 931
		fi
	done
}
932 933 934 935 936 937 938 939 940 941 942
#
# Sync remote urls for submodules
# This makes the value for remote.$remote.url match the value
# specified in .gitmodules.
#
cmd_sync()
{
	while test $# -ne 0
	do
		case "$1" in
		-q|--quiet)
943
			GIT_QUIET=1
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
			shift
			;;
		--)
			shift
			break
			;;
		-*)
			usage
			;;
		*)
			break
			;;
		esac
	done
	cd_to_toplevel
	module_list "$@" |
960
	while read mode sha1 stage sm_path
961
	do
962
		name=$(module_name "$sm_path")
963
		url=$(git config -f .gitmodules --get submodule."$name".url)
964 965 966 967 968 969 970 971

		# Possibly a url relative to parent
		case "$url" in
		./*|../*)
			url=$(resolve_relative_url "$url") || exit
			;;
		esac

972
		if git config "submodule.$name.url" >/dev/null 2>/dev/null
973
		then
974
			say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
975 976
			git config submodule."$name".url "$url"

977
			if test -e "$sm_path"/.git
978 979 980
			then
			(
				clear_local_git_env
981
				cd "$sm_path"
982 983 984 985
				remote=$(get_default_remote)
				git config remote."$remote".url "$url"
			)
			fi
986 987 988
		fi
	done
}
L
Lars Hjemli 已提交
989

990 991 992 993 994 995 996
# This loop parses the command line arguments to find the
# subcommand name to dispatch.  Parsing of the subcommand specific
# options are primarily done by the subcommand implementations.
# Subcommand specific options such as --branch and --cached are
# parsed here as well, for backward compatibility.

while test $# != 0 && test -z "$command"
L
Lars Hjemli 已提交
997 998
do
	case "$1" in
999
	add | foreach | init | update | status | summary | sync)
1000
		command=$1
L
Lars Hjemli 已提交
1001 1002
		;;
	-q|--quiet)
1003
		GIT_QUIET=1
L
Lars Hjemli 已提交
1004
		;;
1005 1006 1007 1008 1009 1010 1011 1012
	-b|--branch)
		case "$2" in
		'')
			usage
			;;
		esac
		branch="$2"; shift
		;;
L
Lars Hjemli 已提交
1013
	--cached)
P
Ping Yin 已提交
1014
		cached="$1"
L
Lars Hjemli 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
		;;
	--)
		break
		;;
	-*)
		usage
		;;
	*)
		break
		;;
	esac
	shift
done

1029 1030 1031 1032 1033 1034
# No command word defaults to "status"
test -n "$command" || command=status

# "-b branch" is accepted only by "add"
if test -n "$branch" && test "$command" != add
then
1035
	usage
1036 1037
fi

P
Ping Yin 已提交
1038 1039
# "--cached" is accepted only by "status" and "summary"
if test -n "$cached" && test "$command" != status -a "$command" != summary
1040
then
L
Lars Hjemli 已提交
1041
	usage
1042 1043 1044
fi

"cmd_$command" "$@"