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