submodule summary: do not shift a non-existent positional variable
[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 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=
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{
e1622bfc 60 git ls-files --error-unmatch --stage -- "$@" | sane_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
1414e578
JL
163 if test -z "$path"; then
164 path=$(echo "$repo" |
165 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
166 fi
167
ec05df35 168 if test -z "$repo" -o -z "$path"; then
ecda0723
SV
169 usage
170 fi
171
ec05df35
ML
172 # assure repo is absolute or relative to parent
173 case "$repo" in
174 ./*|../*)
175 # dereference source url relative to parent's url
176 realrepo=$(resolve_relative_url "$repo") || exit
177 ;;
178 *:*|/*)
179 # absolute url
180 realrepo=$repo
181 ;;
182 *)
183 die "repo URL: '$repo' must be absolute or begin with ./|../"
184 ;;
185 esac
186
db75ada5
MG
187 # normalize path:
188 # multiple //; leading ./; /./; /../; trailing /
189 path=$(printf '%s/\n' "$path" |
190 sed -e '
191 s|//*|/|g
192 s|^\(\./\)*||
193 s|/\./|/|g
194 :start
195 s|\([^/]*\)/\.\./||
196 tstart
197 s|/*$||
198 ')
5be60078 199 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
ecda0723
SV
200 die "'$path' already exists in the index"
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
BJ
224 (
225 unset GIT_DIR
226 cd "$path" &&
227 # ash fails to wordsplit ${branch:+-b "$branch"...}
228 case "$branch" in
229 '') git checkout -f -q ;;
230 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
231 esac
232 ) || die "Unable to checkout submodule '$path'"
d4264ca3
ML
233 fi
234
ecda0723
SV
235 git add "$path" ||
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" &&
ecda0723
SV
240 git add .gitmodules ||
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
a7b3269c 272 module_list |
19a31f9c
ML
273 while read mode sha1 stage path
274 do
275 if test -e "$path"/.git
276 then
15fc56a8 277 say "Entering '$prefix$path'"
1e7f2aad 278 name=$(module_name "$path")
15fc56a8
JH
279 (
280 prefix="$prefix$path/"
281 unset GIT_DIR
282 cd "$path" &&
283 eval "$@" &&
284 if test -n "$recursive"
285 then
286 cmd_foreach "--recursive" "$@"
287 fi
288 ) ||
19a31f9c
ML
289 die "Stopping at '$path'; script returned non-zero status."
290 fi
291 done
292}
293
70c7ac22 294#
211b7f19 295# Register submodules in .git/config
70c7ac22
LH
296#
297# $@ = requested paths (default to all)
298#
23a485e3 299cmd_init()
70c7ac22 300{
5c08dbbd
JH
301 # parse $args after "submodule ... init".
302 while test $# -ne 0
303 do
304 case "$1" in
305 -q|--quiet)
2e6a30ef 306 GIT_QUIET=1
5c08dbbd
JH
307 ;;
308 --)
309 shift
310 break
311 ;;
312 -*)
313 usage
314 ;;
315 *)
316 break
317 ;;
318 esac
319 shift
320 done
321
a7b3269c 322 module_list "$@" |
70c7ac22
LH
323 while read mode sha1 stage path
324 do
211b7f19 325 # Skip already registered paths
941987a5 326 name=$(module_name "$path") || exit
5be60078 327 url=$(git config submodule."$name".url)
211b7f19 328 test -z "$url" || continue
70c7ac22 329
a5099bb4 330 url=$(git config -f .gitmodules submodule."$name".url)
70c7ac22 331 test -z "$url" &&
941987a5 332 die "No url found for submodule path '$path' in .gitmodules"
70c7ac22 333
f31a522a
ML
334 # Possibly a url relative to parent
335 case "$url" in
336 ./*|../*)
8e7e6f39 337 url=$(resolve_relative_url "$url") || exit
f31a522a
ML
338 ;;
339 esac
340
5be60078 341 git config submodule."$name".url "$url" ||
941987a5 342 die "Failed to register url for submodule path '$path'"
70c7ac22 343
32948425
JH
344 upd="$(git config -f .gitmodules submodule."$name".update)"
345 test -z "$upd" ||
346 git config submodule."$name".update "$upd" ||
347 die "Failed to register update mode for submodule path '$path'"
ca2cedba 348
941987a5 349 say "Submodule '$name' ($url) registered for path '$path'"
70c7ac22
LH
350 done
351}
352
353#
211b7f19 354# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
355#
356# $@ = requested paths (default to all)
357#
23a485e3 358cmd_update()
70c7ac22 359{
5c08dbbd 360 # parse $args after "submodule ... update".
b13fd5c1 361 orig_args="$@"
5c08dbbd
JH
362 while test $# -ne 0
363 do
364 case "$1" in
365 -q|--quiet)
d1f63a37 366 shift
2e6a30ef 367 GIT_QUIET=1
5c08dbbd 368 ;;
be4d2c83 369 -i|--init)
d92a3959 370 init=1
be4d2c83 371 shift
be4d2c83 372 ;;
31ca3ac3
FF
373 -N|--no-fetch)
374 shift
375 nofetch=1
376 ;;
ca2cedba
PH
377 -r|--rebase)
378 shift
32948425 379 update="rebase"
ca2cedba 380 ;;
d92a3959
MT
381 --reference)
382 case "$2" in '') usage ;; esac
383 reference="--reference=$2"
384 shift 2
385 ;;
386 --reference=*)
387 reference="$1"
388 shift
389 ;;
42b49178
JH
390 -m|--merge)
391 shift
392 update="merge"
393 ;;
b13fd5c1
JH
394 --recursive)
395 shift
396 recursive=1
397 ;;
5c08dbbd
JH
398 --)
399 shift
400 break
401 ;;
402 -*)
403 usage
404 ;;
405 *)
406 break
407 ;;
408 esac
5c08dbbd
JH
409 done
410
d92a3959
MT
411 if test -n "$init"
412 then
413 cmd_init "--" "$@" || return
414 fi
415
a7b3269c 416 module_list "$@" |
70c7ac22
LH
417 while read mode sha1 stage path
418 do
941987a5 419 name=$(module_name "$path") || exit
5be60078 420 url=$(git config submodule."$name".url)
32948425 421 update_module=$(git config submodule."$name".update)
211b7f19 422 if test -z "$url"
70c7ac22
LH
423 then
424 # Only mention uninitialized submodules when its
425 # path have been specified
426 test "$#" != "0" &&
989206f5 427 say "Submodule path '$path' not initialized" &&
be4d2c83 428 say "Maybe you want to use 'update --init'?"
211b7f19
LH
429 continue
430 fi
431
ba88a1fe 432 if ! test -d "$path"/.git -o -f "$path"/.git
211b7f19 433 then
d92a3959 434 module_clone "$path" "$url" "$reference"|| exit
bf2d8246
LH
435 subsha1=
436 else
51884080 437 subsha1=$(unset GIT_DIR; cd "$path" &&
5be60078 438 git rev-parse --verify HEAD) ||
941987a5 439 die "Unable to find current revision in submodule path '$path'"
70c7ac22 440 fi
211b7f19 441
32948425 442 if ! test -z "$update"
ca2cedba 443 then
32948425 444 update_module=$update
ca2cedba
PH
445 fi
446
70c7ac22
LH
447 if test "$subsha1" != "$sha1"
448 then
b9b378a0
PY
449 force=
450 if test -z "$subsha1"
451 then
452 force="-f"
453 fi
31ca3ac3
FF
454
455 if test -z "$nofetch"
456 then
457 (unset GIT_DIR; cd "$path" &&
458 git-fetch) ||
459 die "Unable to fetch in submodule path '$path'"
460 fi
461
32948425
JH
462 case "$update_module" in
463 rebase)
464 command="git rebase"
ca2cedba
PH
465 action="rebase"
466 msg="rebased onto"
32948425 467 ;;
42b49178
JH
468 merge)
469 command="git merge"
470 action="merge"
471 msg="merged in"
472 ;;
32948425
JH
473 *)
474 command="git checkout $force -q"
ca2cedba
PH
475 action="checkout"
476 msg="checked out"
32948425
JH
477 ;;
478 esac
70c7ac22 479
ca2cedba
PH
480 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
481 die "Unable to $action '$sha1' in submodule path '$path'"
482 say "Submodule path '$path': $msg '$sha1'"
70c7ac22 483 fi
b13fd5c1
JH
484
485 if test -n "$recursive"
486 then
487 (unset GIT_DIR; cd "$path" && cmd_update $orig_args) ||
488 die "Failed to recurse into submodule path '$path'"
489 fi
70c7ac22
LH
490 done
491}
492
bffe71f4
EM
493set_name_rev () {
494 revname=$( (
51884080 495 unset GIT_DIR
bffe71f4 496 cd "$1" && {
5be60078
JH
497 git describe "$2" 2>/dev/null ||
498 git describe --tags "$2" 2>/dev/null ||
f669ac0b
ML
499 git describe --contains "$2" 2>/dev/null ||
500 git describe --all --always "$2"
bffe71f4
EM
501 }
502 ) )
503 test -z "$revname" || revname=" ($revname)"
504}
28f9af5d
PY
505#
506# Show commit summary for submodules in index or working tree
507#
508# If '--cached' is given, show summary between index and given commit,
509# or between working tree and given commit
510#
511# $@ = [commit (default 'HEAD'),] requested paths (default all)
512#
513cmd_summary() {
f2dc06a3 514 summary_limit=-1
d0f64dd4 515 for_status=
1c244f6e 516 diff_cmd=diff-index
f2dc06a3 517
28f9af5d
PY
518 # parse $args after "submodule ... summary".
519 while test $# -ne 0
520 do
521 case "$1" in
522 --cached)
523 cached="$1"
524 ;;
1c244f6e
JL
525 --files)
526 files="$1"
527 ;;
d0f64dd4
PY
528 --for-status)
529 for_status="$1"
530 ;;
f2dc06a3
PY
531 -n|--summary-limit)
532 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
533 then
534 :
535 else
536 usage
537 fi
538 shift
539 ;;
28f9af5d
PY
540 --)
541 shift
542 break
543 ;;
544 -*)
545 usage
546 ;;
547 *)
548 break
549 ;;
550 esac
551 shift
552 done
bffe71f4 553
f2dc06a3
PY
554 test $summary_limit = 0 && return
555
3deea89c 556 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
28f9af5d
PY
557 then
558 head=$rev
caa9c3ca 559 test $# = 0 || shift
3deea89c
JH
560 elif test -z "$1" -o "$1" = "HEAD"
561 then
562 return
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
1c244f6e 577 modules=$(git $diff_cmd $cached --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
1c244f6e 591 git $diff_cmd $cached --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" \
649 git log --pretty=oneline --first-parent $range | wc -l
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".
64b19ffe 718 orig_args="$@"
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
742 shift
743 done
744
a7b3269c 745 module_list "$@" |
70c7ac22
LH
746 while read mode sha1 stage path
747 do
941987a5 748 name=$(module_name "$path") || exit
5be60078 749 url=$(git config submodule."$name".url)
64b19ffe 750 displaypath="$prefix$path"
ba88a1fe 751 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
70c7ac22 752 then
64b19ffe 753 say "-$sha1 $displaypath"
70c7ac22
LH
754 continue;
755 fi
41c7c1bd 756 set_name_rev "$path" "$sha1"
70c7ac22
LH
757 if git diff-files --quiet -- "$path"
758 then
64b19ffe 759 say " $sha1 $displaypath$revname"
70c7ac22
LH
760 else
761 if test -z "$cached"
762 then
51884080 763 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
41c7c1bd 764 set_name_rev "$path" "$sha1"
70c7ac22 765 fi
64b19ffe
JH
766 say "+$sha1 $displaypath$revname"
767 fi
768
769 if test -n "$recursive"
770 then
771 (
772 prefix="$displaypath/"
773 unset GIT_DIR
774 cd "$path" &&
775 cmd_status $orig_args
776 ) ||
777 die "Failed to recurse into submodule path '$path'"
70c7ac22
LH
778 fi
779 done
780}
2327f61e
DA
781#
782# Sync remote urls for submodules
783# This makes the value for remote.$remote.url match the value
784# specified in .gitmodules.
785#
786cmd_sync()
787{
788 while test $# -ne 0
789 do
790 case "$1" in
791 -q|--quiet)
2e6a30ef 792 GIT_QUIET=1
2327f61e
DA
793 shift
794 ;;
795 --)
796 shift
797 break
798 ;;
799 -*)
800 usage
801 ;;
802 *)
803 break
804 ;;
805 esac
806 done
807 cd_to_toplevel
808 module_list "$@" |
809 while read mode sha1 stage path
810 do
811 name=$(module_name "$path")
812 url=$(git config -f .gitmodules --get submodule."$name".url)
baede9f8
JH
813
814 # Possibly a url relative to parent
815 case "$url" in
816 ./*|../*)
817 url=$(resolve_relative_url "$url") || exit
818 ;;
819 esac
820
2327f61e
DA
821 if test -e "$path"/.git
822 then
823 (
824 unset GIT_DIR
825 cd "$path"
826 remote=$(get_default_remote)
827 say "Synchronizing submodule url for '$name'"
828 git config remote."$remote".url "$url"
829 )
830 fi
831 done
832}
70c7ac22 833
5c08dbbd
JH
834# This loop parses the command line arguments to find the
835# subcommand name to dispatch. Parsing of the subcommand specific
836# options are primarily done by the subcommand implementations.
837# Subcommand specific options such as --branch and --cached are
838# parsed here as well, for backward compatibility.
839
840while test $# != 0 && test -z "$command"
70c7ac22
LH
841do
842 case "$1" in
2327f61e 843 add | foreach | init | update | status | summary | sync)
5c08dbbd 844 command=$1
70c7ac22
LH
845 ;;
846 -q|--quiet)
2e6a30ef 847 GIT_QUIET=1
70c7ac22 848 ;;
ecda0723
SV
849 -b|--branch)
850 case "$2" in
851 '')
852 usage
853 ;;
854 esac
855 branch="$2"; shift
856 ;;
70c7ac22 857 --cached)
28f9af5d 858 cached="$1"
70c7ac22
LH
859 ;;
860 --)
861 break
862 ;;
863 -*)
864 usage
865 ;;
866 *)
867 break
868 ;;
869 esac
870 shift
871done
872
5c08dbbd
JH
873# No command word defaults to "status"
874test -n "$command" || command=status
875
876# "-b branch" is accepted only by "add"
877if test -n "$branch" && test "$command" != add
878then
ecda0723 879 usage
5c08dbbd
JH
880fi
881
28f9af5d
PY
882# "--cached" is accepted only by "status" and "summary"
883if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 884then
70c7ac22 885 usage
5c08dbbd
JH
886fi
887
888"cmd_$command" "$@"