Detect console streams more reliably on Windows
[git/mingw/4msysgit.git] / git-submodule.sh
blob2ea78bd0d555b42dcb504301e7ec5a2118da4bc1
1 #!/bin/sh
3 # git-submodule.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 dashless=$(basename "$0" | sed -e 's/-/ /')
8 USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] 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 [--recursive] [--] [<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=
32 custom_name=
34 # The function takes at most 2 arguments. The first argument is the
35 # URL that navigates to the submodule origin repo. When relative, this URL
36 # is relative to the superproject origin URL repo. The second up_path
37 # argument, if specified, is the relative path that navigates
38 # from the submodule working tree to the superproject working tree.
40 # The output of the function is the origin URL of the submodule.
42 # The output will either be an absolute URL or filesystem path (if the
43 # superproject origin URL is an absolute URL or filesystem path,
44 # respectively) or a relative file system path (if the superproject
45 # origin URL is a relative file system path).
47 # When the output is a relative file system path, the path is either
48 # relative to the submodule working tree, if up_path is specified, or to
49 # the superproject working tree otherwise.
50 resolve_relative_url ()
52 remote=$(get_default_remote)
53 remoteurl=$(git config "remote.$remote.url") ||
54 remoteurl=$(pwd) # the repository is its own authoritative upstream
55 url="$1"
56 remoteurl=${remoteurl%/}
57 sep=/
58 up_path="$2"
60 case "$remoteurl" in
61 *:*|/*)
62 is_relative=
64 ./*|../*)
65 is_relative=t
68 is_relative=t
69 remoteurl="./$remoteurl"
71 esac
73 while test -n "$url"
75 case "$url" in
76 ../*)
77 url="${url#../}"
78 case "$remoteurl" in
79 */*)
80 remoteurl="${remoteurl%/*}"
82 *:*)
83 remoteurl="${remoteurl%:*}"
84 sep=:
87 if test -z "$is_relative" || test "." = "$remoteurl"
88 then
89 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
90 else
91 remoteurl=.
94 esac
96 ./*)
97 url="${url#./}"
100 break;;
101 esac
102 done
103 remoteurl="$remoteurl$sep${url%/}"
104 echo "${is_relative:+${up_path}}${remoteurl#./}"
108 # Get submodule info for registered submodules
109 # $@ = path to limit submodule list
111 module_list()
114 git ls-files --error-unmatch --stage -- "$@" ||
115 echo "unmatched pathspec exists"
117 perl -e '
118 my %unmerged = ();
119 my ($null_sha1) = ("0" x 40);
120 my @out = ();
121 my $unmatched = 0;
122 while (<STDIN>) {
123 if (/^unmatched pathspec/) {
124 $unmatched = 1;
125 next;
127 chomp;
128 my ($mode, $sha1, $stage, $path) =
129 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
130 next unless $mode eq "160000";
131 if ($stage ne "0") {
132 if (!$unmerged{$path}++) {
133 push @out, "$mode $null_sha1 U\t$path\n";
135 next;
137 push @out, "$_\n";
139 if ($unmatched) {
140 print "#unmatched\n";
141 } else {
142 print for (@out);
147 die_if_unmatched ()
149 if test "$1" = "#unmatched"
150 then
151 exit 1
156 # Map submodule path to submodule name
158 # $1 = path
160 module_name()
162 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
163 sm_path="$1"
164 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
165 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
166 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
167 test -z "$name" &&
168 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
169 echo "$name"
173 # Clone a submodule
175 # Prior to calling, cmd_update checks that a possibly existing
176 # path is not a git repository.
177 # Likewise, cmd_add checks that path does not exist at all,
178 # since it is the location of a new submodule.
180 module_clone()
182 sm_path=$1
183 name=$2
184 url=$3
185 reference="$4"
186 quiet=
187 if test -n "$GIT_QUIET"
188 then
189 quiet=-q
192 gitdir=
193 gitdir_base=
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"
271 --name)
272 case "$2" in '') usage ;; esac
273 custom_name=$2
274 shift
277 shift
278 break
281 usage
284 break
286 esac
287 shift
288 done
290 repo=$1
291 sm_path=$2
293 if test -z "$sm_path"; then
294 sm_path=$(echo "$repo" |
295 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
298 if test -z "$repo" -o -z "$sm_path"; then
299 usage
302 # assure repo is absolute or relative to parent
303 case "$repo" in
304 ./*|../*)
305 # dereference source url relative to parent's url
306 realrepo=$(resolve_relative_url "$repo") || exit
308 *:*|/*)
309 # absolute url
310 realrepo=$repo
313 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
315 esac
317 # normalize path:
318 # multiple //; leading ./; /./; /../; trailing /
319 sm_path=$(printf '%s/\n' "$sm_path" |
320 sed -e '
321 s|//*|/|g
322 s|^\(\./\)*||
323 s|/\./|/|g
324 :start
325 s|\([^/]*\)/\.\./||
326 tstart
327 s|/*$||
329 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
330 die "$(eval_gettext "'\$sm_path' already exists in the index")"
332 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
333 then
334 cat >&2 <<EOF
335 The following path is ignored by one of your .gitignore files:
336 $(eval_gettextln $sm_path)
337 Use -f if you really want to add it.
339 exit 1
342 if test -n "$custom_name"
343 then
344 sm_name="$custom_name"
345 else
346 sm_name="$sm_path"
349 # perhaps the path exists and is already a git repo, else clone it
350 if test -e "$sm_path"
351 then
352 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
353 then
354 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
355 else
356 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
359 else
360 if test -d ".git/modules/$sm_name"
361 then
362 if test -z "$force"
363 then
364 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
365 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
366 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
367 echo >&2 " $realrepo"
368 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
369 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
370 else
371 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
374 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
376 clear_local_git_env
377 cd "$sm_path" &&
378 # ash fails to wordsplit ${branch:+-b "$branch"...}
379 case "$branch" in
380 '') git checkout -f -q ;;
381 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
382 esac
383 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
385 git config submodule."$sm_name".url "$realrepo"
387 git add $force "$sm_path" ||
388 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
390 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
391 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
392 git add --force .gitmodules ||
393 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
397 # Execute an arbitrary command sequence in each checked out
398 # submodule
400 # $@ = command to execute
402 cmd_foreach()
404 # parse $args after "submodule ... foreach".
405 while test $# -ne 0
407 case "$1" in
408 -q|--quiet)
409 GIT_QUIET=1
411 --recursive)
412 recursive=1
415 usage
418 break
420 esac
421 shift
422 done
424 toplevel=$(pwd)
426 # dup stdin so that it can be restored when running the external
427 # command in the subshell (and a recursive call to this function)
428 exec 3<&0
430 module_list |
431 while read mode sha1 stage sm_path
433 die_if_unmatched "$mode"
434 if test -e "$sm_path"/.git
435 then
436 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
437 name=$(module_name "$sm_path")
439 prefix="$prefix$sm_path/"
440 clear_local_git_env
441 # we make $path available to scripts ...
442 path=$sm_path
443 cd "$sm_path" &&
444 eval "$@" &&
445 if test -n "$recursive"
446 then
447 cmd_foreach "--recursive" "$@"
449 ) <&3 3<&- ||
450 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
452 done
456 # Register submodules in .git/config
458 # $@ = requested paths (default to all)
460 cmd_init()
462 # parse $args after "submodule ... init".
463 while test $# -ne 0
465 case "$1" in
466 -q|--quiet)
467 GIT_QUIET=1
470 shift
471 break
474 usage
477 break
479 esac
480 shift
481 done
483 module_list "$@" |
484 while read mode sha1 stage sm_path
486 die_if_unmatched "$mode"
487 name=$(module_name "$sm_path") || exit
489 # Copy url setting when it is not set yet
490 if test -z "$(git config "submodule.$name.url")"
491 then
492 url=$(git config -f .gitmodules submodule."$name".url)
493 test -z "$url" &&
494 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
496 # Possibly a url relative to parent
497 case "$url" in
498 ./*|../*)
499 url=$(resolve_relative_url "$url") || exit
501 esac
502 git config submodule."$name".url "$url" ||
503 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
505 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
508 # Copy "update" setting when it is not set yet
509 upd="$(git config -f .gitmodules submodule."$name".update)"
510 test -z "$upd" ||
511 test -n "$(git config submodule."$name".update)" ||
512 git config submodule."$name".update "$upd" ||
513 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
514 done
518 # Update each submodule path to correct revision, using clone and checkout as needed
520 # $@ = requested paths (default to all)
522 cmd_update()
524 # parse $args after "submodule ... update".
525 orig_flags=
526 while test $# -ne 0
528 case "$1" in
529 -q|--quiet)
530 GIT_QUIET=1
532 -i|--init)
533 init=1
535 -N|--no-fetch)
536 nofetch=1
538 -f|--force)
539 force=$1
541 -r|--rebase)
542 update="rebase"
544 --reference)
545 case "$2" in '') usage ;; esac
546 reference="--reference=$2"
547 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
548 shift
550 --reference=*)
551 reference="$1"
553 -m|--merge)
554 update="merge"
556 --recursive)
557 recursive=1
559 --checkout)
560 update="checkout"
563 shift
564 break
567 usage
570 break
572 esac
573 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
574 shift
575 done
577 if test -n "$init"
578 then
579 cmd_init "--" "$@" || return
582 cloned_modules=
583 module_list "$@" | {
584 err=
585 while read mode sha1 stage sm_path
587 die_if_unmatched "$mode"
588 if test "$stage" = U
589 then
590 echo >&2 "Skipping unmerged submodule $sm_path"
591 continue
593 name=$(module_name "$sm_path") || exit
594 url=$(git config submodule."$name".url)
595 if ! test -z "$update"
596 then
597 update_module=$update
598 else
599 update_module=$(git config submodule."$name".update)
602 if test "$update_module" = "none"
603 then
604 echo "Skipping submodule '$sm_path'"
605 continue
608 if test -z "$url"
609 then
610 # Only mention uninitialized submodules when its
611 # path have been specified
612 test "$#" != "0" &&
613 say "$(eval_gettext "Submodule path '\$sm_path' not initialized
614 Maybe you want to use 'update --init'?")"
615 continue
618 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
619 then
620 module_clone "$sm_path" "$name" "$url" "$reference" || exit
621 cloned_modules="$cloned_modules;$name"
622 subsha1=
623 else
624 subsha1=$(clear_local_git_env; cd "$sm_path" &&
625 git rev-parse --verify HEAD) ||
626 die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
629 if test "$subsha1" != "$sha1" -o -n "$force"
630 then
631 subforce=$force
632 # If we don't already have a -f flag and the submodule has never been checked out
633 if test -z "$subsha1" -a -z "$force"
634 then
635 subforce="-f"
638 if test -z "$nofetch"
639 then
640 # Run fetch only if $sha1 isn't present or it
641 # is not reachable from a ref.
642 (clear_local_git_env; cd "$sm_path" &&
643 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
644 test -z "$rev") || git-fetch)) ||
645 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
648 # Is this something we just cloned?
649 case ";$cloned_modules;" in
650 *";$name;"*)
651 # then there is no local change to integrate
652 update_module= ;;
653 esac
655 must_die_on_failure=
656 case "$update_module" in
657 rebase)
658 command="git rebase"
659 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
660 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
661 must_die_on_failure=yes
663 merge)
664 command="git merge"
665 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
666 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
667 must_die_on_failure=yes
670 command="git checkout $subforce -q"
671 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
672 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
674 esac
676 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
677 then
678 say "$say_msg"
679 elif test -n "$must_die_on_failure"
680 then
681 die_with_status 2 "$die_msg"
682 else
683 err="${err};$die_msg"
684 continue
688 if test -n "$recursive"
689 then
690 (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
691 res=$?
692 if test $res -gt 0
693 then
694 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
695 if test $res -eq 1
696 then
697 err="${err};$die_msg"
698 continue
699 else
700 die_with_status $res "$die_msg"
704 done
706 if test -n "$err"
707 then
708 OIFS=$IFS
709 IFS=';'
710 for e in $err
712 if test -n "$e"
713 then
714 echo >&2 "$e"
716 done
717 IFS=$OIFS
718 exit 1
723 set_name_rev () {
724 revname=$( (
725 clear_local_git_env
726 cd "$1" && {
727 git describe "$2" 2>/dev/null ||
728 git describe --tags "$2" 2>/dev/null ||
729 git describe --contains "$2" 2>/dev/null ||
730 git describe --all --always "$2"
733 test -z "$revname" || revname=" ($revname)"
736 # Show commit summary for submodules in index or working tree
738 # If '--cached' is given, show summary between index and given commit,
739 # or between working tree and given commit
741 # $@ = [commit (default 'HEAD'),] requested paths (default all)
743 cmd_summary() {
744 summary_limit=-1
745 for_status=
746 diff_cmd=diff-index
748 # parse $args after "submodule ... summary".
749 while test $# -ne 0
751 case "$1" in
752 --cached)
753 cached="$1"
755 --files)
756 files="$1"
758 --for-status)
759 for_status="$1"
761 -n|--summary-limit)
762 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
763 then
765 else
766 usage
768 shift
771 shift
772 break
775 usage
778 break
780 esac
781 shift
782 done
784 test $summary_limit = 0 && return
786 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
787 then
788 head=$rev
789 test $# = 0 || shift
790 elif test -z "$1" -o "$1" = "HEAD"
791 then
792 # before the first commit: compare with an empty tree
793 head=$(git hash-object -w -t tree --stdin </dev/null)
794 test -z "$1" || shift
795 else
796 head="HEAD"
799 if [ -n "$files" ]
800 then
801 test -n "$cached" &&
802 die "$(gettext "The --cached option cannot be used with the --files option")"
803 diff_cmd=diff-files
804 head=
807 cd_to_toplevel
808 # Get modified modules cared by user
809 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
810 sane_egrep '^:([0-7]* )?160000' |
811 while read mod_src mod_dst sha1_src sha1_dst status name
813 # Always show modules deleted or type-changed (blob<->module)
814 test $status = D -o $status = T && echo "$name" && continue
815 # Also show added or modified modules which are checked out
816 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
817 echo "$name"
818 done
821 test -z "$modules" && return
823 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
824 sane_egrep '^:([0-7]* )?160000' |
825 cut -c2- |
826 while read mod_src mod_dst sha1_src sha1_dst status name
828 if test -z "$cached" &&
829 test $sha1_dst = 0000000000000000000000000000000000000000
830 then
831 case "$mod_dst" in
832 160000)
833 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
835 100644 | 100755 | 120000)
836 sha1_dst=$(git hash-object $name)
838 000000)
839 ;; # removed
841 # unexpected type
842 eval_gettextln "unexpected mode \$mod_dst" >&2
843 continue ;;
844 esac
846 missing_src=
847 missing_dst=
849 test $mod_src = 160000 &&
850 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
851 missing_src=t
853 test $mod_dst = 160000 &&
854 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
855 missing_dst=t
857 total_commits=
858 case "$missing_src,$missing_dst" in
860 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
863 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
865 t,t)
866 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
869 errmsg=
870 total_commits=$(
871 if test $mod_src = 160000 -a $mod_dst = 160000
872 then
873 range="$sha1_src...$sha1_dst"
874 elif test $mod_src = 160000
875 then
876 range=$sha1_src
877 else
878 range=$sha1_dst
880 GIT_DIR="$name/.git" \
881 git rev-list --first-parent $range -- | wc -l
883 total_commits=" ($(($total_commits + 0)))"
885 esac
887 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
888 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
889 if test $status = T
890 then
891 blob="$(gettext "blob")"
892 submodule="$(gettext "submodule")"
893 if test $mod_dst = 160000
894 then
895 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
896 else
897 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
899 else
900 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
902 if test -n "$errmsg"
903 then
904 # Don't give error msg for modification whose dst is not submodule
905 # i.e. deleted or changed to blob
906 test $mod_dst = 160000 && echo "$errmsg"
907 else
908 if test $mod_src = 160000 -a $mod_dst = 160000
909 then
910 limit=
911 test $summary_limit -gt 0 && limit="-$summary_limit"
912 GIT_DIR="$name/.git" \
913 git log $limit --pretty='format: %m %s' \
914 --first-parent $sha1_src...$sha1_dst
915 elif test $mod_dst = 160000
916 then
917 GIT_DIR="$name/.git" \
918 git log --pretty='format: > %s' -1 $sha1_dst
919 else
920 GIT_DIR="$name/.git" \
921 git log --pretty='format: < %s' -1 $sha1_src
923 echo
925 echo
926 done |
927 if test -n "$for_status"; then
928 if [ -n "$files" ]; then
929 status_msg="$(gettextln "# Submodules changed but not updated:")"
930 else
931 status_msg="$(gettextln "# Submodule changes to be committed:")"
933 status_sed=$(sed -e 's|^|# |' -e 's|^# $|#|')
934 cat <<EOF
935 $status_msg
937 $status_sed
939 else
944 # List all submodules, prefixed with:
945 # - submodule not initialized
946 # + different revision checked out
948 # If --cached was specified the revision in the index will be printed
949 # instead of the currently checked out revision.
951 # $@ = requested paths (default to all)
953 cmd_status()
955 # parse $args after "submodule ... status".
956 while test $# -ne 0
958 case "$1" in
959 -q|--quiet)
960 GIT_QUIET=1
962 --cached)
963 cached=1
965 --recursive)
966 recursive=1
969 shift
970 break
973 usage
976 break
978 esac
979 shift
980 done
982 module_list "$@" |
983 while read mode sha1 stage sm_path
985 die_if_unmatched "$mode"
986 name=$(module_name "$sm_path") || exit
987 url=$(git config submodule."$name".url)
988 displaypath="$prefix$sm_path"
989 if test "$stage" = U
990 then
991 say "U$sha1 $displaypath"
992 continue
994 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
995 then
996 say "-$sha1 $displaypath"
997 continue;
999 set_name_rev "$sm_path" "$sha1"
1000 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1001 then
1002 say " $sha1 $displaypath$revname"
1003 else
1004 if test -z "$cached"
1005 then
1006 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1007 set_name_rev "$sm_path" "$sha1"
1009 say "+$sha1 $displaypath$revname"
1012 if test -n "$recursive"
1013 then
1015 prefix="$displaypath/"
1016 clear_local_git_env
1017 cd "$sm_path" &&
1018 eval cmd_status
1019 ) ||
1020 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1022 done
1025 # Sync remote urls for submodules
1026 # This makes the value for remote.$remote.url match the value
1027 # specified in .gitmodules.
1029 cmd_sync()
1031 while test $# -ne 0
1033 case "$1" in
1034 -q|--quiet)
1035 GIT_QUIET=1
1036 shift
1038 --recursive)
1039 recursive=1
1040 shift
1043 shift
1044 break
1047 usage
1050 break
1052 esac
1053 done
1054 cd_to_toplevel
1055 module_list "$@" |
1056 while read mode sha1 stage sm_path
1058 die_if_unmatched "$mode"
1059 name=$(module_name "$sm_path")
1060 url=$(git config -f .gitmodules --get submodule."$name".url)
1062 # Possibly a url relative to parent
1063 case "$url" in
1064 ./*|../*)
1065 # rewrite foo/bar as ../.. to find path from
1066 # submodule work tree to superproject work tree
1067 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1068 # guarantee a trailing /
1069 up_path=${up_path%/}/ &&
1070 # path from submodule work tree to submodule origin repo
1071 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1072 # path from superproject work tree to submodule origin repo
1073 super_config_url=$(resolve_relative_url "$url") || exit
1076 sub_origin_url="$url"
1077 super_config_url="$url"
1079 esac
1081 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1082 then
1083 say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")"
1084 git config submodule."$name".url "$super_config_url"
1086 if test -e "$sm_path"/.git
1087 then
1089 clear_local_git_env
1090 cd "$sm_path"
1091 remote=$(get_default_remote)
1092 git config remote."$remote".url "$sub_origin_url"
1094 if test -n "$recursive"
1095 then
1096 prefix="$prefix$sm_path/"
1097 eval cmd_sync
1102 done
1105 # This loop parses the command line arguments to find the
1106 # subcommand name to dispatch. Parsing of the subcommand specific
1107 # options are primarily done by the subcommand implementations.
1108 # Subcommand specific options such as --branch and --cached are
1109 # parsed here as well, for backward compatibility.
1111 while test $# != 0 && test -z "$command"
1113 case "$1" in
1114 add | foreach | init | update | status | summary | sync)
1115 command=$1
1117 -q|--quiet)
1118 GIT_QUIET=1
1120 -b|--branch)
1121 case "$2" in
1123 usage
1125 esac
1126 branch="$2"; shift
1128 --cached)
1129 cached="$1"
1132 break
1135 usage
1138 break
1140 esac
1141 shift
1142 done
1144 # No command word defaults to "status"
1145 if test -z "$command"
1146 then
1147 if test $# = 0
1148 then
1149 command=status
1150 else
1151 usage
1155 # "-b branch" is accepted only by "add"
1156 if test -n "$branch" && test "$command" != add
1157 then
1158 usage
1161 # "--cached" is accepted only by "status" and "summary"
1162 if test -n "$cached" && test "$command" != status -a "$command" != summary
1163 then
1164 usage
1167 "cmd_$command" "$@"