Windows: Always normalize paths to Windows-style
[git/mingw/4msysgit.git] / git-submodule.sh
blob55d60944e31dee1f0f0779af12577ddb9736f81e
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 eval_gettextln "The following path is ignored by one of your .gitignore files:
335 \$sm_path
336 Use -f if you really want to add it." >&2
337 exit 1
340 if test -n "$custom_name"
341 then
342 sm_name="$custom_name"
343 else
344 sm_name="$sm_path"
347 # perhaps the path exists and is already a git repo, else clone it
348 if test -e "$sm_path"
349 then
350 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
351 then
352 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
353 else
354 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
357 else
358 if test -d ".git/modules/$sm_name"
359 then
360 if test -z "$force"
361 then
362 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
363 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
364 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
365 echo >&2 " $realrepo"
366 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
367 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
368 else
369 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
372 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
374 clear_local_git_env
375 cd "$sm_path" &&
376 # ash fails to wordsplit ${branch:+-b "$branch"...}
377 case "$branch" in
378 '') git checkout -f -q ;;
379 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
380 esac
381 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
383 git config submodule."$sm_name".url "$realrepo"
385 git add $force "$sm_path" ||
386 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
388 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
389 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
390 git add --force .gitmodules ||
391 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
395 # Execute an arbitrary command sequence in each checked out
396 # submodule
398 # $@ = command to execute
400 cmd_foreach()
402 # parse $args after "submodule ... foreach".
403 while test $# -ne 0
405 case "$1" in
406 -q|--quiet)
407 GIT_QUIET=1
409 --recursive)
410 recursive=1
413 usage
416 break
418 esac
419 shift
420 done
422 toplevel=$(pwd)
424 # dup stdin so that it can be restored when running the external
425 # command in the subshell (and a recursive call to this function)
426 exec 3<&0
428 module_list |
429 while read mode sha1 stage sm_path
431 die_if_unmatched "$mode"
432 if test -e "$sm_path"/.git
433 then
434 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
435 name=$(module_name "$sm_path")
437 prefix="$prefix$sm_path/"
438 clear_local_git_env
439 # we make $path available to scripts ...
440 path=$sm_path
441 cd "$sm_path" &&
442 eval "$@" &&
443 if test -n "$recursive"
444 then
445 cmd_foreach "--recursive" "$@"
447 ) <&3 3<&- ||
448 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
450 done
454 # Register submodules in .git/config
456 # $@ = requested paths (default to all)
458 cmd_init()
460 # parse $args after "submodule ... init".
461 while test $# -ne 0
463 case "$1" in
464 -q|--quiet)
465 GIT_QUIET=1
468 shift
469 break
472 usage
475 break
477 esac
478 shift
479 done
481 module_list "$@" |
482 while read mode sha1 stage sm_path
484 die_if_unmatched "$mode"
485 name=$(module_name "$sm_path") || exit
487 # Copy url setting when it is not set yet
488 if test -z "$(git config "submodule.$name.url")"
489 then
490 url=$(git config -f .gitmodules submodule."$name".url)
491 test -z "$url" &&
492 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
494 # Possibly a url relative to parent
495 case "$url" in
496 ./*|../*)
497 url=$(resolve_relative_url "$url") || exit
499 esac
500 git config submodule."$name".url "$url" ||
501 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
503 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
506 # Copy "update" setting when it is not set yet
507 upd="$(git config -f .gitmodules submodule."$name".update)"
508 test -z "$upd" ||
509 test -n "$(git config submodule."$name".update)" ||
510 git config submodule."$name".update "$upd" ||
511 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
512 done
516 # Update each submodule path to correct revision, using clone and checkout as needed
518 # $@ = requested paths (default to all)
520 cmd_update()
522 # parse $args after "submodule ... update".
523 orig_flags=
524 while test $# -ne 0
526 case "$1" in
527 -q|--quiet)
528 GIT_QUIET=1
530 -i|--init)
531 init=1
533 -N|--no-fetch)
534 nofetch=1
536 -f|--force)
537 force=$1
539 -r|--rebase)
540 update="rebase"
542 --reference)
543 case "$2" in '') usage ;; esac
544 reference="--reference=$2"
545 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
546 shift
548 --reference=*)
549 reference="$1"
551 -m|--merge)
552 update="merge"
554 --recursive)
555 recursive=1
557 --checkout)
558 update="checkout"
561 shift
562 break
565 usage
568 break
570 esac
571 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
572 shift
573 done
575 if test -n "$init"
576 then
577 cmd_init "--" "$@" || return
580 cloned_modules=
581 module_list "$@" | {
582 err=
583 while read mode sha1 stage sm_path
585 die_if_unmatched "$mode"
586 if test "$stage" = U
587 then
588 echo >&2 "Skipping unmerged submodule $sm_path"
589 continue
591 name=$(module_name "$sm_path") || exit
592 url=$(git config submodule."$name".url)
593 if ! test -z "$update"
594 then
595 update_module=$update
596 else
597 update_module=$(git config submodule."$name".update)
600 if test "$update_module" = "none"
601 then
602 echo "Skipping submodule '$sm_path'"
603 continue
606 if test -z "$url"
607 then
608 # Only mention uninitialized submodules when its
609 # path have been specified
610 test "$#" != "0" &&
611 say "$(eval_gettext "Submodule path '\$sm_path' not initialized
612 Maybe you want to use 'update --init'?")"
613 continue
616 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
617 then
618 module_clone "$sm_path" "$name" "$url" "$reference" || exit
619 cloned_modules="$cloned_modules;$name"
620 subsha1=
621 else
622 subsha1=$(clear_local_git_env; cd "$sm_path" &&
623 git rev-parse --verify HEAD) ||
624 die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
627 if test "$subsha1" != "$sha1" -o -n "$force"
628 then
629 subforce=$force
630 # If we don't already have a -f flag and the submodule has never been checked out
631 if test -z "$subsha1" -a -z "$force"
632 then
633 subforce="-f"
636 if test -z "$nofetch"
637 then
638 # Run fetch only if $sha1 isn't present or it
639 # is not reachable from a ref.
640 (clear_local_git_env; cd "$sm_path" &&
641 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
642 test -z "$rev") || git-fetch)) ||
643 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
646 # Is this something we just cloned?
647 case ";$cloned_modules;" in
648 *";$name;"*)
649 # then there is no local change to integrate
650 update_module= ;;
651 esac
653 must_die_on_failure=
654 case "$update_module" in
655 rebase)
656 command="git rebase"
657 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
658 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
659 must_die_on_failure=yes
661 merge)
662 command="git merge"
663 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
664 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
665 must_die_on_failure=yes
668 command="git checkout $subforce -q"
669 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
670 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
672 esac
674 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
675 then
676 say "$say_msg"
677 elif test -n "$must_die_on_failure"
678 then
679 die_with_status 2 "$die_msg"
680 else
681 err="${err};$die_msg"
682 continue
686 if test -n "$recursive"
687 then
688 (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
689 res=$?
690 if test $res -gt 0
691 then
692 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
693 if test $res -eq 1
694 then
695 err="${err};$die_msg"
696 continue
697 else
698 die_with_status $res "$die_msg"
702 done
704 if test -n "$err"
705 then
706 OIFS=$IFS
707 IFS=';'
708 for e in $err
710 if test -n "$e"
711 then
712 echo >&2 "$e"
714 done
715 IFS=$OIFS
716 exit 1
721 set_name_rev () {
722 revname=$( (
723 clear_local_git_env
724 cd "$1" && {
725 git describe "$2" 2>/dev/null ||
726 git describe --tags "$2" 2>/dev/null ||
727 git describe --contains "$2" 2>/dev/null ||
728 git describe --all --always "$2"
731 test -z "$revname" || revname=" ($revname)"
734 # Show commit summary for submodules in index or working tree
736 # If '--cached' is given, show summary between index and given commit,
737 # or between working tree and given commit
739 # $@ = [commit (default 'HEAD'),] requested paths (default all)
741 cmd_summary() {
742 summary_limit=-1
743 for_status=
744 diff_cmd=diff-index
746 # parse $args after "submodule ... summary".
747 while test $# -ne 0
749 case "$1" in
750 --cached)
751 cached="$1"
753 --files)
754 files="$1"
756 --for-status)
757 for_status="$1"
759 -n|--summary-limit)
760 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
761 then
763 else
764 usage
766 shift
769 shift
770 break
773 usage
776 break
778 esac
779 shift
780 done
782 test $summary_limit = 0 && return
784 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
785 then
786 head=$rev
787 test $# = 0 || shift
788 elif test -z "$1" -o "$1" = "HEAD"
789 then
790 # before the first commit: compare with an empty tree
791 head=$(git hash-object -w -t tree --stdin </dev/null)
792 test -z "$1" || shift
793 else
794 head="HEAD"
797 if [ -n "$files" ]
798 then
799 test -n "$cached" &&
800 die "$(gettext "The --cached option cannot be used with the --files option")"
801 diff_cmd=diff-files
802 head=
805 cd_to_toplevel
806 # Get modified modules cared by user
807 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
808 sane_egrep '^:([0-7]* )?160000' |
809 while read mod_src mod_dst sha1_src sha1_dst status name
811 # Always show modules deleted or type-changed (blob<->module)
812 test $status = D -o $status = T && echo "$name" && continue
813 # Also show added or modified modules which are checked out
814 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
815 echo "$name"
816 done
819 test -z "$modules" && return
821 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
822 sane_egrep '^:([0-7]* )?160000' |
823 cut -c2- |
824 while read mod_src mod_dst sha1_src sha1_dst status name
826 if test -z "$cached" &&
827 test $sha1_dst = 0000000000000000000000000000000000000000
828 then
829 case "$mod_dst" in
830 160000)
831 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
833 100644 | 100755 | 120000)
834 sha1_dst=$(git hash-object $name)
836 000000)
837 ;; # removed
839 # unexpected type
840 eval_gettextln "unexpected mode \$mod_dst" >&2
841 continue ;;
842 esac
844 missing_src=
845 missing_dst=
847 test $mod_src = 160000 &&
848 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
849 missing_src=t
851 test $mod_dst = 160000 &&
852 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
853 missing_dst=t
855 total_commits=
856 case "$missing_src,$missing_dst" in
858 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
861 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
863 t,t)
864 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
867 errmsg=
868 total_commits=$(
869 if test $mod_src = 160000 -a $mod_dst = 160000
870 then
871 range="$sha1_src...$sha1_dst"
872 elif test $mod_src = 160000
873 then
874 range=$sha1_src
875 else
876 range=$sha1_dst
878 GIT_DIR="$name/.git" \
879 git rev-list --first-parent $range -- | wc -l
881 total_commits=" ($(($total_commits + 0)))"
883 esac
885 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
886 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
887 if test $status = T
888 then
889 blob="$(gettext "blob")"
890 submodule="$(gettext "submodule")"
891 if test $mod_dst = 160000
892 then
893 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
894 else
895 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
897 else
898 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
900 if test -n "$errmsg"
901 then
902 # Don't give error msg for modification whose dst is not submodule
903 # i.e. deleted or changed to blob
904 test $mod_dst = 160000 && echo "$errmsg"
905 else
906 if test $mod_src = 160000 -a $mod_dst = 160000
907 then
908 limit=
909 test $summary_limit -gt 0 && limit="-$summary_limit"
910 GIT_DIR="$name/.git" \
911 git log $limit --pretty='format: %m %s' \
912 --first-parent $sha1_src...$sha1_dst
913 elif test $mod_dst = 160000
914 then
915 GIT_DIR="$name/.git" \
916 git log --pretty='format: > %s' -1 $sha1_dst
917 else
918 GIT_DIR="$name/.git" \
919 git log --pretty='format: < %s' -1 $sha1_src
921 echo
923 echo
924 done |
925 if test -n "$for_status"; then
926 if [ -n "$files" ]; then
927 gettextln "# Submodules changed but not updated:"
928 else
929 gettextln "# Submodule changes to be committed:"
931 echo "#"
932 sed -e 's|^|# |' -e 's|^# $|#|'
933 else
938 # List all submodules, prefixed with:
939 # - submodule not initialized
940 # + different revision checked out
942 # If --cached was specified the revision in the index will be printed
943 # instead of the currently checked out revision.
945 # $@ = requested paths (default to all)
947 cmd_status()
949 # parse $args after "submodule ... status".
950 while test $# -ne 0
952 case "$1" in
953 -q|--quiet)
954 GIT_QUIET=1
956 --cached)
957 cached=1
959 --recursive)
960 recursive=1
963 shift
964 break
967 usage
970 break
972 esac
973 shift
974 done
976 module_list "$@" |
977 while read mode sha1 stage sm_path
979 die_if_unmatched "$mode"
980 name=$(module_name "$sm_path") || exit
981 url=$(git config submodule."$name".url)
982 displaypath="$prefix$sm_path"
983 if test "$stage" = U
984 then
985 say "U$sha1 $displaypath"
986 continue
988 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
989 then
990 say "-$sha1 $displaypath"
991 continue;
993 set_name_rev "$sm_path" "$sha1"
994 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
995 then
996 say " $sha1 $displaypath$revname"
997 else
998 if test -z "$cached"
999 then
1000 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1001 set_name_rev "$sm_path" "$sha1"
1003 say "+$sha1 $displaypath$revname"
1006 if test -n "$recursive"
1007 then
1009 prefix="$displaypath/"
1010 clear_local_git_env
1011 cd "$sm_path" &&
1012 eval cmd_status
1013 ) ||
1014 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1016 done
1019 # Sync remote urls for submodules
1020 # This makes the value for remote.$remote.url match the value
1021 # specified in .gitmodules.
1023 cmd_sync()
1025 while test $# -ne 0
1027 case "$1" in
1028 -q|--quiet)
1029 GIT_QUIET=1
1030 shift
1032 --recursive)
1033 recursive=1
1034 shift
1037 shift
1038 break
1041 usage
1044 break
1046 esac
1047 done
1048 cd_to_toplevel
1049 module_list "$@" |
1050 while read mode sha1 stage sm_path
1052 die_if_unmatched "$mode"
1053 name=$(module_name "$sm_path")
1054 url=$(git config -f .gitmodules --get submodule."$name".url)
1056 # Possibly a url relative to parent
1057 case "$url" in
1058 ./*|../*)
1059 # rewrite foo/bar as ../.. to find path from
1060 # submodule work tree to superproject work tree
1061 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1062 # guarantee a trailing /
1063 up_path=${up_path%/}/ &&
1064 # path from submodule work tree to submodule origin repo
1065 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1066 # path from superproject work tree to submodule origin repo
1067 super_config_url=$(resolve_relative_url "$url") || exit
1070 sub_origin_url="$url"
1071 super_config_url="$url"
1073 esac
1075 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1076 then
1077 say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")"
1078 git config submodule."$name".url "$super_config_url"
1080 if test -e "$sm_path"/.git
1081 then
1083 clear_local_git_env
1084 cd "$sm_path"
1085 remote=$(get_default_remote)
1086 git config remote."$remote".url "$sub_origin_url"
1088 if test -n "$recursive"
1089 then
1090 prefix="$prefix$sm_path/"
1091 eval cmd_sync
1096 done
1099 # This loop parses the command line arguments to find the
1100 # subcommand name to dispatch. Parsing of the subcommand specific
1101 # options are primarily done by the subcommand implementations.
1102 # Subcommand specific options such as --branch and --cached are
1103 # parsed here as well, for backward compatibility.
1105 while test $# != 0 && test -z "$command"
1107 case "$1" in
1108 add | foreach | init | update | status | summary | sync)
1109 command=$1
1111 -q|--quiet)
1112 GIT_QUIET=1
1114 -b|--branch)
1115 case "$2" in
1117 usage
1119 esac
1120 branch="$2"; shift
1122 --cached)
1123 cached="$1"
1126 break
1129 usage
1132 break
1134 esac
1135 shift
1136 done
1138 # No command word defaults to "status"
1139 if test -z "$command"
1140 then
1141 if test $# = 0
1142 then
1143 command=status
1144 else
1145 usage
1149 # "-b branch" is accepted only by "add"
1150 if test -n "$branch" && test "$command" != add
1151 then
1152 usage
1155 # "--cached" is accepted only by "status" and "summary"
1156 if test -n "$cached" && test "$command" != status -a "$command" != summary
1157 then
1158 usage
1161 "cmd_$command" "$@"