Revert "Merge branch 'wt/doc-submodule-name-path-confusion-2'"
[git/git.git] / git-submodule.sh
CommitLineData
70c7ac22
LH
1#!/bin/sh
2#
4c8a9db6 3# git-submodule.sh: add, init, update or list git submodules
70c7ac22
LH
4#
5# Copyright (c) 2007 Lars Hjemli
6
1d5bec8b 7dashless=$(basename "$0" | sed -e 's/-/ /')
681b036f 8USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
64b19ffe 9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
1d5bec8b 10 or: $dashless [--quiet] init [--] [<path>...]
cf419828 11 or: $dashless [--quiet] deinit [-f|--force] [--] <path>...
893a9764 12 or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--reference <repository>] [--recursive] [--] [<path>...]
adc54235 13 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
15fc56a8 14 or: $dashless [--quiet] foreach [--recursive] <command>
82f49f29 15 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
8f321a39 16OPTIONS_SPEC=
091a6eb0 17SUBDIRECTORY_OK=Yes
70c7ac22 18. git-sh-setup
d0ad8251 19. git-sh-i18n
98fcf840 20. git-parse-remote
70c7ac22 21require_work_tree
091a6eb0
JK
22wt_prefix=$(git rev-parse --show-prefix)
23cd_to_toplevel
70c7ac22 24
5c08dbbd 25command=
ecda0723 26branch=
d27b876b 27force=
d92a3959 28reference=
70c7ac22 29cached=
48bb3033
GP
30recursive=
31init=
1c244f6e 32files=
06b1abb5 33remote=
31ca3ac3 34nofetch=
32948425 35update=
15fc56a8 36prefix=
73b0898d 37custom_name=
275cd184 38depth=
70c7ac22 39
967b2c66
JS
40# The function takes at most 2 arguments. The first argument is the
41# URL that navigates to the submodule origin repo. When relative, this URL
42# is relative to the superproject origin URL repo. The second up_path
43# argument, if specified, is the relative path that navigates
44# from the submodule working tree to the superproject working tree.
45#
46# The output of the function is the origin URL of the submodule.
47#
48# The output will either be an absolute URL or filesystem path (if the
49# superproject origin URL is an absolute URL or filesystem path,
50# respectively) or a relative file system path (if the superproject
51# origin URL is a relative file system path).
52#
53# When the output is a relative file system path, the path is either
54# relative to the submodule working tree, if up_path is specified, or to
55# the superproject working tree otherwise.
f31a522a
ML
56resolve_relative_url ()
57{
98fcf840 58 remote=$(get_default_remote)
8e7e6f39 59 remoteurl=$(git config "remote.$remote.url") ||
4d689320 60 remoteurl=$(pwd) # the repository is its own authoritative upstream
f31a522a 61 url="$1"
99b120af 62 remoteurl=${remoteurl%/}
ea640cc6 63 sep=/
967b2c66
JS
64 up_path="$2"
65
66 case "$remoteurl" in
67 *:*|/*)
68 is_relative=
69 ;;
758615e2
JS
70 ./*|../*)
71 is_relative=t
72 ;;
967b2c66
JS
73 *)
74 is_relative=t
758615e2 75 remoteurl="./$remoteurl"
967b2c66
JS
76 ;;
77 esac
78
f31a522a
ML
79 while test -n "$url"
80 do
81 case "$url" in
82 ../*)
83 url="${url#../}"
ea640cc6
TR
84 case "$remoteurl" in
85 */*)
86 remoteurl="${remoteurl%/*}"
87 ;;
88 *:*)
89 remoteurl="${remoteurl%:*}"
90 sep=:
91 ;;
92 *)
758615e2
JS
93 if test -z "$is_relative" || test "." = "$remoteurl"
94 then
95 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
96 else
97 remoteurl=.
98 fi
ea640cc6
TR
99 ;;
100 esac
f31a522a
ML
101 ;;
102 ./*)
103 url="${url#./}"
104 ;;
105 *)
106 break;;
107 esac
108 done
758615e2
JS
109 remoteurl="$remoteurl$sep${url%/}"
110 echo "${is_relative:+${up_path}}${remoteurl#./}"
f31a522a
ML
111}
112
091a6eb0
JK
113# Resolve a path to be relative to another path. This is intended for
114# converting submodule paths when git-submodule is run in a subdirectory
115# and only handles paths where the directory separator is '/'.
116#
117# The output is the first argument as a path relative to the second argument,
118# which defaults to $wt_prefix if it is omitted.
119relative_path ()
120{
121 local target curdir result
122 target=$1
123 curdir=${2-$wt_prefix}
124 curdir=${curdir%/}
125 result=
126
127 while test -n "$curdir"
128 do
129 case "$target" in
130 "$curdir/"*)
131 target=${target#"$curdir"/}
132 break
133 ;;
134 esac
135
136 result="${result}../"
137 if test "$curdir" = "${curdir%/*}"
138 then
139 curdir=
140 else
141 curdir="${curdir%/*}"
142 fi
143 done
144
145 echo "$result$target"
146}
147
a7b3269c
DA
148#
149# Get submodule info for registered submodules
150# $@ = path to limit submodule list
151#
152module_list()
153{
091a6eb0 154 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
be9d0a3a 155 (
74671241 156 git ls-files -z --error-unmatch --stage -- "$@" ||
be9d0a3a
HV
157 echo "unmatched pathspec exists"
158 ) |
fcb06a8d 159 @@PERL@@ -e '
313ee0d6
NMC
160 my %unmerged = ();
161 my ($null_sha1) = ("0" x 40);
be9d0a3a
HV
162 my @out = ();
163 my $unmatched = 0;
74671241 164 $/ = "\0";
313ee0d6 165 while (<STDIN>) {
be9d0a3a
HV
166 if (/^unmatched pathspec/) {
167 $unmatched = 1;
168 next;
169 }
313ee0d6
NMC
170 chomp;
171 my ($mode, $sha1, $stage, $path) =
172 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
173 next unless $mode eq "160000";
174 if ($stage ne "0") {
175 if (!$unmerged{$path}++) {
be9d0a3a 176 push @out, "$mode $null_sha1 U\t$path\n";
313ee0d6
NMC
177 }
178 next;
179 }
be9d0a3a
HV
180 push @out, "$_\n";
181 }
182 if ($unmatched) {
183 print "#unmatched\n";
184 } else {
185 print for (@out);
313ee0d6
NMC
186 }
187 '
a7b3269c
DA
188}
189
be9d0a3a
HV
190die_if_unmatched ()
191{
192 if test "$1" = "#unmatched"
193 then
194 exit 1
195 fi
196}
197
88ce00c3
TK
198#
199# Print a submodule configuration setting
200#
201# $1 = submodule name
202# $2 = option name
203# $3 = default value
204#
205# Checks in the usual git-config places first (for overrides),
206# otherwise it falls back on .gitmodules. This allows you to
207# distribute project-wide defaults in .gitmodules, while still
208# customizing individual repositories if necessary. If the option is
209# not in .gitmodules either, print a default value.
210#
211get_submodule_config () {
212 name="$1"
213 option="$2"
214 default="$3"
215 value=$(git config submodule."$name"."$option")
216 if test -z "$value"
217 then
218 value=$(git config -f .gitmodules submodule."$name"."$option")
219 fi
220 printf '%s' "${value:-$default}"
221}
222
223
941987a5
LH
224#
225# Map submodule path to submodule name
226#
227# $1 = path
228#
229module_name()
230{
537601ac 231 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
64394e3a 232 sm_path="$1"
fe22e542 233 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
a5099bb4 234 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
537601ac 235 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
1e42258a 236 test -z "$name" &&
64394e3a 237 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
1e42258a 238 echo "$name"
941987a5 239}
33aa6fff
LH
240
241#
242# Clone a submodule
243#
9adfc1cf
TK
244# $1 = submodule path
245# $2 = submodule name
246# $3 = URL to clone
247# $4 = reference repository to reuse (empty for independent)
248# $5 = depth argument for shallow clones (empty for deep)
23d25e48
TK
249# $6 = (remote-tracking) starting point for the local branch (empty for HEAD)
250# $7 = local branch to create (empty for a detached HEAD, unless $6 is
251# also empty, in which case the local branch is left unchanged)
9adfc1cf 252#
23a485e3 253# Prior to calling, cmd_update checks that a possibly existing
ecda0723 254# path is not a git repository.
23a485e3 255# Likewise, cmd_add checks that path does not exist at all,
ecda0723
SV
256# since it is the location of a new submodule.
257#
33aa6fff
LH
258module_clone()
259{
64394e3a 260 sm_path=$1
73b0898d
JL
261 name=$2
262 url=$3
263 reference="$4"
275cd184 264 depth="$5"
23d25e48
TK
265 start_point="$6"
266 local_branch="$7"
7e60407f
JL
267 quiet=
268 if test -n "$GIT_QUIET"
269 then
270 quiet=-q
271 fi
33aa6fff 272
501770e1
FG
273 gitdir=
274 gitdir_base=
69c30517 275 base_name=$(dirname "$name")
33aa6fff 276
501770e1 277 gitdir=$(git rev-parse --git-dir)
69c30517
JL
278 gitdir_base="$gitdir/modules/$base_name"
279 gitdir="$gitdir/modules/$name"
501770e1
FG
280
281 if test -d "$gitdir"
d92a3959 282 then
64394e3a 283 mkdir -p "$sm_path"
501770e1 284 rm -f "$gitdir/index"
d92a3959 285 else
501770e1 286 mkdir -p "$gitdir_base"
be8779f7
DG
287 (
288 clear_local_git_env
275cd184 289 git clone $quiet ${depth:+"$depth"} -n ${reference:+"$reference"} \
be8779f7
DG
290 --separate-git-dir "$gitdir" "$url" "$sm_path"
291 ) ||
64394e3a 292 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
501770e1 293 fi
ea115a0d 294
6eafa6d0
JL
295 # We already are at the root of the work tree but cd_to_toplevel will
296 # resolve any symlinks that might be present in $PWD
297 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
15b3c82c 298 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
4dce7d9b
JS
299 # normalize Windows-style absolute paths to POSIX-style absolute paths
300 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
301 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
d75219b4
JL
302 # Remove all common leading directories after a sanity check
303 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
304 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
305 fi
306 while test "${a%%/*}" = "${b%%/*}"
307 do
308 a=${a#*/}
309 b=${b#*/}
310 done
311 # Now chop off the trailing '/'s that were added in the beginning
312 a=${a%/}
313 b=${b%/}
314
c5bc42b9
BW
315 # Turn each leading "*/" component into "../"
316 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
64394e3a 317 echo "gitdir: $rel/$a" >"$sm_path/.git"
69c30517 318
c5bc42b9 319 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
23d25e48
TK
320 (
321 clear_local_git_env
322 cd "$sm_path" &&
323 GIT_WORK_TREE=. git config core.worktree "$rel/$b" &&
324 # ash fails to wordsplit ${local_branch:+-B "$local_branch"...}
325 case "$local_branch" in
326 '') git checkout -f -q ${start_point:+"$start_point"} ;;
327 ?*) git checkout -f -q -B "$local_branch" ${start_point:+"$start_point"} ;;
328 esac
329 ) || die "$(eval_gettext "Unable to setup cloned submodule '\$sm_path'")"
33aa6fff
LH
330}
331
862ae6cd
RS
332isnumber()
333{
334 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
335}
336
ecda0723
SV
337#
338# Add a new submodule to the working tree, .gitmodules and the index
339#
ec05df35 340# $@ = repo path
ecda0723
SV
341#
342# optional branch is stored in global branch variable
343#
23a485e3 344cmd_add()
ecda0723 345{
5c08dbbd 346 # parse $args after "submodule ... add".
091a6eb0 347 reference_path=
5c08dbbd
JH
348 while test $# -ne 0
349 do
350 case "$1" in
351 -b | --branch)
352 case "$2" in '') usage ;; esac
353 branch=$2
354 shift
355 ;;
d27b876b
JL
356 -f | --force)
357 force=$1
358 ;;
5c08dbbd 359 -q|--quiet)
2e6a30ef 360 GIT_QUIET=1
5c08dbbd 361 ;;
d92a3959
MT
362 --reference)
363 case "$2" in '') usage ;; esac
091a6eb0 364 reference_path=$2
d92a3959
MT
365 shift
366 ;;
367 --reference=*)
091a6eb0 368 reference_path="${1#--reference=}"
d92a3959 369 ;;
73b0898d
JL
370 --name)
371 case "$2" in '') usage ;; esac
372 custom_name=$2
373 shift
374 ;;
275cd184
FG
375 --depth)
376 case "$2" in '') usage ;; esac
377 depth="--depth=$2"
378 shift
379 ;;
380 --depth=*)
381 depth=$1
382 ;;
5c08dbbd
JH
383 --)
384 shift
385 break
386 ;;
387 -*)
388 usage
389 ;;
390 *)
391 break
392 ;;
393 esac
394 shift
395 done
396
091a6eb0
JK
397 if test -n "$reference_path"
398 then
399 is_absolute_path "$reference_path" ||
400 reference_path="$wt_prefix$reference_path"
401
402 reference="--reference=$reference_path"
403 fi
404
ecda0723 405 repo=$1
64394e3a 406 sm_path=$2
ecda0723 407
64394e3a
RJ
408 if test -z "$sm_path"; then
409 sm_path=$(echo "$repo" |
1414e578
JL
410 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
411 fi
412
64394e3a 413 if test -z "$repo" -o -z "$sm_path"; then
ecda0723
SV
414 usage
415 fi
416
091a6eb0
JK
417 is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
418
ec05df35
ML
419 # assure repo is absolute or relative to parent
420 case "$repo" in
421 ./*|../*)
091a6eb0
JK
422 test -z "$wt_prefix" ||
423 die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
424
ec05df35
ML
425 # dereference source url relative to parent's url
426 realrepo=$(resolve_relative_url "$repo") || exit
427 ;;
428 *:*|/*)
429 # absolute url
430 realrepo=$repo
431 ;;
432 *)
497ee872 433 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
ec05df35
ML
434 ;;
435 esac
436
db75ada5
MG
437 # normalize path:
438 # multiple //; leading ./; /./; /../; trailing /
64394e3a 439 sm_path=$(printf '%s/\n' "$sm_path" |
db75ada5
MG
440 sed -e '
441 s|//*|/|g
442 s|^\(\./\)*||
443 s|/\./|/|g
444 :start
445 s|\([^/]*\)/\.\./||
446 tstart
447 s|/*$||
448 ')
64394e3a
RJ
449 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
450 die "$(eval_gettext "'\$sm_path' already exists in the index")"
ecda0723 451
64394e3a 452 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
d27b876b 453 then
6ff875c5 454 eval_gettextln "The following path is ignored by one of your .gitignore files:
64394e3a 455\$sm_path
6ff875c5 456Use -f if you really want to add it." >&2
d27b876b
JL
457 exit 1
458 fi
459
73b0898d
JL
460 if test -n "$custom_name"
461 then
462 sm_name="$custom_name"
463 else
464 sm_name="$sm_path"
465 fi
466
d4264ca3 467 # perhaps the path exists and is already a git repo, else clone it
64394e3a 468 if test -e "$sm_path"
d4264ca3 469 then
64394e3a 470 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
d4264ca3 471 then
64394e3a 472 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
d4264ca3 473 else
64394e3a 474 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
d4264ca3 475 fi
c2f93917 476
d4264ca3 477 else
4b7c286e
JL
478 if test -d ".git/modules/$sm_name"
479 then
480 if test -z "$force"
481 then
482 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
483 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
484 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
485 echo >&2 " $realrepo"
486 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
487 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
488 else
489 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
490 fi
491 fi
23d25e48
TK
492 if test -n "$branch"
493 then
494 start_point="origin/$branch"
495 local_branch="$branch"
496 else
497 start_point=""
498 local_branch=""
499 fi
500 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" "$start_point" "$local_branch" || exit
d4264ca3 501 fi
73b0898d 502 git config submodule."$sm_name".url "$realrepo"
d4264ca3 503
64394e3a
RJ
504 git add $force "$sm_path" ||
505 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
ecda0723 506
73b0898d
JL
507 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
508 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
b9289227
TK
509 if test -n "$branch"
510 then
511 git config -f .gitmodules submodule."$sm_name".branch "$branch"
512 fi &&
31991b02 513 git add --force .gitmodules ||
64394e3a 514 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
ecda0723
SV
515}
516
19a31f9c
ML
517#
518# Execute an arbitrary command sequence in each checked out
519# submodule
520#
521# $@ = command to execute
522#
523cmd_foreach()
524{
1d5bec8b
JH
525 # parse $args after "submodule ... foreach".
526 while test $# -ne 0
527 do
528 case "$1" in
529 -q|--quiet)
530 GIT_QUIET=1
531 ;;
15fc56a8
JH
532 --recursive)
533 recursive=1
534 ;;
1d5bec8b
JH
535 -*)
536 usage
537 ;;
538 *)
539 break
540 ;;
541 esac
542 shift
543 done
544
f030c96d
ÆAB
545 toplevel=$(pwd)
546
4dca1aa6
BC
547 # dup stdin so that it can be restored when running the external
548 # command in the subshell (and a recursive call to this function)
549 exec 3<&0
550
a7b3269c 551 module_list |
64394e3a 552 while read mode sha1 stage sm_path
19a31f9c 553 do
be9d0a3a 554 die_if_unmatched "$mode"
64394e3a 555 if test -e "$sm_path"/.git
19a31f9c 556 then
091a6eb0
JK
557 displaypath=$(relative_path "$sm_path")
558 say "$(eval_gettext "Entering '\$prefix\$displaypath'")"
64394e3a 559 name=$(module_name "$sm_path")
15fc56a8 560 (
64394e3a 561 prefix="$prefix$sm_path/"
74ae1419 562 clear_local_git_env
64394e3a 563 cd "$sm_path" &&
091a6eb0
JK
564 sm_path=$(relative_path "$sm_path") &&
565 # we make $path available to scripts ...
566 path=$sm_path &&
1c4fb136
AK
567 if test $# -eq 1
568 then
569 eval "$1"
570 else
571 "$@"
572 fi &&
15fc56a8
JH
573 if test -n "$recursive"
574 then
575 cmd_foreach "--recursive" "$@"
576 fi
4dca1aa6 577 ) <&3 3<&- ||
091a6eb0 578 die "$(eval_gettext "Stopping at '\$prefix\$displaypath'; script returned non-zero status.")"
19a31f9c
ML
579 fi
580 done
581}
582
70c7ac22 583#
211b7f19 584# Register submodules in .git/config
70c7ac22
LH
585#
586# $@ = requested paths (default to all)
587#
23a485e3 588cmd_init()
70c7ac22 589{
5c08dbbd
JH
590 # parse $args after "submodule ... init".
591 while test $# -ne 0
592 do
593 case "$1" in
594 -q|--quiet)
2e6a30ef 595 GIT_QUIET=1
5c08dbbd
JH
596 ;;
597 --)
598 shift
599 break
600 ;;
601 -*)
602 usage
603 ;;
604 *)
605 break
606 ;;
607 esac
608 shift
609 done
610
a7b3269c 611 module_list "$@" |
64394e3a 612 while read mode sha1 stage sm_path
70c7ac22 613 do
be9d0a3a 614 die_if_unmatched "$mode"
64394e3a 615 name=$(module_name "$sm_path") || exit
c1c259e2 616
091a6eb0
JK
617 displaypath=$(relative_path "$sm_path")
618
c1c259e2 619 # Copy url setting when it is not set yet
2cd9de3e
JL
620 if test -z "$(git config "submodule.$name.url")"
621 then
622 url=$(git config -f .gitmodules submodule."$name".url)
623 test -z "$url" &&
091a6eb0 624 die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")"
2cd9de3e
JL
625
626 # Possibly a url relative to parent
627 case "$url" in
628 ./*|../*)
629 url=$(resolve_relative_url "$url") || exit
630 ;;
631 esac
632 git config submodule."$name".url "$url" ||
091a6eb0 633 die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")"
c1c259e2 634
091a6eb0 635 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")"
2cd9de3e 636 fi
70c7ac22 637
2cd9de3e 638 # Copy "update" setting when it is not set yet
ac1fbbda
JH
639 if upd="$(git config -f .gitmodules submodule."$name".update)" &&
640 test -n "$upd" &&
641 test -z "$(git config submodule."$name".update)"
642 then
643 case "$upd" in
efa8fd7e 644 checkout | rebase | merge | none)
ac1fbbda
JH
645 ;; # known modes of updating
646 *)
647 echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'"
648 upd=none
649 ;;
650 esac
651 git config submodule."$name".update "$upd" ||
652 die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")"
653 fi
70c7ac22
LH
654 done
655}
656
cf419828
JL
657#
658# Unregister submodules from .git/config and remove their work tree
659#
660# $@ = requested paths (use '.' to deinit all submodules)
661#
662cmd_deinit()
663{
664 # parse $args after "submodule ... deinit".
665 while test $# -ne 0
666 do
667 case "$1" in
668 -f|--force)
669 force=$1
670 ;;
671 -q|--quiet)
672 GIT_QUIET=1
673 ;;
674 --)
675 shift
676 break
677 ;;
678 -*)
679 usage
680 ;;
681 *)
682 break
683 ;;
684 esac
685 shift
686 done
687
688 if test $# = 0
689 then
690 die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
691 fi
692
693 module_list "$@" |
694 while read mode sha1 stage sm_path
695 do
696 die_if_unmatched "$mode"
697 name=$(module_name "$sm_path") || exit
698
091a6eb0
JK
699 displaypath=$(relative_path "$sm_path")
700
cf419828
JL
701 # Remove the submodule work tree (unless the user already did it)
702 if test -d "$sm_path"
703 then
704 # Protect submodules containing a .git directory
705 if test -d "$sm_path/.git"
706 then
091a6eb0 707 echo >&2 "$(eval_gettext "Submodule work tree '\$displaypath' contains a .git directory")"
cf419828
JL
708 die "$(eval_gettext "(use 'rm -rf' if you really want to remove it including all of its history)")"
709 fi
710
711 if test -z "$force"
712 then
7b294bf4 713 git rm -qn "$sm_path" ||
091a6eb0 714 die "$(eval_gettext "Submodule work tree '\$displaypath' contains local modifications; use '-f' to discard them")"
cf419828 715 fi
7b294bf4 716 rm -rf "$sm_path" &&
091a6eb0
JK
717 say "$(eval_gettext "Cleared directory '\$displaypath'")" ||
718 say "$(eval_gettext "Could not remove submodule work tree '\$displaypath'")"
cf419828
JL
719 fi
720
091a6eb0 721 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$displaypath'")"
cf419828
JL
722
723 # Remove the .git/config entries (unless the user already did it)
724 if test -n "$(git config --get-regexp submodule."$name\.")"
725 then
726 # Remove the whole section so we have a clean state when
727 # the user later decides to init this submodule again
728 url=$(git config submodule."$name".url)
729 git config --remove-section submodule."$name" 2>/dev/null &&
091a6eb0 730 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$displaypath'")"
cf419828
JL
731 fi
732 done
733}
734
70c7ac22 735#
211b7f19 736# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
737#
738# $@ = requested paths (default to all)
739#
23a485e3 740cmd_update()
70c7ac22 741{
5c08dbbd
JH
742 # parse $args after "submodule ... update".
743 while test $# -ne 0
744 do
745 case "$1" in
746 -q|--quiet)
2e6a30ef 747 GIT_QUIET=1
5c08dbbd 748 ;;
be4d2c83 749 -i|--init)
d92a3959 750 init=1
be4d2c83 751 ;;
06b1abb5
TK
752 --remote)
753 remote=1
754 ;;
31ca3ac3 755 -N|--no-fetch)
31ca3ac3
FF
756 nofetch=1
757 ;;
9db31bdf
NMC
758 -f|--force)
759 force=$1
760 ;;
ca2cedba 761 -r|--rebase)
32948425 762 update="rebase"
ca2cedba 763 ;;
d92a3959
MT
764 --reference)
765 case "$2" in '') usage ;; esac
766 reference="--reference=$2"
98dbe63d 767 shift
d92a3959
MT
768 ;;
769 --reference=*)
770 reference="$1"
d92a3959 771 ;;
42b49178 772 -m|--merge)
42b49178
JH
773 update="merge"
774 ;;
b13fd5c1 775 --recursive)
b13fd5c1
JH
776 recursive=1
777 ;;
efc5fb6a
JH
778 --checkout)
779 update="checkout"
780 ;;
275cd184
FG
781 --depth)
782 case "$2" in '') usage ;; esac
783 depth="--depth=$2"
784 shift
785 ;;
786 --depth=*)
787 depth=$1
788 ;;
5c08dbbd
JH
789 --)
790 shift
791 break
792 ;;
793 -*)
794 usage
795 ;;
796 *)
797 break
798 ;;
799 esac
98dbe63d 800 shift
5c08dbbd
JH
801 done
802
d92a3959
MT
803 if test -n "$init"
804 then
805 cmd_init "--" "$@" || return
806 fi
807
1b4735d9 808 cloned_modules=
15ffb7cd
FG
809 module_list "$@" | {
810 err=
64394e3a 811 while read mode sha1 stage sm_path
70c7ac22 812 do
be9d0a3a 813 die_if_unmatched "$mode"
313ee0d6
NMC
814 if test "$stage" = U
815 then
75bf5e60 816 echo >&2 "Skipping unmerged submodule $prefix$sm_path"
313ee0d6
NMC
817 continue
818 fi
64394e3a 819 name=$(module_name "$sm_path") || exit
5be60078 820 url=$(git config submodule."$name".url)
23d25e48
TK
821 config_branch=$(get_submodule_config "$name" branch)
822 branch="${config_branch:-master}"
823 local_branch="$branch"
efc5fb6a
JH
824 if ! test -z "$update"
825 then
826 update_module=$update
827 else
828 update_module=$(git config submodule."$name".update)
a2aed08b
TK
829 if test -z "$update_module"
830 then
831 update_module="checkout"
832 fi
efc5fb6a
JH
833 fi
834
091a6eb0
JK
835 displaypath=$(relative_path "$prefix$sm_path")
836
23d25e48
TK
837 case "$update_module" in
838 none)
091a6eb0 839 echo "Skipping submodule '$displaypath'"
efc5fb6a 840 continue
23d25e48
TK
841 ;;
842 checkout)
843 local_branch=""
844 ;;
845 rebase | merge | !*)
846 ;;
847 *)
848 die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
849 esac
efc5fb6a 850
211b7f19 851 if test -z "$url"
70c7ac22
LH
852 then
853 # Only mention uninitialized submodules when its
854 # path have been specified
855 test "$#" != "0" &&
091a6eb0 856 say "$(eval_gettext "Submodule path '\$displaypath' not initialized
1c2ef66f 857Maybe you want to use 'update --init'?")"
211b7f19
LH
858 continue
859 fi
860
64394e3a 861 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
211b7f19 862 then
23d25e48
TK
863 start_point="origin/${branch}"
864 module_clone "$sm_path" "$name" "$url" "$reference" "$depth" "$start_point" "$local_branch" || exit
1b4735d9 865 cloned_modules="$cloned_modules;$name"
bf2d8246
LH
866 subsha1=
867 else
64394e3a 868 subsha1=$(clear_local_git_env; cd "$sm_path" &&
5be60078 869 git rev-parse --verify HEAD) ||
091a6eb0 870 die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
70c7ac22 871 fi
211b7f19 872
06b1abb5
TK
873 if test -n "$remote"
874 then
875 if test -z "$nofetch"
876 then
877 # Fetch remote before determining tracking $sha1
878 (clear_local_git_env; cd "$sm_path" && git-fetch) ||
879 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
880 fi
881 remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
882 sha1=$(clear_local_git_env; cd "$sm_path" &&
883 git rev-parse --verify "${remote_name}/${branch}") ||
884 die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
885 fi
886
01d47215 887 if test "$subsha1" != "$sha1" -o -n "$force"
70c7ac22 888 then
9db31bdf
NMC
889 subforce=$force
890 # If we don't already have a -f flag and the submodule has never been checked out
891 if test -z "$subsha1" -a -z "$force"
b9b378a0 892 then
9db31bdf 893 subforce="-f"
b9b378a0 894 fi
31ca3ac3
FF
895
896 if test -z "$nofetch"
897 then
e5f522d6
JL
898 # Run fetch only if $sha1 isn't present or it
899 # is not reachable from a ref.
64394e3a 900 (clear_local_git_env; cd "$sm_path" &&
f5799e05 901 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
e5f522d6 902 test -z "$rev") || git-fetch)) ||
091a6eb0 903 die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
31ca3ac3
FF
904 fi
905
1b4735d9
SO
906 # Is this something we just cloned?
907 case ";$cloned_modules;" in
908 *";$name;"*)
909 # then there is no local change to integrate
23d25e48 910 update_module='!git reset --hard -q'
1b4735d9
SO
911 esac
912
877449c1 913 must_die_on_failure=
32948425 914 case "$update_module" in
a2aed08b
TK
915 checkout)
916 command="git checkout $subforce -q"
917 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
918 say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
919 ;;
32948425
JH
920 rebase)
921 command="git rebase"
091a6eb0
JK
922 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
923 say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
877449c1 924 must_die_on_failure=yes
32948425 925 ;;
42b49178
JH
926 merge)
927 command="git merge"
091a6eb0
JK
928 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
929 say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
877449c1 930 must_die_on_failure=yes
42b49178 931 ;;
6cb5728c
CP
932 !*)
933 command="${update_module#!}"
934 die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$prefix\$sm_path'")"
935 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': '\$command \$sha1'")"
936 must_die_on_failure=yes
937 ;;
32948425 938 *)
a2aed08b 939 die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
32948425 940 esac
70c7ac22 941
64394e3a 942 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
15ffb7cd 943 then
ff968f03 944 say "$say_msg"
877449c1
JH
945 elif test -n "$must_die_on_failure"
946 then
ff968f03 947 die_with_status 2 "$die_msg"
15ffb7cd 948 else
ff968f03 949 err="${err};$die_msg"
877449c1 950 continue
15ffb7cd 951 fi
70c7ac22 952 fi
b13fd5c1
JH
953
954 if test -n "$recursive"
955 then
75bf5e60
WE
956 (
957 prefix="$prefix$sm_path/"
958 clear_local_git_env
959 cd "$sm_path" &&
36141282 960 eval cmd_update
75bf5e60 961 )
15ffb7cd
FG
962 res=$?
963 if test $res -gt 0
964 then
091a6eb0 965 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
15ffb7cd
FG
966 if test $res -eq 1
967 then
ff968f03 968 err="${err};$die_msg"
15ffb7cd
FG
969 continue
970 else
ff968f03 971 die_with_status $res "$die_msg"
15ffb7cd
FG
972 fi
973 fi
b13fd5c1 974 fi
70c7ac22 975 done
15ffb7cd
FG
976
977 if test -n "$err"
978 then
979 OIFS=$IFS
980 IFS=';'
981 for e in $err
982 do
983 if test -n "$e"
984 then
985 echo >&2 "$e"
986 fi
987 done
988 IFS=$OIFS
989 exit 1
990 fi
991 }
70c7ac22
LH
992}
993
bffe71f4
EM
994set_name_rev () {
995 revname=$( (
74ae1419 996 clear_local_git_env
bffe71f4 997 cd "$1" && {
5be60078
JH
998 git describe "$2" 2>/dev/null ||
999 git describe --tags "$2" 2>/dev/null ||
f669ac0b
ML
1000 git describe --contains "$2" 2>/dev/null ||
1001 git describe --all --always "$2"
bffe71f4
EM
1002 }
1003 ) )
1004 test -z "$revname" || revname=" ($revname)"
1005}
28f9af5d
PY
1006#
1007# Show commit summary for submodules in index or working tree
1008#
1009# If '--cached' is given, show summary between index and given commit,
1010# or between working tree and given commit
1011#
1012# $@ = [commit (default 'HEAD'),] requested paths (default all)
1013#
1014cmd_summary() {
f2dc06a3 1015 summary_limit=-1
d0f64dd4 1016 for_status=
1c244f6e 1017 diff_cmd=diff-index
f2dc06a3 1018
28f9af5d
PY
1019 # parse $args after "submodule ... summary".
1020 while test $# -ne 0
1021 do
1022 case "$1" in
1023 --cached)
1024 cached="$1"
1025 ;;
1c244f6e
JL
1026 --files)
1027 files="$1"
1028 ;;
d0f64dd4
PY
1029 --for-status)
1030 for_status="$1"
1031 ;;
f2dc06a3 1032 -n|--summary-limit)
862ae6cd
RS
1033 summary_limit="$2"
1034 isnumber "$summary_limit" || usage
f2dc06a3
PY
1035 shift
1036 ;;
862ae6cd
RS
1037 --summary-limit=*)
1038 summary_limit="${1#--summary-limit=}"
1039 isnumber "$summary_limit" || usage
1040 ;;
28f9af5d
PY
1041 --)
1042 shift
1043 break
1044 ;;
1045 -*)
1046 usage
1047 ;;
1048 *)
1049 break
1050 ;;
1051 esac
1052 shift
1053 done
bffe71f4 1054
f2dc06a3
PY
1055 test $summary_limit = 0 && return
1056
3deea89c 1057 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
28f9af5d
PY
1058 then
1059 head=$rev
caa9c3ca 1060 test $# = 0 || shift
3deea89c
JH
1061 elif test -z "$1" -o "$1" = "HEAD"
1062 then
14e940d7
JH
1063 # before the first commit: compare with an empty tree
1064 head=$(git hash-object -w -t tree --stdin </dev/null)
2ea6c2c9 1065 test -z "$1" || shift
28f9af5d 1066 else
3deea89c 1067 head="HEAD"
28f9af5d
PY
1068 fi
1069
1c244f6e
JL
1070 if [ -n "$files" ]
1071 then
1072 test -n "$cached" &&
465d6a00 1073 die "$(gettext "The --cached option cannot be used with the --files option")"
1c244f6e
JL
1074 diff_cmd=diff-files
1075 head=
1076 fi
1077
28f9af5d 1078 cd_to_toplevel
091a6eb0 1079 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
28f9af5d 1080 # Get modified modules cared by user
18076502 1081 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
e1622bfc 1082 sane_egrep '^:([0-7]* )?160000' |
2be94509 1083 while read mod_src mod_dst sha1_src sha1_dst status sm_path
28f9af5d
PY
1084 do
1085 # Always show modules deleted or type-changed (blob<->module)
2be94509 1086 test $status = D -o $status = T && echo "$sm_path" && continue
927b26f8 1087 # Respect the ignore setting for --for-status.
1088 if test -n "$for_status"
1089 then
1090 name=$(module_name "$sm_path")
1091 ignore_config=$(get_submodule_config "$name" ignore none)
1092 test $status != A -a $ignore_config = all && continue
1093 fi
28f9af5d 1094 # Also show added or modified modules which are checked out
2be94509 1095 GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
1096 echo "$sm_path"
28f9af5d
PY
1097 done
1098 )
1cb639e6 1099
d0f64dd4
PY
1100 test -z "$modules" && return
1101
18076502 1102 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
e1622bfc 1103 sane_egrep '^:([0-7]* )?160000' |
1cb639e6
PY
1104 cut -c2- |
1105 while read mod_src mod_dst sha1_src sha1_dst status name
1106 do
1107 if test -z "$cached" &&
1108 test $sha1_dst = 0000000000000000000000000000000000000000
1109 then
1110 case "$mod_dst" in
1111 160000)
1112 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
1113 ;;
1114 100644 | 100755 | 120000)
1115 sha1_dst=$(git hash-object $name)
1116 ;;
1117 000000)
1118 ;; # removed
1119 *)
1120 # unexpected type
6ff875c5 1121 eval_gettextln "unexpected mode \$mod_dst" >&2
1cb639e6
PY
1122 continue ;;
1123 esac
1124 fi
1125 missing_src=
1126 missing_dst=
1127
1128 test $mod_src = 160000 &&
30353a40 1129 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1cb639e6
PY
1130 missing_src=t
1131
1132 test $mod_dst = 160000 &&
30353a40 1133 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1cb639e6 1134 missing_dst=t
bffe71f4 1135
091a6eb0
JK
1136 display_name=$(relative_path "$name")
1137
1cb639e6
PY
1138 total_commits=
1139 case "$missing_src,$missing_dst" in
1140 t,)
091a6eb0 1141 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_src")"
1cb639e6
PY
1142 ;;
1143 ,t)
091a6eb0 1144 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_dst")"
1cb639e6
PY
1145 ;;
1146 t,t)
091a6eb0 1147 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
1cb639e6
PY
1148 ;;
1149 *)
1150 errmsg=
1151 total_commits=$(
1152 if test $mod_src = 160000 -a $mod_dst = 160000
1153 then
1154 range="$sha1_src...$sha1_dst"
1155 elif test $mod_src = 160000
1156 then
1157 range=$sha1_src
1158 else
1159 range=$sha1_dst
1160 fi
1161 GIT_DIR="$name/.git" \
b0e621ad 1162 git rev-list --first-parent $range -- | wc -l
1cb639e6 1163 )
eed35595 1164 total_commits=" ($(($total_commits + 0)))"
1cb639e6
PY
1165 ;;
1166 esac
1167
1168 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
1169 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
1170 if test $status = T
1171 then
b3e73449
ÆAB
1172 blob="$(gettext "blob")"
1173 submodule="$(gettext "submodule")"
1cb639e6
PY
1174 if test $mod_dst = 160000
1175 then
091a6eb0 1176 echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1cb639e6 1177 else
091a6eb0 1178 echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1cb639e6
PY
1179 fi
1180 else
091a6eb0 1181 echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1cb639e6
PY
1182 fi
1183 if test -n "$errmsg"
1184 then
1185 # Don't give error msg for modification whose dst is not submodule
1186 # i.e. deleted or changed to blob
1187 test $mod_dst = 160000 && echo "$errmsg"
1188 else
1189 if test $mod_src = 160000 -a $mod_dst = 160000
1190 then
f2dc06a3
PY
1191 limit=
1192 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e6 1193 GIT_DIR="$name/.git" \
f2dc06a3 1194 git log $limit --pretty='format: %m %s' \
1cb639e6
PY
1195 --first-parent $sha1_src...$sha1_dst
1196 elif test $mod_dst = 160000
1197 then
1198 GIT_DIR="$name/.git" \
1199 git log --pretty='format: > %s' -1 $sha1_dst
1200 else
1201 GIT_DIR="$name/.git" \
1202 git log --pretty='format: < %s' -1 $sha1_src
1203 fi
1204 echo
1205 fi
1206 echo
3ba7407b 1207 done
28f9af5d 1208}
70c7ac22 1209#
941987a5 1210# List all submodules, prefixed with:
70c7ac22
LH
1211# - submodule not initialized
1212# + different revision checked out
1213#
1214# If --cached was specified the revision in the index will be printed
1215# instead of the currently checked out revision.
1216#
1217# $@ = requested paths (default to all)
1218#
23a485e3 1219cmd_status()
70c7ac22 1220{
5c08dbbd
JH
1221 # parse $args after "submodule ... status".
1222 while test $# -ne 0
1223 do
1224 case "$1" in
1225 -q|--quiet)
2e6a30ef 1226 GIT_QUIET=1
5c08dbbd
JH
1227 ;;
1228 --cached)
1229 cached=1
1230 ;;
64b19ffe
JH
1231 --recursive)
1232 recursive=1
1233 ;;
5c08dbbd
JH
1234 --)
1235 shift
1236 break
1237 ;;
1238 -*)
1239 usage
1240 ;;
1241 *)
1242 break
1243 ;;
1244 esac
1245 shift
1246 done
1247
a7b3269c 1248 module_list "$@" |
64394e3a 1249 while read mode sha1 stage sm_path
70c7ac22 1250 do
be9d0a3a 1251 die_if_unmatched "$mode"
64394e3a 1252 name=$(module_name "$sm_path") || exit
5be60078 1253 url=$(git config submodule."$name".url)
091a6eb0 1254 displaypath=$(relative_path "$prefix$sm_path")
313ee0d6
NMC
1255 if test "$stage" = U
1256 then
1257 say "U$sha1 $displaypath"
1258 continue
1259 fi
64394e3a 1260 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
70c7ac22 1261 then
64b19ffe 1262 say "-$sha1 $displaypath"
70c7ac22
LH
1263 continue;
1264 fi
64394e3a 1265 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
70c7ac22 1266 then
b545cd15 1267 set_name_rev "$sm_path" "$sha1"
64b19ffe 1268 say " $sha1 $displaypath$revname"
70c7ac22
LH
1269 else
1270 if test -z "$cached"
1271 then
64394e3a 1272 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
70c7ac22 1273 fi
b545cd15 1274 set_name_rev "$sm_path" "$sha1"
64b19ffe
JH
1275 say "+$sha1 $displaypath$revname"
1276 fi
1277
1278 if test -n "$recursive"
1279 then
1280 (
1281 prefix="$displaypath/"
74ae1419 1282 clear_local_git_env
64394e3a 1283 cd "$sm_path" &&
e15bec0e 1284 eval cmd_status
64b19ffe 1285 ) ||
64394e3a 1286 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
70c7ac22
LH
1287 fi
1288 done
1289}
2327f61e
DA
1290#
1291# Sync remote urls for submodules
1292# This makes the value for remote.$remote.url match the value
1293# specified in .gitmodules.
1294#
1295cmd_sync()
1296{
1297 while test $# -ne 0
1298 do
1299 case "$1" in
1300 -q|--quiet)
2e6a30ef 1301 GIT_QUIET=1
2327f61e
DA
1302 shift
1303 ;;
82f49f29
PH
1304 --recursive)
1305 recursive=1
1306 shift
1307 ;;
2327f61e
DA
1308 --)
1309 shift
1310 break
1311 ;;
1312 -*)
1313 usage
1314 ;;
1315 *)
1316 break
1317 ;;
1318 esac
1319 done
1320 cd_to_toplevel
1321 module_list "$@" |
64394e3a 1322 while read mode sha1 stage sm_path
2327f61e 1323 do
be9d0a3a 1324 die_if_unmatched "$mode"
64394e3a 1325 name=$(module_name "$sm_path")
2327f61e 1326 url=$(git config -f .gitmodules --get submodule."$name".url)
baede9f8
JH
1327
1328 # Possibly a url relative to parent
1329 case "$url" in
1330 ./*|../*)
967b2c66
JS
1331 # rewrite foo/bar as ../.. to find path from
1332 # submodule work tree to superproject work tree
1333 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1334 # guarantee a trailing /
1335 up_path=${up_path%/}/ &&
1336 # path from submodule work tree to submodule origin repo
1337 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1338 # path from superproject work tree to submodule origin repo
1339 super_config_url=$(resolve_relative_url "$url") || exit
1340 ;;
1341 *)
1342 sub_origin_url="$url"
1343 super_config_url="$url"
baede9f8
JH
1344 ;;
1345 esac
1346
ccee6086 1347 if git config "submodule.$name.url" >/dev/null 2>/dev/null
2327f61e 1348 then
091a6eb0
JK
1349 displaypath=$(relative_path "$prefix$sm_path")
1350 say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
967b2c66 1351 git config submodule."$name".url "$super_config_url"
ccee6086 1352
64394e3a 1353 if test -e "$sm_path"/.git
ccee6086
JH
1354 then
1355 (
1356 clear_local_git_env
64394e3a 1357 cd "$sm_path"
ccee6086 1358 remote=$(get_default_remote)
967b2c66 1359 git config remote."$remote".url "$sub_origin_url"
82f49f29
PH
1360
1361 if test -n "$recursive"
1362 then
1363 prefix="$prefix$sm_path/"
1364 eval cmd_sync
1365 fi
ccee6086
JH
1366 )
1367 fi
2327f61e
DA
1368 fi
1369 done
1370}
70c7ac22 1371
5c08dbbd
JH
1372# This loop parses the command line arguments to find the
1373# subcommand name to dispatch. Parsing of the subcommand specific
1374# options are primarily done by the subcommand implementations.
1375# Subcommand specific options such as --branch and --cached are
1376# parsed here as well, for backward compatibility.
1377
1378while test $# != 0 && test -z "$command"
70c7ac22
LH
1379do
1380 case "$1" in
cf419828 1381 add | foreach | init | deinit | update | status | summary | sync)
5c08dbbd 1382 command=$1
70c7ac22
LH
1383 ;;
1384 -q|--quiet)
2e6a30ef 1385 GIT_QUIET=1
70c7ac22 1386 ;;
ecda0723
SV
1387 -b|--branch)
1388 case "$2" in
1389 '')
1390 usage
1391 ;;
1392 esac
1393 branch="$2"; shift
1394 ;;
70c7ac22 1395 --cached)
28f9af5d 1396 cached="$1"
70c7ac22
LH
1397 ;;
1398 --)
1399 break
1400 ;;
1401 -*)
1402 usage
1403 ;;
1404 *)
1405 break
1406 ;;
1407 esac
1408 shift
1409done
1410
5c08dbbd 1411# No command word defaults to "status"
af9c9f97
RR
1412if test -z "$command"
1413then
1414 if test $# = 0
1415 then
1416 command=status
1417 else
1418 usage
1419 fi
1420fi
5c08dbbd
JH
1421
1422# "-b branch" is accepted only by "add"
1423if test -n "$branch" && test "$command" != add
1424then
ecda0723 1425 usage
5c08dbbd
JH
1426fi
1427
28f9af5d
PY
1428# "--cached" is accepted only by "status" and "summary"
1429if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 1430then
70c7ac22 1431 usage
5c08dbbd
JH
1432fi
1433
1434"cmd_$command" "$@"