t0008: avoid absolute path on Windows as colon is used in the tests
[git/mingw/4msysgit.git] / git-submodule.sh
blob2016db907ad4b4f7557886c16be3748e01839180
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 --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 while (<STDIN>) {
125 if (/^unmatched pathspec/) {
126 $unmatched = 1;
127 next;
129 chomp;
130 my ($mode, $sha1, $stage, $path) =
131 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
132 next unless $mode eq "160000";
133 if ($stage ne "0") {
134 if (!$unmerged{$path}++) {
135 push @out, "$mode $null_sha1 U\t$path\n";
137 next;
139 push @out, "$_\n";
141 if ($unmatched) {
142 print "#unmatched\n";
143 } else {
144 print for (@out);
149 die_if_unmatched ()
151 if test "$1" = "#unmatched"
152 then
153 exit 1
158 # Print a submodule configuration setting
160 # $1 = submodule name
161 # $2 = option name
162 # $3 = default value
164 # Checks in the usual git-config places first (for overrides),
165 # otherwise it falls back on .gitmodules. This allows you to
166 # distribute project-wide defaults in .gitmodules, while still
167 # customizing individual repositories if necessary. If the option is
168 # not in .gitmodules either, print a default value.
170 get_submodule_config () {
171 name="$1"
172 option="$2"
173 default="$3"
174 value=$(git config submodule."$name"."$option")
175 if test -z "$value"
176 then
177 value=$(git config -f .gitmodules submodule."$name"."$option")
179 printf '%s' "${value:-$default}"
184 # Map submodule path to submodule name
186 # $1 = path
188 module_name()
190 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
191 sm_path="$1"
192 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
193 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
194 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
195 test -z "$name" &&
196 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
197 echo "$name"
201 # Clone a submodule
203 # Prior to calling, cmd_update checks that a possibly existing
204 # path is not a git repository.
205 # Likewise, cmd_add checks that path does not exist at all,
206 # since it is the location of a new submodule.
208 module_clone()
210 sm_path=$1
211 name=$2
212 url=$3
213 reference="$4"
214 quiet=
215 if test -n "$GIT_QUIET"
216 then
217 quiet=-q
220 gitdir=
221 gitdir_base=
222 base_name=$(dirname "$name")
224 gitdir=$(git rev-parse --git-dir)
225 gitdir_base="$gitdir/modules/$base_name"
226 gitdir="$gitdir/modules/$name"
228 if test -d "$gitdir"
229 then
230 mkdir -p "$sm_path"
231 rm -f "$gitdir/index"
232 else
233 mkdir -p "$gitdir_base"
235 clear_local_git_env
236 git clone $quiet -n ${reference:+"$reference"} \
237 --separate-git-dir "$gitdir" "$url" "$sm_path"
238 ) ||
239 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
242 # We already are at the root of the work tree but cd_to_toplevel will
243 # resolve any symlinks that might be present in $PWD
244 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
245 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
246 # Remove all common leading directories after a sanity check
247 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
248 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
250 while test "${a%%/*}" = "${b%%/*}"
252 a=${a#*/}
253 b=${b#*/}
254 done
255 # Now chop off the trailing '/'s that were added in the beginning
256 a=${a%/}
257 b=${b%/}
259 # Turn each leading "*/" component into "../"
260 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
261 echo "gitdir: $rel/$a" >"$sm_path/.git"
263 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
264 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
267 isnumber()
269 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
273 # Add a new submodule to the working tree, .gitmodules and the index
275 # $@ = repo path
277 # optional branch is stored in global branch variable
279 cmd_add()
281 # parse $args after "submodule ... add".
282 while test $# -ne 0
284 case "$1" in
285 -b | --branch)
286 case "$2" in '') usage ;; esac
287 branch=$2
288 shift
290 -f | --force)
291 force=$1
293 -q|--quiet)
294 GIT_QUIET=1
296 --reference)
297 case "$2" in '') usage ;; esac
298 reference="--reference=$2"
299 shift
301 --reference=*)
302 reference="$1"
304 --name)
305 case "$2" in '') usage ;; esac
306 custom_name=$2
307 shift
310 shift
311 break
314 usage
317 break
319 esac
320 shift
321 done
323 repo=$1
324 sm_path=$2
326 if test -z "$sm_path"; then
327 sm_path=$(echo "$repo" |
328 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
331 if test -z "$repo" -o -z "$sm_path"; then
332 usage
335 # assure repo is absolute or relative to parent
336 case "$repo" in
337 ./*|../*)
338 # dereference source url relative to parent's url
339 realrepo=$(resolve_relative_url "$repo") || exit
341 *:*|/*)
342 # absolute url
343 realrepo=$repo
346 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
348 esac
350 # normalize path:
351 # multiple //; leading ./; /./; /../; trailing /
352 sm_path=$(printf '%s/\n' "$sm_path" |
353 sed -e '
354 s|//*|/|g
355 s|^\(\./\)*||
356 s|/\./|/|g
357 :start
358 s|\([^/]*\)/\.\./||
359 tstart
360 s|/*$||
362 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
363 die "$(eval_gettext "'\$sm_path' already exists in the index")"
365 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
366 then
367 cat >&2 <<EOF
368 The following path is ignored by one of your .gitignore files:
369 $(eval_gettextln $sm_path)
370 Use -f if you really want to add it.
372 exit 1
375 if test -n "$custom_name"
376 then
377 sm_name="$custom_name"
378 else
379 sm_name="$sm_path"
382 # perhaps the path exists and is already a git repo, else clone it
383 if test -e "$sm_path"
384 then
385 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
386 then
387 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
388 else
389 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
392 else
393 if test -d ".git/modules/$sm_name"
394 then
395 if test -z "$force"
396 then
397 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
398 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
399 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
400 echo >&2 " $realrepo"
401 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
402 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
403 else
404 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
407 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
409 clear_local_git_env
410 cd "$sm_path" &&
411 # ash fails to wordsplit ${branch:+-b "$branch"...}
412 case "$branch" in
413 '') git checkout -f -q ;;
414 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
415 esac
416 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
418 git config submodule."$sm_name".url "$realrepo"
420 git add $force "$sm_path" ||
421 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
423 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
424 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
425 if test -n "$branch"
426 then
427 git config -f .gitmodules submodule."$sm_name".branch "$branch"
428 fi &&
429 git add --force .gitmodules ||
430 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
434 # Execute an arbitrary command sequence in each checked out
435 # submodule
437 # $@ = command to execute
439 cmd_foreach()
441 # parse $args after "submodule ... foreach".
442 while test $# -ne 0
444 case "$1" in
445 -q|--quiet)
446 GIT_QUIET=1
448 --recursive)
449 recursive=1
452 usage
455 break
457 esac
458 shift
459 done
461 toplevel=$(pwd)
463 # dup stdin so that it can be restored when running the external
464 # command in the subshell (and a recursive call to this function)
465 exec 3<&0
467 module_list |
468 while read mode sha1 stage sm_path
470 die_if_unmatched "$mode"
471 if test -e "$sm_path"/.git
472 then
473 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
474 name=$(module_name "$sm_path")
476 prefix="$prefix$sm_path/"
477 clear_local_git_env
478 # we make $path available to scripts ...
479 path=$sm_path
480 cd "$sm_path" &&
481 eval "$@" &&
482 if test -n "$recursive"
483 then
484 cmd_foreach "--recursive" "$@"
486 ) <&3 3<&- ||
487 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
489 done
493 # Register submodules in .git/config
495 # $@ = requested paths (default to all)
497 cmd_init()
499 # parse $args after "submodule ... init".
500 while test $# -ne 0
502 case "$1" in
503 -q|--quiet)
504 GIT_QUIET=1
507 shift
508 break
511 usage
514 break
516 esac
517 shift
518 done
520 module_list "$@" |
521 while read mode sha1 stage sm_path
523 die_if_unmatched "$mode"
524 name=$(module_name "$sm_path") || exit
526 # Copy url setting when it is not set yet
527 if test -z "$(git config "submodule.$name.url")"
528 then
529 url=$(git config -f .gitmodules submodule."$name".url)
530 test -z "$url" &&
531 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
533 # Possibly a url relative to parent
534 case "$url" in
535 ./*|../*)
536 url=$(resolve_relative_url "$url") || exit
538 esac
539 git config submodule."$name".url "$url" ||
540 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
542 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
545 # Copy "update" setting when it is not set yet
546 upd="$(git config -f .gitmodules submodule."$name".update)"
547 test -z "$upd" ||
548 test -n "$(git config submodule."$name".update)" ||
549 git config submodule."$name".update "$upd" ||
550 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
551 done
555 # Unregister submodules from .git/config and remove their work tree
557 # $@ = requested paths (use '.' to deinit all submodules)
559 cmd_deinit()
561 # parse $args after "submodule ... deinit".
562 while test $# -ne 0
564 case "$1" in
565 -f|--force)
566 force=$1
568 -q|--quiet)
569 GIT_QUIET=1
572 shift
573 break
576 usage
579 break
581 esac
582 shift
583 done
585 if test $# = 0
586 then
587 die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
590 module_list "$@" |
591 while read mode sha1 stage sm_path
593 die_if_unmatched "$mode"
594 name=$(module_name "$sm_path") || exit
596 # Remove the submodule work tree (unless the user already did it)
597 if test -d "$sm_path"
598 then
599 # Protect submodules containing a .git directory
600 if test -d "$sm_path/.git"
601 then
602 echo >&2 "$(eval_gettext "Submodule work tree '\$sm_path' contains a .git directory")"
603 die "$(eval_gettext "(use 'rm -rf' if you really want to remove it including all of its history)")"
606 if test -z "$force"
607 then
608 git rm -qn "$sm_path" ||
609 die "$(eval_gettext "Submodule work tree '\$sm_path' contains local modifications; use '-f' to discard them")"
611 rm -rf "$sm_path" &&
612 say "$(eval_gettext "Cleared directory '\$sm_path'")" ||
613 say "$(eval_gettext "Could not remove submodule work tree '\$sm_path'")"
616 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$sm_path'")"
618 # Remove the .git/config entries (unless the user already did it)
619 if test -n "$(git config --get-regexp submodule."$name\.")"
620 then
621 # Remove the whole section so we have a clean state when
622 # the user later decides to init this submodule again
623 url=$(git config submodule."$name".url)
624 git config --remove-section submodule."$name" 2>/dev/null &&
625 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$sm_path'")"
627 done
631 # Update each submodule path to correct revision, using clone and checkout as needed
633 # $@ = requested paths (default to all)
635 cmd_update()
637 # parse $args after "submodule ... update".
638 orig_flags=
639 while test $# -ne 0
641 case "$1" in
642 -q|--quiet)
643 GIT_QUIET=1
645 -i|--init)
646 init=1
648 --remote)
649 remote=1
651 -N|--no-fetch)
652 nofetch=1
654 -f|--force)
655 force=$1
657 -r|--rebase)
658 update="rebase"
660 --reference)
661 case "$2" in '') usage ;; esac
662 reference="--reference=$2"
663 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
664 shift
666 --reference=*)
667 reference="$1"
669 -m|--merge)
670 update="merge"
672 --recursive)
673 recursive=1
675 --checkout)
676 update="checkout"
679 shift
680 break
683 usage
686 break
688 esac
689 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
690 shift
691 done
693 if test -n "$init"
694 then
695 cmd_init "--" "$@" || return
698 cloned_modules=
699 module_list "$@" | {
700 err=
701 while read mode sha1 stage sm_path
703 die_if_unmatched "$mode"
704 if test "$stage" = U
705 then
706 echo >&2 "Skipping unmerged submodule $prefix$sm_path"
707 continue
709 name=$(module_name "$sm_path") || exit
710 url=$(git config submodule."$name".url)
711 branch=$(get_submodule_config "$name" branch master)
712 if ! test -z "$update"
713 then
714 update_module=$update
715 else
716 update_module=$(git config submodule."$name".update)
719 if test "$update_module" = "none"
720 then
721 echo "Skipping submodule '$prefix$sm_path'"
722 continue
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 '\$prefix\$sm_path' not initialized
731 Maybe you want to use 'update --init'?")"
732 continue
735 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
736 then
737 module_clone "$sm_path" "$name" "$url" "$reference" || 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 '\$prefix\$sm_path'")"
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'")"
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'")"
760 if test "$subsha1" != "$sha1" -o -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" -a -z "$force"
765 then
766 subforce="-f"
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 (clear_local_git_env; cd "$sm_path" &&
774 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
775 test -z "$rev") || git-fetch)) ||
776 die "$(eval_gettext "Unable to fetch in submodule path '\$prefix\$sm_path'")"
779 # Is this something we just cloned?
780 case ";$cloned_modules;" in
781 *";$name;"*)
782 # then there is no local change to integrate
783 update_module= ;;
784 esac
786 must_die_on_failure=
787 case "$update_module" in
788 rebase)
789 command="git rebase"
790 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$prefix\$sm_path'")"
791 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': rebased into '\$sha1'")"
792 must_die_on_failure=yes
794 merge)
795 command="git merge"
796 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$prefix\$sm_path'")"
797 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': merged in '\$sha1'")"
798 must_die_on_failure=yes
801 command="git checkout $subforce -q"
802 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$prefix\$sm_path'")"
803 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': checked out '\$sha1'")"
805 esac
807 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
808 then
809 say "$say_msg"
810 elif test -n "$must_die_on_failure"
811 then
812 die_with_status 2 "$die_msg"
813 else
814 err="${err};$die_msg"
815 continue
819 if test -n "$recursive"
820 then
822 prefix="$prefix$sm_path/"
823 clear_local_git_env
824 cd "$sm_path" &&
825 eval cmd_update "$orig_flags"
827 res=$?
828 if test $res -gt 0
829 then
830 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$prefix\$sm_path'")"
831 if test $res -eq 1
832 then
833 err="${err};$die_msg"
834 continue
835 else
836 die_with_status $res "$die_msg"
840 done
842 if test -n "$err"
843 then
844 OIFS=$IFS
845 IFS=';'
846 for e in $err
848 if test -n "$e"
849 then
850 echo >&2 "$e"
852 done
853 IFS=$OIFS
854 exit 1
859 set_name_rev () {
860 revname=$( (
861 clear_local_git_env
862 cd "$1" && {
863 git describe "$2" 2>/dev/null ||
864 git describe --tags "$2" 2>/dev/null ||
865 git describe --contains "$2" 2>/dev/null ||
866 git describe --all --always "$2"
869 test -z "$revname" || revname=" ($revname)"
872 # Show commit summary for submodules in index or working tree
874 # If '--cached' is given, show summary between index and given commit,
875 # or between working tree and given commit
877 # $@ = [commit (default 'HEAD'),] requested paths (default all)
879 cmd_summary() {
880 summary_limit=-1
881 for_status=
882 diff_cmd=diff-index
884 # parse $args after "submodule ... summary".
885 while test $# -ne 0
887 case "$1" in
888 --cached)
889 cached="$1"
891 --files)
892 files="$1"
894 --for-status)
895 for_status="$1"
897 -n|--summary-limit)
898 summary_limit="$2"
899 isnumber "$summary_limit" || usage
900 shift
902 --summary-limit=*)
903 summary_limit="${1#--summary-limit=}"
904 isnumber "$summary_limit" || usage
907 shift
908 break
911 usage
914 break
916 esac
917 shift
918 done
920 test $summary_limit = 0 && return
922 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
923 then
924 head=$rev
925 test $# = 0 || shift
926 elif test -z "$1" -o "$1" = "HEAD"
927 then
928 # before the first commit: compare with an empty tree
929 head=$(git hash-object -w -t tree --stdin </dev/null)
930 test -z "$1" || shift
931 else
932 head="HEAD"
935 if [ -n "$files" ]
936 then
937 test -n "$cached" &&
938 die "$(gettext "The --cached option cannot be used with the --files option")"
939 diff_cmd=diff-files
940 head=
943 cd_to_toplevel
944 # Get modified modules cared by user
945 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
946 sane_egrep '^:([0-7]* )?160000' |
947 while read mod_src mod_dst sha1_src sha1_dst status name
949 # Always show modules deleted or type-changed (blob<->module)
950 test $status = D -o $status = T && echo "$name" && continue
951 # Also show added or modified modules which are checked out
952 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
953 echo "$name"
954 done
957 test -z "$modules" && return
959 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
960 sane_egrep '^:([0-7]* )?160000' |
961 cut -c2- |
962 while read mod_src mod_dst sha1_src sha1_dst status name
964 if test -z "$cached" &&
965 test $sha1_dst = 0000000000000000000000000000000000000000
966 then
967 case "$mod_dst" in
968 160000)
969 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
971 100644 | 100755 | 120000)
972 sha1_dst=$(git hash-object $name)
974 000000)
975 ;; # removed
977 # unexpected type
978 eval_gettextln "unexpected mode \$mod_dst" >&2
979 continue ;;
980 esac
982 missing_src=
983 missing_dst=
985 test $mod_src = 160000 &&
986 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
987 missing_src=t
989 test $mod_dst = 160000 &&
990 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
991 missing_dst=t
993 total_commits=
994 case "$missing_src,$missing_dst" in
996 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
999 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
1001 t,t)
1002 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
1005 errmsg=
1006 total_commits=$(
1007 if test $mod_src = 160000 -a $mod_dst = 160000
1008 then
1009 range="$sha1_src...$sha1_dst"
1010 elif test $mod_src = 160000
1011 then
1012 range=$sha1_src
1013 else
1014 range=$sha1_dst
1016 GIT_DIR="$name/.git" \
1017 git rev-list --first-parent $range -- | wc -l
1019 total_commits=" ($(($total_commits + 0)))"
1021 esac
1023 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
1024 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
1025 if test $status = T
1026 then
1027 blob="$(gettext "blob")"
1028 submodule="$(gettext "submodule")"
1029 if test $mod_dst = 160000
1030 then
1031 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1032 else
1033 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1035 else
1036 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1038 if test -n "$errmsg"
1039 then
1040 # Don't give error msg for modification whose dst is not submodule
1041 # i.e. deleted or changed to blob
1042 test $mod_dst = 160000 && echo "$errmsg"
1043 else
1044 if test $mod_src = 160000 -a $mod_dst = 160000
1045 then
1046 limit=
1047 test $summary_limit -gt 0 && limit="-$summary_limit"
1048 GIT_DIR="$name/.git" \
1049 git log $limit --pretty='format: %m %s' \
1050 --first-parent $sha1_src...$sha1_dst
1051 elif test $mod_dst = 160000
1052 then
1053 GIT_DIR="$name/.git" \
1054 git log --pretty='format: > %s' -1 $sha1_dst
1055 else
1056 GIT_DIR="$name/.git" \
1057 git log --pretty='format: < %s' -1 $sha1_src
1059 echo
1061 echo
1062 done |
1063 if test -n "$for_status"; then
1064 if [ -n "$files" ]; then
1065 gettextln "Submodules changed but not updated:" | git stripspace -c
1066 else
1067 gettextln "Submodule changes to be committed:" | git stripspace -c
1069 printf "\n" | git stripspace -c
1070 git stripspace -c
1071 else
1076 # List all submodules, prefixed with:
1077 # - submodule not initialized
1078 # + different revision checked out
1080 # If --cached was specified the revision in the index will be printed
1081 # instead of the currently checked out revision.
1083 # $@ = requested paths (default to all)
1085 cmd_status()
1087 # parse $args after "submodule ... status".
1088 while test $# -ne 0
1090 case "$1" in
1091 -q|--quiet)
1092 GIT_QUIET=1
1094 --cached)
1095 cached=1
1097 --recursive)
1098 recursive=1
1101 shift
1102 break
1105 usage
1108 break
1110 esac
1111 shift
1112 done
1114 module_list "$@" |
1115 while read mode sha1 stage sm_path
1117 die_if_unmatched "$mode"
1118 name=$(module_name "$sm_path") || exit
1119 url=$(git config submodule."$name".url)
1120 displaypath="$prefix$sm_path"
1121 if test "$stage" = U
1122 then
1123 say "U$sha1 $displaypath"
1124 continue
1126 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
1127 then
1128 say "-$sha1 $displaypath"
1129 continue;
1131 set_name_rev "$sm_path" "$sha1"
1132 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1133 then
1134 say " $sha1 $displaypath$revname"
1135 else
1136 if test -z "$cached"
1137 then
1138 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1139 set_name_rev "$sm_path" "$sha1"
1141 say "+$sha1 $displaypath$revname"
1144 if test -n "$recursive"
1145 then
1147 prefix="$displaypath/"
1148 clear_local_git_env
1149 cd "$sm_path" &&
1150 eval cmd_status
1151 ) ||
1152 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1154 done
1157 # Sync remote urls for submodules
1158 # This makes the value for remote.$remote.url match the value
1159 # specified in .gitmodules.
1161 cmd_sync()
1163 while test $# -ne 0
1165 case "$1" in
1166 -q|--quiet)
1167 GIT_QUIET=1
1168 shift
1170 --recursive)
1171 recursive=1
1172 shift
1175 shift
1176 break
1179 usage
1182 break
1184 esac
1185 done
1186 cd_to_toplevel
1187 module_list "$@" |
1188 while read mode sha1 stage sm_path
1190 die_if_unmatched "$mode"
1191 name=$(module_name "$sm_path")
1192 url=$(git config -f .gitmodules --get submodule."$name".url)
1194 # Possibly a url relative to parent
1195 case "$url" in
1196 ./*|../*)
1197 # rewrite foo/bar as ../.. to find path from
1198 # submodule work tree to superproject work tree
1199 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1200 # guarantee a trailing /
1201 up_path=${up_path%/}/ &&
1202 # path from submodule work tree to submodule origin repo
1203 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1204 # path from superproject work tree to submodule origin repo
1205 super_config_url=$(resolve_relative_url "$url") || exit
1208 sub_origin_url="$url"
1209 super_config_url="$url"
1211 esac
1213 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1214 then
1215 say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")"
1216 git config submodule."$name".url "$super_config_url"
1218 if test -e "$sm_path"/.git
1219 then
1221 clear_local_git_env
1222 cd "$sm_path"
1223 remote=$(get_default_remote)
1224 git config remote."$remote".url "$sub_origin_url"
1226 if test -n "$recursive"
1227 then
1228 prefix="$prefix$sm_path/"
1229 eval cmd_sync
1234 done
1237 # This loop parses the command line arguments to find the
1238 # subcommand name to dispatch. Parsing of the subcommand specific
1239 # options are primarily done by the subcommand implementations.
1240 # Subcommand specific options such as --branch and --cached are
1241 # parsed here as well, for backward compatibility.
1243 while test $# != 0 && test -z "$command"
1245 case "$1" in
1246 add | foreach | init | deinit | update | status | summary | sync)
1247 command=$1
1249 -q|--quiet)
1250 GIT_QUIET=1
1252 -b|--branch)
1253 case "$2" in
1255 usage
1257 esac
1258 branch="$2"; shift
1260 --cached)
1261 cached="$1"
1264 break
1267 usage
1270 break
1272 esac
1273 shift
1274 done
1276 # No command word defaults to "status"
1277 if test -z "$command"
1278 then
1279 if test $# = 0
1280 then
1281 command=status
1282 else
1283 usage
1287 # "-b branch" is accepted only by "add"
1288 if test -n "$branch" && test "$command" != add
1289 then
1290 usage
1293 # "--cached" is accepted only by "status" and "summary"
1294 if test -n "$cached" && test "$command" != status -a "$command" != summary
1295 then
1296 usage
1299 "cmd_$command" "$@"