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