Merge 'git-gui' into HEAD
[git/mingw/4msysgit.git] / git-submodule.sh
blob247273e09e5075bf1a3180d99ce7acca61e0ff45
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] deinit [-f|--force] [--] <path>...
12 or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
13 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
14 or: $dashless [--quiet] foreach [--recursive] <command>
15 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
16 OPTIONS_SPEC=
17 SUBDIRECTORY_OK=Yes
18 . git-sh-setup
19 . git-sh-i18n
20 . git-parse-remote
21 require_work_tree
22 wt_prefix=$(git rev-parse --show-prefix)
23 cd_to_toplevel
25 command=
26 branch=
27 force=
28 reference=
29 cached=
30 recursive=
31 init=
32 files=
33 remote=
34 nofetch=
35 update=
36 prefix=
37 custom_name=
38 depth=
40 # The function takes at most 2 arguments. The first argument is the
41 # URL that navigates to the submodule origin repo. When relative, this URL
42 # is relative to the superproject origin URL repo. The second up_path
43 # argument, if specified, is the relative path that navigates
44 # from the submodule working tree to the superproject working tree.
46 # The output of the function is the origin URL of the submodule.
48 # The output will either be an absolute URL or filesystem path (if the
49 # superproject origin URL is an absolute URL or filesystem path,
50 # respectively) or a relative file system path (if the superproject
51 # origin URL is a relative file system path).
53 # When the output is a relative file system path, the path is either
54 # relative to the submodule working tree, if up_path is specified, or to
55 # the superproject working tree otherwise.
56 resolve_relative_url ()
58 remote=$(get_default_remote)
59 remoteurl=$(git config "remote.$remote.url") ||
60 remoteurl=$(pwd) # the repository is its own authoritative upstream
61 url="$1"
62 remoteurl=${remoteurl%/}
63 sep=/
64 up_path="$2"
66 case "$remoteurl" in
67 *:*|/*)
68 is_relative=
70 ./*|../*)
71 is_relative=t
74 is_relative=t
75 remoteurl="./$remoteurl"
77 esac
79 while test -n "$url"
81 case "$url" in
82 ../*)
83 url="${url#../}"
84 case "$remoteurl" in
85 */*)
86 remoteurl="${remoteurl%/*}"
88 *:*)
89 remoteurl="${remoteurl%:*}"
90 sep=:
93 if test -z "$is_relative" || test "." = "$remoteurl"
94 then
95 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
96 else
97 remoteurl=.
100 esac
102 ./*)
103 url="${url#./}"
106 break;;
107 esac
108 done
109 remoteurl="$remoteurl$sep${url%/}"
110 echo "${is_relative:+${up_path}}${remoteurl#./}"
113 # Resolve a path to be relative to another path. This is intended for
114 # converting submodule paths when git-submodule is run in a subdirectory
115 # and only handles paths where the directory separator is '/'.
117 # The output is the first argument as a path relative to the second argument,
118 # which defaults to $wt_prefix if it is omitted.
119 relative_path ()
121 local target curdir result
122 target=$1
123 curdir=${2-$wt_prefix}
124 curdir=${curdir%/}
125 result=
127 while test -n "$curdir"
129 case "$target" in
130 "$curdir/"*)
131 target=${target#"$curdir"/}
132 break
134 esac
136 result="${result}../"
137 if test "$curdir" = "${curdir%/*}"
138 then
139 curdir=
140 else
141 curdir="${curdir%/*}"
143 done
145 echo "$result$target"
149 # Get submodule info for registered submodules
150 # $@ = path to limit submodule list
152 module_list()
154 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
156 git ls-files -z --error-unmatch --stage -- "$@" ||
157 echo "unmatched pathspec exists"
159 @@PERL@@ -e '
160 my %unmerged = ();
161 my ($null_sha1) = ("0" x 40);
162 my @out = ();
163 my $unmatched = 0;
164 $/ = "\0";
165 while (<STDIN>) {
166 if (/^unmatched pathspec/) {
167 $unmatched = 1;
168 next;
170 chomp;
171 my ($mode, $sha1, $stage, $path) =
172 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
173 next unless $mode eq "160000";
174 if ($stage ne "0") {
175 if (!$unmerged{$path}++) {
176 push @out, "$mode $null_sha1 U\t$path\n";
178 next;
180 push @out, "$_\n";
182 if ($unmatched) {
183 print "#unmatched\n";
184 } else {
185 print for (@out);
190 die_if_unmatched ()
192 if test "$1" = "#unmatched"
193 then
194 exit 1
199 # Print a submodule configuration setting
201 # $1 = submodule name
202 # $2 = option name
203 # $3 = default value
205 # Checks in the usual git-config places first (for overrides),
206 # otherwise it falls back on .gitmodules. This allows you to
207 # distribute project-wide defaults in .gitmodules, while still
208 # customizing individual repositories if necessary. If the option is
209 # not in .gitmodules either, print a default value.
211 get_submodule_config () {
212 name="$1"
213 option="$2"
214 default="$3"
215 value=$(git config submodule."$name"."$option")
216 if test -z "$value"
217 then
218 value=$(git config -f .gitmodules submodule."$name"."$option")
220 printf '%s' "${value:-$default}"
225 # Map submodule path to submodule name
227 # $1 = path
229 module_name()
231 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
232 sm_path="$1"
233 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
234 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
235 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
236 test -z "$name" &&
237 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
238 echo "$name"
242 # Clone a submodule
244 # Prior to calling, cmd_update checks that a possibly existing
245 # path is not a git repository.
246 # Likewise, cmd_add checks that path does not exist at all,
247 # since it is the location of a new submodule.
249 module_clone()
251 sm_path=$1
252 name=$2
253 url=$3
254 reference="$4"
255 depth="$5"
256 quiet=
257 if test -n "$GIT_QUIET"
258 then
259 quiet=-q
262 gitdir=
263 gitdir_base=
264 base_name=$(dirname "$name")
266 gitdir=$(git rev-parse --git-dir)
267 gitdir_base="$gitdir/modules/$base_name"
268 gitdir="$gitdir/modules/$name"
270 if test -d "$gitdir"
271 then
272 mkdir -p "$sm_path"
273 rm -f "$gitdir/index"
274 else
275 mkdir -p "$gitdir_base"
277 clear_local_git_env
278 git clone $quiet ${depth:+"$depth"} -n ${reference:+"$reference"} \
279 --separate-git-dir "$gitdir" "$url" "$sm_path"
280 ) ||
281 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
284 # We already are at the root of the work tree but cd_to_toplevel will
285 # resolve any symlinks that might be present in $PWD
286 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
287 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
288 # Remove all common leading directories after a sanity check
289 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
290 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
292 while test "${a%%/*}" = "${b%%/*}"
294 a=${a#*/}
295 b=${b#*/}
296 done
297 # Now chop off the trailing '/'s that were added in the beginning
298 a=${a%/}
299 b=${b%/}
301 # Turn each leading "*/" component into "../"
302 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
303 echo "gitdir: $rel/$a" >"$sm_path/.git"
305 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
306 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
309 isnumber()
311 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
315 # Add a new submodule to the working tree, .gitmodules and the index
317 # $@ = repo path
319 # optional branch is stored in global branch variable
321 cmd_add()
323 # parse $args after "submodule ... add".
324 reference_path=
325 while test $# -ne 0
327 case "$1" in
328 -b | --branch)
329 case "$2" in '') usage ;; esac
330 branch=$2
331 shift
333 -f | --force)
334 force=$1
336 -q|--quiet)
337 GIT_QUIET=1
339 --reference)
340 case "$2" in '') usage ;; esac
341 reference_path=$2
342 shift
344 --reference=*)
345 reference_path="${1#--reference=}"
347 --name)
348 case "$2" in '') usage ;; esac
349 custom_name=$2
350 shift
352 --depth)
353 case "$2" in '') usage ;; esac
354 depth="--depth=$2"
355 shift
357 --depth=*)
358 depth=$1
361 shift
362 break
365 usage
368 break
370 esac
371 shift
372 done
374 if test -n "$reference_path"
375 then
376 is_absolute_path "$reference_path" ||
377 reference_path="$wt_prefix$reference_path"
379 reference="--reference=$reference_path"
382 repo=$1
383 sm_path=$2
385 if test -z "$sm_path"; then
386 sm_path=$(echo "$repo" |
387 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
390 if test -z "$repo" -o -z "$sm_path"; then
391 usage
394 is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
396 # assure repo is absolute or relative to parent
397 case "$repo" in
398 ./*|../*)
399 test -z "$wt_prefix" ||
400 die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
402 # dereference source url relative to parent's url
403 realrepo=$(resolve_relative_url "$repo") || exit
405 *:*|/*)
406 # absolute url
407 realrepo=$repo
410 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
412 esac
414 # normalize path:
415 # multiple //; leading ./; /./; /../; trailing /
416 sm_path=$(printf '%s/\n' "$sm_path" |
417 sed -e '
418 s|//*|/|g
419 s|^\(\./\)*||
420 s|/\./|/|g
421 :start
422 s|\([^/]*\)/\.\./||
423 tstart
424 s|/*$||
426 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
427 die "$(eval_gettext "'\$sm_path' already exists in the index")"
429 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
430 then
431 eval_gettextln "The following path is ignored by one of your .gitignore files:
432 \$sm_path
433 Use -f if you really want to add it." >&2
434 exit 1
437 if test -n "$custom_name"
438 then
439 sm_name="$custom_name"
440 else
441 sm_name="$sm_path"
444 # perhaps the path exists and is already a git repo, else clone it
445 if test -e "$sm_path"
446 then
447 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
448 then
449 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
450 else
451 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
454 else
455 if test -d ".git/modules/$sm_name"
456 then
457 if test -z "$force"
458 then
459 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
460 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
461 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
462 echo >&2 " $realrepo"
463 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
464 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
465 else
466 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
469 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" || exit
471 clear_local_git_env
472 cd "$sm_path" &&
473 # ash fails to wordsplit ${branch:+-b "$branch"...}
474 case "$branch" in
475 '') git checkout -f -q ;;
476 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
477 esac
478 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
480 git config submodule."$sm_name".url "$realrepo"
482 git add $force "$sm_path" ||
483 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
485 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
486 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
487 if test -n "$branch"
488 then
489 git config -f .gitmodules submodule."$sm_name".branch "$branch"
490 fi &&
491 git add --force .gitmodules ||
492 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
496 # Execute an arbitrary command sequence in each checked out
497 # submodule
499 # $@ = command to execute
501 cmd_foreach()
503 # parse $args after "submodule ... foreach".
504 while test $# -ne 0
506 case "$1" in
507 -q|--quiet)
508 GIT_QUIET=1
510 --recursive)
511 recursive=1
514 usage
517 break
519 esac
520 shift
521 done
523 toplevel=$(pwd)
525 # dup stdin so that it can be restored when running the external
526 # command in the subshell (and a recursive call to this function)
527 exec 3<&0
529 module_list |
530 while read mode sha1 stage sm_path
532 die_if_unmatched "$mode"
533 if test -e "$sm_path"/.git
534 then
535 displaypath=$(relative_path "$sm_path")
536 say "$(eval_gettext "Entering '\$prefix\$displaypath'")"
537 name=$(module_name "$sm_path")
539 prefix="$prefix$sm_path/"
540 clear_local_git_env
541 cd "$sm_path" &&
542 sm_path=$(relative_path "$sm_path") &&
543 # we make $path available to scripts ...
544 path=$sm_path &&
545 if test $# -eq 1
546 then
547 eval "$1"
548 else
549 "$@"
550 fi &&
551 if test -n "$recursive"
552 then
553 cmd_foreach "--recursive" "$@"
555 ) <&3 3<&- ||
556 die "$(eval_gettext "Stopping at '\$prefix\$displaypath'; script returned non-zero status.")"
558 done
562 # Register submodules in .git/config
564 # $@ = requested paths (default to all)
566 cmd_init()
568 # parse $args after "submodule ... init".
569 while test $# -ne 0
571 case "$1" in
572 -q|--quiet)
573 GIT_QUIET=1
576 shift
577 break
580 usage
583 break
585 esac
586 shift
587 done
589 module_list "$@" |
590 while read mode sha1 stage sm_path
592 die_if_unmatched "$mode"
593 name=$(module_name "$sm_path") || exit
595 displaypath=$(relative_path "$sm_path")
597 # Copy url setting when it is not set yet
598 if test -z "$(git config "submodule.$name.url")"
599 then
600 url=$(git config -f .gitmodules submodule."$name".url)
601 test -z "$url" &&
602 die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")"
604 # Possibly a url relative to parent
605 case "$url" in
606 ./*|../*)
607 url=$(resolve_relative_url "$url") || exit
609 esac
610 git config submodule."$name".url "$url" ||
611 die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")"
613 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")"
616 # Copy "update" setting when it is not set yet
617 if upd="$(git config -f .gitmodules submodule."$name".update)" &&
618 test -n "$upd" &&
619 test -z "$(git config submodule."$name".update)"
620 then
621 case "$upd" in
622 checkout | rebase | merge | none)
623 ;; # known modes of updating
625 echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'"
626 upd=none
628 esac
629 git config submodule."$name".update "$upd" ||
630 die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")"
632 done
636 # Unregister submodules from .git/config and remove their work tree
638 # $@ = requested paths (use '.' to deinit all submodules)
640 cmd_deinit()
642 # parse $args after "submodule ... deinit".
643 while test $# -ne 0
645 case "$1" in
646 -f|--force)
647 force=$1
649 -q|--quiet)
650 GIT_QUIET=1
653 shift
654 break
657 usage
660 break
662 esac
663 shift
664 done
666 if test $# = 0
667 then
668 die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
671 module_list "$@" |
672 while read mode sha1 stage sm_path
674 die_if_unmatched "$mode"
675 name=$(module_name "$sm_path") || exit
677 displaypath=$(relative_path "$sm_path")
679 # Remove the submodule work tree (unless the user already did it)
680 if test -d "$sm_path"
681 then
682 # Protect submodules containing a .git directory
683 if test -d "$sm_path/.git"
684 then
685 echo >&2 "$(eval_gettext "Submodule work tree '\$displaypath' contains a .git directory")"
686 die "$(eval_gettext "(use 'rm -rf' if you really want to remove it including all of its history)")"
689 if test -z "$force"
690 then
691 git rm -qn "$sm_path" ||
692 die "$(eval_gettext "Submodule work tree '\$displaypath' contains local modifications; use '-f' to discard them")"
694 rm -rf "$sm_path" &&
695 say "$(eval_gettext "Cleared directory '\$displaypath'")" ||
696 say "$(eval_gettext "Could not remove submodule work tree '\$displaypath'")"
699 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$displaypath'")"
701 # Remove the .git/config entries (unless the user already did it)
702 if test -n "$(git config --get-regexp submodule."$name\.")"
703 then
704 # Remove the whole section so we have a clean state when
705 # the user later decides to init this submodule again
706 url=$(git config submodule."$name".url)
707 git config --remove-section submodule."$name" 2>/dev/null &&
708 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$displaypath'")"
710 done
714 # Update each submodule path to correct revision, using clone and checkout as needed
716 # $@ = requested paths (default to all)
718 cmd_update()
720 # parse $args after "submodule ... update".
721 while test $# -ne 0
723 case "$1" in
724 -q|--quiet)
725 GIT_QUIET=1
727 -i|--init)
728 init=1
730 --remote)
731 remote=1
733 -N|--no-fetch)
734 nofetch=1
736 -f|--force)
737 force=$1
739 -r|--rebase)
740 update="rebase"
742 --reference)
743 case "$2" in '') usage ;; esac
744 reference="--reference=$2"
745 shift
747 --reference=*)
748 reference="$1"
750 -m|--merge)
751 update="merge"
753 --recursive)
754 recursive=1
756 --checkout)
757 update="checkout"
759 --depth)
760 case "$2" in '') usage ;; esac
761 depth="--depth=$2"
762 shift
764 --depth=*)
765 depth=$1
768 shift
769 break
772 usage
775 break
777 esac
778 shift
779 done
781 if test -n "$init"
782 then
783 cmd_init "--" "$@" || return
786 cloned_modules=
787 module_list "$@" | {
788 err=
789 while read mode sha1 stage sm_path
791 die_if_unmatched "$mode"
792 if test "$stage" = U
793 then
794 echo >&2 "Skipping unmerged submodule $prefix$sm_path"
795 continue
797 name=$(module_name "$sm_path") || exit
798 url=$(git config submodule."$name".url)
799 branch=$(get_submodule_config "$name" branch master)
800 if ! test -z "$update"
801 then
802 update_module=$update
803 else
804 update_module=$(git config submodule."$name".update)
805 case "$update_module" in
807 ;; # Unset update mode
808 checkout | rebase | merge | none)
809 ;; # Known update modes
811 ;; # Custom update command
813 die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
815 esac
818 displaypath=$(relative_path "$prefix$sm_path")
820 if test "$update_module" = "none"
821 then
822 echo "Skipping submodule '$displaypath'"
823 continue
826 if test -z "$url"
827 then
828 # Only mention uninitialized submodules when its
829 # path have been specified
830 test "$#" != "0" &&
831 say "$(eval_gettext "Submodule path '\$displaypath' not initialized
832 Maybe you want to use 'update --init'?")"
833 continue
836 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
837 then
838 module_clone "$sm_path" "$name" "$url" "$reference" "$depth" || exit
839 cloned_modules="$cloned_modules;$name"
840 subsha1=
841 else
842 subsha1=$(clear_local_git_env; cd "$sm_path" &&
843 git rev-parse --verify HEAD) ||
844 die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
847 if test -n "$remote"
848 then
849 if test -z "$nofetch"
850 then
851 # Fetch remote before determining tracking $sha1
852 (clear_local_git_env; cd "$sm_path" && git-fetch) ||
853 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
855 remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
856 sha1=$(clear_local_git_env; cd "$sm_path" &&
857 git rev-parse --verify "${remote_name}/${branch}") ||
858 die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
861 if test "$subsha1" != "$sha1" -o -n "$force"
862 then
863 subforce=$force
864 # If we don't already have a -f flag and the submodule has never been checked out
865 if test -z "$subsha1" -a -z "$force"
866 then
867 subforce="-f"
870 if test -z "$nofetch"
871 then
872 # Run fetch only if $sha1 isn't present or it
873 # is not reachable from a ref.
874 (clear_local_git_env; cd "$sm_path" &&
875 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
876 test -z "$rev") || git-fetch)) ||
877 die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
880 # Is this something we just cloned?
881 case ";$cloned_modules;" in
882 *";$name;"*)
883 # then there is no local change to integrate
884 update_module= ;;
885 esac
887 must_die_on_failure=
888 case "$update_module" in
889 rebase)
890 command="git rebase"
891 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
892 say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
893 must_die_on_failure=yes
895 merge)
896 command="git merge"
897 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
898 say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
899 must_die_on_failure=yes
902 command="${update_module#!}"
903 die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$prefix\$sm_path'")"
904 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': '\$command \$sha1'")"
905 must_die_on_failure=yes
908 command="git checkout $subforce -q"
909 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
910 say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
912 esac
914 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
915 then
916 say "$say_msg"
917 elif test -n "$must_die_on_failure"
918 then
919 die_with_status 2 "$die_msg"
920 else
921 err="${err};$die_msg"
922 continue
926 if test -n "$recursive"
927 then
929 prefix="$prefix$sm_path/"
930 clear_local_git_env
931 cd "$sm_path" &&
932 eval cmd_update
934 res=$?
935 if test $res -gt 0
936 then
937 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
938 if test $res -eq 1
939 then
940 err="${err};$die_msg"
941 continue
942 else
943 die_with_status $res "$die_msg"
947 done
949 if test -n "$err"
950 then
951 OIFS=$IFS
952 IFS=';'
953 for e in $err
955 if test -n "$e"
956 then
957 echo >&2 "$e"
959 done
960 IFS=$OIFS
961 exit 1
966 set_name_rev () {
967 revname=$( (
968 clear_local_git_env
969 cd "$1" && {
970 git describe "$2" 2>/dev/null ||
971 git describe --tags "$2" 2>/dev/null ||
972 git describe --contains "$2" 2>/dev/null ||
973 git describe --all --always "$2"
976 test -z "$revname" || revname=" ($revname)"
979 # Show commit summary for submodules in index or working tree
981 # If '--cached' is given, show summary between index and given commit,
982 # or between working tree and given commit
984 # $@ = [commit (default 'HEAD'),] requested paths (default all)
986 cmd_summary() {
987 summary_limit=-1
988 for_status=
989 diff_cmd=diff-index
991 # parse $args after "submodule ... summary".
992 while test $# -ne 0
994 case "$1" in
995 --cached)
996 cached="$1"
998 --files)
999 files="$1"
1001 --for-status)
1002 for_status="$1"
1004 -n|--summary-limit)
1005 summary_limit="$2"
1006 isnumber "$summary_limit" || usage
1007 shift
1009 --summary-limit=*)
1010 summary_limit="${1#--summary-limit=}"
1011 isnumber "$summary_limit" || usage
1014 shift
1015 break
1018 usage
1021 break
1023 esac
1024 shift
1025 done
1027 test $summary_limit = 0 && return
1029 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
1030 then
1031 head=$rev
1032 test $# = 0 || shift
1033 elif test -z "$1" -o "$1" = "HEAD"
1034 then
1035 # before the first commit: compare with an empty tree
1036 head=$(git hash-object -w -t tree --stdin </dev/null)
1037 test -z "$1" || shift
1038 else
1039 head="HEAD"
1042 if [ -n "$files" ]
1043 then
1044 test -n "$cached" &&
1045 die "$(gettext "The --cached option cannot be used with the --files option")"
1046 diff_cmd=diff-files
1047 head=
1050 cd_to_toplevel
1051 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
1052 # Get modified modules cared by user
1053 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
1054 sane_egrep '^:([0-7]* )?160000' |
1055 while read mod_src mod_dst sha1_src sha1_dst status sm_path
1057 # Always show modules deleted or type-changed (blob<->module)
1058 test $status = D -o $status = T && echo "$sm_path" && continue
1059 # Respect the ignore setting for --for-status.
1060 if test -n "$for_status"
1061 then
1062 name=$(module_name "$sm_path")
1063 ignore_config=$(get_submodule_config "$name" ignore none)
1064 test $status != A -a $ignore_config = all && continue
1066 # Also show added or modified modules which are checked out
1067 GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
1068 echo "$sm_path"
1069 done
1072 test -z "$modules" && return
1074 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
1075 sane_egrep '^:([0-7]* )?160000' |
1076 cut -c2- |
1077 while read mod_src mod_dst sha1_src sha1_dst status name
1079 if test -z "$cached" &&
1080 test $sha1_dst = 0000000000000000000000000000000000000000
1081 then
1082 case "$mod_dst" in
1083 160000)
1084 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
1086 100644 | 100755 | 120000)
1087 sha1_dst=$(git hash-object $name)
1089 000000)
1090 ;; # removed
1092 # unexpected type
1093 eval_gettextln "unexpected mode \$mod_dst" >&2
1094 continue ;;
1095 esac
1097 missing_src=
1098 missing_dst=
1100 test $mod_src = 160000 &&
1101 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1102 missing_src=t
1104 test $mod_dst = 160000 &&
1105 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1106 missing_dst=t
1108 display_name=$(relative_path "$name")
1110 total_commits=
1111 case "$missing_src,$missing_dst" in
1113 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_src")"
1116 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_dst")"
1118 t,t)
1119 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
1122 errmsg=
1123 total_commits=$(
1124 if test $mod_src = 160000 -a $mod_dst = 160000
1125 then
1126 range="$sha1_src...$sha1_dst"
1127 elif test $mod_src = 160000
1128 then
1129 range=$sha1_src
1130 else
1131 range=$sha1_dst
1133 GIT_DIR="$name/.git" \
1134 git rev-list --first-parent $range -- | wc -l
1136 total_commits=" ($(($total_commits + 0)))"
1138 esac
1140 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
1141 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
1142 if test $status = T
1143 then
1144 blob="$(gettext "blob")"
1145 submodule="$(gettext "submodule")"
1146 if test $mod_dst = 160000
1147 then
1148 echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1149 else
1150 echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1152 else
1153 echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1155 if test -n "$errmsg"
1156 then
1157 # Don't give error msg for modification whose dst is not submodule
1158 # i.e. deleted or changed to blob
1159 test $mod_dst = 160000 && echo "$errmsg"
1160 else
1161 if test $mod_src = 160000 -a $mod_dst = 160000
1162 then
1163 limit=
1164 test $summary_limit -gt 0 && limit="-$summary_limit"
1165 GIT_DIR="$name/.git" \
1166 git log $limit --pretty='format: %m %s' \
1167 --first-parent $sha1_src...$sha1_dst
1168 elif test $mod_dst = 160000
1169 then
1170 GIT_DIR="$name/.git" \
1171 git log --pretty='format: > %s' -1 $sha1_dst
1172 else
1173 GIT_DIR="$name/.git" \
1174 git log --pretty='format: < %s' -1 $sha1_src
1176 echo
1178 echo
1179 done
1182 # List all submodules, prefixed with:
1183 # - submodule not initialized
1184 # + different revision checked out
1186 # If --cached was specified the revision in the index will be printed
1187 # instead of the currently checked out revision.
1189 # $@ = requested paths (default to all)
1191 cmd_status()
1193 # parse $args after "submodule ... status".
1194 while test $# -ne 0
1196 case "$1" in
1197 -q|--quiet)
1198 GIT_QUIET=1
1200 --cached)
1201 cached=1
1203 --recursive)
1204 recursive=1
1207 shift
1208 break
1211 usage
1214 break
1216 esac
1217 shift
1218 done
1220 module_list "$@" |
1221 while read mode sha1 stage sm_path
1223 die_if_unmatched "$mode"
1224 name=$(module_name "$sm_path") || exit
1225 url=$(git config submodule."$name".url)
1226 displaypath=$(relative_path "$prefix$sm_path")
1227 if test "$stage" = U
1228 then
1229 say "U$sha1 $displaypath"
1230 continue
1232 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
1233 then
1234 say "-$sha1 $displaypath"
1235 continue;
1237 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1238 then
1239 set_name_rev "$sm_path" "$sha1"
1240 say " $sha1 $displaypath$revname"
1241 else
1242 if test -z "$cached"
1243 then
1244 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1246 set_name_rev "$sm_path" "$sha1"
1247 say "+$sha1 $displaypath$revname"
1250 if test -n "$recursive"
1251 then
1253 prefix="$displaypath/"
1254 clear_local_git_env
1255 cd "$sm_path" &&
1256 eval cmd_status
1257 ) ||
1258 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1260 done
1263 # Sync remote urls for submodules
1264 # This makes the value for remote.$remote.url match the value
1265 # specified in .gitmodules.
1267 cmd_sync()
1269 while test $# -ne 0
1271 case "$1" in
1272 -q|--quiet)
1273 GIT_QUIET=1
1274 shift
1276 --recursive)
1277 recursive=1
1278 shift
1281 shift
1282 break
1285 usage
1288 break
1290 esac
1291 done
1292 cd_to_toplevel
1293 module_list "$@" |
1294 while read mode sha1 stage sm_path
1296 die_if_unmatched "$mode"
1297 name=$(module_name "$sm_path")
1298 url=$(git config -f .gitmodules --get submodule."$name".url)
1300 # Possibly a url relative to parent
1301 case "$url" in
1302 ./*|../*)
1303 # rewrite foo/bar as ../.. to find path from
1304 # submodule work tree to superproject work tree
1305 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1306 # guarantee a trailing /
1307 up_path=${up_path%/}/ &&
1308 # path from submodule work tree to submodule origin repo
1309 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1310 # path from superproject work tree to submodule origin repo
1311 super_config_url=$(resolve_relative_url "$url") || exit
1314 sub_origin_url="$url"
1315 super_config_url="$url"
1317 esac
1319 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1320 then
1321 displaypath=$(relative_path "$prefix$sm_path")
1322 say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
1323 git config submodule."$name".url "$super_config_url"
1325 if test -e "$sm_path"/.git
1326 then
1328 clear_local_git_env
1329 cd "$sm_path"
1330 remote=$(get_default_remote)
1331 git config remote."$remote".url "$sub_origin_url"
1333 if test -n "$recursive"
1334 then
1335 prefix="$prefix$sm_path/"
1336 eval cmd_sync
1341 done
1344 # This loop parses the command line arguments to find the
1345 # subcommand name to dispatch. Parsing of the subcommand specific
1346 # options are primarily done by the subcommand implementations.
1347 # Subcommand specific options such as --branch and --cached are
1348 # parsed here as well, for backward compatibility.
1350 while test $# != 0 && test -z "$command"
1352 case "$1" in
1353 add | foreach | init | deinit | update | status | summary | sync)
1354 command=$1
1356 -q|--quiet)
1357 GIT_QUIET=1
1359 -b|--branch)
1360 case "$2" in
1362 usage
1364 esac
1365 branch="$2"; shift
1367 --cached)
1368 cached="$1"
1371 break
1374 usage
1377 break
1379 esac
1380 shift
1381 done
1383 # No command word defaults to "status"
1384 if test -z "$command"
1385 then
1386 if test $# = 0
1387 then
1388 command=status
1389 else
1390 usage
1394 # "-b branch" is accepted only by "add"
1395 if test -n "$branch" && test "$command" != add
1396 then
1397 usage
1400 # "--cached" is accepted only by "status" and "summary"
1401 if test -n "$cached" && test "$command" != status -a "$command" != summary
1402 then
1403 usage
1406 "cmd_$command" "$@"