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