submodule: fix handling of superproject origin URLs like foo, ./foo and ./foo/bar
[alt-git.git] / git-submodule.sh
blobb0b6ccbe723e7c86a478b19166871245bf10ed5d
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=
63 ./*|../*)
64 is_relative=t
67 is_relative=t
68 remoteurl="./$remoteurl"
70 esac
72 while test -n "$url"
74 case "$url" in
75 ../*)
76 url="${url#../}"
77 case "$remoteurl" in
78 */*)
79 remoteurl="${remoteurl%/*}"
81 *:*)
82 remoteurl="${remoteurl%:*}"
83 sep=:
86 if test -z "$is_relative" || test "." = "$remoteurl"
87 then
88 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
89 else
90 remoteurl=.
93 esac
95 ./*)
96 url="${url#./}"
99 break;;
100 esac
101 done
102 remoteurl="$remoteurl$sep${url%/}"
103 echo "${is_relative:+${up_path}}${remoteurl#./}"
107 # Get submodule info for registered submodules
108 # $@ = path to limit submodule list
110 module_list()
112 git ls-files --error-unmatch --stage -- "$@" |
113 perl -e '
114 my %unmerged = ();
115 my ($null_sha1) = ("0" x 40);
116 while (<STDIN>) {
117 chomp;
118 my ($mode, $sha1, $stage, $path) =
119 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
120 next unless $mode eq "160000";
121 if ($stage ne "0") {
122 if (!$unmerged{$path}++) {
123 print "$mode $null_sha1 U\t$path\n";
125 next;
127 print "$_\n";
133 # Map submodule path to submodule name
135 # $1 = path
137 module_name()
139 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
140 sm_path="$1"
141 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
142 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
143 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
144 test -z "$name" &&
145 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
146 echo "$name"
150 # Clone a submodule
152 # Prior to calling, cmd_update checks that a possibly existing
153 # path is not a git repository.
154 # Likewise, cmd_add checks that path does not exist at all,
155 # since it is the location of a new submodule.
157 module_clone()
159 sm_path=$1
160 url=$2
161 reference="$3"
162 quiet=
163 if test -n "$GIT_QUIET"
164 then
165 quiet=-q
168 gitdir=
169 gitdir_base=
170 name=$(module_name "$sm_path" 2>/dev/null)
171 test -n "$name" || name="$sm_path"
172 base_name=$(dirname "$name")
174 gitdir=$(git rev-parse --git-dir)
175 gitdir_base="$gitdir/modules/$base_name"
176 gitdir="$gitdir/modules/$name"
178 if test -d "$gitdir"
179 then
180 mkdir -p "$sm_path"
181 rm -f "$gitdir/index"
182 else
183 mkdir -p "$gitdir_base"
184 git clone $quiet -n ${reference:+"$reference"} \
185 --separate-git-dir "$gitdir" "$url" "$sm_path" ||
186 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
189 a=$(cd "$gitdir" && pwd)/
190 b=$(cd "$sm_path" && pwd)/
191 # normalize Windows-style absolute paths to POSIX-style absolute paths
192 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
193 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
194 # Remove all common leading directories after a sanity check
195 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
196 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
198 while test "${a%%/*}" = "${b%%/*}"
200 a=${a#*/}
201 b=${b#*/}
202 done
203 # Now chop off the trailing '/'s that were added in the beginning
204 a=${a%/}
205 b=${b%/}
207 # Turn each leading "*/" component into "../"
208 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
209 echo "gitdir: $rel/$a" >"$sm_path/.git"
211 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
212 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
216 # Add a new submodule to the working tree, .gitmodules and the index
218 # $@ = repo path
220 # optional branch is stored in global branch variable
222 cmd_add()
224 # parse $args after "submodule ... add".
225 while test $# -ne 0
227 case "$1" in
228 -b | --branch)
229 case "$2" in '') usage ;; esac
230 branch=$2
231 shift
233 -f | --force)
234 force=$1
236 -q|--quiet)
237 GIT_QUIET=1
239 --reference)
240 case "$2" in '') usage ;; esac
241 reference="--reference=$2"
242 shift
244 --reference=*)
245 reference="$1"
246 shift
249 shift
250 break
253 usage
256 break
258 esac
259 shift
260 done
262 repo=$1
263 sm_path=$2
265 if test -z "$sm_path"; then
266 sm_path=$(echo "$repo" |
267 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
270 if test -z "$repo" -o -z "$sm_path"; then
271 usage
274 # assure repo is absolute or relative to parent
275 case "$repo" in
276 ./*|../*)
277 # dereference source url relative to parent's url
278 realrepo=$(resolve_relative_url "$repo") || exit
280 *:*|/*)
281 # absolute url
282 realrepo=$repo
285 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
287 esac
289 # normalize path:
290 # multiple //; leading ./; /./; /../; trailing /
291 sm_path=$(printf '%s/\n' "$sm_path" |
292 sed -e '
293 s|//*|/|g
294 s|^\(\./\)*||
295 s|/\./|/|g
296 :start
297 s|\([^/]*\)/\.\./||
298 tstart
299 s|/*$||
301 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
302 die "$(eval_gettext "'\$sm_path' already exists in the index")"
304 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
305 then
306 eval_gettextln "The following path is ignored by one of your .gitignore files:
307 \$sm_path
308 Use -f if you really want to add it." >&2
309 exit 1
312 # perhaps the path exists and is already a git repo, else clone it
313 if test -e "$sm_path"
314 then
315 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
316 then
317 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
318 else
319 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
322 else
324 module_clone "$sm_path" "$realrepo" "$reference" || exit
326 clear_local_git_env
327 cd "$sm_path" &&
328 # ash fails to wordsplit ${branch:+-b "$branch"...}
329 case "$branch" in
330 '') git checkout -f -q ;;
331 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
332 esac
333 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
335 git config submodule."$sm_path".url "$realrepo"
337 git add $force "$sm_path" ||
338 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
340 git config -f .gitmodules submodule."$sm_path".path "$sm_path" &&
341 git config -f .gitmodules submodule."$sm_path".url "$repo" &&
342 git add --force .gitmodules ||
343 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
347 # Execute an arbitrary command sequence in each checked out
348 # submodule
350 # $@ = command to execute
352 cmd_foreach()
354 # parse $args after "submodule ... foreach".
355 while test $# -ne 0
357 case "$1" in
358 -q|--quiet)
359 GIT_QUIET=1
361 --recursive)
362 recursive=1
365 usage
368 break
370 esac
371 shift
372 done
374 toplevel=$(pwd)
376 # dup stdin so that it can be restored when running the external
377 # command in the subshell (and a recursive call to this function)
378 exec 3<&0
380 module_list |
381 while read mode sha1 stage sm_path
383 if test -e "$sm_path"/.git
384 then
385 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
386 name=$(module_name "$sm_path")
388 prefix="$prefix$sm_path/"
389 clear_local_git_env
390 # we make $path available to scripts ...
391 path=$sm_path
392 cd "$sm_path" &&
393 eval "$@" &&
394 if test -n "$recursive"
395 then
396 cmd_foreach "--recursive" "$@"
398 ) <&3 3<&- ||
399 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
401 done
405 # Register submodules in .git/config
407 # $@ = requested paths (default to all)
409 cmd_init()
411 # parse $args after "submodule ... init".
412 while test $# -ne 0
414 case "$1" in
415 -q|--quiet)
416 GIT_QUIET=1
419 shift
420 break
423 usage
426 break
428 esac
429 shift
430 done
432 module_list "$@" |
433 while read mode sha1 stage sm_path
435 # Skip already registered paths
436 name=$(module_name "$sm_path") || exit
437 if test -z "$(git config "submodule.$name.url")"
438 then
439 url=$(git config -f .gitmodules submodule."$name".url)
440 test -z "$url" &&
441 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
443 # Possibly a url relative to parent
444 case "$url" in
445 ./*|../*)
446 url=$(resolve_relative_url "$url") || exit
448 esac
449 git config submodule."$name".url "$url" ||
450 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
453 # Copy "update" setting when it is not set yet
454 upd="$(git config -f .gitmodules submodule."$name".update)"
455 test -z "$upd" ||
456 test -n "$(git config submodule."$name".update)" ||
457 git config submodule."$name".update "$upd" ||
458 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
460 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
461 done
465 # Update each submodule path to correct revision, using clone and checkout as needed
467 # $@ = requested paths (default to all)
469 cmd_update()
471 # parse $args after "submodule ... update".
472 orig_flags=
473 while test $# -ne 0
475 case "$1" in
476 -q|--quiet)
477 GIT_QUIET=1
479 -i|--init)
480 init=1
482 -N|--no-fetch)
483 nofetch=1
485 -f|--force)
486 force=$1
488 -r|--rebase)
489 update="rebase"
491 --reference)
492 case "$2" in '') usage ;; esac
493 reference="--reference=$2"
494 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
495 shift
497 --reference=*)
498 reference="$1"
500 -m|--merge)
501 update="merge"
503 --recursive)
504 recursive=1
506 --checkout)
507 update="checkout"
510 shift
511 break
514 usage
517 break
519 esac
520 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
521 shift
522 done
524 if test -n "$init"
525 then
526 cmd_init "--" "$@" || return
529 cloned_modules=
530 module_list "$@" | {
531 err=
532 while read mode sha1 stage sm_path
534 if test "$stage" = U
535 then
536 echo >&2 "Skipping unmerged submodule $sm_path"
537 continue
539 name=$(module_name "$sm_path") || exit
540 url=$(git config submodule."$name".url)
541 if ! test -z "$update"
542 then
543 update_module=$update
544 else
545 update_module=$(git config submodule."$name".update)
548 if test "$update_module" = "none"
549 then
550 echo "Skipping submodule '$sm_path'"
551 continue
554 if test -z "$url"
555 then
556 # Only mention uninitialized submodules when its
557 # path have been specified
558 test "$#" != "0" &&
559 say "$(eval_gettext "Submodule path '\$sm_path' not initialized
560 Maybe you want to use 'update --init'?")"
561 continue
564 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
565 then
566 module_clone "$sm_path" "$url" "$reference"|| exit
567 cloned_modules="$cloned_modules;$name"
568 subsha1=
569 else
570 subsha1=$(clear_local_git_env; cd "$sm_path" &&
571 git rev-parse --verify HEAD) ||
572 die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
575 if test "$subsha1" != "$sha1"
576 then
577 subforce=$force
578 # If we don't already have a -f flag and the submodule has never been checked out
579 if test -z "$subsha1" -a -z "$force"
580 then
581 subforce="-f"
584 if test -z "$nofetch"
585 then
586 # Run fetch only if $sha1 isn't present or it
587 # is not reachable from a ref.
588 (clear_local_git_env; cd "$sm_path" &&
589 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
590 test -z "$rev") || git-fetch)) ||
591 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
594 # Is this something we just cloned?
595 case ";$cloned_modules;" in
596 *";$name;"*)
597 # then there is no local change to integrate
598 update_module= ;;
599 esac
601 must_die_on_failure=
602 case "$update_module" in
603 rebase)
604 command="git rebase"
605 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
606 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
607 must_die_on_failure=yes
609 merge)
610 command="git merge"
611 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
612 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
613 must_die_on_failure=yes
616 command="git checkout $subforce -q"
617 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
618 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
620 esac
622 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
623 then
624 say "$say_msg"
625 elif test -n "$must_die_on_failure"
626 then
627 die_with_status 2 "$die_msg"
628 else
629 err="${err};$die_msg"
630 continue
634 if test -n "$recursive"
635 then
636 (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
637 res=$?
638 if test $res -gt 0
639 then
640 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
641 if test $res -eq 1
642 then
643 err="${err};$die_msg"
644 continue
645 else
646 die_with_status $res "$die_msg"
650 done
652 if test -n "$err"
653 then
654 OIFS=$IFS
655 IFS=';'
656 for e in $err
658 if test -n "$e"
659 then
660 echo >&2 "$e"
662 done
663 IFS=$OIFS
664 exit 1
669 set_name_rev () {
670 revname=$( (
671 clear_local_git_env
672 cd "$1" && {
673 git describe "$2" 2>/dev/null ||
674 git describe --tags "$2" 2>/dev/null ||
675 git describe --contains "$2" 2>/dev/null ||
676 git describe --all --always "$2"
679 test -z "$revname" || revname=" ($revname)"
682 # Show commit summary for submodules in index or working tree
684 # If '--cached' is given, show summary between index and given commit,
685 # or between working tree and given commit
687 # $@ = [commit (default 'HEAD'),] requested paths (default all)
689 cmd_summary() {
690 summary_limit=-1
691 for_status=
692 diff_cmd=diff-index
694 # parse $args after "submodule ... summary".
695 while test $# -ne 0
697 case "$1" in
698 --cached)
699 cached="$1"
701 --files)
702 files="$1"
704 --for-status)
705 for_status="$1"
707 -n|--summary-limit)
708 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
709 then
711 else
712 usage
714 shift
717 shift
718 break
721 usage
724 break
726 esac
727 shift
728 done
730 test $summary_limit = 0 && return
732 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
733 then
734 head=$rev
735 test $# = 0 || shift
736 elif test -z "$1" -o "$1" = "HEAD"
737 then
738 # before the first commit: compare with an empty tree
739 head=$(git hash-object -w -t tree --stdin </dev/null)
740 test -z "$1" || shift
741 else
742 head="HEAD"
745 if [ -n "$files" ]
746 then
747 test -n "$cached" &&
748 die "$(gettext -- "--cached cannot be used with --files")"
749 diff_cmd=diff-files
750 head=
753 cd_to_toplevel
754 # Get modified modules cared by user
755 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
756 sane_egrep '^:([0-7]* )?160000' |
757 while read mod_src mod_dst sha1_src sha1_dst status name
759 # Always show modules deleted or type-changed (blob<->module)
760 test $status = D -o $status = T && echo "$name" && continue
761 # Also show added or modified modules which are checked out
762 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
763 echo "$name"
764 done
767 test -z "$modules" && return
769 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
770 sane_egrep '^:([0-7]* )?160000' |
771 cut -c2- |
772 while read mod_src mod_dst sha1_src sha1_dst status name
774 if test -z "$cached" &&
775 test $sha1_dst = 0000000000000000000000000000000000000000
776 then
777 case "$mod_dst" in
778 160000)
779 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
781 100644 | 100755 | 120000)
782 sha1_dst=$(git hash-object $name)
784 000000)
785 ;; # removed
787 # unexpected type
788 eval_gettextln "unexpected mode \$mod_dst" >&2
789 continue ;;
790 esac
792 missing_src=
793 missing_dst=
795 test $mod_src = 160000 &&
796 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
797 missing_src=t
799 test $mod_dst = 160000 &&
800 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
801 missing_dst=t
803 total_commits=
804 case "$missing_src,$missing_dst" in
806 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
809 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
811 t,t)
812 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
815 errmsg=
816 total_commits=$(
817 if test $mod_src = 160000 -a $mod_dst = 160000
818 then
819 range="$sha1_src...$sha1_dst"
820 elif test $mod_src = 160000
821 then
822 range=$sha1_src
823 else
824 range=$sha1_dst
826 GIT_DIR="$name/.git" \
827 git rev-list --first-parent $range -- | wc -l
829 total_commits=" ($(($total_commits + 0)))"
831 esac
833 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
834 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
835 if test $status = T
836 then
837 blob="$(gettext "blob")"
838 submodule="$(gettext "submodule")"
839 if test $mod_dst = 160000
840 then
841 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
842 else
843 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
845 else
846 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
848 if test -n "$errmsg"
849 then
850 # Don't give error msg for modification whose dst is not submodule
851 # i.e. deleted or changed to blob
852 test $mod_dst = 160000 && echo "$errmsg"
853 else
854 if test $mod_src = 160000 -a $mod_dst = 160000
855 then
856 limit=
857 test $summary_limit -gt 0 && limit="-$summary_limit"
858 GIT_DIR="$name/.git" \
859 git log $limit --pretty='format: %m %s' \
860 --first-parent $sha1_src...$sha1_dst
861 elif test $mod_dst = 160000
862 then
863 GIT_DIR="$name/.git" \
864 git log --pretty='format: > %s' -1 $sha1_dst
865 else
866 GIT_DIR="$name/.git" \
867 git log --pretty='format: < %s' -1 $sha1_src
869 echo
871 echo
872 done |
873 if test -n "$for_status"; then
874 if [ -n "$files" ]; then
875 gettextln "# Submodules changed but not updated:"
876 else
877 gettextln "# Submodule changes to be committed:"
879 echo "#"
880 sed -e 's|^|# |' -e 's|^# $|#|'
881 else
886 # List all submodules, prefixed with:
887 # - submodule not initialized
888 # + different revision checked out
890 # If --cached was specified the revision in the index will be printed
891 # instead of the currently checked out revision.
893 # $@ = requested paths (default to all)
895 cmd_status()
897 # parse $args after "submodule ... status".
898 orig_flags=
899 while test $# -ne 0
901 case "$1" in
902 -q|--quiet)
903 GIT_QUIET=1
905 --cached)
906 cached=1
908 --recursive)
909 recursive=1
912 shift
913 break
916 usage
919 break
921 esac
922 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
923 shift
924 done
926 module_list "$@" |
927 while read mode sha1 stage sm_path
929 name=$(module_name "$sm_path") || exit
930 url=$(git config submodule."$name".url)
931 displaypath="$prefix$sm_path"
932 if test "$stage" = U
933 then
934 say "U$sha1 $displaypath"
935 continue
937 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
938 then
939 say "-$sha1 $displaypath"
940 continue;
942 set_name_rev "$sm_path" "$sha1"
943 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
944 then
945 say " $sha1 $displaypath$revname"
946 else
947 if test -z "$cached"
948 then
949 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
950 set_name_rev "$sm_path" "$sha1"
952 say "+$sha1 $displaypath$revname"
955 if test -n "$recursive"
956 then
958 prefix="$displaypath/"
959 clear_local_git_env
960 cd "$sm_path" &&
961 eval cmd_status "$orig_args"
962 ) ||
963 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
965 done
968 # Sync remote urls for submodules
969 # This makes the value for remote.$remote.url match the value
970 # specified in .gitmodules.
972 cmd_sync()
974 while test $# -ne 0
976 case "$1" in
977 -q|--quiet)
978 GIT_QUIET=1
979 shift
982 shift
983 break
986 usage
989 break
991 esac
992 done
993 cd_to_toplevel
994 module_list "$@" |
995 while read mode sha1 stage sm_path
997 name=$(module_name "$sm_path")
998 url=$(git config -f .gitmodules --get submodule."$name".url)
1000 # Possibly a url relative to parent
1001 case "$url" in
1002 ./*|../*)
1003 # rewrite foo/bar as ../.. to find path from
1004 # submodule work tree to superproject work tree
1005 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1006 # guarantee a trailing /
1007 up_path=${up_path%/}/ &&
1008 # path from submodule work tree to submodule origin repo
1009 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1010 # path from superproject work tree to submodule origin repo
1011 super_config_url=$(resolve_relative_url "$url") || exit
1014 sub_origin_url="$url"
1015 super_config_url="$url"
1017 esac
1019 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1020 then
1021 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
1022 git config submodule."$name".url "$super_config_url"
1024 if test -e "$sm_path"/.git
1025 then
1027 clear_local_git_env
1028 cd "$sm_path"
1029 remote=$(get_default_remote)
1030 git config remote."$remote".url "$sub_origin_url"
1034 done
1037 # This loop parses the command line arguments to find the
1038 # subcommand name to dispatch. Parsing of the subcommand specific
1039 # options are primarily done by the subcommand implementations.
1040 # Subcommand specific options such as --branch and --cached are
1041 # parsed here as well, for backward compatibility.
1043 while test $# != 0 && test -z "$command"
1045 case "$1" in
1046 add | foreach | init | update | status | summary | sync)
1047 command=$1
1049 -q|--quiet)
1050 GIT_QUIET=1
1052 -b|--branch)
1053 case "$2" in
1055 usage
1057 esac
1058 branch="$2"; shift
1060 --cached)
1061 cached="$1"
1064 break
1067 usage
1070 break
1072 esac
1073 shift
1074 done
1076 # No command word defaults to "status"
1077 test -n "$command" || command=status
1079 # "-b branch" is accepted only by "add"
1080 if test -n "$branch" && test "$command" != add
1081 then
1082 usage
1085 # "--cached" is accepted only by "status" and "summary"
1086 if test -n "$cached" && test "$command" != status -a "$command" != summary
1087 then
1088 usage
1091 "cmd_$command" "$@"