submodule update: Add --commit option
[git.git] / git-submodule.sh
1 #!/bin/sh
2 #
3 # git-submodule.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] [--commit] [--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 commit=
25 reference=
26 cached=
27 recursive=
28 init=
29 files=
30 nofetch=
31 update=
32 prefix=
33
34 # The function takes at most 2 arguments. The first argument is the
35 # URL that navigates to the submodule origin repo. When relative, this URL
36 # is relative to the superproject origin URL repo. The second up_path
37 # argument, if specified, is the relative path that navigates
38 # from the submodule working tree to the superproject working tree.
39 #
40 # The output of the function is the origin URL of the submodule.
41 #
42 # The output will either be an absolute URL or filesystem path (if the
43 # superproject origin URL is an absolute URL or filesystem path,
44 # respectively) or a relative file system path (if the superproject
45 # origin URL is a relative file system path).
46 #
47 # When the output is a relative file system path, the path is either
48 # relative to the submodule working tree, if up_path is specified, or to
49 # the superproject working tree otherwise.
50 resolve_relative_url ()
51 {
52         remote=$(get_default_remote)
53         remoteurl=$(git config "remote.$remote.url") ||
54                 remoteurl=$(pwd) # the repository is its own authoritative upstream
55         url="$1"
56         remoteurl=${remoteurl%/}
57         sep=/
58         up_path="$2"
59
60         case "$remoteurl" in
61         *:*|/*)
62                 is_relative=
63                 ;;
64         ./*|../*)
65                 is_relative=t
66                 ;;
67         *)
68                 is_relative=t
69                 remoteurl="./$remoteurl"
70                 ;;
71         esac
72
73         while test -n "$url"
74         do
75                 case "$url" in
76                 ../*)
77                         url="${url#../}"
78                         case "$remoteurl" in
79                         */*)
80                                 remoteurl="${remoteurl%/*}"
81                                 ;;
82                         *:*)
83                                 remoteurl="${remoteurl%:*}"
84                                 sep=:
85                                 ;;
86                         *)
87                                 if test -z "$is_relative" || test "." = "$remoteurl"
88                                 then
89                                         die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
90                                 else
91                                         remoteurl=.
92                                 fi
93                                 ;;
94                         esac
95                         ;;
96                 ./*)
97                         url="${url#./}"
98                         ;;
99                 *)
100                         break;;
101                 esac
102         done
103         remoteurl="$remoteurl$sep${url%/}"
104         echo "${is_relative:+${up_path}}${remoteurl#./}"
105 }
106
107 #
108 # Get submodule info for registered submodules
109 # $@ = path to limit submodule list
110 #
111 module_list()
112 {
113         (
114                 git ls-files --error-unmatch --stage -- "$@" ||
115                 echo "unmatched pathspec exists"
116         ) |
117         perl -e '
118         my %unmerged = ();
119         my ($null_sha1) = ("0" x 40);
120         my @out = ();
121         my $unmatched = 0;
122         while (<STDIN>) {
123                 if (/^unmatched pathspec/) {
124                         $unmatched = 1;
125                         next;
126                 }
127                 chomp;
128                 my ($mode, $sha1, $stage, $path) =
129                         /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
130                 next unless $mode eq "160000";
131                 if ($stage ne "0") {
132                         if (!$unmerged{$path}++) {
133                                 push @out, "$mode $null_sha1 U\t$path\n";
134                         }
135                         next;
136                 }
137                 push @out, "$_\n";
138         }
139         if ($unmatched) {
140                 print "#unmatched\n";
141         } else {
142                 print for (@out);
143         }
144         '
145 }
146
147 die_if_unmatched ()
148 {
149         if test "$1" = "#unmatched"
150         then
151                 exit 1
152         fi
153 }
154
155 #
156 # Map submodule path to submodule name
157 #
158 # $1 = path
159 #
160 module_name()
161 {
162         # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
163         sm_path="$1"
164         re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
165         name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
166                 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
167         test -z "$name" &&
168         die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
169         echo "$name"
170 }
171
172 #
173 # Clone a submodule
174 #
175 # Prior to calling, cmd_update checks that a possibly existing
176 # path is not a git repository.
177 # Likewise, cmd_add checks that path does not exist at all,
178 # since it is the location of a new submodule.
179 #
180 module_clone()
181 {
182         sm_path=$1
183         url=$2
184         reference="$3"
185         quiet=
186         if test -n "$GIT_QUIET"
187         then
188                 quiet=-q
189         fi
190
191         gitdir=
192         gitdir_base=
193         name=$(module_name "$sm_path" 2>/dev/null)
194         test -n "$name" || name="$sm_path"
195         base_name=$(dirname "$name")
196
197         gitdir=$(git rev-parse --git-dir)
198         gitdir_base="$gitdir/modules/$base_name"
199         gitdir="$gitdir/modules/$name"
200
201         if test -d "$gitdir"
202         then
203                 mkdir -p "$sm_path"
204                 rm -f "$gitdir/index"
205         else
206                 mkdir -p "$gitdir_base"
207                 (
208                         clear_local_git_env
209                         git clone $quiet -n ${reference:+"$reference"} \
210                                 --separate-git-dir "$gitdir" "$url" "$sm_path"
211                 ) ||
212                 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
213         fi
214
215         # We already are at the root of the work tree but cd_to_toplevel will
216         # resolve any symlinks that might be present in $PWD
217         a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
218         b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
219         # normalize Windows-style absolute paths to POSIX-style absolute paths
220         case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
221         case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
222         # Remove all common leading directories after a sanity check
223         if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
224                 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
225         fi
226         while test "${a%%/*}" = "${b%%/*}"
227         do
228                 a=${a#*/}
229                 b=${b#*/}
230         done
231         # Now chop off the trailing '/'s that were added in the beginning
232         a=${a%/}
233         b=${b%/}
234
235         # Turn each leading "*/" component into "../"
236         rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
237         echo "gitdir: $rel/$a" >"$sm_path/.git"
238
239         rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
240         (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
241 }
242
243 #
244 # Commit changed submodule gitlinks
245 #
246 # $1 = name-a;sha1-a;subsha1-a\n[name-b;sha1-b;subsha1-b\n...]
247 #
248 commit_changes()
249 {
250         echo "commiting $1"
251         OIFS="$IFS"
252         IFS=";"
253         paths=$(echo "$1" |
254                 while read name sm_path sha1 subsha1
255                 do
256                         echo "$sm_path"
257                 done
258                 )
259         names=$(echo "$1" |
260                 while read name sm_path sha1 subsha1
261                 do
262                         printf ' %s' "$name"
263                 done
264                 )
265         summary="$(eval_gettext "Updated submodules:")$names"
266         body=$(echo "$1" |
267                 while read name sm_path sha1 subsha1
268                 do
269                         if test "$name" = "$sm_path"
270                         then
271                                 printf 'Changes to %s:\n\n' "$name"
272                         else
273                                 printf 'Changes to %s (%s):\n\n' "$name" "$sm_path"
274                         fi
275                         (
276                                 clear_local_git_env
277                                 cd "$sm_path" &&
278                                 git shortlog "${sha1}..${subsha1}" ||
279                                 die "$(eval_gettext "Unable to generate shortlog in submodule path '\$sm_path'")"
280                         )
281                 done
282                 )
283         IFS="$OIFS"
284         message="$(printf '%s\n\n%s\n' "$summary" "$body")"
285         echo "message: [$message]"
286         git commit -m "$message" $paths
287 }
288
289 #
290 # Add a new submodule to the working tree, .gitmodules and the index
291 #
292 # $@ = repo path
293 #
294 # optional branch is stored in global branch variable
295 #
296 cmd_add()
297 {
298         # parse $args after "submodule ... add".
299         while test $# -ne 0
300         do
301                 case "$1" in
302                 -b | --branch)
303                         case "$2" in '') usage ;; esac
304                         branch=$2
305                         shift
306                         ;;
307                 -f | --force)
308                         force=$1
309                         ;;
310                 -q|--quiet)
311                         GIT_QUIET=1
312                         ;;
313                 --reference)
314                         case "$2" in '') usage ;; esac
315                         reference="--reference=$2"
316                         shift
317                         ;;
318                 --reference=*)
319                         reference="$1"
320                         shift
321                         ;;
322                 --)
323                         shift
324                         break
325                         ;;
326                 -*)
327                         usage
328                         ;;
329                 *)
330                         break
331                         ;;
332                 esac
333                 shift
334         done
335
336         repo=$1
337         sm_path=$2
338
339         if test -z "$sm_path"; then
340                 sm_path=$(echo "$repo" |
341                         sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
342         fi
343
344         if test -z "$repo" -o -z "$sm_path"; then
345                 usage
346         fi
347
348         # assure repo is absolute or relative to parent
349         case "$repo" in
350         ./*|../*)
351                 # dereference source url relative to parent's url
352                 realrepo=$(resolve_relative_url "$repo") || exit
353                 ;;
354         *:*|/*)
355                 # absolute url
356                 realrepo=$repo
357                 ;;
358         *)
359                 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
360         ;;
361         esac
362
363         # normalize path:
364         # multiple //; leading ./; /./; /../; trailing /
365         sm_path=$(printf '%s/\n' "$sm_path" |
366                 sed -e '
367                         s|//*|/|g
368                         s|^\(\./\)*||
369                         s|/\./|/|g
370                         :start
371                         s|\([^/]*\)/\.\./||
372                         tstart
373                         s|/*$||
374                 ')
375         git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
376         die "$(eval_gettext "'\$sm_path' already exists in the index")"
377
378         if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
379         then
380                 eval_gettextln "The following path is ignored by one of your .gitignore files:
381 \$sm_path
382 Use -f if you really want to add it." >&2
383                 exit 1
384         fi
385
386         # perhaps the path exists and is already a git repo, else clone it
387         if test -e "$sm_path"
388         then
389                 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
390                 then
391                         eval_gettextln "Adding existing repo at '\$sm_path' to the index"
392                 else
393                         die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
394                 fi
395
396         else
397
398                 module_clone "$sm_path" "$realrepo" "$reference" || exit
399                 (
400                         clear_local_git_env
401                         cd "$sm_path" &&
402                         # ash fails to wordsplit ${branch:+-b "$branch"...}
403                         case "$branch" in
404                         '') git checkout -f -q ;;
405                         ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
406                         esac
407                 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
408         fi
409         git config submodule."$sm_path".url "$realrepo"
410
411         git add $force "$sm_path" ||
412         die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
413
414         git config -f .gitmodules submodule."$sm_path".path "$sm_path" &&
415         git config -f .gitmodules submodule."$sm_path".url "$repo" &&
416         git add --force .gitmodules ||
417         die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
418 }
419
420 #
421 # Execute an arbitrary command sequence in each checked out
422 # submodule
423 #
424 # $@ = command to execute
425 #
426 cmd_foreach()
427 {
428         # parse $args after "submodule ... foreach".
429         while test $# -ne 0
430         do
431                 case "$1" in
432                 -q|--quiet)
433                         GIT_QUIET=1
434                         ;;
435                 --recursive)
436                         recursive=1
437                         ;;
438                 -*)
439                         usage
440                         ;;
441                 *)
442                         break
443                         ;;
444                 esac
445                 shift
446         done
447
448         toplevel=$(pwd)
449
450         # dup stdin so that it can be restored when running the external
451         # command in the subshell (and a recursive call to this function)
452         exec 3<&0
453
454         module_list |
455         while read mode sha1 stage sm_path
456         do
457                 die_if_unmatched "$mode"
458                 if test -e "$sm_path"/.git
459                 then
460                         say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
461                         name=$(module_name "$sm_path")
462                         (
463                                 prefix="$prefix$sm_path/"
464                                 clear_local_git_env
465                                 # we make $path available to scripts ...
466                                 path=$sm_path
467                                 cd "$sm_path" &&
468                                 eval "$@" &&
469                                 if test -n "$recursive"
470                                 then
471                                         cmd_foreach "--recursive" "$@"
472                                 fi
473                         ) <&3 3<&- ||
474                         die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
475                 fi
476         done
477 }
478
479 #
480 # Register submodules in .git/config
481 #
482 # $@ = requested paths (default to all)
483 #
484 cmd_init()
485 {
486         # parse $args after "submodule ... init".
487         while test $# -ne 0
488         do
489                 case "$1" in
490                 -q|--quiet)
491                         GIT_QUIET=1
492                         ;;
493                 --)
494                         shift
495                         break
496                         ;;
497                 -*)
498                         usage
499                         ;;
500                 *)
501                         break
502                         ;;
503                 esac
504                 shift
505         done
506
507         module_list "$@" |
508         while read mode sha1 stage sm_path
509         do
510                 die_if_unmatched "$mode"
511                 name=$(module_name "$sm_path") || exit
512
513                 # Copy url setting when it is not set yet
514                 if test -z "$(git config "submodule.$name.url")"
515                 then
516                         url=$(git config -f .gitmodules submodule."$name".url)
517                         test -z "$url" &&
518                         die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
519
520                         # Possibly a url relative to parent
521                         case "$url" in
522                         ./*|../*)
523                                 url=$(resolve_relative_url "$url") || exit
524                                 ;;
525                         esac
526                         git config submodule."$name".url "$url" ||
527                         die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
528
529                         say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
530                 fi
531
532                 # Copy "update" setting when it is not set yet
533                 upd="$(git config -f .gitmodules submodule."$name".update)"
534                 test -z "$upd" ||
535                 test -n "$(git config submodule."$name".update)" ||
536                 git config submodule."$name".update "$upd" ||
537                 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
538         done
539 }
540
541 #
542 # Update each submodule path to correct revision, using clone and checkout as needed
543 #
544 # $@ = requested paths (default to all)
545 #
546 cmd_update()
547 {
548         # parse $args after "submodule ... update".
549         orig_flags=
550         while test $# -ne 0
551         do
552                 case "$1" in
553                 -q|--quiet)
554                         GIT_QUIET=1
555                         ;;
556                 -i|--init)
557                         init=1
558                         ;;
559                 -N|--no-fetch)
560                         nofetch=1
561                         ;;
562                 -f|--force)
563                         force=$1
564                         ;;
565                 --commit)
566                         commit=1
567                         ;;
568                 -r|--rebase)
569                         update="rebase"
570                         ;;
571                 --reference)
572                         case "$2" in '') usage ;; esac
573                         reference="--reference=$2"
574                         orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
575                         shift
576                         ;;
577                 --reference=*)
578                         reference="$1"
579                         ;;
580                 -m|--merge)
581                         update="merge"
582                         ;;
583                 --recursive)
584                         recursive=1
585                         ;;
586                 --checkout)
587                         update="checkout"
588                         ;;
589                 --)
590                         shift
591                         break
592                         ;;
593                 -*)
594                         usage
595                         ;;
596                 *)
597                         break
598                         ;;
599                 esac
600                 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
601                 shift
602         done
603
604         if test -n "$init"
605         then
606                 cmd_init "--" "$@" || return
607         fi
608
609         cloned_modules=
610         changed_modules=
611         module_list "$@" | {
612         err=
613         while read mode sha1 stage sm_path
614         do
615                 die_if_unmatched "$mode"
616                 if test "$stage" = U
617                 then
618                         echo >&2 "Skipping unmerged submodule $sm_path"
619                         continue
620                 fi
621                 name=$(module_name "$sm_path") || exit
622                 url=$(git config submodule."$name".url)
623                 if ! test -z "$update"
624                 then
625                         update_module=$update
626                 else
627                         update_module=$(git config submodule."$name".update)
628                 fi
629
630                 if test "$update_module" = "none"
631                 then
632                         echo "Skipping submodule '$sm_path'"
633                         continue
634                 fi
635
636                 if test -z "$url"
637                 then
638                         # Only mention uninitialized submodules when its
639                         # path have been specified
640                         test "$#" != "0" &&
641                         say "$(eval_gettext "Submodule path '\$sm_path' not initialized
642 Maybe you want to use 'update --init'?")"
643                         continue
644                 fi
645
646                 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
647                 then
648                         module_clone "$sm_path" "$url" "$reference"|| exit
649                         cloned_modules="$cloned_modules;$name"
650                         subsha1=
651                 else
652                         subsha1=$(clear_local_git_env; cd "$sm_path" &&
653                                 git rev-parse --verify HEAD) ||
654                         die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
655                 fi
656
657                 if test "$subsha1" != "$sha1" -o -n "$force"
658                 then
659                         subforce=$force
660                         # If we don't already have a -f flag and the submodule has never been checked out
661                         if test -z "$subsha1" -a -z "$force"
662                         then
663                                 subforce="-f"
664                         fi
665
666                         if test -z "$nofetch"
667                         then
668                                 # Run fetch only if $sha1 isn't present or it
669                                 # is not reachable from a ref.
670                                 (clear_local_git_env; cd "$sm_path" &&
671                                         ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
672                                          test -z "$rev") || git-fetch)) ||
673                                 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
674                         fi
675
676                         # Is this something we just cloned?
677                         case ";$cloned_modules;" in
678                         *";$name;"*)
679                                 # then there is no local change to integrate
680                                 update_module= ;;
681                         esac
682
683                         must_die_on_failure=
684                         case "$update_module" in
685                         rebase)
686                                 command="git rebase"
687                                 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
688                                 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
689                                 must_die_on_failure=yes
690                                 ;;
691                         merge)
692                                 command="git merge"
693                                 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
694                                 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
695                                 must_die_on_failure=yes
696                                 ;;
697                         *)
698                                 command="git checkout $subforce -q"
699                                 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
700                                 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
701                                 ;;
702                         esac
703
704                         if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
705                         then
706                                 say "$say_msg"
707                         elif test -n "$must_die_on_failure"
708                         then
709                                 die_with_status 2 "$die_msg"
710                         else
711                                 err="${err};$die_msg"
712                                 continue
713                         fi
714
715                         subsha1=$(clear_local_git_env; cd "$sm_path" &&
716                                 git rev-parse --verify HEAD) ||
717                         die "$(eval_gettext "Unable to find new revision in submodule path '\$sm_path'")"
718
719                         if test "$subsha1" != "$sha1"
720                         then
721                                 changed_modules=$(printf '%s%s\n' "$changed_modules" "$name;$sm_path;$sha1;$subsha1")
722                         fi
723                 fi
724
725                 if test -n "$recursive"
726                 then
727                         (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
728                         res=$?
729                         if test $res -gt 0
730                         then
731                                 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
732                                 if test $res -eq 1
733                                 then
734                                         err="${err};$die_msg"
735                                         continue
736                                 else
737                                         die_with_status $res "$die_msg"
738                                 fi
739                         fi
740                 fi
741         done
742
743         if test -z "$err" -a -n "$commit" -a -n "$changed_modules"
744         then
745                 commit_changes "$changed_modules"
746         fi
747
748         if test -n "$err"
749         then
750                 OIFS=$IFS
751                 IFS=';'
752                 for e in $err
753                 do
754                         if test -n "$e"
755                         then
756                                 echo >&2 "$e"
757                         fi
758                 done
759                 IFS=$OIFS
760                 exit 1
761         fi
762         }
763 }
764
765 set_name_rev () {
766         revname=$( (
767                 clear_local_git_env
768                 cd "$1" && {
769                         git describe "$2" 2>/dev/null ||
770                         git describe --tags "$2" 2>/dev/null ||
771                         git describe --contains "$2" 2>/dev/null ||
772                         git describe --all --always "$2"
773                 }
774         ) )
775         test -z "$revname" || revname=" ($revname)"
776 }
777 #
778 # Show commit summary for submodules in index or working tree
779 #
780 # If '--cached' is given, show summary between index and given commit,
781 # or between working tree and given commit
782 #
783 # $@ = [commit (default 'HEAD'),] requested paths (default all)
784 #
785 cmd_summary() {
786         summary_limit=-1
787         for_status=
788         diff_cmd=diff-index
789
790         # parse $args after "submodule ... summary".
791         while test $# -ne 0
792         do
793                 case "$1" in
794                 --cached)
795                         cached="$1"
796                         ;;
797                 --files)
798                         files="$1"
799                         ;;
800                 --for-status)
801                         for_status="$1"
802                         ;;
803                 -n|--summary-limit)
804                         if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
805                         then
806                                 :
807                         else
808                                 usage
809                         fi
810                         shift
811                         ;;
812                 --)
813                         shift
814                         break
815                         ;;
816                 -*)
817                         usage
818                         ;;
819                 *)
820                         break
821                         ;;
822                 esac
823                 shift
824         done
825
826         test $summary_limit = 0 && return
827
828         if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
829         then
830                 head=$rev
831                 test $# = 0 || shift
832         elif test -z "$1" -o "$1" = "HEAD"
833         then
834                 # before the first commit: compare with an empty tree
835                 head=$(git hash-object -w -t tree --stdin </dev/null)
836                 test -z "$1" || shift
837         else
838                 head="HEAD"
839         fi
840
841         if [ -n "$files" ]
842         then
843                 test -n "$cached" &&
844                 die "$(gettext "The --cached option cannot be used with the --files option")"
845                 diff_cmd=diff-files
846                 head=
847         fi
848
849         cd_to_toplevel
850         # Get modified modules cared by user
851         modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
852                 sane_egrep '^:([0-7]* )?160000' |
853                 while read mod_src mod_dst sha1_src sha1_dst status name
854                 do
855                         # Always show modules deleted or type-changed (blob<->module)
856                         test $status = D -o $status = T && echo "$name" && continue
857                         # Also show added or modified modules which are checked out
858                         GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
859                         echo "$name"
860                 done
861         )
862
863         test -z "$modules" && return
864
865         git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
866         sane_egrep '^:([0-7]* )?160000' |
867         cut -c2- |
868         while read mod_src mod_dst sha1_src sha1_dst status name
869         do
870                 if test -z "$cached" &&
871                         test $sha1_dst = 0000000000000000000000000000000000000000
872                 then
873                         case "$mod_dst" in
874                         160000)
875                                 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
876                                 ;;
877                         100644 | 100755 | 120000)
878                                 sha1_dst=$(git hash-object $name)
879                                 ;;
880                         000000)
881                                 ;; # removed
882                         *)
883                                 # unexpected type
884                                 eval_gettextln "unexpected mode \$mod_dst" >&2
885                                 continue ;;
886                         esac
887                 fi
888                 missing_src=
889                 missing_dst=
890
891                 test $mod_src = 160000 &&
892                 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
893                 missing_src=t
894
895                 test $mod_dst = 160000 &&
896                 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
897                 missing_dst=t
898
899                 total_commits=
900                 case "$missing_src,$missing_dst" in
901                 t,)
902                         errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_src")"
903                         ;;
904                 ,t)
905                         errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_dst")"
906                         ;;
907                 t,t)
908                         errmsg="$(eval_gettext "  Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
909                         ;;
910                 *)
911                         errmsg=
912                         total_commits=$(
913                         if test $mod_src = 160000 -a $mod_dst = 160000
914                         then
915                                 range="$sha1_src...$sha1_dst"
916                         elif test $mod_src = 160000
917                         then
918                                 range=$sha1_src
919                         else
920                                 range=$sha1_dst
921                         fi
922                         GIT_DIR="$name/.git" \
923                         git rev-list --first-parent $range -- | wc -l
924                         )
925                         total_commits=" ($(($total_commits + 0)))"
926                         ;;
927                 esac
928
929                 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
930                 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
931                 if test $status = T
932                 then
933                         blob="$(gettext "blob")"
934                         submodule="$(gettext "submodule")"
935                         if test $mod_dst = 160000
936                         then
937                                 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
938                         else
939                                 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
940                         fi
941                 else
942                         echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
943                 fi
944                 if test -n "$errmsg"
945                 then
946                         # Don't give error msg for modification whose dst is not submodule
947                         # i.e. deleted or changed to blob
948                         test $mod_dst = 160000 && echo "$errmsg"
949                 else
950                         if test $mod_src = 160000 -a $mod_dst = 160000
951                         then
952                                 limit=
953                                 test $summary_limit -gt 0 && limit="-$summary_limit"
954                                 GIT_DIR="$name/.git" \
955                                 git log $limit --pretty='format:  %m %s' \
956                                 --first-parent $sha1_src...$sha1_dst
957                         elif test $mod_dst = 160000
958                         then
959                                 GIT_DIR="$name/.git" \
960                                 git log --pretty='format:  > %s' -1 $sha1_dst
961                         else
962                                 GIT_DIR="$name/.git" \
963                                 git log --pretty='format:  < %s' -1 $sha1_src
964                         fi
965                         echo
966                 fi
967                 echo
968         done |
969         if test -n "$for_status"; then
970                 if [ -n "$files" ]; then
971                         gettextln "# Submodules changed but not updated:"
972                 else
973                         gettextln "# Submodule changes to be committed:"
974                 fi
975                 echo "#"
976                 sed -e 's|^|# |' -e 's|^# $|#|'
977         else
978                 cat
979         fi
980 }
981 #
982 # List all submodules, prefixed with:
983 #  - submodule not initialized
984 #  + different revision checked out
985 #
986 # If --cached was specified the revision in the index will be printed
987 # instead of the currently checked out revision.
988 #
989 # $@ = requested paths (default to all)
990 #
991 cmd_status()
992 {
993         # parse $args after "submodule ... status".
994         orig_flags=
995         while test $# -ne 0
996         do
997                 case "$1" in
998                 -q|--quiet)
999                         GIT_QUIET=1
1000                         ;;
1001                 --cached)
1002                         cached=1
1003                         ;;
1004                 --recursive)
1005                         recursive=1
1006                         ;;
1007                 --)
1008                         shift
1009                         break
1010                         ;;
1011                 -*)
1012                         usage
1013                         ;;
1014                 *)
1015                         break
1016                         ;;
1017                 esac
1018                 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
1019                 shift
1020         done
1021
1022         module_list "$@" |
1023         while read mode sha1 stage sm_path
1024         do
1025                 die_if_unmatched "$mode"
1026                 name=$(module_name "$sm_path") || exit
1027                 url=$(git config submodule."$name".url)
1028                 displaypath="$prefix$sm_path"
1029                 if test "$stage" = U
1030                 then
1031                         say "U$sha1 $displaypath"
1032                         continue
1033                 fi
1034                 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
1035                 then
1036                         say "-$sha1 $displaypath"
1037                         continue;
1038                 fi
1039                 set_name_rev "$sm_path" "$sha1"
1040                 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1041                 then
1042                         say " $sha1 $displaypath$revname"
1043                 else
1044                         if test -z "$cached"
1045                         then
1046                                 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1047                                 set_name_rev "$sm_path" "$sha1"
1048                         fi
1049                         say "+$sha1 $displaypath$revname"
1050                 fi
1051
1052                 if test -n "$recursive"
1053                 then
1054                         (
1055                                 prefix="$displaypath/"
1056                                 clear_local_git_env
1057                                 cd "$sm_path" &&
1058                                 eval cmd_status "$orig_args"
1059                         ) ||
1060                         die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1061                 fi
1062         done
1063 }
1064 #
1065 # Sync remote urls for submodules
1066 # This makes the value for remote.$remote.url match the value
1067 # specified in .gitmodules.
1068 #
1069 cmd_sync()
1070 {
1071         while test $# -ne 0
1072         do
1073                 case "$1" in
1074                 -q|--quiet)
1075                         GIT_QUIET=1
1076                         shift
1077                         ;;
1078                 --)
1079                         shift
1080                         break
1081                         ;;
1082                 -*)
1083                         usage
1084                         ;;
1085                 *)
1086                         break
1087                         ;;
1088                 esac
1089         done
1090         cd_to_toplevel
1091         module_list "$@" |
1092         while read mode sha1 stage sm_path
1093         do
1094                 die_if_unmatched "$mode"
1095                 name=$(module_name "$sm_path")
1096                 url=$(git config -f .gitmodules --get submodule."$name".url)
1097
1098                 # Possibly a url relative to parent
1099                 case "$url" in
1100                 ./*|../*)
1101                         # rewrite foo/bar as ../.. to find path from
1102                         # submodule work tree to superproject work tree
1103                         up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1104                         # guarantee a trailing /
1105                         up_path=${up_path%/}/ &&
1106                         # path from submodule work tree to submodule origin repo
1107                         sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1108                         # path from superproject work tree to submodule origin repo
1109                         super_config_url=$(resolve_relative_url "$url") || exit
1110                         ;;
1111                 *)
1112                         sub_origin_url="$url"
1113                         super_config_url="$url"
1114                         ;;
1115                 esac
1116
1117                 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1118                 then
1119                         say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
1120                         git config submodule."$name".url "$super_config_url"
1121
1122                         if test -e "$sm_path"/.git
1123                         then
1124                         (
1125                                 clear_local_git_env
1126                                 cd "$sm_path"
1127                                 remote=$(get_default_remote)
1128                                 git config remote."$remote".url "$sub_origin_url"
1129                         )
1130                         fi
1131                 fi
1132         done
1133 }
1134
1135 # This loop parses the command line arguments to find the
1136 # subcommand name to dispatch.  Parsing of the subcommand specific
1137 # options are primarily done by the subcommand implementations.
1138 # Subcommand specific options such as --branch and --cached are
1139 # parsed here as well, for backward compatibility.
1140
1141 while test $# != 0 && test -z "$command"
1142 do
1143         case "$1" in
1144         add | foreach | init | update | status | summary | sync)
1145                 command=$1
1146                 ;;
1147         -q|--quiet)
1148                 GIT_QUIET=1
1149                 ;;
1150         -b|--branch)
1151                 case "$2" in
1152                 '')
1153                         usage
1154                         ;;
1155                 esac
1156                 branch="$2"; shift
1157                 ;;
1158         --cached)
1159                 cached="$1"
1160                 ;;
1161         --)
1162                 break
1163                 ;;
1164         -*)
1165                 usage
1166                 ;;
1167         *)
1168                 break
1169                 ;;
1170         esac
1171         shift
1172 done
1173
1174 # No command word defaults to "status"
1175 if test -z "$command"
1176 then
1177     if test $# = 0
1178     then
1179         command=status
1180     else
1181         usage
1182     fi
1183 fi
1184
1185 # "-b branch" is accepted only by "add"
1186 if test -n "$branch" && test "$command" != add
1187 then
1188         usage
1189 fi
1190
1191 # "--cached" is accepted only by "status" and "summary"
1192 if test -n "$cached" && test "$command" != status -a "$command" != summary
1193 then
1194         usage
1195 fi
1196
1197 "cmd_$command" "$@"