t4027,4041: Use test -s to test for an empty file
[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=
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
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".
b13fd5c1 363 orig_args="$@"
5c08dbbd
JH
364 while test $# -ne 0
365 do
366 case "$1" in
367 -q|--quiet)
d1f63a37 368 shift
2e6a30ef 369 GIT_QUIET=1
5c08dbbd 370 ;;
be4d2c83 371 -i|--init)
d92a3959 372 init=1
be4d2c83 373 shift
be4d2c83 374 ;;
31ca3ac3
FF
375 -N|--no-fetch)
376 shift
377 nofetch=1
378 ;;
ca2cedba
PH
379 -r|--rebase)
380 shift
32948425 381 update="rebase"
ca2cedba 382 ;;
d92a3959
MT
383 --reference)
384 case "$2" in '') usage ;; esac
385 reference="--reference=$2"
386 shift 2
387 ;;
388 --reference=*)
389 reference="$1"
390 shift
391 ;;
42b49178
JH
392 -m|--merge)
393 shift
394 update="merge"
395 ;;
b13fd5c1
JH
396 --recursive)
397 shift
398 recursive=1
399 ;;
5c08dbbd
JH
400 --)
401 shift
402 break
403 ;;
404 -*)
405 usage
406 ;;
407 *)
408 break
409 ;;
410 esac
5c08dbbd
JH
411 done
412
d92a3959
MT
413 if test -n "$init"
414 then
415 cmd_init "--" "$@" || return
416 fi
417
a7b3269c 418 module_list "$@" |
70c7ac22
LH
419 while read mode sha1 stage path
420 do
941987a5 421 name=$(module_name "$path") || exit
5be60078 422 url=$(git config submodule."$name".url)
32948425 423 update_module=$(git config submodule."$name".update)
211b7f19 424 if test -z "$url"
70c7ac22
LH
425 then
426 # Only mention uninitialized submodules when its
427 # path have been specified
428 test "$#" != "0" &&
989206f5 429 say "Submodule path '$path' not initialized" &&
be4d2c83 430 say "Maybe you want to use 'update --init'?"
211b7f19
LH
431 continue
432 fi
433
ba88a1fe 434 if ! test -d "$path"/.git -o -f "$path"/.git
211b7f19 435 then
d92a3959 436 module_clone "$path" "$url" "$reference"|| exit
bf2d8246
LH
437 subsha1=
438 else
74ae1419 439 subsha1=$(clear_local_git_env; cd "$path" &&
5be60078 440 git rev-parse --verify HEAD) ||
941987a5 441 die "Unable to find current revision in submodule path '$path'"
70c7ac22 442 fi
211b7f19 443
32948425 444 if ! test -z "$update"
ca2cedba 445 then
32948425 446 update_module=$update
ca2cedba
PH
447 fi
448
70c7ac22
LH
449 if test "$subsha1" != "$sha1"
450 then
b9b378a0
PY
451 force=
452 if test -z "$subsha1"
453 then
454 force="-f"
455 fi
31ca3ac3
FF
456
457 if test -z "$nofetch"
458 then
74ae1419 459 (clear_local_git_env; cd "$path" &&
31ca3ac3
FF
460 git-fetch) ||
461 die "Unable to fetch in submodule path '$path'"
462 fi
463
32948425
JH
464 case "$update_module" in
465 rebase)
466 command="git rebase"
ca2cedba
PH
467 action="rebase"
468 msg="rebased onto"
32948425 469 ;;
42b49178
JH
470 merge)
471 command="git merge"
472 action="merge"
473 msg="merged in"
474 ;;
32948425
JH
475 *)
476 command="git checkout $force -q"
ca2cedba
PH
477 action="checkout"
478 msg="checked out"
32948425
JH
479 ;;
480 esac
70c7ac22 481
74ae1419 482 (clear_local_git_env; cd "$path" && $command "$sha1") ||
ca2cedba
PH
483 die "Unable to $action '$sha1' in submodule path '$path'"
484 say "Submodule path '$path': $msg '$sha1'"
70c7ac22 485 fi
b13fd5c1
JH
486
487 if test -n "$recursive"
488 then
74ae1419 489 (clear_local_git_env; cd "$path" && cmd_update $orig_args) ||
b13fd5c1
JH
490 die "Failed to recurse into submodule path '$path'"
491 fi
70c7ac22
LH
492 done
493}
494
bffe71f4
EM
495set_name_rev () {
496 revname=$( (
74ae1419 497 clear_local_git_env
bffe71f4 498 cd "$1" && {
5be60078
JH
499 git describe "$2" 2>/dev/null ||
500 git describe --tags "$2" 2>/dev/null ||
f669ac0b
ML
501 git describe --contains "$2" 2>/dev/null ||
502 git describe --all --always "$2"
bffe71f4
EM
503 }
504 ) )
505 test -z "$revname" || revname=" ($revname)"
506}
28f9af5d
PY
507#
508# Show commit summary for submodules in index or working tree
509#
510# If '--cached' is given, show summary between index and given commit,
511# or between working tree and given commit
512#
513# $@ = [commit (default 'HEAD'),] requested paths (default all)
514#
515cmd_summary() {
f2dc06a3 516 summary_limit=-1
d0f64dd4 517 for_status=
1c244f6e 518 diff_cmd=diff-index
f2dc06a3 519
28f9af5d
PY
520 # parse $args after "submodule ... summary".
521 while test $# -ne 0
522 do
523 case "$1" in
524 --cached)
525 cached="$1"
526 ;;
1c244f6e
JL
527 --files)
528 files="$1"
529 ;;
d0f64dd4
PY
530 --for-status)
531 for_status="$1"
532 ;;
f2dc06a3
PY
533 -n|--summary-limit)
534 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
535 then
536 :
537 else
538 usage
539 fi
540 shift
541 ;;
28f9af5d
PY
542 --)
543 shift
544 break
545 ;;
546 -*)
547 usage
548 ;;
549 *)
550 break
551 ;;
552 esac
553 shift
554 done
bffe71f4 555
f2dc06a3
PY
556 test $summary_limit = 0 && return
557
3deea89c 558 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
28f9af5d
PY
559 then
560 head=$rev
caa9c3ca 561 test $# = 0 || shift
3deea89c
JH
562 elif test -z "$1" -o "$1" = "HEAD"
563 then
14e940d7
JH
564 # before the first commit: compare with an empty tree
565 head=$(git hash-object -w -t tree --stdin </dev/null)
2ea6c2c9 566 test -z "$1" || shift
28f9af5d 567 else
3deea89c 568 head="HEAD"
28f9af5d
PY
569 fi
570
1c244f6e
JL
571 if [ -n "$files" ]
572 then
573 test -n "$cached" &&
574 die "--cached cannot be used with --files"
575 diff_cmd=diff-files
576 head=
577 fi
578
28f9af5d
PY
579 cd_to_toplevel
580 # Get modified modules cared by user
1c244f6e 581 modules=$(git $diff_cmd $cached --raw $head -- "$@" |
e1622bfc 582 sane_egrep '^:([0-7]* )?160000' |
28f9af5d
PY
583 while read mod_src mod_dst sha1_src sha1_dst status name
584 do
585 # Always show modules deleted or type-changed (blob<->module)
586 test $status = D -o $status = T && echo "$name" && continue
587 # Also show added or modified modules which are checked out
588 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
589 echo "$name"
590 done
591 )
1cb639e6 592
d0f64dd4
PY
593 test -z "$modules" && return
594
1c244f6e 595 git $diff_cmd $cached --raw $head -- $modules |
e1622bfc 596 sane_egrep '^:([0-7]* )?160000' |
1cb639e6
PY
597 cut -c2- |
598 while read mod_src mod_dst sha1_src sha1_dst status name
599 do
600 if test -z "$cached" &&
601 test $sha1_dst = 0000000000000000000000000000000000000000
602 then
603 case "$mod_dst" in
604 160000)
605 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
606 ;;
607 100644 | 100755 | 120000)
608 sha1_dst=$(git hash-object $name)
609 ;;
610 000000)
611 ;; # removed
612 *)
613 # unexpected type
614 echo >&2 "unexpected mode $mod_dst"
615 continue ;;
616 esac
617 fi
618 missing_src=
619 missing_dst=
620
621 test $mod_src = 160000 &&
30353a40 622 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1cb639e6
PY
623 missing_src=t
624
625 test $mod_dst = 160000 &&
30353a40 626 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1cb639e6 627 missing_dst=t
bffe71f4 628
1cb639e6
PY
629 total_commits=
630 case "$missing_src,$missing_dst" in
631 t,)
632 errmsg=" Warn: $name doesn't contain commit $sha1_src"
633 ;;
634 ,t)
635 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
636 ;;
637 t,t)
638 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
639 ;;
640 *)
641 errmsg=
642 total_commits=$(
643 if test $mod_src = 160000 -a $mod_dst = 160000
644 then
645 range="$sha1_src...$sha1_dst"
646 elif test $mod_src = 160000
647 then
648 range=$sha1_src
649 else
650 range=$sha1_dst
651 fi
652 GIT_DIR="$name/.git" \
653 git log --pretty=oneline --first-parent $range | wc -l
654 )
eed35595 655 total_commits=" ($(($total_commits + 0)))"
1cb639e6
PY
656 ;;
657 esac
658
659 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
660 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
661 if test $status = T
662 then
663 if test $mod_dst = 160000
664 then
665 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
666 else
667 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
668 fi
669 else
670 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
671 fi
672 if test -n "$errmsg"
673 then
674 # Don't give error msg for modification whose dst is not submodule
675 # i.e. deleted or changed to blob
676 test $mod_dst = 160000 && echo "$errmsg"
677 else
678 if test $mod_src = 160000 -a $mod_dst = 160000
679 then
f2dc06a3
PY
680 limit=
681 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e6 682 GIT_DIR="$name/.git" \
f2dc06a3 683 git log $limit --pretty='format: %m %s' \
1cb639e6
PY
684 --first-parent $sha1_src...$sha1_dst
685 elif test $mod_dst = 160000
686 then
687 GIT_DIR="$name/.git" \
688 git log --pretty='format: > %s' -1 $sha1_dst
689 else
690 GIT_DIR="$name/.git" \
691 git log --pretty='format: < %s' -1 $sha1_src
692 fi
693 echo
694 fi
695 echo
d0f64dd4
PY
696 done |
697 if test -n "$for_status"; then
f17a5d34
JL
698 if [ -n "$files" ]; then
699 echo "# Submodules changed but not updated:"
700 else
701 echo "# Submodule changes to be committed:"
702 fi
d0f64dd4
PY
703 echo "#"
704 sed -e 's|^|# |' -e 's|^# $|#|'
705 else
706 cat
707 fi
28f9af5d 708}
70c7ac22 709#
941987a5 710# List all submodules, prefixed with:
70c7ac22
LH
711# - submodule not initialized
712# + different revision checked out
713#
714# If --cached was specified the revision in the index will be printed
715# instead of the currently checked out revision.
716#
717# $@ = requested paths (default to all)
718#
23a485e3 719cmd_status()
70c7ac22 720{
5c08dbbd 721 # parse $args after "submodule ... status".
64b19ffe 722 orig_args="$@"
5c08dbbd
JH
723 while test $# -ne 0
724 do
725 case "$1" in
726 -q|--quiet)
2e6a30ef 727 GIT_QUIET=1
5c08dbbd
JH
728 ;;
729 --cached)
730 cached=1
731 ;;
64b19ffe
JH
732 --recursive)
733 recursive=1
734 ;;
5c08dbbd
JH
735 --)
736 shift
737 break
738 ;;
739 -*)
740 usage
741 ;;
742 *)
743 break
744 ;;
745 esac
746 shift
747 done
748
a7b3269c 749 module_list "$@" |
70c7ac22
LH
750 while read mode sha1 stage path
751 do
941987a5 752 name=$(module_name "$path") || exit
5be60078 753 url=$(git config submodule."$name".url)
64b19ffe 754 displaypath="$prefix$path"
ba88a1fe 755 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
70c7ac22 756 then
64b19ffe 757 say "-$sha1 $displaypath"
70c7ac22
LH
758 continue;
759 fi
41c7c1bd 760 set_name_rev "$path" "$sha1"
70c7ac22
LH
761 if git diff-files --quiet -- "$path"
762 then
64b19ffe 763 say " $sha1 $displaypath$revname"
70c7ac22
LH
764 else
765 if test -z "$cached"
766 then
74ae1419 767 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
41c7c1bd 768 set_name_rev "$path" "$sha1"
70c7ac22 769 fi
64b19ffe
JH
770 say "+$sha1 $displaypath$revname"
771 fi
772
773 if test -n "$recursive"
774 then
775 (
776 prefix="$displaypath/"
74ae1419 777 clear_local_git_env
64b19ffe
JH
778 cd "$path" &&
779 cmd_status $orig_args
780 ) ||
781 die "Failed to recurse into submodule path '$path'"
70c7ac22
LH
782 fi
783 done
784}
2327f61e
DA
785#
786# Sync remote urls for submodules
787# This makes the value for remote.$remote.url match the value
788# specified in .gitmodules.
789#
790cmd_sync()
791{
792 while test $# -ne 0
793 do
794 case "$1" in
795 -q|--quiet)
2e6a30ef 796 GIT_QUIET=1
2327f61e
DA
797 shift
798 ;;
799 --)
800 shift
801 break
802 ;;
803 -*)
804 usage
805 ;;
806 *)
807 break
808 ;;
809 esac
810 done
811 cd_to_toplevel
812 module_list "$@" |
813 while read mode sha1 stage path
814 do
815 name=$(module_name "$path")
816 url=$(git config -f .gitmodules --get submodule."$name".url)
baede9f8
JH
817
818 # Possibly a url relative to parent
819 case "$url" in
820 ./*|../*)
821 url=$(resolve_relative_url "$url") || exit
822 ;;
823 esac
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)
831 say "Synchronizing submodule url for '$name'"
832 git config remote."$remote".url "$url"
833 )
834 fi
835 done
836}
70c7ac22 837
5c08dbbd
JH
838# This loop parses the command line arguments to find the
839# subcommand name to dispatch. Parsing of the subcommand specific
840# options are primarily done by the subcommand implementations.
841# Subcommand specific options such as --branch and --cached are
842# parsed here as well, for backward compatibility.
843
844while test $# != 0 && test -z "$command"
70c7ac22
LH
845do
846 case "$1" in
2327f61e 847 add | foreach | init | update | status | summary | sync)
5c08dbbd 848 command=$1
70c7ac22
LH
849 ;;
850 -q|--quiet)
2e6a30ef 851 GIT_QUIET=1
70c7ac22 852 ;;
ecda0723
SV
853 -b|--branch)
854 case "$2" in
855 '')
856 usage
857 ;;
858 esac
859 branch="$2"; shift
860 ;;
70c7ac22 861 --cached)
28f9af5d 862 cached="$1"
70c7ac22
LH
863 ;;
864 --)
865 break
866 ;;
867 -*)
868 usage
869 ;;
870 *)
871 break
872 ;;
873 esac
874 shift
875done
876
5c08dbbd
JH
877# No command word defaults to "status"
878test -n "$command" || command=status
879
880# "-b branch" is accepted only by "add"
881if test -n "$branch" && test "$command" != add
882then
ecda0723 883 usage
5c08dbbd
JH
884fi
885
28f9af5d
PY
886# "--cached" is accepted only by "status" and "summary"
887if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 888then
70c7ac22 889 usage
5c08dbbd
JH
890fi
891
892"cmd_$command" "$@"