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