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