merge-recursive: Do not look at working tree during a virtual ancestor merge
[git/mjg.git] / git-submodule.sh
blob814d0d914eaf994462e61559b0692c958f97513b
1 #!/bin/sh
3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 dashless=$(basename "$0" | sed -e 's/-/ /')
8 USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>]
9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
12 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
13 or: $dashless [--quiet] foreach [--recursive] <command>
14 or: $dashless [--quiet] sync [--] [<path>...]"
15 OPTIONS_SPEC=
16 . git-sh-setup
17 . git-sh-i18n
18 . git-parse-remote
19 require_work_tree
21 command=
22 branch=
23 force=
24 reference=
25 cached=
26 recursive=
27 init=
28 files=
29 nofetch=
30 update=
31 prefix=
33 # Resolve relative url by appending to parent's url
34 resolve_relative_url ()
36 remote=$(get_default_remote)
37 remoteurl=$(git config "remote.$remote.url") ||
38 remoteurl=$(pwd) # the repository is its own authoritative upstream
39 url="$1"
40 remoteurl=${remoteurl%/}
41 sep=/
42 while test -n "$url"
44 case "$url" in
45 ../*)
46 url="${url#../}"
47 case "$remoteurl" in
48 */*)
49 remoteurl="${remoteurl%/*}"
51 *:*)
52 remoteurl="${remoteurl%:*}"
53 sep=:
56 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
58 esac
60 ./*)
61 url="${url#./}"
64 break;;
65 esac
66 done
67 echo "$remoteurl$sep${url%/}"
71 # Get submodule info for registered submodules
72 # $@ = path to limit submodule list
74 module_list()
76 git ls-files --error-unmatch --stage -- "$@" |
77 perl -e '
78 my %unmerged = ();
79 my ($null_sha1) = ("0" x 40);
80 while (<STDIN>) {
81 chomp;
82 my ($mode, $sha1, $stage, $path) =
83 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
84 next unless $mode eq "160000";
85 if ($stage ne "0") {
86 if (!$unmerged{$path}++) {
87 print "$mode $null_sha1 U\t$path\n";
89 next;
91 print "$_\n";
97 # Map submodule path to submodule name
99 # $1 = path
101 module_name()
103 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
104 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
105 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
106 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
107 test -z "$name" &&
108 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$path'")"
109 echo "$name"
113 # Clone a submodule
115 # Prior to calling, cmd_update checks that a possibly existing
116 # path is not a git repository.
117 # Likewise, cmd_add checks that path does not exist at all,
118 # since it is the location of a new submodule.
120 module_clone()
122 path=$1
123 url=$2
124 reference="$3"
125 quiet=
126 if test -n "$GIT_QUIET"
127 then
128 quiet=-q
131 if test -n "$reference"
132 then
133 git-clone $quiet "$reference" -n "$url" "$path"
134 else
135 git-clone $quiet -n "$url" "$path"
136 fi ||
137 die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")"
141 # Add a new submodule to the working tree, .gitmodules and the index
143 # $@ = repo path
145 # optional branch is stored in global branch variable
147 cmd_add()
149 # parse $args after "submodule ... add".
150 while test $# -ne 0
152 case "$1" in
153 -b | --branch)
154 case "$2" in '') usage ;; esac
155 branch=$2
156 shift
158 -f | --force)
159 force=$1
161 -q|--quiet)
162 GIT_QUIET=1
164 --reference)
165 case "$2" in '') usage ;; esac
166 reference="--reference=$2"
167 shift
169 --reference=*)
170 reference="$1"
171 shift
174 shift
175 break
178 usage
181 break
183 esac
184 shift
185 done
187 repo=$1
188 path=$2
190 if test -z "$path"; then
191 path=$(echo "$repo" |
192 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
195 if test -z "$repo" -o -z "$path"; then
196 usage
199 # assure repo is absolute or relative to parent
200 case "$repo" in
201 ./*|../*)
202 # dereference source url relative to parent's url
203 realrepo=$(resolve_relative_url "$repo") || exit
205 *:*|/*)
206 # absolute url
207 realrepo=$repo
210 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
212 esac
214 # normalize path:
215 # multiple //; leading ./; /./; /../; trailing /
216 path=$(printf '%s/\n' "$path" |
217 sed -e '
218 s|//*|/|g
219 s|^\(\./\)*||
220 s|/\./|/|g
221 :start
222 s|\([^/]*\)/\.\./||
223 tstart
224 s|/*$||
226 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
227 die "$(eval_gettext "'\$path' already exists in the index")"
229 if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
230 then
231 eval_gettextln "The following path is ignored by one of your .gitignore files:
232 \$path
233 Use -f if you really want to add it." >&2
234 exit 1
237 # perhaps the path exists and is already a git repo, else clone it
238 if test -e "$path"
239 then
240 if test -d "$path"/.git -o -f "$path"/.git
241 then
242 eval_gettextln "Adding existing repo at '\$path' to the index"
243 else
244 die "$(eval_gettext "'\$path' already exists and is not a valid git repo")"
247 else
249 module_clone "$path" "$realrepo" "$reference" || exit
251 clear_local_git_env
252 cd "$path" &&
253 # ash fails to wordsplit ${branch:+-b "$branch"...}
254 case "$branch" in
255 '') git checkout -f -q ;;
256 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
257 esac
258 ) || die "$(eval_gettext "Unable to checkout submodule '\$path'")"
260 git config submodule."$path".url "$realrepo"
262 git add $force "$path" ||
263 die "$(eval_gettext "Failed to add submodule '\$path'")"
265 git config -f .gitmodules submodule."$path".path "$path" &&
266 git config -f .gitmodules submodule."$path".url "$repo" &&
267 git add --force .gitmodules ||
268 die "$(eval_gettext "Failed to register submodule '\$path'")"
272 # Execute an arbitrary command sequence in each checked out
273 # submodule
275 # $@ = command to execute
277 cmd_foreach()
279 # parse $args after "submodule ... foreach".
280 while test $# -ne 0
282 case "$1" in
283 -q|--quiet)
284 GIT_QUIET=1
286 --recursive)
287 recursive=1
290 usage
293 break
295 esac
296 shift
297 done
299 toplevel=$(pwd)
301 # dup stdin so that it can be restored when running the external
302 # command in the subshell (and a recursive call to this function)
303 exec 3<&0
305 module_list |
306 while read mode sha1 stage path
308 if test -e "$path"/.git
309 then
310 say "$(eval_gettext "Entering '\$prefix\$path'")"
311 name=$(module_name "$path")
313 prefix="$prefix$path/"
314 clear_local_git_env
315 cd "$path" &&
316 eval "$@" &&
317 if test -n "$recursive"
318 then
319 cmd_foreach "--recursive" "$@"
321 ) <&3 3<&- ||
322 die "$(eval_gettext "Stopping at '\$path'; script returned non-zero status.")"
324 done
328 # Register submodules in .git/config
330 # $@ = requested paths (default to all)
332 cmd_init()
334 # parse $args after "submodule ... init".
335 while test $# -ne 0
337 case "$1" in
338 -q|--quiet)
339 GIT_QUIET=1
342 shift
343 break
346 usage
349 break
351 esac
352 shift
353 done
355 module_list "$@" |
356 while read mode sha1 stage path
358 # Skip already registered paths
359 name=$(module_name "$path") || exit
360 if test -z "$(git config "submodule.$name.url")"
361 then
362 url=$(git config -f .gitmodules submodule."$name".url)
363 test -z "$url" &&
364 die "$(eval_gettext "No url found for submodule path '\$path' in .gitmodules")"
366 # Possibly a url relative to parent
367 case "$url" in
368 ./*|../*)
369 url=$(resolve_relative_url "$url") || exit
371 esac
372 git config submodule."$name".url "$url" ||
373 die "$(eval_gettext "Failed to register url for submodule path '\$path'")"
376 # Copy "update" setting when it is not set yet
377 upd="$(git config -f .gitmodules submodule."$name".update)"
378 test -z "$upd" ||
379 test -n "$(git config submodule."$name".update)" ||
380 git config submodule."$name".update "$upd" ||
381 die "$(eval_gettext "Failed to register update mode for submodule path '\$path'")"
383 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$path'")"
384 done
388 # Update each submodule path to correct revision, using clone and checkout as needed
390 # $@ = requested paths (default to all)
392 cmd_update()
394 # parse $args after "submodule ... update".
395 orig_flags=
396 while test $# -ne 0
398 case "$1" in
399 -q|--quiet)
400 GIT_QUIET=1
402 -i|--init)
403 init=1
405 -N|--no-fetch)
406 nofetch=1
408 -f|--force)
409 force=$1
411 -r|--rebase)
412 update="rebase"
414 --reference)
415 case "$2" in '') usage ;; esac
416 reference="--reference=$2"
417 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
418 shift
420 --reference=*)
421 reference="$1"
423 -m|--merge)
424 update="merge"
426 --recursive)
427 recursive=1
430 shift
431 break
434 usage
437 break
439 esac
440 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
441 shift
442 done
444 if test -n "$init"
445 then
446 cmd_init "--" "$@" || return
449 cloned_modules=
450 module_list "$@" | {
451 err=
452 while read mode sha1 stage path
454 if test "$stage" = U
455 then
456 echo >&2 "Skipping unmerged submodule $path"
457 continue
459 name=$(module_name "$path") || exit
460 url=$(git config submodule."$name".url)
461 update_module=$(git config submodule."$name".update)
462 if test -z "$url"
463 then
464 # Only mention uninitialized submodules when its
465 # path have been specified
466 test "$#" != "0" &&
467 say "$(eval_gettext "Submodule path '\$path' not initialized
468 Maybe you want to use 'update --init'?")"
469 continue
472 if ! test -d "$path"/.git -o -f "$path"/.git
473 then
474 module_clone "$path" "$url" "$reference"|| exit
475 cloned_modules="$cloned_modules;$name"
476 subsha1=
477 else
478 subsha1=$(clear_local_git_env; cd "$path" &&
479 git rev-parse --verify HEAD) ||
480 die "$(eval_gettext "Unable to find current revision in submodule path '\$path'")"
483 if ! test -z "$update"
484 then
485 update_module=$update
488 if test "$subsha1" != "$sha1"
489 then
490 subforce=$force
491 # If we don't already have a -f flag and the submodule has never been checked out
492 if test -z "$subsha1" -a -z "$force"
493 then
494 subforce="-f"
497 if test -z "$nofetch"
498 then
499 # Run fetch only if $sha1 isn't present or it
500 # is not reachable from a ref.
501 (clear_local_git_env; cd "$path" &&
502 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
503 test -z "$rev") || git-fetch)) ||
504 die "$(eval_gettext "Unable to fetch in submodule path '\$path'")"
507 # Is this something we just cloned?
508 case ";$cloned_modules;" in
509 *";$name;"*)
510 # then there is no local change to integrate
511 update_module= ;;
512 esac
514 must_die_on_failure=
515 case "$update_module" in
516 rebase)
517 command="git rebase"
518 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$path'")"
519 say_msg="$(eval_gettext "Submodule path '\$path': rebased into '\$sha1'")"
520 must_die_on_failure=yes
522 merge)
523 command="git merge"
524 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$path'")"
525 say_msg="$(eval_gettext "Submodule path '\$path': merged in '\$sha1'")"
526 must_die_on_failure=yes
529 command="git checkout $subforce -q"
530 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$path'")"
531 say_msg="$(eval_gettext "Submodule path '\$path': checked out '\$sha1'")"
533 esac
535 if (clear_local_git_env; cd "$path" && $command "$sha1")
536 then
537 say "$say_msg"
538 elif test -n "$must_die_on_failure"
539 then
540 die_with_status 2 "$die_msg"
541 else
542 err="${err};$die_msg"
543 continue
547 if test -n "$recursive"
548 then
549 (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags")
550 res=$?
551 if test $res -gt 0
552 then
553 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$path'")"
554 if test $res -eq 1
555 then
556 err="${err};$die_msg"
557 continue
558 else
559 die_with_status $res "$die_msg"
563 done
565 if test -n "$err"
566 then
567 OIFS=$IFS
568 IFS=';'
569 for e in $err
571 if test -n "$e"
572 then
573 echo >&2 "$e"
575 done
576 IFS=$OIFS
577 exit 1
582 set_name_rev () {
583 revname=$( (
584 clear_local_git_env
585 cd "$1" && {
586 git describe "$2" 2>/dev/null ||
587 git describe --tags "$2" 2>/dev/null ||
588 git describe --contains "$2" 2>/dev/null ||
589 git describe --all --always "$2"
592 test -z "$revname" || revname=" ($revname)"
595 # Show commit summary for submodules in index or working tree
597 # If '--cached' is given, show summary between index and given commit,
598 # or between working tree and given commit
600 # $@ = [commit (default 'HEAD'),] requested paths (default all)
602 cmd_summary() {
603 summary_limit=-1
604 for_status=
605 diff_cmd=diff-index
607 # parse $args after "submodule ... summary".
608 while test $# -ne 0
610 case "$1" in
611 --cached)
612 cached="$1"
614 --files)
615 files="$1"
617 --for-status)
618 for_status="$1"
620 -n|--summary-limit)
621 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
622 then
624 else
625 usage
627 shift
630 shift
631 break
634 usage
637 break
639 esac
640 shift
641 done
643 test $summary_limit = 0 && return
645 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
646 then
647 head=$rev
648 test $# = 0 || shift
649 elif test -z "$1" -o "$1" = "HEAD"
650 then
651 # before the first commit: compare with an empty tree
652 head=$(git hash-object -w -t tree --stdin </dev/null)
653 test -z "$1" || shift
654 else
655 head="HEAD"
658 if [ -n "$files" ]
659 then
660 test -n "$cached" &&
661 die "$(gettext -- "--cached cannot be used with --files")"
662 diff_cmd=diff-files
663 head=
666 cd_to_toplevel
667 # Get modified modules cared by user
668 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
669 sane_egrep '^:([0-7]* )?160000' |
670 while read mod_src mod_dst sha1_src sha1_dst status name
672 # Always show modules deleted or type-changed (blob<->module)
673 test $status = D -o $status = T && echo "$name" && continue
674 # Also show added or modified modules which are checked out
675 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
676 echo "$name"
677 done
680 test -z "$modules" && return
682 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
683 sane_egrep '^:([0-7]* )?160000' |
684 cut -c2- |
685 while read mod_src mod_dst sha1_src sha1_dst status name
687 if test -z "$cached" &&
688 test $sha1_dst = 0000000000000000000000000000000000000000
689 then
690 case "$mod_dst" in
691 160000)
692 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
694 100644 | 100755 | 120000)
695 sha1_dst=$(git hash-object $name)
697 000000)
698 ;; # removed
700 # unexpected type
701 eval_gettextln "unexpected mode \$mod_dst" >&2
702 continue ;;
703 esac
705 missing_src=
706 missing_dst=
708 test $mod_src = 160000 &&
709 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
710 missing_src=t
712 test $mod_dst = 160000 &&
713 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
714 missing_dst=t
716 total_commits=
717 case "$missing_src,$missing_dst" in
719 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
722 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
724 t,t)
725 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
728 errmsg=
729 total_commits=$(
730 if test $mod_src = 160000 -a $mod_dst = 160000
731 then
732 range="$sha1_src...$sha1_dst"
733 elif test $mod_src = 160000
734 then
735 range=$sha1_src
736 else
737 range=$sha1_dst
739 GIT_DIR="$name/.git" \
740 git rev-list --first-parent $range -- | wc -l
742 total_commits=" ($(($total_commits + 0)))"
744 esac
746 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
747 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
748 if test $status = T
749 then
750 blob="$(gettext "blob")"
751 submodule="$(gettext "submodule")"
752 if test $mod_dst = 160000
753 then
754 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
755 else
756 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
758 else
759 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
761 if test -n "$errmsg"
762 then
763 # Don't give error msg for modification whose dst is not submodule
764 # i.e. deleted or changed to blob
765 test $mod_dst = 160000 && echo "$errmsg"
766 else
767 if test $mod_src = 160000 -a $mod_dst = 160000
768 then
769 limit=
770 test $summary_limit -gt 0 && limit="-$summary_limit"
771 GIT_DIR="$name/.git" \
772 git log $limit --pretty='format: %m %s' \
773 --first-parent $sha1_src...$sha1_dst
774 elif test $mod_dst = 160000
775 then
776 GIT_DIR="$name/.git" \
777 git log --pretty='format: > %s' -1 $sha1_dst
778 else
779 GIT_DIR="$name/.git" \
780 git log --pretty='format: < %s' -1 $sha1_src
782 echo
784 echo
785 done |
786 if test -n "$for_status"; then
787 if [ -n "$files" ]; then
788 gettextln "# Submodules changed but not updated:"
789 else
790 gettextln "# Submodule changes to be committed:"
792 echo "#"
793 sed -e 's|^|# |' -e 's|^# $|#|'
794 else
799 # List all submodules, prefixed with:
800 # - submodule not initialized
801 # + different revision checked out
803 # If --cached was specified the revision in the index will be printed
804 # instead of the currently checked out revision.
806 # $@ = requested paths (default to all)
808 cmd_status()
810 # parse $args after "submodule ... status".
811 orig_flags=
812 while test $# -ne 0
814 case "$1" in
815 -q|--quiet)
816 GIT_QUIET=1
818 --cached)
819 cached=1
821 --recursive)
822 recursive=1
825 shift
826 break
829 usage
832 break
834 esac
835 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
836 shift
837 done
839 module_list "$@" |
840 while read mode sha1 stage path
842 name=$(module_name "$path") || exit
843 url=$(git config submodule."$name".url)
844 displaypath="$prefix$path"
845 if test "$stage" = U
846 then
847 say "U$sha1 $displaypath"
848 continue
850 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
851 then
852 say "-$sha1 $displaypath"
853 continue;
855 set_name_rev "$path" "$sha1"
856 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
857 then
858 say " $sha1 $displaypath$revname"
859 else
860 if test -z "$cached"
861 then
862 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
863 set_name_rev "$path" "$sha1"
865 say "+$sha1 $displaypath$revname"
868 if test -n "$recursive"
869 then
871 prefix="$displaypath/"
872 clear_local_git_env
873 cd "$path" &&
874 eval cmd_status "$orig_args"
875 ) ||
876 die "$(eval_gettext "Failed to recurse into submodule path '\$path'")"
878 done
881 # Sync remote urls for submodules
882 # This makes the value for remote.$remote.url match the value
883 # specified in .gitmodules.
885 cmd_sync()
887 while test $# -ne 0
889 case "$1" in
890 -q|--quiet)
891 GIT_QUIET=1
892 shift
895 shift
896 break
899 usage
902 break
904 esac
905 done
906 cd_to_toplevel
907 module_list "$@" |
908 while read mode sha1 stage path
910 name=$(module_name "$path")
911 url=$(git config -f .gitmodules --get submodule."$name".url)
913 # Possibly a url relative to parent
914 case "$url" in
915 ./*|../*)
916 url=$(resolve_relative_url "$url") || exit
918 esac
920 if git config "submodule.$name.url" >/dev/null 2>/dev/null
921 then
922 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
923 git config submodule."$name".url "$url"
925 if test -e "$path"/.git
926 then
928 clear_local_git_env
929 cd "$path"
930 remote=$(get_default_remote)
931 git config remote."$remote".url "$url"
935 done
938 # This loop parses the command line arguments to find the
939 # subcommand name to dispatch. Parsing of the subcommand specific
940 # options are primarily done by the subcommand implementations.
941 # Subcommand specific options such as --branch and --cached are
942 # parsed here as well, for backward compatibility.
944 while test $# != 0 && test -z "$command"
946 case "$1" in
947 add | foreach | init | update | status | summary | sync)
948 command=$1
950 -q|--quiet)
951 GIT_QUIET=1
953 -b|--branch)
954 case "$2" in
956 usage
958 esac
959 branch="$2"; shift
961 --cached)
962 cached="$1"
965 break
968 usage
971 break
973 esac
974 shift
975 done
977 # No command word defaults to "status"
978 test -n "$command" || command=status
980 # "-b branch" is accepted only by "add"
981 if test -n "$branch" && test "$command" != add
982 then
983 usage
986 # "--cached" is accepted only by "status" and "summary"
987 if test -n "$cached" && test "$command" != status -a "$command" != summary
988 then
989 usage
992 "cmd_$command" "$@"