Windows: Always normalize paths to Windows-style
[git/mingw/4msysgit.git] / git-submodule.sh
blob372ab01e54b71ba662d41bdc226d9372f1aa2361
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 eval_gettextln "The following path is ignored by one of your .gitignore files:
368 \$sm_path
369 Use -f if you really want to add it." >&2
370 exit 1
373 if test -n "$custom_name"
374 then
375 sm_name="$custom_name"
376 else
377 sm_name="$sm_path"
380 # perhaps the path exists and is already a git repo, else clone it
381 if test -e "$sm_path"
382 then
383 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
384 then
385 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
386 else
387 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
390 else
391 if test -d ".git/modules/$sm_name"
392 then
393 if test -z "$force"
394 then
395 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
396 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
397 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
398 echo >&2 " $realrepo"
399 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
400 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
401 else
402 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
405 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
407 clear_local_git_env
408 cd "$sm_path" &&
409 # ash fails to wordsplit ${branch:+-b "$branch"...}
410 case "$branch" in
411 '') git checkout -f -q ;;
412 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
413 esac
414 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
416 git config submodule."$sm_name".url "$realrepo"
418 git add $force "$sm_path" ||
419 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
421 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
422 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
423 if test -n "$branch"
424 then
425 git config -f .gitmodules submodule."$sm_name".branch "$branch"
426 fi &&
427 git add --force .gitmodules ||
428 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
432 # Execute an arbitrary command sequence in each checked out
433 # submodule
435 # $@ = command to execute
437 cmd_foreach()
439 # parse $args after "submodule ... foreach".
440 while test $# -ne 0
442 case "$1" in
443 -q|--quiet)
444 GIT_QUIET=1
446 --recursive)
447 recursive=1
450 usage
453 break
455 esac
456 shift
457 done
459 toplevel=$(pwd)
461 # dup stdin so that it can be restored when running the external
462 # command in the subshell (and a recursive call to this function)
463 exec 3<&0
465 module_list |
466 while read mode sha1 stage sm_path
468 die_if_unmatched "$mode"
469 if test -e "$sm_path"/.git
470 then
471 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
472 name=$(module_name "$sm_path")
474 prefix="$prefix$sm_path/"
475 clear_local_git_env
476 # we make $path available to scripts ...
477 path=$sm_path
478 cd "$sm_path" &&
479 eval "$@" &&
480 if test -n "$recursive"
481 then
482 cmd_foreach "--recursive" "$@"
484 ) <&3 3<&- ||
485 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
487 done
491 # Register submodules in .git/config
493 # $@ = requested paths (default to all)
495 cmd_init()
497 # parse $args after "submodule ... init".
498 while test $# -ne 0
500 case "$1" in
501 -q|--quiet)
502 GIT_QUIET=1
505 shift
506 break
509 usage
512 break
514 esac
515 shift
516 done
518 module_list "$@" |
519 while read mode sha1 stage sm_path
521 die_if_unmatched "$mode"
522 name=$(module_name "$sm_path") || exit
524 # Copy url setting when it is not set yet
525 if test -z "$(git config "submodule.$name.url")"
526 then
527 url=$(git config -f .gitmodules submodule."$name".url)
528 test -z "$url" &&
529 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
531 # Possibly a url relative to parent
532 case "$url" in
533 ./*|../*)
534 url=$(resolve_relative_url "$url") || exit
536 esac
537 git config submodule."$name".url "$url" ||
538 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
540 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
543 # Copy "update" setting when it is not set yet
544 upd="$(git config -f .gitmodules submodule."$name".update)"
545 test -z "$upd" ||
546 test -n "$(git config submodule."$name".update)" ||
547 git config submodule."$name".update "$upd" ||
548 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
549 done
553 # Unregister submodules from .git/config and remove their work tree
555 # $@ = requested paths (use '.' to deinit all submodules)
557 cmd_deinit()
559 # parse $args after "submodule ... deinit".
560 while test $# -ne 0
562 case "$1" in
563 -f|--force)
564 force=$1
566 -q|--quiet)
567 GIT_QUIET=1
570 shift
571 break
574 usage
577 break
579 esac
580 shift
581 done
583 if test $# = 0
584 then
585 die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
588 module_list "$@" |
589 while read mode sha1 stage sm_path
591 die_if_unmatched "$mode"
592 name=$(module_name "$sm_path") || exit
594 # Remove the submodule work tree (unless the user already did it)
595 if test -d "$sm_path"
596 then
597 # Protect submodules containing a .git directory
598 if test -d "$sm_path/.git"
599 then
600 echo >&2 "$(eval_gettext "Submodule work tree '\$sm_path' contains a .git directory")"
601 die "$(eval_gettext "(use 'rm -rf' if you really want to remove it including all of its history)")"
604 if test -z "$force"
605 then
606 git rm -qn "$sm_path" ||
607 die "$(eval_gettext "Submodule work tree '\$sm_path' contains local modifications; use '-f' to discard them")"
609 rm -rf "$sm_path" &&
610 say "$(eval_gettext "Cleared directory '\$sm_path'")" ||
611 say "$(eval_gettext "Could not remove submodule work tree '\$sm_path'")"
614 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$sm_path'")"
616 # Remove the .git/config entries (unless the user already did it)
617 if test -n "$(git config --get-regexp submodule."$name\.")"
618 then
619 # Remove the whole section so we have a clean state when
620 # the user later decides to init this submodule again
621 url=$(git config submodule."$name".url)
622 git config --remove-section submodule."$name" 2>/dev/null &&
623 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$sm_path'")"
625 done
629 # Update each submodule path to correct revision, using clone and checkout as needed
631 # $@ = requested paths (default to all)
633 cmd_update()
635 # parse $args after "submodule ... update".
636 orig_flags=
637 while test $# -ne 0
639 case "$1" in
640 -q|--quiet)
641 GIT_QUIET=1
643 -i|--init)
644 init=1
646 --remote)
647 remote=1
649 -N|--no-fetch)
650 nofetch=1
652 -f|--force)
653 force=$1
655 -r|--rebase)
656 update="rebase"
658 --reference)
659 case "$2" in '') usage ;; esac
660 reference="--reference=$2"
661 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
662 shift
664 --reference=*)
665 reference="$1"
667 -m|--merge)
668 update="merge"
670 --recursive)
671 recursive=1
673 --checkout)
674 update="checkout"
677 shift
678 break
681 usage
684 break
686 esac
687 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
688 shift
689 done
691 if test -n "$init"
692 then
693 cmd_init "--" "$@" || return
696 cloned_modules=
697 module_list "$@" | {
698 err=
699 while read mode sha1 stage sm_path
701 die_if_unmatched "$mode"
702 if test "$stage" = U
703 then
704 echo >&2 "Skipping unmerged submodule $prefix$sm_path"
705 continue
707 name=$(module_name "$sm_path") || exit
708 url=$(git config submodule."$name".url)
709 branch=$(get_submodule_config "$name" branch master)
710 if ! test -z "$update"
711 then
712 update_module=$update
713 else
714 update_module=$(git config submodule."$name".update)
717 if test "$update_module" = "none"
718 then
719 echo "Skipping submodule '$prefix$sm_path'"
720 continue
723 if test -z "$url"
724 then
725 # Only mention uninitialized submodules when its
726 # path have been specified
727 test "$#" != "0" &&
728 say "$(eval_gettext "Submodule path '\$prefix\$sm_path' not initialized
729 Maybe you want to use 'update --init'?")"
730 continue
733 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
734 then
735 module_clone "$sm_path" "$name" "$url" "$reference" || exit
736 cloned_modules="$cloned_modules;$name"
737 subsha1=
738 else
739 subsha1=$(clear_local_git_env; cd "$sm_path" &&
740 git rev-parse --verify HEAD) ||
741 die "$(eval_gettext "Unable to find current revision in submodule path '\$prefix\$sm_path'")"
744 if test -n "$remote"
745 then
746 if test -z "$nofetch"
747 then
748 # Fetch remote before determining tracking $sha1
749 (clear_local_git_env; cd "$sm_path" && git-fetch) ||
750 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
752 remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
753 sha1=$(clear_local_git_env; cd "$sm_path" &&
754 git rev-parse --verify "${remote_name}/${branch}") ||
755 die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
758 if test "$subsha1" != "$sha1" -o -n "$force"
759 then
760 subforce=$force
761 # If we don't already have a -f flag and the submodule has never been checked out
762 if test -z "$subsha1" -a -z "$force"
763 then
764 subforce="-f"
767 if test -z "$nofetch"
768 then
769 # Run fetch only if $sha1 isn't present or it
770 # is not reachable from a ref.
771 (clear_local_git_env; cd "$sm_path" &&
772 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
773 test -z "$rev") || git-fetch)) ||
774 die "$(eval_gettext "Unable to fetch in submodule path '\$prefix\$sm_path'")"
777 # Is this something we just cloned?
778 case ";$cloned_modules;" in
779 *";$name;"*)
780 # then there is no local change to integrate
781 update_module= ;;
782 esac
784 must_die_on_failure=
785 case "$update_module" in
786 rebase)
787 command="git rebase"
788 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$prefix\$sm_path'")"
789 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': rebased into '\$sha1'")"
790 must_die_on_failure=yes
792 merge)
793 command="git merge"
794 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$prefix\$sm_path'")"
795 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': merged in '\$sha1'")"
796 must_die_on_failure=yes
799 command="git checkout $subforce -q"
800 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$prefix\$sm_path'")"
801 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': checked out '\$sha1'")"
803 esac
805 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
806 then
807 say "$say_msg"
808 elif test -n "$must_die_on_failure"
809 then
810 die_with_status 2 "$die_msg"
811 else
812 err="${err};$die_msg"
813 continue
817 if test -n "$recursive"
818 then
820 prefix="$prefix$sm_path/"
821 clear_local_git_env
822 cd "$sm_path" &&
823 eval cmd_update "$orig_flags"
825 res=$?
826 if test $res -gt 0
827 then
828 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$prefix\$sm_path'")"
829 if test $res -eq 1
830 then
831 err="${err};$die_msg"
832 continue
833 else
834 die_with_status $res "$die_msg"
838 done
840 if test -n "$err"
841 then
842 OIFS=$IFS
843 IFS=';'
844 for e in $err
846 if test -n "$e"
847 then
848 echo >&2 "$e"
850 done
851 IFS=$OIFS
852 exit 1
857 set_name_rev () {
858 revname=$( (
859 clear_local_git_env
860 cd "$1" && {
861 git describe "$2" 2>/dev/null ||
862 git describe --tags "$2" 2>/dev/null ||
863 git describe --contains "$2" 2>/dev/null ||
864 git describe --all --always "$2"
867 test -z "$revname" || revname=" ($revname)"
870 # Show commit summary for submodules in index or working tree
872 # If '--cached' is given, show summary between index and given commit,
873 # or between working tree and given commit
875 # $@ = [commit (default 'HEAD'),] requested paths (default all)
877 cmd_summary() {
878 summary_limit=-1
879 for_status=
880 diff_cmd=diff-index
882 # parse $args after "submodule ... summary".
883 while test $# -ne 0
885 case "$1" in
886 --cached)
887 cached="$1"
889 --files)
890 files="$1"
892 --for-status)
893 for_status="$1"
895 -n|--summary-limit)
896 summary_limit="$2"
897 isnumber "$summary_limit" || usage
898 shift
900 --summary-limit=*)
901 summary_limit="${1#--summary-limit=}"
902 isnumber "$summary_limit" || usage
905 shift
906 break
909 usage
912 break
914 esac
915 shift
916 done
918 test $summary_limit = 0 && return
920 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
921 then
922 head=$rev
923 test $# = 0 || shift
924 elif test -z "$1" -o "$1" = "HEAD"
925 then
926 # before the first commit: compare with an empty tree
927 head=$(git hash-object -w -t tree --stdin </dev/null)
928 test -z "$1" || shift
929 else
930 head="HEAD"
933 if [ -n "$files" ]
934 then
935 test -n "$cached" &&
936 die "$(gettext "The --cached option cannot be used with the --files option")"
937 diff_cmd=diff-files
938 head=
941 cd_to_toplevel
942 # Get modified modules cared by user
943 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
944 sane_egrep '^:([0-7]* )?160000' |
945 while read mod_src mod_dst sha1_src sha1_dst status name
947 # Always show modules deleted or type-changed (blob<->module)
948 test $status = D -o $status = T && echo "$name" && continue
949 # Also show added or modified modules which are checked out
950 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
951 echo "$name"
952 done
955 test -z "$modules" && return
957 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
958 sane_egrep '^:([0-7]* )?160000' |
959 cut -c2- |
960 while read mod_src mod_dst sha1_src sha1_dst status name
962 if test -z "$cached" &&
963 test $sha1_dst = 0000000000000000000000000000000000000000
964 then
965 case "$mod_dst" in
966 160000)
967 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
969 100644 | 100755 | 120000)
970 sha1_dst=$(git hash-object $name)
972 000000)
973 ;; # removed
975 # unexpected type
976 eval_gettextln "unexpected mode \$mod_dst" >&2
977 continue ;;
978 esac
980 missing_src=
981 missing_dst=
983 test $mod_src = 160000 &&
984 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
985 missing_src=t
987 test $mod_dst = 160000 &&
988 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
989 missing_dst=t
991 total_commits=
992 case "$missing_src,$missing_dst" in
994 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
997 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
999 t,t)
1000 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
1003 errmsg=
1004 total_commits=$(
1005 if test $mod_src = 160000 -a $mod_dst = 160000
1006 then
1007 range="$sha1_src...$sha1_dst"
1008 elif test $mod_src = 160000
1009 then
1010 range=$sha1_src
1011 else
1012 range=$sha1_dst
1014 GIT_DIR="$name/.git" \
1015 git rev-list --first-parent $range -- | wc -l
1017 total_commits=" ($(($total_commits + 0)))"
1019 esac
1021 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
1022 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
1023 if test $status = T
1024 then
1025 blob="$(gettext "blob")"
1026 submodule="$(gettext "submodule")"
1027 if test $mod_dst = 160000
1028 then
1029 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1030 else
1031 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1033 else
1034 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1036 if test -n "$errmsg"
1037 then
1038 # Don't give error msg for modification whose dst is not submodule
1039 # i.e. deleted or changed to blob
1040 test $mod_dst = 160000 && echo "$errmsg"
1041 else
1042 if test $mod_src = 160000 -a $mod_dst = 160000
1043 then
1044 limit=
1045 test $summary_limit -gt 0 && limit="-$summary_limit"
1046 GIT_DIR="$name/.git" \
1047 git log $limit --pretty='format: %m %s' \
1048 --first-parent $sha1_src...$sha1_dst
1049 elif test $mod_dst = 160000
1050 then
1051 GIT_DIR="$name/.git" \
1052 git log --pretty='format: > %s' -1 $sha1_dst
1053 else
1054 GIT_DIR="$name/.git" \
1055 git log --pretty='format: < %s' -1 $sha1_src
1057 echo
1059 echo
1060 done |
1061 if test -n "$for_status"; then
1062 if [ -n "$files" ]; then
1063 gettextln "Submodules changed but not updated:" | git stripspace -c
1064 else
1065 gettextln "Submodule changes to be committed:" | git stripspace -c
1067 printf "\n" | git stripspace -c
1068 git stripspace -c
1069 else
1074 # List all submodules, prefixed with:
1075 # - submodule not initialized
1076 # + different revision checked out
1078 # If --cached was specified the revision in the index will be printed
1079 # instead of the currently checked out revision.
1081 # $@ = requested paths (default to all)
1083 cmd_status()
1085 # parse $args after "submodule ... status".
1086 while test $# -ne 0
1088 case "$1" in
1089 -q|--quiet)
1090 GIT_QUIET=1
1092 --cached)
1093 cached=1
1095 --recursive)
1096 recursive=1
1099 shift
1100 break
1103 usage
1106 break
1108 esac
1109 shift
1110 done
1112 module_list "$@" |
1113 while read mode sha1 stage sm_path
1115 die_if_unmatched "$mode"
1116 name=$(module_name "$sm_path") || exit
1117 url=$(git config submodule."$name".url)
1118 displaypath="$prefix$sm_path"
1119 if test "$stage" = U
1120 then
1121 say "U$sha1 $displaypath"
1122 continue
1124 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
1125 then
1126 say "-$sha1 $displaypath"
1127 continue;
1129 set_name_rev "$sm_path" "$sha1"
1130 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1131 then
1132 say " $sha1 $displaypath$revname"
1133 else
1134 if test -z "$cached"
1135 then
1136 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1137 set_name_rev "$sm_path" "$sha1"
1139 say "+$sha1 $displaypath$revname"
1142 if test -n "$recursive"
1143 then
1145 prefix="$displaypath/"
1146 clear_local_git_env
1147 cd "$sm_path" &&
1148 eval cmd_status
1149 ) ||
1150 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1152 done
1155 # Sync remote urls for submodules
1156 # This makes the value for remote.$remote.url match the value
1157 # specified in .gitmodules.
1159 cmd_sync()
1161 while test $# -ne 0
1163 case "$1" in
1164 -q|--quiet)
1165 GIT_QUIET=1
1166 shift
1168 --recursive)
1169 recursive=1
1170 shift
1173 shift
1174 break
1177 usage
1180 break
1182 esac
1183 done
1184 cd_to_toplevel
1185 module_list "$@" |
1186 while read mode sha1 stage sm_path
1188 die_if_unmatched "$mode"
1189 name=$(module_name "$sm_path")
1190 url=$(git config -f .gitmodules --get submodule."$name".url)
1192 # Possibly a url relative to parent
1193 case "$url" in
1194 ./*|../*)
1195 # rewrite foo/bar as ../.. to find path from
1196 # submodule work tree to superproject work tree
1197 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1198 # guarantee a trailing /
1199 up_path=${up_path%/}/ &&
1200 # path from submodule work tree to submodule origin repo
1201 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1202 # path from superproject work tree to submodule origin repo
1203 super_config_url=$(resolve_relative_url "$url") || exit
1206 sub_origin_url="$url"
1207 super_config_url="$url"
1209 esac
1211 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1212 then
1213 say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")"
1214 git config submodule."$name".url "$super_config_url"
1216 if test -e "$sm_path"/.git
1217 then
1219 clear_local_git_env
1220 cd "$sm_path"
1221 remote=$(get_default_remote)
1222 git config remote."$remote".url "$sub_origin_url"
1224 if test -n "$recursive"
1225 then
1226 prefix="$prefix$sm_path/"
1227 eval cmd_sync
1232 done
1235 # This loop parses the command line arguments to find the
1236 # subcommand name to dispatch. Parsing of the subcommand specific
1237 # options are primarily done by the subcommand implementations.
1238 # Subcommand specific options such as --branch and --cached are
1239 # parsed here as well, for backward compatibility.
1241 while test $# != 0 && test -z "$command"
1243 case "$1" in
1244 add | foreach | init | deinit | update | status | summary | sync)
1245 command=$1
1247 -q|--quiet)
1248 GIT_QUIET=1
1250 -b|--branch)
1251 case "$2" in
1253 usage
1255 esac
1256 branch="$2"; shift
1258 --cached)
1259 cached="$1"
1262 break
1265 usage
1268 break
1270 esac
1271 shift
1272 done
1274 # No command word defaults to "status"
1275 if test -z "$command"
1276 then
1277 if test $# = 0
1278 then
1279 command=status
1280 else
1281 usage
1285 # "-b branch" is accepted only by "add"
1286 if test -n "$branch" && test "$command" != add
1287 then
1288 usage
1291 # "--cached" is accepted only by "status" and "summary"
1292 if test -n "$cached" && test "$command" != status -a "$command" != summary
1293 then
1294 usage
1297 "cmd_$command" "$@"