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