clone --single: limit the fetch refspec to fetched branch
[git.git] / git-submodule.sh
blobe89b516039347ce8d3f28efaf8894d75e6bf1d8e
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] [--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 # Resolve relative url by appending to parent's url
34 resolve_relative_url ()
36 remote=$(get_default_remote)
37 remoteurl=$(git config "remote.$remote.url") ||
38 remoteurl=$(pwd) # the repository is its own authoritative upstream
39 url="$1"
40 remoteurl=${remoteurl%/}
41 sep=/
42 while test -n "$url"
44 case "$url" in
45 ../*)
46 url="${url#../}"
47 case "$remoteurl" in
48 */*)
49 remoteurl="${remoteurl%/*}"
51 *:*)
52 remoteurl="${remoteurl%:*}"
53 sep=:
56 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
58 esac
60 ./*)
61 url="${url#./}"
64 break;;
65 esac
66 done
67 echo "$remoteurl$sep${url%/}"
71 # Get submodule info for registered submodules
72 # $@ = path to limit submodule list
74 module_list()
77 git ls-files --error-unmatch --stage -- "$@" ||
78 echo "unmatched pathspec exists"
79 ) |
80 perl -e '
81 my %unmerged = ();
82 my ($null_sha1) = ("0" x 40);
83 my @out = ();
84 my $unmatched = 0;
85 while (<STDIN>) {
86 if (/^unmatched pathspec/) {
87 $unmatched = 1;
88 next;
90 chomp;
91 my ($mode, $sha1, $stage, $path) =
92 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
93 next unless $mode eq "160000";
94 if ($stage ne "0") {
95 if (!$unmerged{$path}++) {
96 push @out, "$mode $null_sha1 U\t$path\n";
98 next;
100 push @out, "$_\n";
102 if ($unmatched) {
103 print "#unmatched\n";
104 } else {
105 print for (@out);
110 die_if_unmatched ()
112 if test "$1" = "#unmatched"
113 then
114 exit 1
119 # Map submodule path to submodule name
121 # $1 = path
123 module_name()
125 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
126 sm_path="$1"
127 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
128 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
129 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
130 test -z "$name" &&
131 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
132 echo "$name"
136 # Clone a submodule
138 # Prior to calling, cmd_update checks that a possibly existing
139 # path is not a git repository.
140 # Likewise, cmd_add checks that path does not exist at all,
141 # since it is the location of a new submodule.
143 module_clone()
145 sm_path=$1
146 url=$2
147 reference="$3"
148 quiet=
149 if test -n "$GIT_QUIET"
150 then
151 quiet=-q
154 gitdir=
155 gitdir_base=
156 name=$(module_name "$sm_path" 2>/dev/null)
157 test -n "$name" || name="$sm_path"
158 base_name=$(dirname "$name")
160 gitdir=$(git rev-parse --git-dir)
161 gitdir_base="$gitdir/modules/$base_name"
162 gitdir="$gitdir/modules/$name"
164 if test -d "$gitdir"
165 then
166 mkdir -p "$sm_path"
167 rm -f "$gitdir/index"
168 else
169 mkdir -p "$gitdir_base"
170 git clone $quiet -n ${reference:+"$reference"} \
171 --separate-git-dir "$gitdir" "$url" "$sm_path" ||
172 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
175 # We already are at the root of the work tree but cd_to_toplevel will
176 # resolve any symlinks that might be present in $PWD
177 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
178 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
179 # normalize Windows-style absolute paths to POSIX-style absolute paths
180 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
181 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
182 # Remove all common leading directories after a sanity check
183 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
184 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
186 while test "${a%%/*}" = "${b%%/*}"
188 a=${a#*/}
189 b=${b#*/}
190 done
191 # Now chop off the trailing '/'s that were added in the beginning
192 a=${a%/}
193 b=${b%/}
195 # Turn each leading "*/" component into "../"
196 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
197 echo "gitdir: $rel/$a" >"$sm_path/.git"
199 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
200 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
204 # Add a new submodule to the working tree, .gitmodules and the index
206 # $@ = repo path
208 # optional branch is stored in global branch variable
210 cmd_add()
212 # parse $args after "submodule ... add".
213 while test $# -ne 0
215 case "$1" in
216 -b | --branch)
217 case "$2" in '') usage ;; esac
218 branch=$2
219 shift
221 -f | --force)
222 force=$1
224 -q|--quiet)
225 GIT_QUIET=1
227 --reference)
228 case "$2" in '') usage ;; esac
229 reference="--reference=$2"
230 shift
232 --reference=*)
233 reference="$1"
234 shift
237 shift
238 break
241 usage
244 break
246 esac
247 shift
248 done
250 repo=$1
251 sm_path=$2
253 if test -z "$sm_path"; then
254 sm_path=$(echo "$repo" |
255 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
258 if test -z "$repo" -o -z "$sm_path"; then
259 usage
262 # assure repo is absolute or relative to parent
263 case "$repo" in
264 ./*|../*)
265 # dereference source url relative to parent's url
266 realrepo=$(resolve_relative_url "$repo") || exit
268 *:*|/*)
269 # absolute url
270 realrepo=$repo
273 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
275 esac
277 # normalize path:
278 # multiple //; leading ./; /./; /../; trailing /
279 sm_path=$(printf '%s/\n' "$sm_path" |
280 sed -e '
281 s|//*|/|g
282 s|^\(\./\)*||
283 s|/\./|/|g
284 :start
285 s|\([^/]*\)/\.\./||
286 tstart
287 s|/*$||
289 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
290 die "$(eval_gettext "'\$sm_path' already exists in the index")"
292 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
293 then
294 eval_gettextln "The following path is ignored by one of your .gitignore files:
295 \$sm_path
296 Use -f if you really want to add it." >&2
297 exit 1
300 # perhaps the path exists and is already a git repo, else clone it
301 if test -e "$sm_path"
302 then
303 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
304 then
305 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
306 else
307 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
310 else
312 module_clone "$sm_path" "$realrepo" "$reference" || exit
314 clear_local_git_env
315 cd "$sm_path" &&
316 # ash fails to wordsplit ${branch:+-b "$branch"...}
317 case "$branch" in
318 '') git checkout -f -q ;;
319 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
320 esac
321 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
323 git config submodule."$sm_path".url "$realrepo"
325 git add $force "$sm_path" ||
326 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
328 git config -f .gitmodules submodule."$sm_path".path "$sm_path" &&
329 git config -f .gitmodules submodule."$sm_path".url "$repo" &&
330 git add --force .gitmodules ||
331 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
335 # Execute an arbitrary command sequence in each checked out
336 # submodule
338 # $@ = command to execute
340 cmd_foreach()
342 # parse $args after "submodule ... foreach".
343 while test $# -ne 0
345 case "$1" in
346 -q|--quiet)
347 GIT_QUIET=1
349 --recursive)
350 recursive=1
353 usage
356 break
358 esac
359 shift
360 done
362 toplevel=$(pwd)
364 # dup stdin so that it can be restored when running the external
365 # command in the subshell (and a recursive call to this function)
366 exec 3<&0
368 module_list |
369 while read mode sha1 stage sm_path
371 die_if_unmatched "$mode"
372 if test -e "$sm_path"/.git
373 then
374 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
375 name=$(module_name "$sm_path")
377 prefix="$prefix$sm_path/"
378 clear_local_git_env
379 # we make $path available to scripts ...
380 path=$sm_path
381 cd "$sm_path" &&
382 eval "$@" &&
383 if test -n "$recursive"
384 then
385 cmd_foreach "--recursive" "$@"
387 ) <&3 3<&- ||
388 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
390 done
394 # Register submodules in .git/config
396 # $@ = requested paths (default to all)
398 cmd_init()
400 # parse $args after "submodule ... init".
401 while test $# -ne 0
403 case "$1" in
404 -q|--quiet)
405 GIT_QUIET=1
408 shift
409 break
412 usage
415 break
417 esac
418 shift
419 done
421 module_list "$@" |
422 while read mode sha1 stage sm_path
424 die_if_unmatched "$mode"
425 name=$(module_name "$sm_path") || exit
427 # Copy url setting when it is not set yet
428 if test -z "$(git config "submodule.$name.url")"
429 then
430 url=$(git config -f .gitmodules submodule."$name".url)
431 test -z "$url" &&
432 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
434 # Possibly a url relative to parent
435 case "$url" in
436 ./*|../*)
437 url=$(resolve_relative_url "$url") || exit
439 esac
440 git config submodule."$name".url "$url" ||
441 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
443 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
446 # Copy "update" setting when it is not set yet
447 upd="$(git config -f .gitmodules submodule."$name".update)"
448 test -z "$upd" ||
449 test -n "$(git config submodule."$name".update)" ||
450 git config submodule."$name".update "$upd" ||
451 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
452 done
456 # Update each submodule path to correct revision, using clone and checkout as needed
458 # $@ = requested paths (default to all)
460 cmd_update()
462 # parse $args after "submodule ... update".
463 orig_flags=
464 while test $# -ne 0
466 case "$1" in
467 -q|--quiet)
468 GIT_QUIET=1
470 -i|--init)
471 init=1
473 -N|--no-fetch)
474 nofetch=1
476 -f|--force)
477 force=$1
479 -r|--rebase)
480 update="rebase"
482 --reference)
483 case "$2" in '') usage ;; esac
484 reference="--reference=$2"
485 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
486 shift
488 --reference=*)
489 reference="$1"
491 -m|--merge)
492 update="merge"
494 --recursive)
495 recursive=1
497 --checkout)
498 update="checkout"
501 shift
502 break
505 usage
508 break
510 esac
511 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
512 shift
513 done
515 if test -n "$init"
516 then
517 cmd_init "--" "$@" || return
520 cloned_modules=
521 module_list "$@" | {
522 err=
523 while read mode sha1 stage sm_path
525 die_if_unmatched "$mode"
526 if test "$stage" = U
527 then
528 echo >&2 "Skipping unmerged submodule $sm_path"
529 continue
531 name=$(module_name "$sm_path") || exit
532 url=$(git config submodule."$name".url)
533 if ! test -z "$update"
534 then
535 update_module=$update
536 else
537 update_module=$(git config submodule."$name".update)
540 if test "$update_module" = "none"
541 then
542 echo "Skipping submodule '$sm_path'"
543 continue
546 if test -z "$url"
547 then
548 # Only mention uninitialized submodules when its
549 # path have been specified
550 test "$#" != "0" &&
551 say "$(eval_gettext "Submodule path '\$sm_path' not initialized
552 Maybe you want to use 'update --init'?")"
553 continue
556 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
557 then
558 module_clone "$sm_path" "$url" "$reference"|| exit
559 cloned_modules="$cloned_modules;$name"
560 subsha1=
561 else
562 subsha1=$(clear_local_git_env; cd "$sm_path" &&
563 git rev-parse --verify HEAD) ||
564 die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
567 if test "$subsha1" != "$sha1" -o -n "$force"
568 then
569 subforce=$force
570 # If we don't already have a -f flag and the submodule has never been checked out
571 if test -z "$subsha1" -a -z "$force"
572 then
573 subforce="-f"
576 if test -z "$nofetch"
577 then
578 # Run fetch only if $sha1 isn't present or it
579 # is not reachable from a ref.
580 (clear_local_git_env; cd "$sm_path" &&
581 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
582 test -z "$rev") || git-fetch)) ||
583 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
586 # Is this something we just cloned?
587 case ";$cloned_modules;" in
588 *";$name;"*)
589 # then there is no local change to integrate
590 update_module= ;;
591 esac
593 must_die_on_failure=
594 case "$update_module" in
595 rebase)
596 command="git rebase"
597 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
598 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
599 must_die_on_failure=yes
601 merge)
602 command="git merge"
603 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
604 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
605 must_die_on_failure=yes
608 command="git checkout $subforce -q"
609 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
610 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
612 esac
614 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
615 then
616 say "$say_msg"
617 elif test -n "$must_die_on_failure"
618 then
619 die_with_status 2 "$die_msg"
620 else
621 err="${err};$die_msg"
622 continue
626 if test -n "$recursive"
627 then
628 (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
629 res=$?
630 if test $res -gt 0
631 then
632 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
633 if test $res -eq 1
634 then
635 err="${err};$die_msg"
636 continue
637 else
638 die_with_status $res "$die_msg"
642 done
644 if test -n "$err"
645 then
646 OIFS=$IFS
647 IFS=';'
648 for e in $err
650 if test -n "$e"
651 then
652 echo >&2 "$e"
654 done
655 IFS=$OIFS
656 exit 1
661 set_name_rev () {
662 revname=$( (
663 clear_local_git_env
664 cd "$1" && {
665 git describe "$2" 2>/dev/null ||
666 git describe --tags "$2" 2>/dev/null ||
667 git describe --contains "$2" 2>/dev/null ||
668 git describe --all --always "$2"
671 test -z "$revname" || revname=" ($revname)"
674 # Show commit summary for submodules in index or working tree
676 # If '--cached' is given, show summary between index and given commit,
677 # or between working tree and given commit
679 # $@ = [commit (default 'HEAD'),] requested paths (default all)
681 cmd_summary() {
682 summary_limit=-1
683 for_status=
684 diff_cmd=diff-index
686 # parse $args after "submodule ... summary".
687 while test $# -ne 0
689 case "$1" in
690 --cached)
691 cached="$1"
693 --files)
694 files="$1"
696 --for-status)
697 for_status="$1"
699 -n|--summary-limit)
700 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
701 then
703 else
704 usage
706 shift
709 shift
710 break
713 usage
716 break
718 esac
719 shift
720 done
722 test $summary_limit = 0 && return
724 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
725 then
726 head=$rev
727 test $# = 0 || shift
728 elif test -z "$1" -o "$1" = "HEAD"
729 then
730 # before the first commit: compare with an empty tree
731 head=$(git hash-object -w -t tree --stdin </dev/null)
732 test -z "$1" || shift
733 else
734 head="HEAD"
737 if [ -n "$files" ]
738 then
739 test -n "$cached" &&
740 die "$(gettext -- "--cached cannot be used with --files")"
741 diff_cmd=diff-files
742 head=
745 cd_to_toplevel
746 # Get modified modules cared by user
747 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
748 sane_egrep '^:([0-7]* )?160000' |
749 while read mod_src mod_dst sha1_src sha1_dst status name
751 # Always show modules deleted or type-changed (blob<->module)
752 test $status = D -o $status = T && echo "$name" && continue
753 # Also show added or modified modules which are checked out
754 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
755 echo "$name"
756 done
759 test -z "$modules" && return
761 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
762 sane_egrep '^:([0-7]* )?160000' |
763 cut -c2- |
764 while read mod_src mod_dst sha1_src sha1_dst status name
766 if test -z "$cached" &&
767 test $sha1_dst = 0000000000000000000000000000000000000000
768 then
769 case "$mod_dst" in
770 160000)
771 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
773 100644 | 100755 | 120000)
774 sha1_dst=$(git hash-object $name)
776 000000)
777 ;; # removed
779 # unexpected type
780 eval_gettextln "unexpected mode \$mod_dst" >&2
781 continue ;;
782 esac
784 missing_src=
785 missing_dst=
787 test $mod_src = 160000 &&
788 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
789 missing_src=t
791 test $mod_dst = 160000 &&
792 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
793 missing_dst=t
795 total_commits=
796 case "$missing_src,$missing_dst" in
798 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
801 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
803 t,t)
804 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
807 errmsg=
808 total_commits=$(
809 if test $mod_src = 160000 -a $mod_dst = 160000
810 then
811 range="$sha1_src...$sha1_dst"
812 elif test $mod_src = 160000
813 then
814 range=$sha1_src
815 else
816 range=$sha1_dst
818 GIT_DIR="$name/.git" \
819 git rev-list --first-parent $range -- | wc -l
821 total_commits=" ($(($total_commits + 0)))"
823 esac
825 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
826 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
827 if test $status = T
828 then
829 blob="$(gettext "blob")"
830 submodule="$(gettext "submodule")"
831 if test $mod_dst = 160000
832 then
833 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
834 else
835 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
837 else
838 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
840 if test -n "$errmsg"
841 then
842 # Don't give error msg for modification whose dst is not submodule
843 # i.e. deleted or changed to blob
844 test $mod_dst = 160000 && echo "$errmsg"
845 else
846 if test $mod_src = 160000 -a $mod_dst = 160000
847 then
848 limit=
849 test $summary_limit -gt 0 && limit="-$summary_limit"
850 GIT_DIR="$name/.git" \
851 git log $limit --pretty='format: %m %s' \
852 --first-parent $sha1_src...$sha1_dst
853 elif test $mod_dst = 160000
854 then
855 GIT_DIR="$name/.git" \
856 git log --pretty='format: > %s' -1 $sha1_dst
857 else
858 GIT_DIR="$name/.git" \
859 git log --pretty='format: < %s' -1 $sha1_src
861 echo
863 echo
864 done |
865 if test -n "$for_status"; then
866 if [ -n "$files" ]; then
867 gettextln "# Submodules changed but not updated:"
868 else
869 gettextln "# Submodule changes to be committed:"
871 echo "#"
872 sed -e 's|^|# |' -e 's|^# $|#|'
873 else
878 # List all submodules, prefixed with:
879 # - submodule not initialized
880 # + different revision checked out
882 # If --cached was specified the revision in the index will be printed
883 # instead of the currently checked out revision.
885 # $@ = requested paths (default to all)
887 cmd_status()
889 # parse $args after "submodule ... status".
890 orig_flags=
891 while test $# -ne 0
893 case "$1" in
894 -q|--quiet)
895 GIT_QUIET=1
897 --cached)
898 cached=1
900 --recursive)
901 recursive=1
904 shift
905 break
908 usage
911 break
913 esac
914 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
915 shift
916 done
918 module_list "$@" |
919 while read mode sha1 stage sm_path
921 die_if_unmatched "$mode"
922 name=$(module_name "$sm_path") || exit
923 url=$(git config submodule."$name".url)
924 displaypath="$prefix$sm_path"
925 if test "$stage" = U
926 then
927 say "U$sha1 $displaypath"
928 continue
930 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
931 then
932 say "-$sha1 $displaypath"
933 continue;
935 set_name_rev "$sm_path" "$sha1"
936 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
937 then
938 say " $sha1 $displaypath$revname"
939 else
940 if test -z "$cached"
941 then
942 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
943 set_name_rev "$sm_path" "$sha1"
945 say "+$sha1 $displaypath$revname"
948 if test -n "$recursive"
949 then
951 prefix="$displaypath/"
952 clear_local_git_env
953 cd "$sm_path" &&
954 eval cmd_status "$orig_args"
955 ) ||
956 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
958 done
961 # Sync remote urls for submodules
962 # This makes the value for remote.$remote.url match the value
963 # specified in .gitmodules.
965 cmd_sync()
967 while test $# -ne 0
969 case "$1" in
970 -q|--quiet)
971 GIT_QUIET=1
972 shift
975 shift
976 break
979 usage
982 break
984 esac
985 done
986 cd_to_toplevel
987 module_list "$@" |
988 while read mode sha1 stage sm_path
990 die_if_unmatched "$mode"
991 name=$(module_name "$sm_path")
992 url=$(git config -f .gitmodules --get submodule."$name".url)
994 # Possibly a url relative to parent
995 case "$url" in
996 ./*|../*)
997 url=$(resolve_relative_url "$url") || exit
999 esac
1001 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1002 then
1003 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
1004 git config submodule."$name".url "$url"
1006 if test -e "$sm_path"/.git
1007 then
1009 clear_local_git_env
1010 cd "$sm_path"
1011 remote=$(get_default_remote)
1012 git config remote."$remote".url "$url"
1016 done
1019 # This loop parses the command line arguments to find the
1020 # subcommand name to dispatch. Parsing of the subcommand specific
1021 # options are primarily done by the subcommand implementations.
1022 # Subcommand specific options such as --branch and --cached are
1023 # parsed here as well, for backward compatibility.
1025 while test $# != 0 && test -z "$command"
1027 case "$1" in
1028 add | foreach | init | update | status | summary | sync)
1029 command=$1
1031 -q|--quiet)
1032 GIT_QUIET=1
1034 -b|--branch)
1035 case "$2" in
1037 usage
1039 esac
1040 branch="$2"; shift
1042 --cached)
1043 cached="$1"
1046 break
1049 usage
1052 break
1054 esac
1055 shift
1056 done
1058 # No command word defaults to "status"
1059 test -n "$command" || command=status
1061 # "-b branch" is accepted only by "add"
1062 if test -n "$branch" && test "$command" != add
1063 then
1064 usage
1067 # "--cached" is accepted only by "status" and "summary"
1068 if test -n "$cached" && test "$command" != status -a "$command" != summary
1069 then
1070 usage
1073 "cmd_$command" "$@"