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