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