Merge branch 'nd/retire-fnmatch'
[git.git] / git-submodule.sh
blob22ec5b63b4cd36dd10277e842970f9534a623a2f
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] [--remote] [-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 remote=
30 nofetch=
31 update=
32 prefix=
33 custom_name=
35 # The function takes at most 2 arguments. The first argument is the
36 # URL that navigates to the submodule origin repo. When relative, this URL
37 # is relative to the superproject origin URL repo. The second up_path
38 # argument, if specified, is the relative path that navigates
39 # from the submodule working tree to the superproject working tree.
41 # The output of the function is the origin URL of the submodule.
43 # The output will either be an absolute URL or filesystem path (if the
44 # superproject origin URL is an absolute URL or filesystem path,
45 # respectively) or a relative file system path (if the superproject
46 # origin URL is a relative file system path).
48 # When the output is a relative file system path, the path is either
49 # relative to the submodule working tree, if up_path is specified, or to
50 # the superproject working tree otherwise.
51 resolve_relative_url ()
53 remote=$(get_default_remote)
54 remoteurl=$(git config "remote.$remote.url") ||
55 remoteurl=$(pwd) # the repository is its own authoritative upstream
56 url="$1"
57 remoteurl=${remoteurl%/}
58 sep=/
59 up_path="$2"
61 case "$remoteurl" in
62 *:*|/*)
63 is_relative=
65 ./*|../*)
66 is_relative=t
69 is_relative=t
70 remoteurl="./$remoteurl"
72 esac
74 while test -n "$url"
76 case "$url" in
77 ../*)
78 url="${url#../}"
79 case "$remoteurl" in
80 */*)
81 remoteurl="${remoteurl%/*}"
83 *:*)
84 remoteurl="${remoteurl%:*}"
85 sep=:
88 if test -z "$is_relative" || test "." = "$remoteurl"
89 then
90 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
91 else
92 remoteurl=.
95 esac
97 ./*)
98 url="${url#./}"
101 break;;
102 esac
103 done
104 remoteurl="$remoteurl$sep${url%/}"
105 echo "${is_relative:+${up_path}}${remoteurl#./}"
109 # Get submodule info for registered submodules
110 # $@ = path to limit submodule list
112 module_list()
115 git ls-files --error-unmatch --stage -- "$@" ||
116 echo "unmatched pathspec exists"
118 perl -e '
119 my %unmerged = ();
120 my ($null_sha1) = ("0" x 40);
121 my @out = ();
122 my $unmatched = 0;
123 while (<STDIN>) {
124 if (/^unmatched pathspec/) {
125 $unmatched = 1;
126 next;
128 chomp;
129 my ($mode, $sha1, $stage, $path) =
130 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
131 next unless $mode eq "160000";
132 if ($stage ne "0") {
133 if (!$unmerged{$path}++) {
134 push @out, "$mode $null_sha1 U\t$path\n";
136 next;
138 push @out, "$_\n";
140 if ($unmatched) {
141 print "#unmatched\n";
142 } else {
143 print for (@out);
148 die_if_unmatched ()
150 if test "$1" = "#unmatched"
151 then
152 exit 1
157 # Print a submodule configuration setting
159 # $1 = submodule name
160 # $2 = option name
161 # $3 = default value
163 # Checks in the usual git-config places first (for overrides),
164 # otherwise it falls back on .gitmodules. This allows you to
165 # distribute project-wide defaults in .gitmodules, while still
166 # customizing individual repositories if necessary. If the option is
167 # not in .gitmodules either, print a default value.
169 get_submodule_config () {
170 name="$1"
171 option="$2"
172 default="$3"
173 value=$(git config submodule."$name"."$option")
174 if test -z "$value"
175 then
176 value=$(git config -f .gitmodules submodule."$name"."$option")
178 printf '%s' "${value:-$default}"
183 # Map submodule path to submodule name
185 # $1 = path
187 module_name()
189 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
190 sm_path="$1"
191 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
192 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
193 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
194 test -z "$name" &&
195 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
196 echo "$name"
200 # Clone a submodule
202 # Prior to calling, cmd_update checks that a possibly existing
203 # path is not a git repository.
204 # Likewise, cmd_add checks that path does not exist at all,
205 # since it is the location of a new submodule.
207 module_clone()
209 sm_path=$1
210 name=$2
211 url=$3
212 reference="$4"
213 quiet=
214 if test -n "$GIT_QUIET"
215 then
216 quiet=-q
219 gitdir=
220 gitdir_base=
221 base_name=$(dirname "$name")
223 gitdir=$(git rev-parse --git-dir)
224 gitdir_base="$gitdir/modules/$base_name"
225 gitdir="$gitdir/modules/$name"
227 if test -d "$gitdir"
228 then
229 mkdir -p "$sm_path"
230 rm -f "$gitdir/index"
231 else
232 mkdir -p "$gitdir_base"
234 clear_local_git_env
235 git clone $quiet -n ${reference:+"$reference"} \
236 --separate-git-dir "$gitdir" "$url" "$sm_path"
237 ) ||
238 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
241 # We already are at the root of the work tree but cd_to_toplevel will
242 # resolve any symlinks that might be present in $PWD
243 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
244 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
245 # normalize Windows-style absolute paths to POSIX-style absolute paths
246 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
247 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
248 # Remove all common leading directories after a sanity check
249 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
250 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
252 while test "${a%%/*}" = "${b%%/*}"
254 a=${a#*/}
255 b=${b#*/}
256 done
257 # Now chop off the trailing '/'s that were added in the beginning
258 a=${a%/}
259 b=${b%/}
261 # Turn each leading "*/" component into "../"
262 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
263 echo "gitdir: $rel/$a" >"$sm_path/.git"
265 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
266 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
270 # Add a new submodule to the working tree, .gitmodules and the index
272 # $@ = repo path
274 # optional branch is stored in global branch variable
276 cmd_add()
278 # parse $args after "submodule ... add".
279 while test $# -ne 0
281 case "$1" in
282 -b | --branch)
283 case "$2" in '') usage ;; esac
284 branch=$2
285 shift
287 -f | --force)
288 force=$1
290 -q|--quiet)
291 GIT_QUIET=1
293 --reference)
294 case "$2" in '') usage ;; esac
295 reference="--reference=$2"
296 shift
298 --reference=*)
299 reference="$1"
301 --name)
302 case "$2" in '') usage ;; esac
303 custom_name=$2
304 shift
307 shift
308 break
311 usage
314 break
316 esac
317 shift
318 done
320 repo=$1
321 sm_path=$2
323 if test -z "$sm_path"; then
324 sm_path=$(echo "$repo" |
325 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
328 if test -z "$repo" -o -z "$sm_path"; then
329 usage
332 # assure repo is absolute or relative to parent
333 case "$repo" in
334 ./*|../*)
335 # dereference source url relative to parent's url
336 realrepo=$(resolve_relative_url "$repo") || exit
338 *:*|/*)
339 # absolute url
340 realrepo=$repo
343 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
345 esac
347 # normalize path:
348 # multiple //; leading ./; /./; /../; trailing /
349 sm_path=$(printf '%s/\n' "$sm_path" |
350 sed -e '
351 s|//*|/|g
352 s|^\(\./\)*||
353 s|/\./|/|g
354 :start
355 s|\([^/]*\)/\.\./||
356 tstart
357 s|/*$||
359 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
360 die "$(eval_gettext "'\$sm_path' already exists in the index")"
362 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
363 then
364 eval_gettextln "The following path is ignored by one of your .gitignore files:
365 \$sm_path
366 Use -f if you really want to add it." >&2
367 exit 1
370 if test -n "$custom_name"
371 then
372 sm_name="$custom_name"
373 else
374 sm_name="$sm_path"
377 # perhaps the path exists and is already a git repo, else clone it
378 if test -e "$sm_path"
379 then
380 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
381 then
382 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
383 else
384 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
387 else
388 if test -d ".git/modules/$sm_name"
389 then
390 if test -z "$force"
391 then
392 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
393 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
394 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
395 echo >&2 " $realrepo"
396 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
397 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
398 else
399 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
402 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
404 clear_local_git_env
405 cd "$sm_path" &&
406 # ash fails to wordsplit ${branch:+-b "$branch"...}
407 case "$branch" in
408 '') git checkout -f -q ;;
409 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
410 esac
411 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
413 git config submodule."$sm_name".url "$realrepo"
415 git add $force "$sm_path" ||
416 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
418 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
419 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
420 if test -n "$branch"
421 then
422 git config -f .gitmodules submodule."$sm_name".branch "$branch"
423 fi &&
424 git add --force .gitmodules ||
425 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
429 # Execute an arbitrary command sequence in each checked out
430 # submodule
432 # $@ = command to execute
434 cmd_foreach()
436 # parse $args after "submodule ... foreach".
437 while test $# -ne 0
439 case "$1" in
440 -q|--quiet)
441 GIT_QUIET=1
443 --recursive)
444 recursive=1
447 usage
450 break
452 esac
453 shift
454 done
456 toplevel=$(pwd)
458 # dup stdin so that it can be restored when running the external
459 # command in the subshell (and a recursive call to this function)
460 exec 3<&0
462 module_list |
463 while read mode sha1 stage sm_path
465 die_if_unmatched "$mode"
466 if test -e "$sm_path"/.git
467 then
468 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
469 name=$(module_name "$sm_path")
471 prefix="$prefix$sm_path/"
472 clear_local_git_env
473 # we make $path available to scripts ...
474 path=$sm_path
475 cd "$sm_path" &&
476 eval "$@" &&
477 if test -n "$recursive"
478 then
479 cmd_foreach "--recursive" "$@"
481 ) <&3 3<&- ||
482 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
484 done
488 # Register submodules in .git/config
490 # $@ = requested paths (default to all)
492 cmd_init()
494 # parse $args after "submodule ... init".
495 while test $# -ne 0
497 case "$1" in
498 -q|--quiet)
499 GIT_QUIET=1
502 shift
503 break
506 usage
509 break
511 esac
512 shift
513 done
515 module_list "$@" |
516 while read mode sha1 stage sm_path
518 die_if_unmatched "$mode"
519 name=$(module_name "$sm_path") || exit
521 # Copy url setting when it is not set yet
522 if test -z "$(git config "submodule.$name.url")"
523 then
524 url=$(git config -f .gitmodules submodule."$name".url)
525 test -z "$url" &&
526 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
528 # Possibly a url relative to parent
529 case "$url" in
530 ./*|../*)
531 url=$(resolve_relative_url "$url") || exit
533 esac
534 git config submodule."$name".url "$url" ||
535 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
537 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
540 # Copy "update" setting when it is not set yet
541 upd="$(git config -f .gitmodules submodule."$name".update)"
542 test -z "$upd" ||
543 test -n "$(git config submodule."$name".update)" ||
544 git config submodule."$name".update "$upd" ||
545 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
546 done
550 # Update each submodule path to correct revision, using clone and checkout as needed
552 # $@ = requested paths (default to all)
554 cmd_update()
556 # parse $args after "submodule ... update".
557 orig_flags=
558 while test $# -ne 0
560 case "$1" in
561 -q|--quiet)
562 GIT_QUIET=1
564 -i|--init)
565 init=1
567 --remote)
568 remote=1
570 -N|--no-fetch)
571 nofetch=1
573 -f|--force)
574 force=$1
576 -r|--rebase)
577 update="rebase"
579 --reference)
580 case "$2" in '') usage ;; esac
581 reference="--reference=$2"
582 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
583 shift
585 --reference=*)
586 reference="$1"
588 -m|--merge)
589 update="merge"
591 --recursive)
592 recursive=1
594 --checkout)
595 update="checkout"
598 shift
599 break
602 usage
605 break
607 esac
608 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
609 shift
610 done
612 if test -n "$init"
613 then
614 cmd_init "--" "$@" || return
617 cloned_modules=
618 module_list "$@" | {
619 err=
620 while read mode sha1 stage sm_path
622 die_if_unmatched "$mode"
623 if test "$stage" = U
624 then
625 echo >&2 "Skipping unmerged submodule $sm_path"
626 continue
628 name=$(module_name "$sm_path") || exit
629 url=$(git config submodule."$name".url)
630 branch=$(get_submodule_config "$name" branch master)
631 if ! test -z "$update"
632 then
633 update_module=$update
634 else
635 update_module=$(git config submodule."$name".update)
638 if test "$update_module" = "none"
639 then
640 echo "Skipping submodule '$sm_path'"
641 continue
644 if test -z "$url"
645 then
646 # Only mention uninitialized submodules when its
647 # path have been specified
648 test "$#" != "0" &&
649 say "$(eval_gettext "Submodule path '\$sm_path' not initialized
650 Maybe you want to use 'update --init'?")"
651 continue
654 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
655 then
656 module_clone "$sm_path" "$name" "$url" "$reference" || exit
657 cloned_modules="$cloned_modules;$name"
658 subsha1=
659 else
660 subsha1=$(clear_local_git_env; cd "$sm_path" &&
661 git rev-parse --verify HEAD) ||
662 die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
665 if test -n "$remote"
666 then
667 if test -z "$nofetch"
668 then
669 # Fetch remote before determining tracking $sha1
670 (clear_local_git_env; cd "$sm_path" && git-fetch) ||
671 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
673 remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
674 sha1=$(clear_local_git_env; cd "$sm_path" &&
675 git rev-parse --verify "${remote_name}/${branch}") ||
676 die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
679 if test "$subsha1" != "$sha1" -o -n "$force"
680 then
681 subforce=$force
682 # If we don't already have a -f flag and the submodule has never been checked out
683 if test -z "$subsha1" -a -z "$force"
684 then
685 subforce="-f"
688 if test -z "$nofetch"
689 then
690 # Run fetch only if $sha1 isn't present or it
691 # is not reachable from a ref.
692 (clear_local_git_env; cd "$sm_path" &&
693 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
694 test -z "$rev") || git-fetch)) ||
695 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
698 # Is this something we just cloned?
699 case ";$cloned_modules;" in
700 *";$name;"*)
701 # then there is no local change to integrate
702 update_module= ;;
703 esac
705 must_die_on_failure=
706 case "$update_module" in
707 rebase)
708 command="git rebase"
709 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
710 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
711 must_die_on_failure=yes
713 merge)
714 command="git merge"
715 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
716 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
717 must_die_on_failure=yes
720 command="git checkout $subforce -q"
721 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
722 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
724 esac
726 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
727 then
728 say "$say_msg"
729 elif test -n "$must_die_on_failure"
730 then
731 die_with_status 2 "$die_msg"
732 else
733 err="${err};$die_msg"
734 continue
738 if test -n "$recursive"
739 then
740 (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
741 res=$?
742 if test $res -gt 0
743 then
744 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
745 if test $res -eq 1
746 then
747 err="${err};$die_msg"
748 continue
749 else
750 die_with_status $res "$die_msg"
754 done
756 if test -n "$err"
757 then
758 OIFS=$IFS
759 IFS=';'
760 for e in $err
762 if test -n "$e"
763 then
764 echo >&2 "$e"
766 done
767 IFS=$OIFS
768 exit 1
773 set_name_rev () {
774 revname=$( (
775 clear_local_git_env
776 cd "$1" && {
777 git describe "$2" 2>/dev/null ||
778 git describe --tags "$2" 2>/dev/null ||
779 git describe --contains "$2" 2>/dev/null ||
780 git describe --all --always "$2"
783 test -z "$revname" || revname=" ($revname)"
786 # Show commit summary for submodules in index or working tree
788 # If '--cached' is given, show summary between index and given commit,
789 # or between working tree and given commit
791 # $@ = [commit (default 'HEAD'),] requested paths (default all)
793 cmd_summary() {
794 summary_limit=-1
795 for_status=
796 diff_cmd=diff-index
798 # parse $args after "submodule ... summary".
799 while test $# -ne 0
801 case "$1" in
802 --cached)
803 cached="$1"
805 --files)
806 files="$1"
808 --for-status)
809 for_status="$1"
811 -n|--summary-limit)
812 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
813 then
815 else
816 usage
818 shift
821 shift
822 break
825 usage
828 break
830 esac
831 shift
832 done
834 test $summary_limit = 0 && return
836 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
837 then
838 head=$rev
839 test $# = 0 || shift
840 elif test -z "$1" -o "$1" = "HEAD"
841 then
842 # before the first commit: compare with an empty tree
843 head=$(git hash-object -w -t tree --stdin </dev/null)
844 test -z "$1" || shift
845 else
846 head="HEAD"
849 if [ -n "$files" ]
850 then
851 test -n "$cached" &&
852 die "$(gettext "The --cached option cannot be used with the --files option")"
853 diff_cmd=diff-files
854 head=
857 cd_to_toplevel
858 # Get modified modules cared by user
859 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
860 sane_egrep '^:([0-7]* )?160000' |
861 while read mod_src mod_dst sha1_src sha1_dst status name
863 # Always show modules deleted or type-changed (blob<->module)
864 test $status = D -o $status = T && echo "$name" && continue
865 # Also show added or modified modules which are checked out
866 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
867 echo "$name"
868 done
871 test -z "$modules" && return
873 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
874 sane_egrep '^:([0-7]* )?160000' |
875 cut -c2- |
876 while read mod_src mod_dst sha1_src sha1_dst status name
878 if test -z "$cached" &&
879 test $sha1_dst = 0000000000000000000000000000000000000000
880 then
881 case "$mod_dst" in
882 160000)
883 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
885 100644 | 100755 | 120000)
886 sha1_dst=$(git hash-object $name)
888 000000)
889 ;; # removed
891 # unexpected type
892 eval_gettextln "unexpected mode \$mod_dst" >&2
893 continue ;;
894 esac
896 missing_src=
897 missing_dst=
899 test $mod_src = 160000 &&
900 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
901 missing_src=t
903 test $mod_dst = 160000 &&
904 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
905 missing_dst=t
907 total_commits=
908 case "$missing_src,$missing_dst" in
910 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
913 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
915 t,t)
916 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
919 errmsg=
920 total_commits=$(
921 if test $mod_src = 160000 -a $mod_dst = 160000
922 then
923 range="$sha1_src...$sha1_dst"
924 elif test $mod_src = 160000
925 then
926 range=$sha1_src
927 else
928 range=$sha1_dst
930 GIT_DIR="$name/.git" \
931 git rev-list --first-parent $range -- | wc -l
933 total_commits=" ($(($total_commits + 0)))"
935 esac
937 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
938 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
939 if test $status = T
940 then
941 blob="$(gettext "blob")"
942 submodule="$(gettext "submodule")"
943 if test $mod_dst = 160000
944 then
945 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
946 else
947 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
949 else
950 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
952 if test -n "$errmsg"
953 then
954 # Don't give error msg for modification whose dst is not submodule
955 # i.e. deleted or changed to blob
956 test $mod_dst = 160000 && echo "$errmsg"
957 else
958 if test $mod_src = 160000 -a $mod_dst = 160000
959 then
960 limit=
961 test $summary_limit -gt 0 && limit="-$summary_limit"
962 GIT_DIR="$name/.git" \
963 git log $limit --pretty='format: %m %s' \
964 --first-parent $sha1_src...$sha1_dst
965 elif test $mod_dst = 160000
966 then
967 GIT_DIR="$name/.git" \
968 git log --pretty='format: > %s' -1 $sha1_dst
969 else
970 GIT_DIR="$name/.git" \
971 git log --pretty='format: < %s' -1 $sha1_src
973 echo
975 echo
976 done |
977 if test -n "$for_status"; then
978 if [ -n "$files" ]; then
979 gettextln "# Submodules changed but not updated:"
980 else
981 gettextln "# Submodule changes to be committed:"
983 echo "#"
984 sed -e 's|^|# |' -e 's|^# $|#|'
985 else
990 # List all submodules, prefixed with:
991 # - submodule not initialized
992 # + different revision checked out
994 # If --cached was specified the revision in the index will be printed
995 # instead of the currently checked out revision.
997 # $@ = requested paths (default to all)
999 cmd_status()
1001 # parse $args after "submodule ... status".
1002 while test $# -ne 0
1004 case "$1" in
1005 -q|--quiet)
1006 GIT_QUIET=1
1008 --cached)
1009 cached=1
1011 --recursive)
1012 recursive=1
1015 shift
1016 break
1019 usage
1022 break
1024 esac
1025 shift
1026 done
1028 module_list "$@" |
1029 while read mode sha1 stage sm_path
1031 die_if_unmatched "$mode"
1032 name=$(module_name "$sm_path") || exit
1033 url=$(git config submodule."$name".url)
1034 displaypath="$prefix$sm_path"
1035 if test "$stage" = U
1036 then
1037 say "U$sha1 $displaypath"
1038 continue
1040 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
1041 then
1042 say "-$sha1 $displaypath"
1043 continue;
1045 set_name_rev "$sm_path" "$sha1"
1046 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1047 then
1048 say " $sha1 $displaypath$revname"
1049 else
1050 if test -z "$cached"
1051 then
1052 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1053 set_name_rev "$sm_path" "$sha1"
1055 say "+$sha1 $displaypath$revname"
1058 if test -n "$recursive"
1059 then
1061 prefix="$displaypath/"
1062 clear_local_git_env
1063 cd "$sm_path" &&
1064 eval cmd_status
1065 ) ||
1066 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1068 done
1071 # Sync remote urls for submodules
1072 # This makes the value for remote.$remote.url match the value
1073 # specified in .gitmodules.
1075 cmd_sync()
1077 while test $# -ne 0
1079 case "$1" in
1080 -q|--quiet)
1081 GIT_QUIET=1
1082 shift
1084 --recursive)
1085 recursive=1
1086 shift
1089 shift
1090 break
1093 usage
1096 break
1098 esac
1099 done
1100 cd_to_toplevel
1101 module_list "$@" |
1102 while read mode sha1 stage sm_path
1104 die_if_unmatched "$mode"
1105 name=$(module_name "$sm_path")
1106 url=$(git config -f .gitmodules --get submodule."$name".url)
1108 # Possibly a url relative to parent
1109 case "$url" in
1110 ./*|../*)
1111 # rewrite foo/bar as ../.. to find path from
1112 # submodule work tree to superproject work tree
1113 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1114 # guarantee a trailing /
1115 up_path=${up_path%/}/ &&
1116 # path from submodule work tree to submodule origin repo
1117 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1118 # path from superproject work tree to submodule origin repo
1119 super_config_url=$(resolve_relative_url "$url") || exit
1122 sub_origin_url="$url"
1123 super_config_url="$url"
1125 esac
1127 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1128 then
1129 say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")"
1130 git config submodule."$name".url "$super_config_url"
1132 if test -e "$sm_path"/.git
1133 then
1135 clear_local_git_env
1136 cd "$sm_path"
1137 remote=$(get_default_remote)
1138 git config remote."$remote".url "$sub_origin_url"
1140 if test -n "$recursive"
1141 then
1142 prefix="$prefix$sm_path/"
1143 eval cmd_sync
1148 done
1151 # This loop parses the command line arguments to find the
1152 # subcommand name to dispatch. Parsing of the subcommand specific
1153 # options are primarily done by the subcommand implementations.
1154 # Subcommand specific options such as --branch and --cached are
1155 # parsed here as well, for backward compatibility.
1157 while test $# != 0 && test -z "$command"
1159 case "$1" in
1160 add | foreach | init | update | status | summary | sync)
1161 command=$1
1163 -q|--quiet)
1164 GIT_QUIET=1
1166 -b|--branch)
1167 case "$2" in
1169 usage
1171 esac
1172 branch="$2"; shift
1174 --cached)
1175 cached="$1"
1178 break
1181 usage
1184 break
1186 esac
1187 shift
1188 done
1190 # No command word defaults to "status"
1191 if test -z "$command"
1192 then
1193 if test $# = 0
1194 then
1195 command=status
1196 else
1197 usage
1201 # "-b branch" is accepted only by "add"
1202 if test -n "$branch" && test "$command" != add
1203 then
1204 usage
1207 # "--cached" is accepted only by "status" and "summary"
1208 if test -n "$cached" && test "$command" != status -a "$command" != summary
1209 then
1210 usage
1213 "cmd_$command" "$@"