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