submodule: Fix t7400, t7405, t7406 for msysGit
[git/dscho.git] / git-submodule.sh
blob4d21cbb35e164adb7ee18a79d003cb475ad792da
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 cat >&2 <<EOF
232 The following path is ignored by one of your .gitignore files:
233 $(gettext $path)
234 Use -f if you really want to add it.
236 exit 1
239 # perhaps the path exists and is already a git repo, else clone it
240 if test -e "$path"
241 then
242 if test -d "$path"/.git -o -f "$path"/.git
243 then
244 eval_gettext "Adding existing repo at '\$path' to the index"; echo
245 else
246 die "$(eval_gettext "'\$path' already exists and is not a valid git repo")"
249 else
251 module_clone "$path" "$realrepo" "$reference" || exit
253 clear_local_git_env
254 cd "$path" &&
255 # ash fails to wordsplit ${branch:+-b "$branch"...}
256 case "$branch" in
257 '') git checkout -f -q ;;
258 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
259 esac
260 ) || die "$(eval_gettext "Unable to checkout submodule '\$path'")"
262 git config submodule."$path".url "$realrepo"
264 git add $force "$path" ||
265 die "$(eval_gettext "Failed to add submodule '\$path'")"
267 git config -f .gitmodules submodule."$path".path "$path" &&
268 git config -f .gitmodules submodule."$path".url "$repo" &&
269 git add --force .gitmodules ||
270 die "$(eval_gettext "Failed to register submodule '\$path'")"
274 # Execute an arbitrary command sequence in each checked out
275 # submodule
277 # $@ = command to execute
279 cmd_foreach()
281 # parse $args after "submodule ... foreach".
282 while test $# -ne 0
284 case "$1" in
285 -q|--quiet)
286 GIT_QUIET=1
288 --recursive)
289 recursive=1
292 usage
295 break
297 esac
298 shift
299 done
301 toplevel=$(pwd)
303 # dup stdin so that it can be restored when running the external
304 # command in the subshell (and a recursive call to this function)
305 exec 3<&0
307 module_list |
308 while read mode sha1 stage path
310 if test -e "$path"/.git
311 then
312 say "$(eval_gettext "Entering '\$prefix\$path'")"
313 name=$(module_name "$path")
315 prefix="$prefix$path/"
316 clear_local_git_env
317 cd "$path" &&
318 eval "$@" &&
319 if test -n "$recursive"
320 then
321 cmd_foreach "--recursive" "$@"
323 ) <&3 3<&- ||
324 die "$(eval_gettext "Stopping at '\$path'; script returned non-zero status.")"
326 done
330 # Register submodules in .git/config
332 # $@ = requested paths (default to all)
334 cmd_init()
336 # parse $args after "submodule ... init".
337 while test $# -ne 0
339 case "$1" in
340 -q|--quiet)
341 GIT_QUIET=1
344 shift
345 break
348 usage
351 break
353 esac
354 shift
355 done
357 module_list "$@" |
358 while read mode sha1 stage path
360 # Skip already registered paths
361 name=$(module_name "$path") || exit
362 if test -z "$(git config "submodule.$name.url")"
363 then
364 url=$(git config -f .gitmodules submodule."$name".url)
365 test -z "$url" &&
366 die "$(eval_gettext "No url found for submodule path '\$path' in .gitmodules")"
368 # Possibly a url relative to parent
369 case "$url" in
370 ./*|../*)
371 url=$(resolve_relative_url "$url") || exit
373 esac
374 git config submodule."$name".url "$url" ||
375 die "$(eval_gettext "Failed to register url for submodule path '\$path'")"
378 # Copy "update" setting when it is not set yet
379 upd="$(git config -f .gitmodules submodule."$name".update)"
380 test -z "$upd" ||
381 test -n "$(git config submodule."$name".update)" ||
382 git config submodule."$name".update "$upd" ||
383 die "$(eval_gettext "Failed to register update mode for submodule path '\$path'")"
385 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$path'")"
386 done
390 # Update each submodule path to correct revision, using clone and checkout as needed
392 # $@ = requested paths (default to all)
394 cmd_update()
396 # parse $args after "submodule ... update".
397 orig_flags=
398 while test $# -ne 0
400 case "$1" in
401 -q|--quiet)
402 GIT_QUIET=1
404 -i|--init)
405 init=1
407 -N|--no-fetch)
408 nofetch=1
410 -f|--force)
411 force=$1
413 -r|--rebase)
414 update="rebase"
416 --reference)
417 case "$2" in '') usage ;; esac
418 reference="--reference=$2"
419 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
420 shift
422 --reference=*)
423 reference="$1"
425 -m|--merge)
426 update="merge"
428 --recursive)
429 recursive=1
432 shift
433 break
436 usage
439 break
441 esac
442 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
443 shift
444 done
446 if test -n "$init"
447 then
448 cmd_init "--" "$@" || return
451 cloned_modules=
452 module_list "$@" | {
453 err=
454 while read mode sha1 stage path
456 if test "$stage" = U
457 then
458 echo >&2 "Skipping unmerged submodule $path"
459 continue
461 name=$(module_name "$path") || exit
462 url=$(git config submodule."$name".url)
463 update_module=$(git config submodule."$name".update)
464 if test -z "$url"
465 then
466 # Only mention uninitialized submodules when its
467 # path have been specified
468 test "$#" != "0" &&
469 say "$(eval_gettext "Submodule path '\$path' not initialized
470 Maybe you want to use 'update --init'?")"
471 continue
474 if ! test -d "$path"/.git -o -f "$path"/.git
475 then
476 module_clone "$path" "$url" "$reference"|| exit
477 cloned_modules="$cloned_modules;$name"
478 subsha1=
479 else
480 subsha1=$(clear_local_git_env; cd "$path" &&
481 git rev-parse --verify HEAD) ||
482 die "$(eval_gettext "Unable to find current revision in submodule path '\$path'")"
485 if ! test -z "$update"
486 then
487 update_module=$update
490 if test "$subsha1" != "$sha1"
491 then
492 subforce=$force
493 # If we don't already have a -f flag and the submodule has never been checked out
494 if test -z "$subsha1" -a -z "$force"
495 then
496 subforce="-f"
499 if test -z "$nofetch"
500 then
501 # Run fetch only if $sha1 isn't present or it
502 # is not reachable from a ref.
503 (clear_local_git_env; cd "$path" &&
504 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
505 test -z "$rev") || git-fetch)) ||
506 die "$(eval_gettext "Unable to fetch in submodule path '\$path'")"
509 # Is this something we just cloned?
510 case ";$cloned_modules;" in
511 *";$name;"*)
512 # then there is no local change to integrate
513 update_module= ;;
514 esac
516 must_die_on_failure=
517 case "$update_module" in
518 rebase)
519 command="git rebase"
520 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$path'")"
521 say_msg="$(eval_gettext "Submodule path '\$path': rebased into '\$sha1'")"
522 must_die_on_failure=yes
524 merge)
525 command="git merge"
526 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$path'")"
527 say_msg="$(eval_gettext "Submodule path '\$path': merged in '\$sha1'")"
528 must_die_on_failure=yes
531 command="git checkout $subforce -q"
532 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$path'")"
533 say_msg="$(eval_gettext "Submodule path '\$path': checked out '\$sha1'")"
535 esac
537 if (clear_local_git_env; cd "$path" && $command "$sha1")
538 then
539 say "$say_msg"
540 elif test -n "$must_die_on_failure"
541 then
542 die_with_status 2 "$die_msg"
543 else
544 err="${err};$die_msg"
545 continue
549 if test -n "$recursive"
550 then
551 (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags")
552 res=$?
553 if test $res -gt 0
554 then
555 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$path'")"
556 if test $res -eq 1
557 then
558 err="${err};$die_msg"
559 continue
560 else
561 die_with_status $res "$die_msg"
565 done
567 if test -n "$err"
568 then
569 OIFS=$IFS
570 IFS=';'
571 for e in $err
573 if test -n "$e"
574 then
575 echo >&2 "$e"
577 done
578 IFS=$OIFS
579 exit 1
584 set_name_rev () {
585 revname=$( (
586 clear_local_git_env
587 cd "$1" && {
588 git describe "$2" 2>/dev/null ||
589 git describe --tags "$2" 2>/dev/null ||
590 git describe --contains "$2" 2>/dev/null ||
591 git describe --all --always "$2"
594 test -z "$revname" || revname=" ($revname)"
597 # Show commit summary for submodules in index or working tree
599 # If '--cached' is given, show summary between index and given commit,
600 # or between working tree and given commit
602 # $@ = [commit (default 'HEAD'),] requested paths (default all)
604 cmd_summary() {
605 summary_limit=-1
606 for_status=
607 diff_cmd=diff-index
609 # parse $args after "submodule ... summary".
610 while test $# -ne 0
612 case "$1" in
613 --cached)
614 cached="$1"
616 --files)
617 files="$1"
619 --for-status)
620 for_status="$1"
622 -n|--summary-limit)
623 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
624 then
626 else
627 usage
629 shift
632 shift
633 break
636 usage
639 break
641 esac
642 shift
643 done
645 test $summary_limit = 0 && return
647 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
648 then
649 head=$rev
650 test $# = 0 || shift
651 elif test -z "$1" -o "$1" = "HEAD"
652 then
653 # before the first commit: compare with an empty tree
654 head=$(git hash-object -w -t tree --stdin </dev/null)
655 test -z "$1" || shift
656 else
657 head="HEAD"
660 if [ -n "$files" ]
661 then
662 test -n "$cached" &&
663 die "$(gettext -- "--cached cannot be used with --files")"
664 diff_cmd=diff-files
665 head=
668 cd_to_toplevel
669 # Get modified modules cared by user
670 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
671 sane_egrep '^:([0-7]* )?160000' |
672 while read mod_src mod_dst sha1_src sha1_dst status name
674 # Always show modules deleted or type-changed (blob<->module)
675 test $status = D -o $status = T && echo "$name" && continue
676 # Also show added or modified modules which are checked out
677 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
678 echo "$name"
679 done
682 test -z "$modules" && return
684 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
685 sane_egrep '^:([0-7]* )?160000' |
686 cut -c2- |
687 while read mod_src mod_dst sha1_src sha1_dst status name
689 if test -z "$cached" &&
690 test $sha1_dst = 0000000000000000000000000000000000000000
691 then
692 case "$mod_dst" in
693 160000)
694 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
696 100644 | 100755 | 120000)
697 sha1_dst=$(git hash-object $name)
699 000000)
700 ;; # removed
702 # unexpected type
704 eval_gettext "unexpected mode \$mod_dst" &&
705 echo
706 ) >&2
707 continue ;;
708 esac
710 missing_src=
711 missing_dst=
713 test $mod_src = 160000 &&
714 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
715 missing_src=t
717 test $mod_dst = 160000 &&
718 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
719 missing_dst=t
721 total_commits=
722 case "$missing_src,$missing_dst" in
724 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
727 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
729 t,t)
730 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
733 errmsg=
734 total_commits=$(
735 if test $mod_src = 160000 -a $mod_dst = 160000
736 then
737 range="$sha1_src...$sha1_dst"
738 elif test $mod_src = 160000
739 then
740 range=$sha1_src
741 else
742 range=$sha1_dst
744 GIT_DIR="$name/.git" \
745 git rev-list --first-parent $range -- | wc -l
747 total_commits=" ($(($total_commits + 0)))"
749 esac
751 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
752 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
753 if test $status = T
754 then
755 blob="$(gettext "blob")"
756 submodule="$(gettext "submodule")"
757 if test $mod_dst = 160000
758 then
759 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
760 else
761 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
763 else
764 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
766 if test -n "$errmsg"
767 then
768 # Don't give error msg for modification whose dst is not submodule
769 # i.e. deleted or changed to blob
770 test $mod_dst = 160000 && echo "$errmsg"
771 else
772 if test $mod_src = 160000 -a $mod_dst = 160000
773 then
774 limit=
775 test $summary_limit -gt 0 && limit="-$summary_limit"
776 GIT_DIR="$name/.git" \
777 git log $limit --pretty='format: %m %s' \
778 --first-parent $sha1_src...$sha1_dst
779 elif test $mod_dst = 160000
780 then
781 GIT_DIR="$name/.git" \
782 git log --pretty='format: > %s' -1 $sha1_dst
783 else
784 GIT_DIR="$name/.git" \
785 git log --pretty='format: < %s' -1 $sha1_src
787 echo
789 echo
790 done |
791 if test -n "$for_status"; then
792 if [ -n "$files" ]; then
793 status_msg="$(gettext "# Submodules changed but not updated:")"
794 else
795 status_msg="$(gettext "# Submodule changes to be committed:")"
797 status_sed=$(sed -e 's|^|# |' -e 's|^# $|#|')
798 cat <<EOF
799 $status_msg
801 $status_sed
803 else
808 # List all submodules, prefixed with:
809 # - submodule not initialized
810 # + different revision checked out
812 # If --cached was specified the revision in the index will be printed
813 # instead of the currently checked out revision.
815 # $@ = requested paths (default to all)
817 cmd_status()
819 # parse $args after "submodule ... status".
820 orig_flags=
821 while test $# -ne 0
823 case "$1" in
824 -q|--quiet)
825 GIT_QUIET=1
827 --cached)
828 cached=1
830 --recursive)
831 recursive=1
834 shift
835 break
838 usage
841 break
843 esac
844 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
845 shift
846 done
848 module_list "$@" |
849 while read mode sha1 stage path
851 name=$(module_name "$path") || exit
852 url=$(git config submodule."$name".url)
853 displaypath="$prefix$path"
854 if test "$stage" = U
855 then
856 say "U$sha1 $displaypath"
857 continue
859 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
860 then
861 say "-$sha1 $displaypath"
862 continue;
864 set_name_rev "$path" "$sha1"
865 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
866 then
867 say " $sha1 $displaypath$revname"
868 else
869 if test -z "$cached"
870 then
871 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
872 set_name_rev "$path" "$sha1"
874 say "+$sha1 $displaypath$revname"
877 if test -n "$recursive"
878 then
880 prefix="$displaypath/"
881 clear_local_git_env
882 cd "$path" &&
883 eval cmd_status "$orig_args"
884 ) ||
885 die "$(eval_gettext "Failed to recurse into submodule path '\$path'")"
887 done
890 # Sync remote urls for submodules
891 # This makes the value for remote.$remote.url match the value
892 # specified in .gitmodules.
894 cmd_sync()
896 while test $# -ne 0
898 case "$1" in
899 -q|--quiet)
900 GIT_QUIET=1
901 shift
904 shift
905 break
908 usage
911 break
913 esac
914 done
915 cd_to_toplevel
916 module_list "$@" |
917 while read mode sha1 stage path
919 name=$(module_name "$path")
920 url=$(git config -f .gitmodules --get submodule."$name".url)
922 # Possibly a url relative to parent
923 case "$url" in
924 ./*|../*)
925 url=$(resolve_relative_url "$url") || exit
927 esac
929 if git config "submodule.$name.url" >/dev/null 2>/dev/null
930 then
931 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
932 git config submodule."$name".url "$url"
934 if test -e "$path"/.git
935 then
937 clear_local_git_env
938 cd "$path"
939 remote=$(get_default_remote)
940 git config remote."$remote".url "$url"
944 done
947 # This loop parses the command line arguments to find the
948 # subcommand name to dispatch. Parsing of the subcommand specific
949 # options are primarily done by the subcommand implementations.
950 # Subcommand specific options such as --branch and --cached are
951 # parsed here as well, for backward compatibility.
953 while test $# != 0 && test -z "$command"
955 case "$1" in
956 add | foreach | init | update | status | summary | sync)
957 command=$1
959 -q|--quiet)
960 GIT_QUIET=1
962 -b|--branch)
963 case "$2" in
965 usage
967 esac
968 branch="$2"; shift
970 --cached)
971 cached="$1"
974 break
977 usage
980 break
982 esac
983 shift
984 done
986 # No command word defaults to "status"
987 test -n "$command" || command=status
989 # "-b branch" is accepted only by "add"
990 if test -n "$branch" && test "$command" != add
991 then
992 usage
995 # "--cached" is accepted only by "status" and "summary"
996 if test -n "$cached" && test "$command" != status -a "$command" != summary
997 then
998 usage
1001 "cmd_$command" "$@"