gitweb: Allow line number toggling with Javascript
[git/dscho.git] / git-submodule.sh
blob595811f319163d89b101071a6436b590479a9583
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 # 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()
113 git ls-files --error-unmatch --stage -- "$@" ||
114 echo "unmatched pathspec exists"
116 perl -e '
117 my %unmerged = ();
118 my ($null_sha1) = ("0" x 40);
119 my @out = ();
120 my $unmatched = 0;
121 while (<STDIN>) {
122 if (/^unmatched pathspec/) {
123 $unmatched = 1;
124 next;
126 chomp;
127 my ($mode, $sha1, $stage, $path) =
128 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
129 next unless $mode eq "160000";
130 if ($stage ne "0") {
131 if (!$unmerged{$path}++) {
132 push @out, "$mode $null_sha1 U\t$path\n";
134 next;
136 push @out, "$_\n";
138 if ($unmatched) {
139 print "#unmatched\n";
140 } else {
141 print for (@out);
146 die_if_unmatched ()
148 if test "$1" = "#unmatched"
149 then
150 exit 1
155 # Map submodule path to submodule name
157 # $1 = path
159 module_name()
161 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
162 sm_path="$1"
163 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
164 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
165 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
166 test -z "$name" &&
167 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
168 echo "$name"
172 # Clone a submodule
174 # Prior to calling, cmd_update checks that a possibly existing
175 # path is not a git repository.
176 # Likewise, cmd_add checks that path does not exist at all,
177 # since it is the location of a new submodule.
179 module_clone()
181 sm_path=$1
182 url=$2
183 reference="$3"
184 quiet=
185 if test -n "$GIT_QUIET"
186 then
187 quiet=-q
190 gitdir=
191 gitdir_base=
192 name=$(module_name "$sm_path" 2>/dev/null)
193 test -n "$name" || name="$sm_path"
194 base_name=$(dirname "$name")
196 gitdir=$(git rev-parse --git-dir)
197 gitdir_base="$gitdir/modules/$base_name"
198 gitdir="$gitdir/modules/$name"
200 if test -d "$gitdir"
201 then
202 mkdir -p "$sm_path"
203 rm -f "$gitdir/index"
204 else
205 mkdir -p "$gitdir_base"
207 clear_local_git_env
208 git clone $quiet -n ${reference:+"$reference"} \
209 --separate-git-dir "$gitdir" "$url" "$sm_path"
210 ) ||
211 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
214 # We already are at the root of the work tree but cd_to_toplevel will
215 # resolve any symlinks that might be present in $PWD
216 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
217 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
218 # Remove all common leading directories after a sanity check
219 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
220 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
222 while test "${a%%/*}" = "${b%%/*}"
224 a=${a#*/}
225 b=${b#*/}
226 done
227 # Now chop off the trailing '/'s that were added in the beginning
228 a=${a%/}
229 b=${b%/}
231 # Turn each leading "*/" component into "../"
232 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
233 echo "gitdir: $rel/$a" >"$sm_path/.git"
235 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
236 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
240 # Add a new submodule to the working tree, .gitmodules and the index
242 # $@ = repo path
244 # optional branch is stored in global branch variable
246 cmd_add()
248 # parse $args after "submodule ... add".
249 while test $# -ne 0
251 case "$1" in
252 -b | --branch)
253 case "$2" in '') usage ;; esac
254 branch=$2
255 shift
257 -f | --force)
258 force=$1
260 -q|--quiet)
261 GIT_QUIET=1
263 --reference)
264 case "$2" in '') usage ;; esac
265 reference="--reference=$2"
266 shift
268 --reference=*)
269 reference="$1"
270 shift
273 shift
274 break
277 usage
280 break
282 esac
283 shift
284 done
286 repo=$1
287 sm_path=$2
289 if test -z "$sm_path"; then
290 sm_path=$(echo "$repo" |
291 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
294 if test -z "$repo" -o -z "$sm_path"; then
295 usage
298 # assure repo is absolute or relative to parent
299 case "$repo" in
300 ./*|../*)
301 # dereference source url relative to parent's url
302 realrepo=$(resolve_relative_url "$repo") || exit
304 *:*|/*)
305 # absolute url
306 realrepo=$repo
309 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
311 esac
313 # normalize path:
314 # multiple //; leading ./; /./; /../; trailing /
315 sm_path=$(printf '%s/\n' "$sm_path" |
316 sed -e '
317 s|//*|/|g
318 s|^\(\./\)*||
319 s|/\./|/|g
320 :start
321 s|\([^/]*\)/\.\./||
322 tstart
323 s|/*$||
325 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
326 die "$(eval_gettext "'\$sm_path' already exists in the index")"
328 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
329 then
330 eval_gettextln "The following path is ignored by one of your .gitignore files:
331 \$sm_path
332 Use -f if you really want to add it." >&2
333 exit 1
336 # perhaps the path exists and is already a git repo, else clone it
337 if test -e "$sm_path"
338 then
339 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
340 then
341 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
342 else
343 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
346 else
348 module_clone "$sm_path" "$realrepo" "$reference" || exit
350 clear_local_git_env
351 cd "$sm_path" &&
352 # ash fails to wordsplit ${branch:+-b "$branch"...}
353 case "$branch" in
354 '') git checkout -f -q ;;
355 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
356 esac
357 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
359 git config submodule."$sm_path".url "$realrepo"
361 git add $force "$sm_path" ||
362 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
364 git config -f .gitmodules submodule."$sm_path".path "$sm_path" &&
365 git config -f .gitmodules submodule."$sm_path".url "$repo" &&
366 git add --force .gitmodules ||
367 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
371 # Execute an arbitrary command sequence in each checked out
372 # submodule
374 # $@ = command to execute
376 cmd_foreach()
378 # parse $args after "submodule ... foreach".
379 while test $# -ne 0
381 case "$1" in
382 -q|--quiet)
383 GIT_QUIET=1
385 --recursive)
386 recursive=1
389 usage
392 break
394 esac
395 shift
396 done
398 toplevel=$(pwd)
400 # dup stdin so that it can be restored when running the external
401 # command in the subshell (and a recursive call to this function)
402 exec 3<&0
404 module_list |
405 while read mode sha1 stage sm_path
407 die_if_unmatched "$mode"
408 if test -e "$sm_path"/.git
409 then
410 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
411 name=$(module_name "$sm_path")
413 prefix="$prefix$sm_path/"
414 clear_local_git_env
415 # we make $path available to scripts ...
416 path=$sm_path
417 cd "$sm_path" &&
418 eval "$@" &&
419 if test -n "$recursive"
420 then
421 cmd_foreach "--recursive" "$@"
423 ) <&3 3<&- ||
424 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
426 done
430 # Register submodules in .git/config
432 # $@ = requested paths (default to all)
434 cmd_init()
436 # parse $args after "submodule ... init".
437 while test $# -ne 0
439 case "$1" in
440 -q|--quiet)
441 GIT_QUIET=1
444 shift
445 break
448 usage
451 break
453 esac
454 shift
455 done
457 module_list "$@" |
458 while read mode sha1 stage sm_path
460 die_if_unmatched "$mode"
461 name=$(module_name "$sm_path") || exit
463 # Copy url setting when it is not set yet
464 if test -z "$(git config "submodule.$name.url")"
465 then
466 url=$(git config -f .gitmodules submodule."$name".url)
467 test -z "$url" &&
468 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
470 # Possibly a url relative to parent
471 case "$url" in
472 ./*|../*)
473 url=$(resolve_relative_url "$url") || exit
475 esac
476 git config submodule."$name".url "$url" ||
477 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
479 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
482 # Copy "update" setting when it is not set yet
483 upd="$(git config -f .gitmodules submodule."$name".update)"
484 test -z "$upd" ||
485 test -n "$(git config submodule."$name".update)" ||
486 git config submodule."$name".update "$upd" ||
487 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
488 done
492 # Update each submodule path to correct revision, using clone and checkout as needed
494 # $@ = requested paths (default to all)
496 cmd_update()
498 # parse $args after "submodule ... update".
499 orig_flags=
500 while test $# -ne 0
502 case "$1" in
503 -q|--quiet)
504 GIT_QUIET=1
506 -i|--init)
507 init=1
509 -N|--no-fetch)
510 nofetch=1
512 -f|--force)
513 force=$1
515 -r|--rebase)
516 update="rebase"
518 --reference)
519 case "$2" in '') usage ;; esac
520 reference="--reference=$2"
521 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
522 shift
524 --reference=*)
525 reference="$1"
527 -m|--merge)
528 update="merge"
530 --recursive)
531 recursive=1
533 --checkout)
534 update="checkout"
537 shift
538 break
541 usage
544 break
546 esac
547 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
548 shift
549 done
551 if test -n "$init"
552 then
553 cmd_init "--" "$@" || return
556 cloned_modules=
557 module_list "$@" | {
558 err=
559 while read mode sha1 stage sm_path
561 die_if_unmatched "$mode"
562 if test "$stage" = U
563 then
564 echo >&2 "Skipping unmerged submodule $sm_path"
565 continue
567 name=$(module_name "$sm_path") || exit
568 url=$(git config submodule."$name".url)
569 if ! test -z "$update"
570 then
571 update_module=$update
572 else
573 update_module=$(git config submodule."$name".update)
576 if test "$update_module" = "none"
577 then
578 echo "Skipping submodule '$sm_path'"
579 continue
582 if test -z "$url"
583 then
584 # Only mention uninitialized submodules when its
585 # path have been specified
586 test "$#" != "0" &&
587 say "$(eval_gettext "Submodule path '\$sm_path' not initialized
588 Maybe you want to use 'update --init'?")"
589 continue
592 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
593 then
594 module_clone "$sm_path" "$url" "$reference"|| exit
595 cloned_modules="$cloned_modules;$name"
596 subsha1=
597 else
598 subsha1=$(clear_local_git_env; cd "$sm_path" &&
599 git rev-parse --verify HEAD) ||
600 die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
603 if test "$subsha1" != "$sha1" -o -n "$force"
604 then
605 subforce=$force
606 # If we don't already have a -f flag and the submodule has never been checked out
607 if test -z "$subsha1" -a -z "$force"
608 then
609 subforce="-f"
612 if test -z "$nofetch"
613 then
614 # Run fetch only if $sha1 isn't present or it
615 # is not reachable from a ref.
616 (clear_local_git_env; cd "$sm_path" &&
617 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
618 test -z "$rev") || git-fetch)) ||
619 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
622 # Is this something we just cloned?
623 case ";$cloned_modules;" in
624 *";$name;"*)
625 # then there is no local change to integrate
626 update_module= ;;
627 esac
629 must_die_on_failure=
630 case "$update_module" in
631 rebase)
632 command="git rebase"
633 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
634 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
635 must_die_on_failure=yes
637 merge)
638 command="git merge"
639 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
640 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
641 must_die_on_failure=yes
644 command="git checkout $subforce -q"
645 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
646 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
648 esac
650 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
651 then
652 say "$say_msg"
653 elif test -n "$must_die_on_failure"
654 then
655 die_with_status 2 "$die_msg"
656 else
657 err="${err};$die_msg"
658 continue
662 if test -n "$recursive"
663 then
664 (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
665 res=$?
666 if test $res -gt 0
667 then
668 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
669 if test $res -eq 1
670 then
671 err="${err};$die_msg"
672 continue
673 else
674 die_with_status $res "$die_msg"
678 done
680 if test -n "$err"
681 then
682 OIFS=$IFS
683 IFS=';'
684 for e in $err
686 if test -n "$e"
687 then
688 echo >&2 "$e"
690 done
691 IFS=$OIFS
692 exit 1
697 set_name_rev () {
698 revname=$( (
699 clear_local_git_env
700 cd "$1" && {
701 git describe "$2" 2>/dev/null ||
702 git describe --tags "$2" 2>/dev/null ||
703 git describe --contains "$2" 2>/dev/null ||
704 git describe --all --always "$2"
707 test -z "$revname" || revname=" ($revname)"
710 # Show commit summary for submodules in index or working tree
712 # If '--cached' is given, show summary between index and given commit,
713 # or between working tree and given commit
715 # $@ = [commit (default 'HEAD'),] requested paths (default all)
717 cmd_summary() {
718 summary_limit=-1
719 for_status=
720 diff_cmd=diff-index
722 # parse $args after "submodule ... summary".
723 while test $# -ne 0
725 case "$1" in
726 --cached)
727 cached="$1"
729 --files)
730 files="$1"
732 --for-status)
733 for_status="$1"
735 -n|--summary-limit)
736 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
737 then
739 else
740 usage
742 shift
745 shift
746 break
749 usage
752 break
754 esac
755 shift
756 done
758 test $summary_limit = 0 && return
760 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
761 then
762 head=$rev
763 test $# = 0 || shift
764 elif test -z "$1" -o "$1" = "HEAD"
765 then
766 # before the first commit: compare with an empty tree
767 head=$(git hash-object -w -t tree --stdin </dev/null)
768 test -z "$1" || shift
769 else
770 head="HEAD"
773 if [ -n "$files" ]
774 then
775 test -n "$cached" &&
776 die "$(gettext "The --cached option cannot be used with the --files option")"
777 diff_cmd=diff-files
778 head=
781 cd_to_toplevel
782 # Get modified modules cared by user
783 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
784 sane_egrep '^:([0-7]* )?160000' |
785 while read mod_src mod_dst sha1_src sha1_dst status name
787 # Always show modules deleted or type-changed (blob<->module)
788 test $status = D -o $status = T && echo "$name" && continue
789 # Also show added or modified modules which are checked out
790 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
791 echo "$name"
792 done
795 test -z "$modules" && return
797 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
798 sane_egrep '^:([0-7]* )?160000' |
799 cut -c2- |
800 while read mod_src mod_dst sha1_src sha1_dst status name
802 if test -z "$cached" &&
803 test $sha1_dst = 0000000000000000000000000000000000000000
804 then
805 case "$mod_dst" in
806 160000)
807 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
809 100644 | 100755 | 120000)
810 sha1_dst=$(git hash-object $name)
812 000000)
813 ;; # removed
815 # unexpected type
816 eval_gettextln "unexpected mode \$mod_dst" >&2
817 continue ;;
818 esac
820 missing_src=
821 missing_dst=
823 test $mod_src = 160000 &&
824 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
825 missing_src=t
827 test $mod_dst = 160000 &&
828 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
829 missing_dst=t
831 total_commits=
832 case "$missing_src,$missing_dst" in
834 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
837 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
839 t,t)
840 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
843 errmsg=
844 total_commits=$(
845 if test $mod_src = 160000 -a $mod_dst = 160000
846 then
847 range="$sha1_src...$sha1_dst"
848 elif test $mod_src = 160000
849 then
850 range=$sha1_src
851 else
852 range=$sha1_dst
854 GIT_DIR="$name/.git" \
855 git rev-list --first-parent $range -- | wc -l
857 total_commits=" ($(($total_commits + 0)))"
859 esac
861 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
862 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
863 if test $status = T
864 then
865 blob="$(gettext "blob")"
866 submodule="$(gettext "submodule")"
867 if test $mod_dst = 160000
868 then
869 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
870 else
871 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
873 else
874 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
876 if test -n "$errmsg"
877 then
878 # Don't give error msg for modification whose dst is not submodule
879 # i.e. deleted or changed to blob
880 test $mod_dst = 160000 && echo "$errmsg"
881 else
882 if test $mod_src = 160000 -a $mod_dst = 160000
883 then
884 limit=
885 test $summary_limit -gt 0 && limit="-$summary_limit"
886 GIT_DIR="$name/.git" \
887 git log $limit --pretty='format: %m %s' \
888 --first-parent $sha1_src...$sha1_dst
889 elif test $mod_dst = 160000
890 then
891 GIT_DIR="$name/.git" \
892 git log --pretty='format: > %s' -1 $sha1_dst
893 else
894 GIT_DIR="$name/.git" \
895 git log --pretty='format: < %s' -1 $sha1_src
897 echo
899 echo
900 done |
901 if test -n "$for_status"; then
902 if [ -n "$files" ]; then
903 gettextln "# Submodules changed but not updated:"
904 else
905 gettextln "# Submodule changes to be committed:"
907 echo "#"
908 sed -e 's|^|# |' -e 's|^# $|#|'
909 else
914 # List all submodules, prefixed with:
915 # - submodule not initialized
916 # + different revision checked out
918 # If --cached was specified the revision in the index will be printed
919 # instead of the currently checked out revision.
921 # $@ = requested paths (default to all)
923 cmd_status()
925 # parse $args after "submodule ... status".
926 orig_flags=
927 while test $# -ne 0
929 case "$1" in
930 -q|--quiet)
931 GIT_QUIET=1
933 --cached)
934 cached=1
936 --recursive)
937 recursive=1
940 shift
941 break
944 usage
947 break
949 esac
950 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
951 shift
952 done
954 module_list "$@" |
955 while read mode sha1 stage sm_path
957 die_if_unmatched "$mode"
958 name=$(module_name "$sm_path") || exit
959 url=$(git config submodule."$name".url)
960 displaypath="$prefix$sm_path"
961 if test "$stage" = U
962 then
963 say "U$sha1 $displaypath"
964 continue
966 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
967 then
968 say "-$sha1 $displaypath"
969 continue;
971 set_name_rev "$sm_path" "$sha1"
972 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
973 then
974 say " $sha1 $displaypath$revname"
975 else
976 if test -z "$cached"
977 then
978 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
979 set_name_rev "$sm_path" "$sha1"
981 say "+$sha1 $displaypath$revname"
984 if test -n "$recursive"
985 then
987 prefix="$displaypath/"
988 clear_local_git_env
989 cd "$sm_path" &&
990 eval cmd_status "$orig_args"
991 ) ||
992 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
994 done
997 # Sync remote urls for submodules
998 # This makes the value for remote.$remote.url match the value
999 # specified in .gitmodules.
1001 cmd_sync()
1003 while test $# -ne 0
1005 case "$1" in
1006 -q|--quiet)
1007 GIT_QUIET=1
1008 shift
1011 shift
1012 break
1015 usage
1018 break
1020 esac
1021 done
1022 cd_to_toplevel
1023 module_list "$@" |
1024 while read mode sha1 stage sm_path
1026 die_if_unmatched "$mode"
1027 name=$(module_name "$sm_path")
1028 url=$(git config -f .gitmodules --get submodule."$name".url)
1030 # Possibly a url relative to parent
1031 case "$url" in
1032 ./*|../*)
1033 # rewrite foo/bar as ../.. to find path from
1034 # submodule work tree to superproject work tree
1035 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1036 # guarantee a trailing /
1037 up_path=${up_path%/}/ &&
1038 # path from submodule work tree to submodule origin repo
1039 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1040 # path from superproject work tree to submodule origin repo
1041 super_config_url=$(resolve_relative_url "$url") || exit
1044 sub_origin_url="$url"
1045 super_config_url="$url"
1047 esac
1049 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1050 then
1051 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
1052 git config submodule."$name".url "$super_config_url"
1054 if test -e "$sm_path"/.git
1055 then
1057 clear_local_git_env
1058 cd "$sm_path"
1059 remote=$(get_default_remote)
1060 git config remote."$remote".url "$sub_origin_url"
1064 done
1067 # This loop parses the command line arguments to find the
1068 # subcommand name to dispatch. Parsing of the subcommand specific
1069 # options are primarily done by the subcommand implementations.
1070 # Subcommand specific options such as --branch and --cached are
1071 # parsed here as well, for backward compatibility.
1073 while test $# != 0 && test -z "$command"
1075 case "$1" in
1076 add | foreach | init | update | status | summary | sync)
1077 command=$1
1079 -q|--quiet)
1080 GIT_QUIET=1
1082 -b|--branch)
1083 case "$2" in
1085 usage
1087 esac
1088 branch="$2"; shift
1090 --cached)
1091 cached="$1"
1094 break
1097 usage
1100 break
1102 esac
1103 shift
1104 done
1106 # No command word defaults to "status"
1107 if test -z "$command"
1108 then
1109 if test $# = 0
1110 then
1111 command=status
1112 else
1113 usage
1117 # "-b branch" is accepted only by "add"
1118 if test -n "$branch" && test "$command" != add
1119 then
1120 usage
1123 # "--cached" is accepted only by "status" and "summary"
1124 if test -n "$cached" && test "$command" != status -a "$command" != summary
1125 then
1126 usage
1129 "cmd_$command" "$@"