mergetool--lib: simplify guess_merge_tool()
[git/git.git] / git-submodule.sh
CommitLineData
70c7ac22
LH
1#!/bin/sh
2#
ecda0723 3# git-submodules.sh: add, init, update or list git submodules
70c7ac22
LH
4#
5# Copyright (c) 2007 Lars Hjemli
6
1d5bec8b
JH
7dashless=$(basename "$0" | sed -e 's/-/ /')
8USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> <path>
64b19ffe 9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
1d5bec8b 10 or: $dashless [--quiet] init [--] [<path>...]
b13fd5c1 11 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
adc54235 12 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
15fc56a8 13 or: $dashless [--quiet] foreach [--recursive] <command>
1d5bec8b 14 or: $dashless [--quiet] sync [--] [<path>...]"
8f321a39 15OPTIONS_SPEC=
70c7ac22 16. git-sh-setup
98fcf840 17. git-parse-remote
70c7ac22
LH
18require_work_tree
19
5c08dbbd 20command=
ecda0723 21branch=
d92a3959 22reference=
70c7ac22 23cached=
1c244f6e 24files=
31ca3ac3 25nofetch=
32948425 26update=
15fc56a8 27prefix=
70c7ac22 28
f31a522a
ML
29# Resolve relative url by appending to parent's url
30resolve_relative_url ()
31{
98fcf840 32 remote=$(get_default_remote)
8e7e6f39
ML
33 remoteurl=$(git config "remote.$remote.url") ||
34 die "remote ($remote) does not have a url defined in .git/config"
f31a522a 35 url="$1"
99b120af 36 remoteurl=${remoteurl%/}
f31a522a
ML
37 while test -n "$url"
38 do
39 case "$url" in
40 ../*)
41 url="${url#../}"
42 remoteurl="${remoteurl%/*}"
43 ;;
44 ./*)
45 url="${url#./}"
46 ;;
47 *)
48 break;;
49 esac
50 done
99b120af 51 echo "$remoteurl/${url%/}"
f31a522a
ML
52}
53
a7b3269c
DA
54#
55# Get submodule info for registered submodules
56# $@ = path to limit submodule list
57#
58module_list()
59{
496917b7 60 git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
a7b3269c
DA
61}
62
941987a5
LH
63#
64# Map submodule path to submodule name
65#
66# $1 = path
67#
68module_name()
69{
537601ac 70 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
fe22e542 71 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
a5099bb4 72 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
537601ac 73 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
941987a5
LH
74 test -z "$name" &&
75 die "No submodule mapping found in .gitmodules for path '$path'"
76 echo "$name"
77}
33aa6fff
LH
78
79#
80# Clone a submodule
81#
23a485e3 82# Prior to calling, cmd_update checks that a possibly existing
ecda0723 83# path is not a git repository.
23a485e3 84# Likewise, cmd_add checks that path does not exist at all,
ecda0723
SV
85# since it is the location of a new submodule.
86#
33aa6fff
LH
87module_clone()
88{
89 path=$1
90 url=$2
d92a3959 91 reference="$3"
33aa6fff
LH
92
93 # If there already is a directory at the submodule path,
94 # expect it to be empty (since that is the default checkout
95 # action) and try to remove it.
96 # Note: if $path is a symlink to a directory the test will
97 # succeed but the rmdir will fail. We might want to fix this.
98 if test -d "$path"
99 then
100 rmdir "$path" 2>/dev/null ||
0484682e 101 die "Directory '$path' exists, but is neither empty nor a git repository"
33aa6fff
LH
102 fi
103
104 test -e "$path" &&
105 die "A file already exist at path '$path'"
106
d92a3959
MT
107 if test -n "$reference"
108 then
109 git-clone "$reference" -n "$url" "$path"
110 else
111 git-clone -n "$url" "$path"
112 fi ||
941987a5 113 die "Clone of '$url' into submodule path '$path' failed"
33aa6fff
LH
114}
115
ecda0723
SV
116#
117# Add a new submodule to the working tree, .gitmodules and the index
118#
ec05df35 119# $@ = repo path
ecda0723
SV
120#
121# optional branch is stored in global branch variable
122#
23a485e3 123cmd_add()
ecda0723 124{
5c08dbbd
JH
125 # parse $args after "submodule ... add".
126 while test $# -ne 0
127 do
128 case "$1" in
129 -b | --branch)
130 case "$2" in '') usage ;; esac
131 branch=$2
132 shift
133 ;;
134 -q|--quiet)
2e6a30ef 135 GIT_QUIET=1
5c08dbbd 136 ;;
d92a3959
MT
137 --reference)
138 case "$2" in '') usage ;; esac
139 reference="--reference=$2"
140 shift
141 ;;
142 --reference=*)
143 reference="$1"
144 shift
145 ;;
5c08dbbd
JH
146 --)
147 shift
148 break
149 ;;
150 -*)
151 usage
152 ;;
153 *)
154 break
155 ;;
156 esac
157 shift
158 done
159
ecda0723
SV
160 repo=$1
161 path=$2
162
ec05df35 163 if test -z "$repo" -o -z "$path"; then
ecda0723
SV
164 usage
165 fi
166
ec05df35
ML
167 # assure repo is absolute or relative to parent
168 case "$repo" in
169 ./*|../*)
170 # dereference source url relative to parent's url
171 realrepo=$(resolve_relative_url "$repo") || exit
172 ;;
173 *:*|/*)
174 # absolute url
175 realrepo=$repo
176 ;;
177 *)
178 die "repo URL: '$repo' must be absolute or begin with ./|../"
179 ;;
180 esac
181
db75ada5
MG
182 # normalize path:
183 # multiple //; leading ./; /./; /../; trailing /
184 path=$(printf '%s/\n' "$path" |
185 sed -e '
186 s|//*|/|g
187 s|^\(\./\)*||
188 s|/\./|/|g
189 :start
190 s|\([^/]*\)/\.\./||
191 tstart
192 s|/*$||
193 ')
5be60078 194 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
ecda0723
SV
195 die "'$path' already exists in the index"
196
d4264ca3
ML
197 # perhaps the path exists and is already a git repo, else clone it
198 if test -e "$path"
199 then
e9656473 200 if test -d "$path"/.git -o -f "$path"/.git
d4264ca3
ML
201 then
202 echo "Adding existing repo at '$path' to the index"
203 else
204 die "'$path' already exists and is not a valid git repo"
205 fi
c2f93917
ML
206
207 case "$repo" in
208 ./*|../*)
209 url=$(resolve_relative_url "$repo") || exit
210 ;;
211 *)
212 url="$repo"
213 ;;
214 esac
215 git config submodule."$path".url "$url"
d4264ca3 216 else
d4264ca3 217
d92a3959 218 module_clone "$path" "$realrepo" "$reference" || exit
ea10b60c
BJ
219 (
220 unset GIT_DIR
221 cd "$path" &&
222 # ash fails to wordsplit ${branch:+-b "$branch"...}
223 case "$branch" in
224 '') git checkout -f -q ;;
225 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
226 esac
227 ) || die "Unable to checkout submodule '$path'"
d4264ca3
ML
228 fi
229
ecda0723
SV
230 git add "$path" ||
231 die "Failed to add submodule '$path'"
232
a5099bb4
IY
233 git config -f .gitmodules submodule."$path".path "$path" &&
234 git config -f .gitmodules submodule."$path".url "$repo" &&
ecda0723
SV
235 git add .gitmodules ||
236 die "Failed to register submodule '$path'"
237}
238
19a31f9c
ML
239#
240# Execute an arbitrary command sequence in each checked out
241# submodule
242#
243# $@ = command to execute
244#
245cmd_foreach()
246{
1d5bec8b
JH
247 # parse $args after "submodule ... foreach".
248 while test $# -ne 0
249 do
250 case "$1" in
251 -q|--quiet)
252 GIT_QUIET=1
253 ;;
15fc56a8
JH
254 --recursive)
255 recursive=1
256 ;;
1d5bec8b
JH
257 -*)
258 usage
259 ;;
260 *)
261 break
262 ;;
263 esac
264 shift
265 done
266
a7b3269c 267 module_list |
19a31f9c
ML
268 while read mode sha1 stage path
269 do
270 if test -e "$path"/.git
271 then
15fc56a8 272 say "Entering '$prefix$path'"
1e7f2aad 273 name=$(module_name "$path")
15fc56a8
JH
274 (
275 prefix="$prefix$path/"
276 unset GIT_DIR
277 cd "$path" &&
278 eval "$@" &&
279 if test -n "$recursive"
280 then
281 cmd_foreach "--recursive" "$@"
282 fi
283 ) ||
19a31f9c
ML
284 die "Stopping at '$path'; script returned non-zero status."
285 fi
286 done
287}
288
70c7ac22 289#
211b7f19 290# Register submodules in .git/config
70c7ac22
LH
291#
292# $@ = requested paths (default to all)
293#
23a485e3 294cmd_init()
70c7ac22 295{
5c08dbbd
JH
296 # parse $args after "submodule ... init".
297 while test $# -ne 0
298 do
299 case "$1" in
300 -q|--quiet)
2e6a30ef 301 GIT_QUIET=1
5c08dbbd
JH
302 ;;
303 --)
304 shift
305 break
306 ;;
307 -*)
308 usage
309 ;;
310 *)
311 break
312 ;;
313 esac
314 shift
315 done
316
a7b3269c 317 module_list "$@" |
70c7ac22
LH
318 while read mode sha1 stage path
319 do
211b7f19 320 # Skip already registered paths
941987a5 321 name=$(module_name "$path") || exit
5be60078 322 url=$(git config submodule."$name".url)
211b7f19 323 test -z "$url" || continue
70c7ac22 324
a5099bb4 325 url=$(git config -f .gitmodules submodule."$name".url)
70c7ac22 326 test -z "$url" &&
941987a5 327 die "No url found for submodule path '$path' in .gitmodules"
70c7ac22 328
f31a522a
ML
329 # Possibly a url relative to parent
330 case "$url" in
331 ./*|../*)
8e7e6f39 332 url=$(resolve_relative_url "$url") || exit
f31a522a
ML
333 ;;
334 esac
335
5be60078 336 git config submodule."$name".url "$url" ||
941987a5 337 die "Failed to register url for submodule path '$path'"
70c7ac22 338
32948425
JH
339 upd="$(git config -f .gitmodules submodule."$name".update)"
340 test -z "$upd" ||
341 git config submodule."$name".update "$upd" ||
342 die "Failed to register update mode for submodule path '$path'"
ca2cedba 343
941987a5 344 say "Submodule '$name' ($url) registered for path '$path'"
70c7ac22
LH
345 done
346}
347
348#
211b7f19 349# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
350#
351# $@ = requested paths (default to all)
352#
23a485e3 353cmd_update()
70c7ac22 354{
5c08dbbd 355 # parse $args after "submodule ... update".
b13fd5c1 356 orig_args="$@"
5c08dbbd
JH
357 while test $# -ne 0
358 do
359 case "$1" in
360 -q|--quiet)
d1f63a37 361 shift
2e6a30ef 362 GIT_QUIET=1
5c08dbbd 363 ;;
be4d2c83 364 -i|--init)
d92a3959 365 init=1
be4d2c83 366 shift
be4d2c83 367 ;;
31ca3ac3
FF
368 -N|--no-fetch)
369 shift
370 nofetch=1
371 ;;
ca2cedba
PH
372 -r|--rebase)
373 shift
32948425 374 update="rebase"
ca2cedba 375 ;;
d92a3959
MT
376 --reference)
377 case "$2" in '') usage ;; esac
378 reference="--reference=$2"
379 shift 2
380 ;;
381 --reference=*)
382 reference="$1"
383 shift
384 ;;
42b49178
JH
385 -m|--merge)
386 shift
387 update="merge"
388 ;;
b13fd5c1
JH
389 --recursive)
390 shift
391 recursive=1
392 ;;
5c08dbbd
JH
393 --)
394 shift
395 break
396 ;;
397 -*)
398 usage
399 ;;
400 *)
401 break
402 ;;
403 esac
5c08dbbd
JH
404 done
405
d92a3959
MT
406 if test -n "$init"
407 then
408 cmd_init "--" "$@" || return
409 fi
410
a7b3269c 411 module_list "$@" |
70c7ac22
LH
412 while read mode sha1 stage path
413 do
941987a5 414 name=$(module_name "$path") || exit
5be60078 415 url=$(git config submodule."$name".url)
32948425 416 update_module=$(git config submodule."$name".update)
211b7f19 417 if test -z "$url"
70c7ac22
LH
418 then
419 # Only mention uninitialized submodules when its
420 # path have been specified
421 test "$#" != "0" &&
989206f5 422 say "Submodule path '$path' not initialized" &&
be4d2c83 423 say "Maybe you want to use 'update --init'?"
211b7f19
LH
424 continue
425 fi
426
ba88a1fe 427 if ! test -d "$path"/.git -o -f "$path"/.git
211b7f19 428 then
d92a3959 429 module_clone "$path" "$url" "$reference"|| exit
bf2d8246
LH
430 subsha1=
431 else
51884080 432 subsha1=$(unset GIT_DIR; cd "$path" &&
5be60078 433 git rev-parse --verify HEAD) ||
941987a5 434 die "Unable to find current revision in submodule path '$path'"
70c7ac22 435 fi
211b7f19 436
32948425 437 if ! test -z "$update"
ca2cedba 438 then
32948425 439 update_module=$update
ca2cedba
PH
440 fi
441
70c7ac22
LH
442 if test "$subsha1" != "$sha1"
443 then
b9b378a0
PY
444 force=
445 if test -z "$subsha1"
446 then
447 force="-f"
448 fi
31ca3ac3
FF
449
450 if test -z "$nofetch"
451 then
452 (unset GIT_DIR; cd "$path" &&
453 git-fetch) ||
454 die "Unable to fetch in submodule path '$path'"
455 fi
456
32948425
JH
457 case "$update_module" in
458 rebase)
459 command="git rebase"
ca2cedba
PH
460 action="rebase"
461 msg="rebased onto"
32948425 462 ;;
42b49178
JH
463 merge)
464 command="git merge"
465 action="merge"
466 msg="merged in"
467 ;;
32948425
JH
468 *)
469 command="git checkout $force -q"
ca2cedba
PH
470 action="checkout"
471 msg="checked out"
32948425
JH
472 ;;
473 esac
70c7ac22 474
ca2cedba
PH
475 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
476 die "Unable to $action '$sha1' in submodule path '$path'"
477 say "Submodule path '$path': $msg '$sha1'"
70c7ac22 478 fi
b13fd5c1
JH
479
480 if test -n "$recursive"
481 then
482 (unset GIT_DIR; cd "$path" && cmd_update $orig_args) ||
483 die "Failed to recurse into submodule path '$path'"
484 fi
70c7ac22
LH
485 done
486}
487
bffe71f4
EM
488set_name_rev () {
489 revname=$( (
51884080 490 unset GIT_DIR
bffe71f4 491 cd "$1" && {
5be60078
JH
492 git describe "$2" 2>/dev/null ||
493 git describe --tags "$2" 2>/dev/null ||
f669ac0b
ML
494 git describe --contains "$2" 2>/dev/null ||
495 git describe --all --always "$2"
bffe71f4
EM
496 }
497 ) )
498 test -z "$revname" || revname=" ($revname)"
499}
28f9af5d
PY
500#
501# Show commit summary for submodules in index or working tree
502#
503# If '--cached' is given, show summary between index and given commit,
504# or between working tree and given commit
505#
506# $@ = [commit (default 'HEAD'),] requested paths (default all)
507#
508cmd_summary() {
f2dc06a3 509 summary_limit=-1
d0f64dd4 510 for_status=
1c244f6e 511 diff_cmd=diff-index
f2dc06a3 512
28f9af5d
PY
513 # parse $args after "submodule ... summary".
514 while test $# -ne 0
515 do
516 case "$1" in
517 --cached)
518 cached="$1"
519 ;;
1c244f6e
JL
520 --files)
521 files="$1"
522 ;;
d0f64dd4
PY
523 --for-status)
524 for_status="$1"
525 ;;
f2dc06a3
PY
526 -n|--summary-limit)
527 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
528 then
529 :
530 else
531 usage
532 fi
533 shift
534 ;;
28f9af5d
PY
535 --)
536 shift
537 break
538 ;;
539 -*)
540 usage
541 ;;
542 *)
543 break
544 ;;
545 esac
546 shift
547 done
bffe71f4 548
f2dc06a3
PY
549 test $summary_limit = 0 && return
550
30353a40 551 if rev=$(git rev-parse -q --verify "$1^0")
28f9af5d
PY
552 then
553 head=$rev
554 shift
555 else
556 head=HEAD
557 fi
558
1c244f6e
JL
559 if [ -n "$files" ]
560 then
561 test -n "$cached" &&
562 die "--cached cannot be used with --files"
563 diff_cmd=diff-files
564 head=
565 fi
566
28f9af5d
PY
567 cd_to_toplevel
568 # Get modified modules cared by user
1c244f6e 569 modules=$(git $diff_cmd $cached --raw $head -- "$@" |
759ad19e 570 egrep '^:([0-7]* )?160000' |
28f9af5d
PY
571 while read mod_src mod_dst sha1_src sha1_dst status name
572 do
573 # Always show modules deleted or type-changed (blob<->module)
574 test $status = D -o $status = T && echo "$name" && continue
575 # Also show added or modified modules which are checked out
576 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
577 echo "$name"
578 done
579 )
1cb639e6 580
d0f64dd4
PY
581 test -z "$modules" && return
582
1c244f6e 583 git $diff_cmd $cached --raw $head -- $modules |
759ad19e 584 egrep '^:([0-7]* )?160000' |
1cb639e6
PY
585 cut -c2- |
586 while read mod_src mod_dst sha1_src sha1_dst status name
587 do
588 if test -z "$cached" &&
589 test $sha1_dst = 0000000000000000000000000000000000000000
590 then
591 case "$mod_dst" in
592 160000)
593 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
594 ;;
595 100644 | 100755 | 120000)
596 sha1_dst=$(git hash-object $name)
597 ;;
598 000000)
599 ;; # removed
600 *)
601 # unexpected type
602 echo >&2 "unexpected mode $mod_dst"
603 continue ;;
604 esac
605 fi
606 missing_src=
607 missing_dst=
608
609 test $mod_src = 160000 &&
30353a40 610 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1cb639e6
PY
611 missing_src=t
612
613 test $mod_dst = 160000 &&
30353a40 614 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1cb639e6 615 missing_dst=t
bffe71f4 616
1cb639e6
PY
617 total_commits=
618 case "$missing_src,$missing_dst" in
619 t,)
620 errmsg=" Warn: $name doesn't contain commit $sha1_src"
621 ;;
622 ,t)
623 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
624 ;;
625 t,t)
626 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
627 ;;
628 *)
629 errmsg=
630 total_commits=$(
631 if test $mod_src = 160000 -a $mod_dst = 160000
632 then
633 range="$sha1_src...$sha1_dst"
634 elif test $mod_src = 160000
635 then
636 range=$sha1_src
637 else
638 range=$sha1_dst
639 fi
640 GIT_DIR="$name/.git" \
641 git log --pretty=oneline --first-parent $range | wc -l
642 )
eed35595 643 total_commits=" ($(($total_commits + 0)))"
1cb639e6
PY
644 ;;
645 esac
646
647 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
648 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
649 if test $status = T
650 then
651 if test $mod_dst = 160000
652 then
653 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
654 else
655 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
656 fi
657 else
658 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
659 fi
660 if test -n "$errmsg"
661 then
662 # Don't give error msg for modification whose dst is not submodule
663 # i.e. deleted or changed to blob
664 test $mod_dst = 160000 && echo "$errmsg"
665 else
666 if test $mod_src = 160000 -a $mod_dst = 160000
667 then
f2dc06a3
PY
668 limit=
669 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e6 670 GIT_DIR="$name/.git" \
f2dc06a3 671 git log $limit --pretty='format: %m %s' \
1cb639e6
PY
672 --first-parent $sha1_src...$sha1_dst
673 elif test $mod_dst = 160000
674 then
675 GIT_DIR="$name/.git" \
676 git log --pretty='format: > %s' -1 $sha1_dst
677 else
678 GIT_DIR="$name/.git" \
679 git log --pretty='format: < %s' -1 $sha1_src
680 fi
681 echo
682 fi
683 echo
d0f64dd4
PY
684 done |
685 if test -n "$for_status"; then
686 echo "# Modified submodules:"
687 echo "#"
688 sed -e 's|^|# |' -e 's|^# $|#|'
689 else
690 cat
691 fi
28f9af5d 692}
70c7ac22 693#
941987a5 694# List all submodules, prefixed with:
70c7ac22
LH
695# - submodule not initialized
696# + different revision checked out
697#
698# If --cached was specified the revision in the index will be printed
699# instead of the currently checked out revision.
700#
701# $@ = requested paths (default to all)
702#
23a485e3 703cmd_status()
70c7ac22 704{
5c08dbbd 705 # parse $args after "submodule ... status".
64b19ffe 706 orig_args="$@"
5c08dbbd
JH
707 while test $# -ne 0
708 do
709 case "$1" in
710 -q|--quiet)
2e6a30ef 711 GIT_QUIET=1
5c08dbbd
JH
712 ;;
713 --cached)
714 cached=1
715 ;;
64b19ffe
JH
716 --recursive)
717 recursive=1
718 ;;
5c08dbbd
JH
719 --)
720 shift
721 break
722 ;;
723 -*)
724 usage
725 ;;
726 *)
727 break
728 ;;
729 esac
730 shift
731 done
732
a7b3269c 733 module_list "$@" |
70c7ac22
LH
734 while read mode sha1 stage path
735 do
941987a5 736 name=$(module_name "$path") || exit
5be60078 737 url=$(git config submodule."$name".url)
64b19ffe 738 displaypath="$prefix$path"
ba88a1fe 739 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
70c7ac22 740 then
64b19ffe 741 say "-$sha1 $displaypath"
70c7ac22
LH
742 continue;
743 fi
41c7c1bd 744 set_name_rev "$path" "$sha1"
70c7ac22
LH
745 if git diff-files --quiet -- "$path"
746 then
64b19ffe 747 say " $sha1 $displaypath$revname"
70c7ac22
LH
748 else
749 if test -z "$cached"
750 then
51884080 751 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
41c7c1bd 752 set_name_rev "$path" "$sha1"
70c7ac22 753 fi
64b19ffe
JH
754 say "+$sha1 $displaypath$revname"
755 fi
756
757 if test -n "$recursive"
758 then
759 (
760 prefix="$displaypath/"
761 unset GIT_DIR
762 cd "$path" &&
763 cmd_status $orig_args
764 ) ||
765 die "Failed to recurse into submodule path '$path'"
70c7ac22
LH
766 fi
767 done
768}
2327f61e
DA
769#
770# Sync remote urls for submodules
771# This makes the value for remote.$remote.url match the value
772# specified in .gitmodules.
773#
774cmd_sync()
775{
776 while test $# -ne 0
777 do
778 case "$1" in
779 -q|--quiet)
2e6a30ef 780 GIT_QUIET=1
2327f61e
DA
781 shift
782 ;;
783 --)
784 shift
785 break
786 ;;
787 -*)
788 usage
789 ;;
790 *)
791 break
792 ;;
793 esac
794 done
795 cd_to_toplevel
796 module_list "$@" |
797 while read mode sha1 stage path
798 do
799 name=$(module_name "$path")
800 url=$(git config -f .gitmodules --get submodule."$name".url)
baede9f8
JH
801
802 # Possibly a url relative to parent
803 case "$url" in
804 ./*|../*)
805 url=$(resolve_relative_url "$url") || exit
806 ;;
807 esac
808
2327f61e
DA
809 if test -e "$path"/.git
810 then
811 (
812 unset GIT_DIR
813 cd "$path"
814 remote=$(get_default_remote)
815 say "Synchronizing submodule url for '$name'"
816 git config remote."$remote".url "$url"
817 )
818 fi
819 done
820}
70c7ac22 821
5c08dbbd
JH
822# This loop parses the command line arguments to find the
823# subcommand name to dispatch. Parsing of the subcommand specific
824# options are primarily done by the subcommand implementations.
825# Subcommand specific options such as --branch and --cached are
826# parsed here as well, for backward compatibility.
827
828while test $# != 0 && test -z "$command"
70c7ac22
LH
829do
830 case "$1" in
2327f61e 831 add | foreach | init | update | status | summary | sync)
5c08dbbd 832 command=$1
70c7ac22
LH
833 ;;
834 -q|--quiet)
2e6a30ef 835 GIT_QUIET=1
70c7ac22 836 ;;
ecda0723
SV
837 -b|--branch)
838 case "$2" in
839 '')
840 usage
841 ;;
842 esac
843 branch="$2"; shift
844 ;;
70c7ac22 845 --cached)
28f9af5d 846 cached="$1"
70c7ac22
LH
847 ;;
848 --)
849 break
850 ;;
851 -*)
852 usage
853 ;;
854 *)
855 break
856 ;;
857 esac
858 shift
859done
860
5c08dbbd
JH
861# No command word defaults to "status"
862test -n "$command" || command=status
863
864# "-b branch" is accepted only by "add"
865if test -n "$branch" && test "$command" != add
866then
ecda0723 867 usage
5c08dbbd
JH
868fi
869
28f9af5d
PY
870# "--cached" is accepted only by "status" and "summary"
871if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 872then
70c7ac22 873 usage
5c08dbbd
JH
874fi
875
876"cmd_$command" "$@"