Merge branch 'nd/clone-no-separate-git-dir-with-bare'
[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         if test -n "$branch"
421         then
422                 git config -f .gitmodules submodule."$sm_name".branch "$branch"
423         fi &&
424         git add --force .gitmodules ||
425         die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
426 }
427
428 #
429 # Execute an arbitrary command sequence in each checked out
430 # submodule
431 #
432 # $@ = command to execute
433 #
434 cmd_foreach()
435 {
436         # parse $args after "submodule ... foreach".
437         while test $# -ne 0
438         do
439                 case "$1" in
440                 -q|--quiet)
441                         GIT_QUIET=1
442                         ;;
443                 --recursive)
444                         recursive=1
445                         ;;
446                 -*)
447                         usage
448                         ;;
449                 *)
450                         break
451                         ;;
452                 esac
453                 shift
454         done
455
456         toplevel=$(pwd)
457
458         # dup stdin so that it can be restored when running the external
459         # command in the subshell (and a recursive call to this function)
460         exec 3<&0
461
462         module_list |
463         while read mode sha1 stage sm_path
464         do
465                 die_if_unmatched "$mode"
466                 if test -e "$sm_path"/.git
467                 then
468                         say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
469                         name=$(module_name "$sm_path")
470                         (
471                                 prefix="$prefix$sm_path/"
472                                 clear_local_git_env
473                                 # we make $path available to scripts ...
474                                 path=$sm_path
475                                 cd "$sm_path" &&
476                                 eval "$@" &&
477                                 if test -n "$recursive"
478                                 then
479                                         cmd_foreach "--recursive" "$@"
480                                 fi
481                         ) <&3 3<&- ||
482                         die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
483                 fi
484         done
485 }
486
487 #
488 # Register submodules in .git/config
489 #
490 # $@ = requested paths (default to all)
491 #
492 cmd_init()
493 {
494         # parse $args after "submodule ... init".
495         while test $# -ne 0
496         do
497                 case "$1" in
498                 -q|--quiet)
499                         GIT_QUIET=1
500                         ;;
501                 --)
502                         shift
503                         break
504                         ;;
505                 -*)
506                         usage
507                         ;;
508                 *)
509                         break
510                         ;;
511                 esac
512                 shift
513         done
514
515         module_list "$@" |
516         while read mode sha1 stage sm_path
517         do
518                 die_if_unmatched "$mode"
519                 name=$(module_name "$sm_path") || exit
520
521                 # Copy url setting when it is not set yet
522                 if test -z "$(git config "submodule.$name.url")"
523                 then
524                         url=$(git config -f .gitmodules submodule."$name".url)
525                         test -z "$url" &&
526                         die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
527
528                         # Possibly a url relative to parent
529                         case "$url" in
530                         ./*|../*)
531                                 url=$(resolve_relative_url "$url") || exit
532                                 ;;
533                         esac
534                         git config submodule."$name".url "$url" ||
535                         die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
536
537                         say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
538                 fi
539
540                 # Copy "update" setting when it is not set yet
541                 upd="$(git config -f .gitmodules submodule."$name".update)"
542                 test -z "$upd" ||
543                 test -n "$(git config submodule."$name".update)" ||
544                 git config submodule."$name".update "$upd" ||
545                 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
546         done
547 }
548
549 #
550 # Update each submodule path to correct revision, using clone and checkout as needed
551 #
552 # $@ = requested paths (default to all)
553 #
554 cmd_update()
555 {
556         # parse $args after "submodule ... update".
557         orig_flags=
558         while test $# -ne 0
559         do
560                 case "$1" in
561                 -q|--quiet)
562                         GIT_QUIET=1
563                         ;;
564                 -i|--init)
565                         init=1
566                         ;;
567                 --remote)
568                         remote=1
569                         ;;
570                 -N|--no-fetch)
571                         nofetch=1
572                         ;;
573                 -f|--force)
574                         force=$1
575                         ;;
576                 -r|--rebase)
577                         update="rebase"
578                         ;;
579                 --reference)
580                         case "$2" in '') usage ;; esac
581                         reference="--reference=$2"
582                         orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
583                         shift
584                         ;;
585                 --reference=*)
586                         reference="$1"
587                         ;;
588                 -m|--merge)
589                         update="merge"
590                         ;;
591                 --recursive)
592                         recursive=1
593                         ;;
594                 --checkout)
595                         update="checkout"
596                         ;;
597                 --)
598                         shift
599                         break
600                         ;;
601                 -*)
602                         usage
603                         ;;
604                 *)
605                         break
606                         ;;
607                 esac
608                 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
609                 shift
610         done
611
612         if test -n "$init"
613         then
614                 cmd_init "--" "$@" || return
615         fi
616
617         cloned_modules=
618         module_list "$@" | {
619         err=
620         while read mode sha1 stage sm_path
621         do
622                 die_if_unmatched "$mode"
623                 if test "$stage" = U
624                 then
625                         echo >&2 "Skipping unmerged submodule $sm_path"
626                         continue
627                 fi
628                 name=$(module_name "$sm_path") || exit
629                 url=$(git config submodule."$name".url)
630                 branch=$(get_submodule_config "$name" branch master)
631                 if ! test -z "$update"
632                 then
633                         update_module=$update
634                 else
635                         update_module=$(git config submodule."$name".update)
636                 fi
637
638                 if test "$update_module" = "none"
639                 then
640                         echo "Skipping submodule '$sm_path'"
641                         continue
642                 fi
643
644                 if test -z "$url"
645                 then
646                         # Only mention uninitialized submodules when its
647                         # path have been specified
648                         test "$#" != "0" &&
649                         say "$(eval_gettext "Submodule path '\$sm_path' not initialized
650 Maybe you want to use 'update --init'?")"
651                         continue
652                 fi
653
654                 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
655                 then
656                         module_clone "$sm_path" "$name" "$url" "$reference" || exit
657                         cloned_modules="$cloned_modules;$name"
658                         subsha1=
659                 else
660                         subsha1=$(clear_local_git_env; cd "$sm_path" &&
661                                 git rev-parse --verify HEAD) ||
662                         die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
663                 fi
664
665                 if test -n "$remote"
666                 then
667                         if test -z "$nofetch"
668                         then
669                                 # Fetch remote before determining tracking $sha1
670                                 (clear_local_git_env; cd "$sm_path" && git-fetch) ||
671                                 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
672                         fi
673                         remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
674                         sha1=$(clear_local_git_env; cd "$sm_path" &&
675                                 git rev-parse --verify "${remote_name}/${branch}") ||
676                         die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
677                 fi
678
679                 if test "$subsha1" != "$sha1" -o -n "$force"
680                 then
681                         subforce=$force
682                         # If we don't already have a -f flag and the submodule has never been checked out
683                         if test -z "$subsha1" -a -z "$force"
684                         then
685                                 subforce="-f"
686                         fi
687
688                         if test -z "$nofetch"
689                         then
690                                 # Run fetch only if $sha1 isn't present or it
691                                 # is not reachable from a ref.
692                                 (clear_local_git_env; cd "$sm_path" &&
693                                         ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
694                                          test -z "$rev") || git-fetch)) ||
695                                 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
696                         fi
697
698                         # Is this something we just cloned?
699                         case ";$cloned_modules;" in
700                         *";$name;"*)
701                                 # then there is no local change to integrate
702                                 update_module= ;;
703                         esac
704
705                         must_die_on_failure=
706                         case "$update_module" in
707                         rebase)
708                                 command="git rebase"
709                                 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
710                                 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
711                                 must_die_on_failure=yes
712                                 ;;
713                         merge)
714                                 command="git merge"
715                                 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
716                                 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
717                                 must_die_on_failure=yes
718                                 ;;
719                         *)
720                                 command="git checkout $subforce -q"
721                                 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
722                                 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
723                                 ;;
724                         esac
725
726                         if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
727                         then
728                                 say "$say_msg"
729                         elif test -n "$must_die_on_failure"
730                         then
731                                 die_with_status 2 "$die_msg"
732                         else
733                                 err="${err};$die_msg"
734                                 continue
735                         fi
736                 fi
737
738                 if test -n "$recursive"
739                 then
740                         (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
741                         res=$?
742                         if test $res -gt 0
743                         then
744                                 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
745                                 if test $res -eq 1
746                                 then
747                                         err="${err};$die_msg"
748                                         continue
749                                 else
750                                         die_with_status $res "$die_msg"
751                                 fi
752                         fi
753                 fi
754         done
755
756         if test -n "$err"
757         then
758                 OIFS=$IFS
759                 IFS=';'
760                 for e in $err
761                 do
762                         if test -n "$e"
763                         then
764                                 echo >&2 "$e"
765                         fi
766                 done
767                 IFS=$OIFS
768                 exit 1
769         fi
770         }
771 }
772
773 set_name_rev () {
774         revname=$( (
775                 clear_local_git_env
776                 cd "$1" && {
777                         git describe "$2" 2>/dev/null ||
778                         git describe --tags "$2" 2>/dev/null ||
779                         git describe --contains "$2" 2>/dev/null ||
780                         git describe --all --always "$2"
781                 }
782         ) )
783         test -z "$revname" || revname=" ($revname)"
784 }
785 #
786 # Show commit summary for submodules in index or working tree
787 #
788 # If '--cached' is given, show summary between index and given commit,
789 # or between working tree and given commit
790 #
791 # $@ = [commit (default 'HEAD'),] requested paths (default all)
792 #
793 cmd_summary() {
794         summary_limit=-1
795         for_status=
796         diff_cmd=diff-index
797
798         # parse $args after "submodule ... summary".
799         while test $# -ne 0
800         do
801                 case "$1" in
802                 --cached)
803                         cached="$1"
804                         ;;
805                 --files)
806                         files="$1"
807                         ;;
808                 --for-status)
809                         for_status="$1"
810                         ;;
811                 -n|--summary-limit)
812                         if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
813                         then
814                                 :
815                         else
816                                 usage
817                         fi
818                         shift
819                         ;;
820                 --)
821                         shift
822                         break
823                         ;;
824                 -*)
825                         usage
826                         ;;
827                 *)
828                         break
829                         ;;
830                 esac
831                 shift
832         done
833
834         test $summary_limit = 0 && return
835
836         if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
837         then
838                 head=$rev
839                 test $# = 0 || shift
840         elif test -z "$1" -o "$1" = "HEAD"
841         then
842                 # before the first commit: compare with an empty tree
843                 head=$(git hash-object -w -t tree --stdin </dev/null)
844                 test -z "$1" || shift
845         else
846                 head="HEAD"
847         fi
848
849         if [ -n "$files" ]
850         then
851                 test -n "$cached" &&
852                 die "$(gettext "The --cached option cannot be used with the --files option")"
853                 diff_cmd=diff-files
854                 head=
855         fi
856
857         cd_to_toplevel
858         # Get modified modules cared by user
859         modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
860                 sane_egrep '^:([0-7]* )?160000' |
861                 while read mod_src mod_dst sha1_src sha1_dst status name
862                 do
863                         # Always show modules deleted or type-changed (blob<->module)
864                         test $status = D -o $status = T && echo "$name" && continue
865                         # Also show added or modified modules which are checked out
866                         GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
867                         echo "$name"
868                 done
869         )
870
871         test -z "$modules" && return
872
873         git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
874         sane_egrep '^:([0-7]* )?160000' |
875         cut -c2- |
876         while read mod_src mod_dst sha1_src sha1_dst status name
877         do
878                 if test -z "$cached" &&
879                         test $sha1_dst = 0000000000000000000000000000000000000000
880                 then
881                         case "$mod_dst" in
882                         160000)
883                                 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
884                                 ;;
885                         100644 | 100755 | 120000)
886                                 sha1_dst=$(git hash-object $name)
887                                 ;;
888                         000000)
889                                 ;; # removed
890                         *)
891                                 # unexpected type
892                                 eval_gettextln "unexpected mode \$mod_dst" >&2
893                                 continue ;;
894                         esac
895                 fi
896                 missing_src=
897                 missing_dst=
898
899                 test $mod_src = 160000 &&
900                 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
901                 missing_src=t
902
903                 test $mod_dst = 160000 &&
904                 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
905                 missing_dst=t
906
907                 total_commits=
908                 case "$missing_src,$missing_dst" in
909                 t,)
910                         errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_src")"
911                         ;;
912                 ,t)
913                         errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_dst")"
914                         ;;
915                 t,t)
916                         errmsg="$(eval_gettext "  Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
917                         ;;
918                 *)
919                         errmsg=
920                         total_commits=$(
921                         if test $mod_src = 160000 -a $mod_dst = 160000
922                         then
923                                 range="$sha1_src...$sha1_dst"
924                         elif test $mod_src = 160000
925                         then
926                                 range=$sha1_src
927                         else
928                                 range=$sha1_dst
929                         fi
930                         GIT_DIR="$name/.git" \
931                         git rev-list --first-parent $range -- | wc -l
932                         )
933                         total_commits=" ($(($total_commits + 0)))"
934                         ;;
935                 esac
936
937                 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
938                 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
939                 if test $status = T
940                 then
941                         blob="$(gettext "blob")"
942                         submodule="$(gettext "submodule")"
943                         if test $mod_dst = 160000
944                         then
945                                 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
946                         else
947                                 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
948                         fi
949                 else
950                         echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
951                 fi
952                 if test -n "$errmsg"
953                 then
954                         # Don't give error msg for modification whose dst is not submodule
955                         # i.e. deleted or changed to blob
956                         test $mod_dst = 160000 && echo "$errmsg"
957                 else
958                         if test $mod_src = 160000 -a $mod_dst = 160000
959                         then
960                                 limit=
961                                 test $summary_limit -gt 0 && limit="-$summary_limit"
962                                 GIT_DIR="$name/.git" \
963                                 git log $limit --pretty='format:  %m %s' \
964                                 --first-parent $sha1_src...$sha1_dst
965                         elif test $mod_dst = 160000
966                         then
967                                 GIT_DIR="$name/.git" \
968                                 git log --pretty='format:  > %s' -1 $sha1_dst
969                         else
970                                 GIT_DIR="$name/.git" \
971                                 git log --pretty='format:  < %s' -1 $sha1_src
972                         fi
973                         echo
974                 fi
975                 echo
976         done |
977         if test -n "$for_status"; then
978                 if [ -n "$files" ]; then
979                         gettextln "# Submodules changed but not updated:"
980                 else
981                         gettextln "# Submodule changes to be committed:"
982                 fi
983                 echo "#"
984                 sed -e 's|^|# |' -e 's|^# $|#|'
985         else
986                 cat
987         fi
988 }
989 #
990 # List all submodules, prefixed with:
991 #  - submodule not initialized
992 #  + different revision checked out
993 #
994 # If --cached was specified the revision in the index will be printed
995 # instead of the currently checked out revision.
996 #
997 # $@ = requested paths (default to all)
998 #
999 cmd_status()
1000 {
1001         # parse $args after "submodule ... status".
1002         while test $# -ne 0
1003         do
1004                 case "$1" in
1005                 -q|--quiet)
1006                         GIT_QUIET=1
1007                         ;;
1008                 --cached)
1009                         cached=1
1010                         ;;
1011                 --recursive)
1012                         recursive=1
1013                         ;;
1014                 --)
1015                         shift
1016                         break
1017                         ;;
1018                 -*)
1019                         usage
1020                         ;;
1021                 *)
1022                         break
1023                         ;;
1024                 esac
1025                 shift
1026         done
1027
1028         module_list "$@" |
1029         while read mode sha1 stage sm_path
1030         do
1031                 die_if_unmatched "$mode"
1032                 name=$(module_name "$sm_path") || exit
1033                 url=$(git config submodule."$name".url)
1034                 displaypath="$prefix$sm_path"
1035                 if test "$stage" = U
1036                 then
1037                         say "U$sha1 $displaypath"
1038                         continue
1039                 fi
1040                 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
1041                 then
1042                         say "-$sha1 $displaypath"
1043                         continue;
1044                 fi
1045                 set_name_rev "$sm_path" "$sha1"
1046                 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1047                 then
1048                         say " $sha1 $displaypath$revname"
1049                 else
1050                         if test -z "$cached"
1051                         then
1052                                 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1053                                 set_name_rev "$sm_path" "$sha1"
1054                         fi
1055                         say "+$sha1 $displaypath$revname"
1056                 fi
1057
1058                 if test -n "$recursive"
1059                 then
1060                         (
1061                                 prefix="$displaypath/"
1062                                 clear_local_git_env
1063                                 cd "$sm_path" &&
1064                                 eval cmd_status
1065                         ) ||
1066                         die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1067                 fi
1068         done
1069 }
1070 #
1071 # Sync remote urls for submodules
1072 # This makes the value for remote.$remote.url match the value
1073 # specified in .gitmodules.
1074 #
1075 cmd_sync()
1076 {
1077         while test $# -ne 0
1078         do
1079                 case "$1" in
1080                 -q|--quiet)
1081                         GIT_QUIET=1
1082                         shift
1083                         ;;
1084                 --recursive)
1085                         recursive=1
1086                         shift
1087                         ;;
1088                 --)
1089                         shift
1090                         break
1091                         ;;
1092                 -*)
1093                         usage
1094                         ;;
1095                 *)
1096                         break
1097                         ;;
1098                 esac
1099         done
1100         cd_to_toplevel
1101         module_list "$@" |
1102         while read mode sha1 stage sm_path
1103         do
1104                 die_if_unmatched "$mode"
1105                 name=$(module_name "$sm_path")
1106                 url=$(git config -f .gitmodules --get submodule."$name".url)
1107
1108                 # Possibly a url relative to parent
1109                 case "$url" in
1110                 ./*|../*)
1111                         # rewrite foo/bar as ../.. to find path from
1112                         # submodule work tree to superproject work tree
1113                         up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1114                         # guarantee a trailing /
1115                         up_path=${up_path%/}/ &&
1116                         # path from submodule work tree to submodule origin repo
1117                         sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1118                         # path from superproject work tree to submodule origin repo
1119                         super_config_url=$(resolve_relative_url "$url") || exit
1120                         ;;
1121                 *)
1122                         sub_origin_url="$url"
1123                         super_config_url="$url"
1124                         ;;
1125                 esac
1126
1127                 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1128                 then
1129                         say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")"
1130                         git config submodule."$name".url "$super_config_url"
1131
1132                         if test -e "$sm_path"/.git
1133                         then
1134                         (
1135                                 clear_local_git_env
1136                                 cd "$sm_path"
1137                                 remote=$(get_default_remote)
1138                                 git config remote."$remote".url "$sub_origin_url"
1139
1140                                 if test -n "$recursive"
1141                                 then
1142                                         prefix="$prefix$sm_path/"
1143                                         eval cmd_sync
1144                                 fi
1145                         )
1146                         fi
1147                 fi
1148         done
1149 }
1150
1151 # This loop parses the command line arguments to find the
1152 # subcommand name to dispatch.  Parsing of the subcommand specific
1153 # options are primarily done by the subcommand implementations.
1154 # Subcommand specific options such as --branch and --cached are
1155 # parsed here as well, for backward compatibility.
1156
1157 while test $# != 0 && test -z "$command"
1158 do
1159         case "$1" in
1160         add | foreach | init | update | status | summary | sync)
1161                 command=$1
1162                 ;;
1163         -q|--quiet)
1164                 GIT_QUIET=1
1165                 ;;
1166         -b|--branch)
1167                 case "$2" in
1168                 '')
1169                         usage
1170                         ;;
1171                 esac
1172                 branch="$2"; shift
1173                 ;;
1174         --cached)
1175                 cached="$1"
1176                 ;;
1177         --)
1178                 break
1179                 ;;
1180         -*)
1181                 usage
1182                 ;;
1183         *)
1184                 break
1185                 ;;
1186         esac
1187         shift
1188 done
1189
1190 # No command word defaults to "status"
1191 if test -z "$command"
1192 then
1193     if test $# = 0
1194     then
1195         command=status
1196     else
1197         usage
1198     fi
1199 fi
1200
1201 # "-b branch" is accepted only by "add"
1202 if test -n "$branch" && test "$command" != add
1203 then
1204         usage
1205 fi
1206
1207 # "--cached" is accepted only by "status" and "summary"
1208 if test -n "$cached" && test "$command" != status -a "$command" != summary
1209 then
1210         usage
1211 fi
1212
1213 "cmd_$command" "$@"