mailmap: handle mailmap blobs without trailing newlines
[git/git-svn.git] / git-submodule.sh
blob48bdf843244503df247e0dbdef3c5541cd9eb48e
1 #!/bin/sh
3 # git-submodule.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
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] [--] <path>...
12 or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--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 . git-sh-setup
18 . git-sh-i18n
19 . git-parse-remote
20 require_work_tree
22 command=
23 branch=
24 force=
25 reference=
26 cached=
27 recursive=
28 init=
29 files=
30 remote=
31 nofetch=
32 update=
33 prefix=
34 custom_name=
36 # The function takes at most 2 arguments. The first argument is the
37 # URL that navigates to the submodule origin repo. When relative, this URL
38 # is relative to the superproject origin URL repo. The second up_path
39 # argument, if specified, is the relative path that navigates
40 # from the submodule working tree to the superproject working tree.
42 # The output of the function is the origin URL of the submodule.
44 # The output will either be an absolute URL or filesystem path (if the
45 # superproject origin URL is an absolute URL or filesystem path,
46 # respectively) or a relative file system path (if the superproject
47 # origin URL is a relative file system path).
49 # When the output is a relative file system path, the path is either
50 # relative to the submodule working tree, if up_path is specified, or to
51 # the superproject working tree otherwise.
52 resolve_relative_url ()
54 remote=$(get_default_remote)
55 remoteurl=$(git config "remote.$remote.url") ||
56 remoteurl=$(pwd) # the repository is its own authoritative upstream
57 url="$1"
58 remoteurl=${remoteurl%/}
59 sep=/
60 up_path="$2"
62 case "$remoteurl" in
63 *:*|/*)
64 is_relative=
66 ./*|../*)
67 is_relative=t
70 is_relative=t
71 remoteurl="./$remoteurl"
73 esac
75 while test -n "$url"
77 case "$url" in
78 ../*)
79 url="${url#../}"
80 case "$remoteurl" in
81 */*)
82 remoteurl="${remoteurl%/*}"
84 *:*)
85 remoteurl="${remoteurl%:*}"
86 sep=:
89 if test -z "$is_relative" || test "." = "$remoteurl"
90 then
91 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
92 else
93 remoteurl=.
96 esac
98 ./*)
99 url="${url#./}"
102 break;;
103 esac
104 done
105 remoteurl="$remoteurl$sep${url%/}"
106 echo "${is_relative:+${up_path}}${remoteurl#./}"
110 # Get submodule info for registered submodules
111 # $@ = path to limit submodule list
113 module_list()
116 git ls-files -z --error-unmatch --stage -- "$@" ||
117 echo "unmatched pathspec exists"
119 perl -e '
120 my %unmerged = ();
121 my ($null_sha1) = ("0" x 40);
122 my @out = ();
123 my $unmatched = 0;
124 $/ = "\0";
125 while (<STDIN>) {
126 if (/^unmatched pathspec/) {
127 $unmatched = 1;
128 next;
130 chomp;
131 my ($mode, $sha1, $stage, $path) =
132 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
133 next unless $mode eq "160000";
134 if ($stage ne "0") {
135 if (!$unmerged{$path}++) {
136 push @out, "$mode $null_sha1 U\t$path\n";
138 next;
140 push @out, "$_\n";
142 if ($unmatched) {
143 print "#unmatched\n";
144 } else {
145 print for (@out);
150 die_if_unmatched ()
152 if test "$1" = "#unmatched"
153 then
154 exit 1
159 # Print a submodule configuration setting
161 # $1 = submodule name
162 # $2 = option name
163 # $3 = default value
165 # Checks in the usual git-config places first (for overrides),
166 # otherwise it falls back on .gitmodules. This allows you to
167 # distribute project-wide defaults in .gitmodules, while still
168 # customizing individual repositories if necessary. If the option is
169 # not in .gitmodules either, print a default value.
171 get_submodule_config () {
172 name="$1"
173 option="$2"
174 default="$3"
175 value=$(git config submodule."$name"."$option")
176 if test -z "$value"
177 then
178 value=$(git config -f .gitmodules submodule."$name"."$option")
180 printf '%s' "${value:-$default}"
185 # Map submodule path to submodule name
187 # $1 = path
189 module_name()
191 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
192 sm_path="$1"
193 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
194 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
195 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
196 test -z "$name" &&
197 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
198 echo "$name"
202 # Clone a submodule
204 # Prior to calling, cmd_update checks that a possibly existing
205 # path is not a git repository.
206 # Likewise, cmd_add checks that path does not exist at all,
207 # since it is the location of a new submodule.
209 module_clone()
211 sm_path=$1
212 name=$2
213 url=$3
214 reference="$4"
215 quiet=
216 if test -n "$GIT_QUIET"
217 then
218 quiet=-q
221 gitdir=
222 gitdir_base=
223 base_name=$(dirname "$name")
225 gitdir=$(git rev-parse --git-dir)
226 gitdir_base="$gitdir/modules/$base_name"
227 gitdir="$gitdir/modules/$name"
229 if test -d "$gitdir"
230 then
231 mkdir -p "$sm_path"
232 rm -f "$gitdir/index"
233 else
234 mkdir -p "$gitdir_base"
236 clear_local_git_env
237 git clone $quiet -n ${reference:+"$reference"} \
238 --separate-git-dir "$gitdir" "$url" "$sm_path"
239 ) ||
240 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
243 # We already are at the root of the work tree but cd_to_toplevel will
244 # resolve any symlinks that might be present in $PWD
245 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
246 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
247 # normalize Windows-style absolute paths to POSIX-style absolute paths
248 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
249 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
250 # Remove all common leading directories after a sanity check
251 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
252 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
254 while test "${a%%/*}" = "${b%%/*}"
256 a=${a#*/}
257 b=${b#*/}
258 done
259 # Now chop off the trailing '/'s that were added in the beginning
260 a=${a%/}
261 b=${b%/}
263 # Turn each leading "*/" component into "../"
264 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
265 echo "gitdir: $rel/$a" >"$sm_path/.git"
267 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
268 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
271 isnumber()
273 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
277 # Add a new submodule to the working tree, .gitmodules and the index
279 # $@ = repo path
281 # optional branch is stored in global branch variable
283 cmd_add()
285 # parse $args after "submodule ... add".
286 while test $# -ne 0
288 case "$1" in
289 -b | --branch)
290 case "$2" in '') usage ;; esac
291 branch=$2
292 shift
294 -f | --force)
295 force=$1
297 -q|--quiet)
298 GIT_QUIET=1
300 --reference)
301 case "$2" in '') usage ;; esac
302 reference="--reference=$2"
303 shift
305 --reference=*)
306 reference="$1"
308 --name)
309 case "$2" in '') usage ;; esac
310 custom_name=$2
311 shift
314 shift
315 break
318 usage
321 break
323 esac
324 shift
325 done
327 repo=$1
328 sm_path=$2
330 if test -z "$sm_path"; then
331 sm_path=$(echo "$repo" |
332 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
335 if test -z "$repo" -o -z "$sm_path"; then
336 usage
339 # assure repo is absolute or relative to parent
340 case "$repo" in
341 ./*|../*)
342 # dereference source url relative to parent's url
343 realrepo=$(resolve_relative_url "$repo") || exit
345 *:*|/*)
346 # absolute url
347 realrepo=$repo
350 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
352 esac
354 # normalize path:
355 # multiple //; leading ./; /./; /../; trailing /
356 sm_path=$(printf '%s/\n' "$sm_path" |
357 sed -e '
358 s|//*|/|g
359 s|^\(\./\)*||
360 s|/\./|/|g
361 :start
362 s|\([^/]*\)/\.\./||
363 tstart
364 s|/*$||
366 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
367 die "$(eval_gettext "'\$sm_path' already exists in the index")"
369 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
370 then
371 eval_gettextln "The following path is ignored by one of your .gitignore files:
372 \$sm_path
373 Use -f if you really want to add it." >&2
374 exit 1
377 if test -n "$custom_name"
378 then
379 sm_name="$custom_name"
380 else
381 sm_name="$sm_path"
384 # perhaps the path exists and is already a git repo, else clone it
385 if test -e "$sm_path"
386 then
387 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
388 then
389 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
390 else
391 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
394 else
395 if test -d ".git/modules/$sm_name"
396 then
397 if test -z "$force"
398 then
399 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
400 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
401 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
402 echo >&2 " $realrepo"
403 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
404 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
405 else
406 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
409 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
411 clear_local_git_env
412 cd "$sm_path" &&
413 # ash fails to wordsplit ${branch:+-b "$branch"...}
414 case "$branch" in
415 '') git checkout -f -q ;;
416 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
417 esac
418 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
420 git config submodule."$sm_name".url "$realrepo"
422 git add $force "$sm_path" ||
423 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
425 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
426 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
427 if test -n "$branch"
428 then
429 git config -f .gitmodules submodule."$sm_name".branch "$branch"
430 fi &&
431 git add --force .gitmodules ||
432 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
436 # Execute an arbitrary command sequence in each checked out
437 # submodule
439 # $@ = command to execute
441 cmd_foreach()
443 # parse $args after "submodule ... foreach".
444 while test $# -ne 0
446 case "$1" in
447 -q|--quiet)
448 GIT_QUIET=1
450 --recursive)
451 recursive=1
454 usage
457 break
459 esac
460 shift
461 done
463 toplevel=$(pwd)
465 # dup stdin so that it can be restored when running the external
466 # command in the subshell (and a recursive call to this function)
467 exec 3<&0
469 module_list |
470 while read mode sha1 stage sm_path
472 die_if_unmatched "$mode"
473 if test -e "$sm_path"/.git
474 then
475 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
476 name=$(module_name "$sm_path")
478 prefix="$prefix$sm_path/"
479 clear_local_git_env
480 # we make $path available to scripts ...
481 path=$sm_path
482 cd "$sm_path" &&
483 eval "$@" &&
484 if test -n "$recursive"
485 then
486 cmd_foreach "--recursive" "$@"
488 ) <&3 3<&- ||
489 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
491 done
495 # Register submodules in .git/config
497 # $@ = requested paths (default to all)
499 cmd_init()
501 # parse $args after "submodule ... init".
502 while test $# -ne 0
504 case "$1" in
505 -q|--quiet)
506 GIT_QUIET=1
509 shift
510 break
513 usage
516 break
518 esac
519 shift
520 done
522 module_list "$@" |
523 while read mode sha1 stage sm_path
525 die_if_unmatched "$mode"
526 name=$(module_name "$sm_path") || exit
528 # Copy url setting when it is not set yet
529 if test -z "$(git config "submodule.$name.url")"
530 then
531 url=$(git config -f .gitmodules submodule."$name".url)
532 test -z "$url" &&
533 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
535 # Possibly a url relative to parent
536 case "$url" in
537 ./*|../*)
538 url=$(resolve_relative_url "$url") || exit
540 esac
541 git config submodule."$name".url "$url" ||
542 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
544 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
547 # Copy "update" setting when it is not set yet
548 upd="$(git config -f .gitmodules submodule."$name".update)"
549 test -z "$upd" ||
550 test -n "$(git config submodule."$name".update)" ||
551 git config submodule."$name".update "$upd" ||
552 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
553 done
557 # Unregister submodules from .git/config and remove their work tree
559 # $@ = requested paths (use '.' to deinit all submodules)
561 cmd_deinit()
563 # parse $args after "submodule ... deinit".
564 while test $# -ne 0
566 case "$1" in
567 -f|--force)
568 force=$1
570 -q|--quiet)
571 GIT_QUIET=1
574 shift
575 break
578 usage
581 break
583 esac
584 shift
585 done
587 if test $# = 0
588 then
589 die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
592 module_list "$@" |
593 while read mode sha1 stage sm_path
595 die_if_unmatched "$mode"
596 name=$(module_name "$sm_path") || exit
598 # Remove the submodule work tree (unless the user already did it)
599 if test -d "$sm_path"
600 then
601 # Protect submodules containing a .git directory
602 if test -d "$sm_path/.git"
603 then
604 echo >&2 "$(eval_gettext "Submodule work tree '\$sm_path' contains a .git directory")"
605 die "$(eval_gettext "(use 'rm -rf' if you really want to remove it including all of its history)")"
608 if test -z "$force"
609 then
610 git rm -qn "$sm_path" ||
611 die "$(eval_gettext "Submodule work tree '\$sm_path' contains local modifications; use '-f' to discard them")"
613 rm -rf "$sm_path" &&
614 say "$(eval_gettext "Cleared directory '\$sm_path'")" ||
615 say "$(eval_gettext "Could not remove submodule work tree '\$sm_path'")"
618 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$sm_path'")"
620 # Remove the .git/config entries (unless the user already did it)
621 if test -n "$(git config --get-regexp submodule."$name\.")"
622 then
623 # Remove the whole section so we have a clean state when
624 # the user later decides to init this submodule again
625 url=$(git config submodule."$name".url)
626 git config --remove-section submodule."$name" 2>/dev/null &&
627 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$sm_path'")"
629 done
633 # Update each submodule path to correct revision, using clone and checkout as needed
635 # $@ = requested paths (default to all)
637 cmd_update()
639 # parse $args after "submodule ... update".
640 orig_flags=
641 while test $# -ne 0
643 case "$1" in
644 -q|--quiet)
645 GIT_QUIET=1
647 -i|--init)
648 init=1
650 --remote)
651 remote=1
653 -N|--no-fetch)
654 nofetch=1
656 -f|--force)
657 force=$1
659 -r|--rebase)
660 update="rebase"
662 --reference)
663 case "$2" in '') usage ;; esac
664 reference="--reference=$2"
665 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
666 shift
668 --reference=*)
669 reference="$1"
671 -m|--merge)
672 update="merge"
674 --recursive)
675 recursive=1
677 --checkout)
678 update="checkout"
681 shift
682 break
685 usage
688 break
690 esac
691 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
692 shift
693 done
695 if test -n "$init"
696 then
697 cmd_init "--" "$@" || return
700 cloned_modules=
701 module_list "$@" | {
702 err=
703 while read mode sha1 stage sm_path
705 die_if_unmatched "$mode"
706 if test "$stage" = U
707 then
708 echo >&2 "Skipping unmerged submodule $prefix$sm_path"
709 continue
711 name=$(module_name "$sm_path") || exit
712 url=$(git config submodule."$name".url)
713 branch=$(get_submodule_config "$name" branch master)
714 if ! test -z "$update"
715 then
716 update_module=$update
717 else
718 update_module=$(git config submodule."$name".update)
721 if test "$update_module" = "none"
722 then
723 echo "Skipping submodule '$prefix$sm_path'"
724 continue
727 if test -z "$url"
728 then
729 # Only mention uninitialized submodules when its
730 # path have been specified
731 test "$#" != "0" &&
732 say "$(eval_gettext "Submodule path '\$prefix\$sm_path' not initialized
733 Maybe you want to use 'update --init'?")"
734 continue
737 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
738 then
739 module_clone "$sm_path" "$name" "$url" "$reference" || exit
740 cloned_modules="$cloned_modules;$name"
741 subsha1=
742 else
743 subsha1=$(clear_local_git_env; cd "$sm_path" &&
744 git rev-parse --verify HEAD) ||
745 die "$(eval_gettext "Unable to find current revision in submodule path '\$prefix\$sm_path'")"
748 if test -n "$remote"
749 then
750 if test -z "$nofetch"
751 then
752 # Fetch remote before determining tracking $sha1
753 (clear_local_git_env; cd "$sm_path" && git-fetch) ||
754 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
756 remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
757 sha1=$(clear_local_git_env; cd "$sm_path" &&
758 git rev-parse --verify "${remote_name}/${branch}") ||
759 die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
762 if test "$subsha1" != "$sha1" -o -n "$force"
763 then
764 subforce=$force
765 # If we don't already have a -f flag and the submodule has never been checked out
766 if test -z "$subsha1" -a -z "$force"
767 then
768 subforce="-f"
771 if test -z "$nofetch"
772 then
773 # Run fetch only if $sha1 isn't present or it
774 # is not reachable from a ref.
775 (clear_local_git_env; cd "$sm_path" &&
776 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
777 test -z "$rev") || git-fetch)) ||
778 die "$(eval_gettext "Unable to fetch in submodule path '\$prefix\$sm_path'")"
781 # Is this something we just cloned?
782 case ";$cloned_modules;" in
783 *";$name;"*)
784 # then there is no local change to integrate
785 update_module= ;;
786 esac
788 must_die_on_failure=
789 case "$update_module" in
790 rebase)
791 command="git rebase"
792 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$prefix\$sm_path'")"
793 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': rebased into '\$sha1'")"
794 must_die_on_failure=yes
796 merge)
797 command="git merge"
798 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$prefix\$sm_path'")"
799 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': merged in '\$sha1'")"
800 must_die_on_failure=yes
803 command="git checkout $subforce -q"
804 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$prefix\$sm_path'")"
805 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': checked out '\$sha1'")"
807 esac
809 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
810 then
811 say "$say_msg"
812 elif test -n "$must_die_on_failure"
813 then
814 die_with_status 2 "$die_msg"
815 else
816 err="${err};$die_msg"
817 continue
821 if test -n "$recursive"
822 then
824 prefix="$prefix$sm_path/"
825 clear_local_git_env
826 cd "$sm_path" &&
827 eval cmd_update "$orig_flags"
829 res=$?
830 if test $res -gt 0
831 then
832 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$prefix\$sm_path'")"
833 if test $res -eq 1
834 then
835 err="${err};$die_msg"
836 continue
837 else
838 die_with_status $res "$die_msg"
842 done
844 if test -n "$err"
845 then
846 OIFS=$IFS
847 IFS=';'
848 for e in $err
850 if test -n "$e"
851 then
852 echo >&2 "$e"
854 done
855 IFS=$OIFS
856 exit 1
861 set_name_rev () {
862 revname=$( (
863 clear_local_git_env
864 cd "$1" && {
865 git describe "$2" 2>/dev/null ||
866 git describe --tags "$2" 2>/dev/null ||
867 git describe --contains "$2" 2>/dev/null ||
868 git describe --all --always "$2"
871 test -z "$revname" || revname=" ($revname)"
874 # Show commit summary for submodules in index or working tree
876 # If '--cached' is given, show summary between index and given commit,
877 # or between working tree and given commit
879 # $@ = [commit (default 'HEAD'),] requested paths (default all)
881 cmd_summary() {
882 summary_limit=-1
883 for_status=
884 diff_cmd=diff-index
886 # parse $args after "submodule ... summary".
887 while test $# -ne 0
889 case "$1" in
890 --cached)
891 cached="$1"
893 --files)
894 files="$1"
896 --for-status)
897 for_status="$1"
899 -n|--summary-limit)
900 summary_limit="$2"
901 isnumber "$summary_limit" || usage
902 shift
904 --summary-limit=*)
905 summary_limit="${1#--summary-limit=}"
906 isnumber "$summary_limit" || usage
909 shift
910 break
913 usage
916 break
918 esac
919 shift
920 done
922 test $summary_limit = 0 && return
924 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
925 then
926 head=$rev
927 test $# = 0 || shift
928 elif test -z "$1" -o "$1" = "HEAD"
929 then
930 # before the first commit: compare with an empty tree
931 head=$(git hash-object -w -t tree --stdin </dev/null)
932 test -z "$1" || shift
933 else
934 head="HEAD"
937 if [ -n "$files" ]
938 then
939 test -n "$cached" &&
940 die "$(gettext "The --cached option cannot be used with the --files option")"
941 diff_cmd=diff-files
942 head=
945 cd_to_toplevel
946 # Get modified modules cared by user
947 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
948 sane_egrep '^:([0-7]* )?160000' |
949 while read mod_src mod_dst sha1_src sha1_dst status name
951 # Always show modules deleted or type-changed (blob<->module)
952 test $status = D -o $status = T && echo "$name" && continue
953 # Also show added or modified modules which are checked out
954 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
955 echo "$name"
956 done
959 test -z "$modules" && return
961 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
962 sane_egrep '^:([0-7]* )?160000' |
963 cut -c2- |
964 while read mod_src mod_dst sha1_src sha1_dst status name
966 if test -z "$cached" &&
967 test $sha1_dst = 0000000000000000000000000000000000000000
968 then
969 case "$mod_dst" in
970 160000)
971 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
973 100644 | 100755 | 120000)
974 sha1_dst=$(git hash-object $name)
976 000000)
977 ;; # removed
979 # unexpected type
980 eval_gettextln "unexpected mode \$mod_dst" >&2
981 continue ;;
982 esac
984 missing_src=
985 missing_dst=
987 test $mod_src = 160000 &&
988 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
989 missing_src=t
991 test $mod_dst = 160000 &&
992 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
993 missing_dst=t
995 total_commits=
996 case "$missing_src,$missing_dst" in
998 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
1001 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
1003 t,t)
1004 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
1007 errmsg=
1008 total_commits=$(
1009 if test $mod_src = 160000 -a $mod_dst = 160000
1010 then
1011 range="$sha1_src...$sha1_dst"
1012 elif test $mod_src = 160000
1013 then
1014 range=$sha1_src
1015 else
1016 range=$sha1_dst
1018 GIT_DIR="$name/.git" \
1019 git rev-list --first-parent $range -- | wc -l
1021 total_commits=" ($(($total_commits + 0)))"
1023 esac
1025 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
1026 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
1027 if test $status = T
1028 then
1029 blob="$(gettext "blob")"
1030 submodule="$(gettext "submodule")"
1031 if test $mod_dst = 160000
1032 then
1033 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1034 else
1035 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1037 else
1038 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1040 if test -n "$errmsg"
1041 then
1042 # Don't give error msg for modification whose dst is not submodule
1043 # i.e. deleted or changed to blob
1044 test $mod_dst = 160000 && echo "$errmsg"
1045 else
1046 if test $mod_src = 160000 -a $mod_dst = 160000
1047 then
1048 limit=
1049 test $summary_limit -gt 0 && limit="-$summary_limit"
1050 GIT_DIR="$name/.git" \
1051 git log $limit --pretty='format: %m %s' \
1052 --first-parent $sha1_src...$sha1_dst
1053 elif test $mod_dst = 160000
1054 then
1055 GIT_DIR="$name/.git" \
1056 git log --pretty='format: > %s' -1 $sha1_dst
1057 else
1058 GIT_DIR="$name/.git" \
1059 git log --pretty='format: < %s' -1 $sha1_src
1061 echo
1063 echo
1064 done |
1065 if test -n "$for_status"; then
1066 if [ -n "$files" ]; then
1067 gettextln "Submodules changed but not updated:" | git stripspace -c
1068 else
1069 gettextln "Submodule changes to be committed:" | git stripspace -c
1071 printf "\n" | git stripspace -c
1072 git stripspace -c
1073 else
1078 # List all submodules, prefixed with:
1079 # - submodule not initialized
1080 # + different revision checked out
1082 # If --cached was specified the revision in the index will be printed
1083 # instead of the currently checked out revision.
1085 # $@ = requested paths (default to all)
1087 cmd_status()
1089 # parse $args after "submodule ... status".
1090 while test $# -ne 0
1092 case "$1" in
1093 -q|--quiet)
1094 GIT_QUIET=1
1096 --cached)
1097 cached=1
1099 --recursive)
1100 recursive=1
1103 shift
1104 break
1107 usage
1110 break
1112 esac
1113 shift
1114 done
1116 module_list "$@" |
1117 while read mode sha1 stage sm_path
1119 die_if_unmatched "$mode"
1120 name=$(module_name "$sm_path") || exit
1121 url=$(git config submodule."$name".url)
1122 displaypath="$prefix$sm_path"
1123 if test "$stage" = U
1124 then
1125 say "U$sha1 $displaypath"
1126 continue
1128 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
1129 then
1130 say "-$sha1 $displaypath"
1131 continue;
1133 set_name_rev "$sm_path" "$sha1"
1134 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1135 then
1136 say " $sha1 $displaypath$revname"
1137 else
1138 if test -z "$cached"
1139 then
1140 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1141 set_name_rev "$sm_path" "$sha1"
1143 say "+$sha1 $displaypath$revname"
1146 if test -n "$recursive"
1147 then
1149 prefix="$displaypath/"
1150 clear_local_git_env
1151 cd "$sm_path" &&
1152 eval cmd_status
1153 ) ||
1154 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1156 done
1159 # Sync remote urls for submodules
1160 # This makes the value for remote.$remote.url match the value
1161 # specified in .gitmodules.
1163 cmd_sync()
1165 while test $# -ne 0
1167 case "$1" in
1168 -q|--quiet)
1169 GIT_QUIET=1
1170 shift
1172 --recursive)
1173 recursive=1
1174 shift
1177 shift
1178 break
1181 usage
1184 break
1186 esac
1187 done
1188 cd_to_toplevel
1189 module_list "$@" |
1190 while read mode sha1 stage sm_path
1192 die_if_unmatched "$mode"
1193 name=$(module_name "$sm_path")
1194 url=$(git config -f .gitmodules --get submodule."$name".url)
1196 # Possibly a url relative to parent
1197 case "$url" in
1198 ./*|../*)
1199 # rewrite foo/bar as ../.. to find path from
1200 # submodule work tree to superproject work tree
1201 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1202 # guarantee a trailing /
1203 up_path=${up_path%/}/ &&
1204 # path from submodule work tree to submodule origin repo
1205 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1206 # path from superproject work tree to submodule origin repo
1207 super_config_url=$(resolve_relative_url "$url") || exit
1210 sub_origin_url="$url"
1211 super_config_url="$url"
1213 esac
1215 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1216 then
1217 say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")"
1218 git config submodule."$name".url "$super_config_url"
1220 if test -e "$sm_path"/.git
1221 then
1223 clear_local_git_env
1224 cd "$sm_path"
1225 remote=$(get_default_remote)
1226 git config remote."$remote".url "$sub_origin_url"
1228 if test -n "$recursive"
1229 then
1230 prefix="$prefix$sm_path/"
1231 eval cmd_sync
1236 done
1239 # This loop parses the command line arguments to find the
1240 # subcommand name to dispatch. Parsing of the subcommand specific
1241 # options are primarily done by the subcommand implementations.
1242 # Subcommand specific options such as --branch and --cached are
1243 # parsed here as well, for backward compatibility.
1245 while test $# != 0 && test -z "$command"
1247 case "$1" in
1248 add | foreach | init | deinit | update | status | summary | sync)
1249 command=$1
1251 -q|--quiet)
1252 GIT_QUIET=1
1254 -b|--branch)
1255 case "$2" in
1257 usage
1259 esac
1260 branch="$2"; shift
1262 --cached)
1263 cached="$1"
1266 break
1269 usage
1272 break
1274 esac
1275 shift
1276 done
1278 # No command word defaults to "status"
1279 if test -z "$command"
1280 then
1281 if test $# = 0
1282 then
1283 command=status
1284 else
1285 usage
1289 # "-b branch" is accepted only by "add"
1290 if test -n "$branch" && test "$command" != add
1291 then
1292 usage
1295 # "--cached" is accepted only by "status" and "summary"
1296 if test -n "$cached" && test "$command" != status -a "$command" != summary
1297 then
1298 usage
1301 "cmd_$command" "$@"