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