submodule: fix sync handling of some relative superproject origin URLs
[git/mingw.git] / git-submodule.sh
blob0a3e1465e39cb9c5973718496ff6867728884ec0
1 #!/bin/sh
3 # git-submodules.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] [--reference <repository>] [--] <repository> [<path>]
9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
12 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
13 or: $dashless [--quiet] foreach [--recursive] <command>
14 or: $dashless [--quiet] sync [--] [<path>...]"
15 OPTIONS_SPEC=
16 . git-sh-setup
17 . git-sh-i18n
18 . git-parse-remote
19 require_work_tree
21 command=
22 branch=
23 force=
24 reference=
25 cached=
26 recursive=
27 init=
28 files=
29 nofetch=
30 update=
31 prefix=
33 # The function takes at most 2 arguments. The first argument is the
34 # URL that navigates to the submodule origin repo. When relative, this URL
35 # is relative to the superproject origin URL repo. The second up_path
36 # argument, if specified, is the relative path that navigates
37 # from the submodule working tree to the superproject working tree.
39 # The output of the function is the origin URL of the submodule.
41 # The output will either be an absolute URL or filesystem path (if the
42 # superproject origin URL is an absolute URL or filesystem path,
43 # respectively) or a relative file system path (if the superproject
44 # origin URL is a relative file system path).
46 # When the output is a relative file system path, the path is either
47 # relative to the submodule working tree, if up_path is specified, or to
48 # the superproject working tree otherwise.
49 resolve_relative_url ()
51 remote=$(get_default_remote)
52 remoteurl=$(git config "remote.$remote.url") ||
53 remoteurl=$(pwd) # the repository is its own authoritative upstream
54 url="$1"
55 remoteurl=${remoteurl%/}
56 sep=/
57 up_path="$2"
59 case "$remoteurl" in
60 *:*|/*)
61 is_relative=
64 is_relative=t
66 esac
68 while test -n "$url"
70 case "$url" in
71 ../*)
72 url="${url#../}"
73 case "$remoteurl" in
74 */*)
75 remoteurl="${remoteurl%/*}"
77 *:*)
78 remoteurl="${remoteurl%:*}"
79 sep=:
82 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
84 esac
86 ./*)
87 url="${url#./}"
90 break;;
91 esac
92 done
93 echo "${is_relative:+${up_path}}$remoteurl$sep${url%/}"
97 # Get submodule info for registered submodules
98 # $@ = path to limit submodule list
100 module_list()
102 git ls-files --error-unmatch --stage -- "$@" |
103 perl -e '
104 my %unmerged = ();
105 my ($null_sha1) = ("0" x 40);
106 while (<STDIN>) {
107 chomp;
108 my ($mode, $sha1, $stage, $path) =
109 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
110 next unless $mode eq "160000";
111 if ($stage ne "0") {
112 if (!$unmerged{$path}++) {
113 print "$mode $null_sha1 U\t$path\n";
115 next;
117 print "$_\n";
123 # Map submodule path to submodule name
125 # $1 = path
127 module_name()
129 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
130 sm_path="$1"
131 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
132 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
133 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
134 test -z "$name" &&
135 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
136 echo "$name"
140 # Clone a submodule
142 # Prior to calling, cmd_update checks that a possibly existing
143 # path is not a git repository.
144 # Likewise, cmd_add checks that path does not exist at all,
145 # since it is the location of a new submodule.
147 module_clone()
149 sm_path=$1
150 url=$2
151 reference="$3"
152 quiet=
153 if test -n "$GIT_QUIET"
154 then
155 quiet=-q
158 gitdir=
159 gitdir_base=
160 name=$(module_name "$sm_path" 2>/dev/null)
161 test -n "$name" || name="$sm_path"
162 base_name=$(dirname "$name")
164 gitdir=$(git rev-parse --git-dir)
165 gitdir_base="$gitdir/modules/$base_name"
166 gitdir="$gitdir/modules/$name"
168 if test -d "$gitdir"
169 then
170 mkdir -p "$sm_path"
171 rm -f "$gitdir/index"
172 else
173 mkdir -p "$gitdir_base"
174 git clone $quiet -n ${reference:+"$reference"} \
175 --separate-git-dir "$gitdir" "$url" "$sm_path" ||
176 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
179 a=$(cd "$gitdir" && pwd)/
180 b=$(cd "$sm_path" && pwd)/
181 # normalize Windows-style absolute paths to POSIX-style absolute paths
182 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
183 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
184 # Remove all common leading directories after a sanity check
185 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
186 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
188 while test "${a%%/*}" = "${b%%/*}"
190 a=${a#*/}
191 b=${b#*/}
192 done
193 # Now chop off the trailing '/'s that were added in the beginning
194 a=${a%/}
195 b=${b%/}
197 # Turn each leading "*/" component into "../"
198 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
199 echo "gitdir: $rel/$a" >"$sm_path/.git"
201 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
202 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
206 # Add a new submodule to the working tree, .gitmodules and the index
208 # $@ = repo path
210 # optional branch is stored in global branch variable
212 cmd_add()
214 # parse $args after "submodule ... add".
215 while test $# -ne 0
217 case "$1" in
218 -b | --branch)
219 case "$2" in '') usage ;; esac
220 branch=$2
221 shift
223 -f | --force)
224 force=$1
226 -q|--quiet)
227 GIT_QUIET=1
229 --reference)
230 case "$2" in '') usage ;; esac
231 reference="--reference=$2"
232 shift
234 --reference=*)
235 reference="$1"
236 shift
239 shift
240 break
243 usage
246 break
248 esac
249 shift
250 done
252 repo=$1
253 sm_path=$2
255 if test -z "$sm_path"; then
256 sm_path=$(echo "$repo" |
257 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
260 if test -z "$repo" -o -z "$sm_path"; then
261 usage
264 # assure repo is absolute or relative to parent
265 case "$repo" in
266 ./*|../*)
267 # dereference source url relative to parent's url
268 realrepo=$(resolve_relative_url "$repo") || exit
270 *:*|/*)
271 # absolute url
272 realrepo=$repo
275 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
277 esac
279 # normalize path:
280 # multiple //; leading ./; /./; /../; trailing /
281 sm_path=$(printf '%s/\n' "$sm_path" |
282 sed -e '
283 s|//*|/|g
284 s|^\(\./\)*||
285 s|/\./|/|g
286 :start
287 s|\([^/]*\)/\.\./||
288 tstart
289 s|/*$||
291 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
292 die "$(eval_gettext "'\$sm_path' already exists in the index")"
294 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
295 then
296 eval_gettextln "The following path is ignored by one of your .gitignore files:
297 \$sm_path
298 Use -f if you really want to add it." >&2
299 exit 1
302 # perhaps the path exists and is already a git repo, else clone it
303 if test -e "$sm_path"
304 then
305 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
306 then
307 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
308 else
309 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
312 else
314 module_clone "$sm_path" "$realrepo" "$reference" || exit
316 clear_local_git_env
317 cd "$sm_path" &&
318 # ash fails to wordsplit ${branch:+-b "$branch"...}
319 case "$branch" in
320 '') git checkout -f -q ;;
321 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
322 esac
323 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
325 git config submodule."$sm_path".url "$realrepo"
327 git add $force "$sm_path" ||
328 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
330 git config -f .gitmodules submodule."$sm_path".path "$sm_path" &&
331 git config -f .gitmodules submodule."$sm_path".url "$repo" &&
332 git add --force .gitmodules ||
333 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
337 # Execute an arbitrary command sequence in each checked out
338 # submodule
340 # $@ = command to execute
342 cmd_foreach()
344 # parse $args after "submodule ... foreach".
345 while test $# -ne 0
347 case "$1" in
348 -q|--quiet)
349 GIT_QUIET=1
351 --recursive)
352 recursive=1
355 usage
358 break
360 esac
361 shift
362 done
364 toplevel=$(pwd)
366 # dup stdin so that it can be restored when running the external
367 # command in the subshell (and a recursive call to this function)
368 exec 3<&0
370 module_list |
371 while read mode sha1 stage sm_path
373 if test -e "$sm_path"/.git
374 then
375 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
376 name=$(module_name "$sm_path")
378 prefix="$prefix$sm_path/"
379 clear_local_git_env
380 # we make $path available to scripts ...
381 path=$sm_path
382 cd "$sm_path" &&
383 eval "$@" &&
384 if test -n "$recursive"
385 then
386 cmd_foreach "--recursive" "$@"
388 ) <&3 3<&- ||
389 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
391 done
395 # Register submodules in .git/config
397 # $@ = requested paths (default to all)
399 cmd_init()
401 # parse $args after "submodule ... init".
402 while test $# -ne 0
404 case "$1" in
405 -q|--quiet)
406 GIT_QUIET=1
409 shift
410 break
413 usage
416 break
418 esac
419 shift
420 done
422 module_list "$@" |
423 while read mode sha1 stage sm_path
425 # Skip already registered paths
426 name=$(module_name "$sm_path") || exit
427 if test -z "$(git config "submodule.$name.url")"
428 then
429 url=$(git config -f .gitmodules submodule."$name".url)
430 test -z "$url" &&
431 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
433 # Possibly a url relative to parent
434 case "$url" in
435 ./*|../*)
436 url=$(resolve_relative_url "$url") || exit
438 esac
439 git config submodule."$name".url "$url" ||
440 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
443 # Copy "update" setting when it is not set yet
444 upd="$(git config -f .gitmodules submodule."$name".update)"
445 test -z "$upd" ||
446 test -n "$(git config submodule."$name".update)" ||
447 git config submodule."$name".update "$upd" ||
448 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
450 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
451 done
455 # Update each submodule path to correct revision, using clone and checkout as needed
457 # $@ = requested paths (default to all)
459 cmd_update()
461 # parse $args after "submodule ... update".
462 orig_flags=
463 while test $# -ne 0
465 case "$1" in
466 -q|--quiet)
467 GIT_QUIET=1
469 -i|--init)
470 init=1
472 -N|--no-fetch)
473 nofetch=1
475 -f|--force)
476 force=$1
478 -r|--rebase)
479 update="rebase"
481 --reference)
482 case "$2" in '') usage ;; esac
483 reference="--reference=$2"
484 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
485 shift
487 --reference=*)
488 reference="$1"
490 -m|--merge)
491 update="merge"
493 --recursive)
494 recursive=1
496 --checkout)
497 update="checkout"
500 shift
501 break
504 usage
507 break
509 esac
510 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
511 shift
512 done
514 if test -n "$init"
515 then
516 cmd_init "--" "$@" || return
519 cloned_modules=
520 module_list "$@" | {
521 err=
522 while read mode sha1 stage sm_path
524 if test "$stage" = U
525 then
526 echo >&2 "Skipping unmerged submodule $sm_path"
527 continue
529 name=$(module_name "$sm_path") || exit
530 url=$(git config submodule."$name".url)
531 if ! test -z "$update"
532 then
533 update_module=$update
534 else
535 update_module=$(git config submodule."$name".update)
538 if test "$update_module" = "none"
539 then
540 echo "Skipping submodule '$sm_path'"
541 continue
544 if test -z "$url"
545 then
546 # Only mention uninitialized submodules when its
547 # path have been specified
548 test "$#" != "0" &&
549 say "$(eval_gettext "Submodule path '\$sm_path' not initialized
550 Maybe you want to use 'update --init'?")"
551 continue
554 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
555 then
556 module_clone "$sm_path" "$url" "$reference"|| exit
557 cloned_modules="$cloned_modules;$name"
558 subsha1=
559 else
560 subsha1=$(clear_local_git_env; cd "$sm_path" &&
561 git rev-parse --verify HEAD) ||
562 die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
565 if test "$subsha1" != "$sha1"
566 then
567 subforce=$force
568 # If we don't already have a -f flag and the submodule has never been checked out
569 if test -z "$subsha1" -a -z "$force"
570 then
571 subforce="-f"
574 if test -z "$nofetch"
575 then
576 # Run fetch only if $sha1 isn't present or it
577 # is not reachable from a ref.
578 (clear_local_git_env; cd "$sm_path" &&
579 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
580 test -z "$rev") || git-fetch)) ||
581 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
584 # Is this something we just cloned?
585 case ";$cloned_modules;" in
586 *";$name;"*)
587 # then there is no local change to integrate
588 update_module= ;;
589 esac
591 must_die_on_failure=
592 case "$update_module" in
593 rebase)
594 command="git rebase"
595 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
596 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
597 must_die_on_failure=yes
599 merge)
600 command="git merge"
601 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
602 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
603 must_die_on_failure=yes
606 command="git checkout $subforce -q"
607 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
608 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
610 esac
612 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
613 then
614 say "$say_msg"
615 elif test -n "$must_die_on_failure"
616 then
617 die_with_status 2 "$die_msg"
618 else
619 err="${err};$die_msg"
620 continue
624 if test -n "$recursive"
625 then
626 (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
627 res=$?
628 if test $res -gt 0
629 then
630 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
631 if test $res -eq 1
632 then
633 err="${err};$die_msg"
634 continue
635 else
636 die_with_status $res "$die_msg"
640 done
642 if test -n "$err"
643 then
644 OIFS=$IFS
645 IFS=';'
646 for e in $err
648 if test -n "$e"
649 then
650 echo >&2 "$e"
652 done
653 IFS=$OIFS
654 exit 1
659 set_name_rev () {
660 revname=$( (
661 clear_local_git_env
662 cd "$1" && {
663 git describe "$2" 2>/dev/null ||
664 git describe --tags "$2" 2>/dev/null ||
665 git describe --contains "$2" 2>/dev/null ||
666 git describe --all --always "$2"
669 test -z "$revname" || revname=" ($revname)"
672 # Show commit summary for submodules in index or working tree
674 # If '--cached' is given, show summary between index and given commit,
675 # or between working tree and given commit
677 # $@ = [commit (default 'HEAD'),] requested paths (default all)
679 cmd_summary() {
680 summary_limit=-1
681 for_status=
682 diff_cmd=diff-index
684 # parse $args after "submodule ... summary".
685 while test $# -ne 0
687 case "$1" in
688 --cached)
689 cached="$1"
691 --files)
692 files="$1"
694 --for-status)
695 for_status="$1"
697 -n|--summary-limit)
698 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
699 then
701 else
702 usage
704 shift
707 shift
708 break
711 usage
714 break
716 esac
717 shift
718 done
720 test $summary_limit = 0 && return
722 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
723 then
724 head=$rev
725 test $# = 0 || shift
726 elif test -z "$1" -o "$1" = "HEAD"
727 then
728 # before the first commit: compare with an empty tree
729 head=$(git hash-object -w -t tree --stdin </dev/null)
730 test -z "$1" || shift
731 else
732 head="HEAD"
735 if [ -n "$files" ]
736 then
737 test -n "$cached" &&
738 die "$(gettext -- "--cached cannot be used with --files")"
739 diff_cmd=diff-files
740 head=
743 cd_to_toplevel
744 # Get modified modules cared by user
745 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
746 sane_egrep '^:([0-7]* )?160000' |
747 while read mod_src mod_dst sha1_src sha1_dst status name
749 # Always show modules deleted or type-changed (blob<->module)
750 test $status = D -o $status = T && echo "$name" && continue
751 # Also show added or modified modules which are checked out
752 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
753 echo "$name"
754 done
757 test -z "$modules" && return
759 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
760 sane_egrep '^:([0-7]* )?160000' |
761 cut -c2- |
762 while read mod_src mod_dst sha1_src sha1_dst status name
764 if test -z "$cached" &&
765 test $sha1_dst = 0000000000000000000000000000000000000000
766 then
767 case "$mod_dst" in
768 160000)
769 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
771 100644 | 100755 | 120000)
772 sha1_dst=$(git hash-object $name)
774 000000)
775 ;; # removed
777 # unexpected type
778 eval_gettextln "unexpected mode \$mod_dst" >&2
779 continue ;;
780 esac
782 missing_src=
783 missing_dst=
785 test $mod_src = 160000 &&
786 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
787 missing_src=t
789 test $mod_dst = 160000 &&
790 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
791 missing_dst=t
793 total_commits=
794 case "$missing_src,$missing_dst" in
796 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
799 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
801 t,t)
802 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
805 errmsg=
806 total_commits=$(
807 if test $mod_src = 160000 -a $mod_dst = 160000
808 then
809 range="$sha1_src...$sha1_dst"
810 elif test $mod_src = 160000
811 then
812 range=$sha1_src
813 else
814 range=$sha1_dst
816 GIT_DIR="$name/.git" \
817 git rev-list --first-parent $range -- | wc -l
819 total_commits=" ($(($total_commits + 0)))"
821 esac
823 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
824 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
825 if test $status = T
826 then
827 blob="$(gettext "blob")"
828 submodule="$(gettext "submodule")"
829 if test $mod_dst = 160000
830 then
831 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
832 else
833 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
835 else
836 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
838 if test -n "$errmsg"
839 then
840 # Don't give error msg for modification whose dst is not submodule
841 # i.e. deleted or changed to blob
842 test $mod_dst = 160000 && echo "$errmsg"
843 else
844 if test $mod_src = 160000 -a $mod_dst = 160000
845 then
846 limit=
847 test $summary_limit -gt 0 && limit="-$summary_limit"
848 GIT_DIR="$name/.git" \
849 git log $limit --pretty='format: %m %s' \
850 --first-parent $sha1_src...$sha1_dst
851 elif test $mod_dst = 160000
852 then
853 GIT_DIR="$name/.git" \
854 git log --pretty='format: > %s' -1 $sha1_dst
855 else
856 GIT_DIR="$name/.git" \
857 git log --pretty='format: < %s' -1 $sha1_src
859 echo
861 echo
862 done |
863 if test -n "$for_status"; then
864 if [ -n "$files" ]; then
865 gettextln "# Submodules changed but not updated:"
866 else
867 gettextln "# Submodule changes to be committed:"
869 echo "#"
870 sed -e 's|^|# |' -e 's|^# $|#|'
871 else
876 # List all submodules, prefixed with:
877 # - submodule not initialized
878 # + different revision checked out
880 # If --cached was specified the revision in the index will be printed
881 # instead of the currently checked out revision.
883 # $@ = requested paths (default to all)
885 cmd_status()
887 # parse $args after "submodule ... status".
888 orig_flags=
889 while test $# -ne 0
891 case "$1" in
892 -q|--quiet)
893 GIT_QUIET=1
895 --cached)
896 cached=1
898 --recursive)
899 recursive=1
902 shift
903 break
906 usage
909 break
911 esac
912 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
913 shift
914 done
916 module_list "$@" |
917 while read mode sha1 stage sm_path
919 name=$(module_name "$sm_path") || exit
920 url=$(git config submodule."$name".url)
921 displaypath="$prefix$sm_path"
922 if test "$stage" = U
923 then
924 say "U$sha1 $displaypath"
925 continue
927 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
928 then
929 say "-$sha1 $displaypath"
930 continue;
932 set_name_rev "$sm_path" "$sha1"
933 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
934 then
935 say " $sha1 $displaypath$revname"
936 else
937 if test -z "$cached"
938 then
939 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
940 set_name_rev "$sm_path" "$sha1"
942 say "+$sha1 $displaypath$revname"
945 if test -n "$recursive"
946 then
948 prefix="$displaypath/"
949 clear_local_git_env
950 cd "$sm_path" &&
951 eval cmd_status "$orig_args"
952 ) ||
953 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
955 done
958 # Sync remote urls for submodules
959 # This makes the value for remote.$remote.url match the value
960 # specified in .gitmodules.
962 cmd_sync()
964 while test $# -ne 0
966 case "$1" in
967 -q|--quiet)
968 GIT_QUIET=1
969 shift
972 shift
973 break
976 usage
979 break
981 esac
982 done
983 cd_to_toplevel
984 module_list "$@" |
985 while read mode sha1 stage sm_path
987 name=$(module_name "$sm_path")
988 url=$(git config -f .gitmodules --get submodule."$name".url)
990 # Possibly a url relative to parent
991 case "$url" in
992 ./*|../*)
993 # rewrite foo/bar as ../.. to find path from
994 # submodule work tree to superproject work tree
995 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
996 # guarantee a trailing /
997 up_path=${up_path%/}/ &&
998 # path from submodule work tree to submodule origin repo
999 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1000 # path from superproject work tree to submodule origin repo
1001 super_config_url=$(resolve_relative_url "$url") || exit
1004 sub_origin_url="$url"
1005 super_config_url="$url"
1007 esac
1009 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1010 then
1011 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
1012 git config submodule."$name".url "$super_config_url"
1014 if test -e "$sm_path"/.git
1015 then
1017 clear_local_git_env
1018 cd "$sm_path"
1019 remote=$(get_default_remote)
1020 git config remote."$remote".url "$sub_origin_url"
1024 done
1027 # This loop parses the command line arguments to find the
1028 # subcommand name to dispatch. Parsing of the subcommand specific
1029 # options are primarily done by the subcommand implementations.
1030 # Subcommand specific options such as --branch and --cached are
1031 # parsed here as well, for backward compatibility.
1033 while test $# != 0 && test -z "$command"
1035 case "$1" in
1036 add | foreach | init | update | status | summary | sync)
1037 command=$1
1039 -q|--quiet)
1040 GIT_QUIET=1
1042 -b|--branch)
1043 case "$2" in
1045 usage
1047 esac
1048 branch="$2"; shift
1050 --cached)
1051 cached="$1"
1054 break
1057 usage
1060 break
1062 esac
1063 shift
1064 done
1066 # No command word defaults to "status"
1067 test -n "$command" || command=status
1069 # "-b branch" is accepted only by "add"
1070 if test -n "$branch" && test "$command" != add
1071 then
1072 usage
1075 # "--cached" is accepted only by "status" and "summary"
1076 if test -n "$cached" && test "$command" != status -a "$command" != summary
1077 then
1078 usage
1081 "cmd_$command" "$@"