imap-send: support subjectAltName as well
[git/jnareb-git.git] / git-submodule.sh
blobc94218b8779be63e1441480ef3828fb4ce0b61b0
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-parse-remote
18 require_work_tree
20 command=
21 branch=
22 force=
23 reference=
24 cached=
25 recursive=
26 init=
27 files=
28 nofetch=
29 update=
30 prefix=
32 # Resolve relative url by appending to parent's url
33 resolve_relative_url ()
35 remote=$(get_default_remote)
36 remoteurl=$(git config "remote.$remote.url") ||
37 remoteurl=$(pwd) # the repository is its own authoritative upstream
38 url="$1"
39 remoteurl=${remoteurl%/}
40 sep=/
41 while test -n "$url"
43 case "$url" in
44 ../*)
45 url="${url#../}"
46 case "$remoteurl" in
47 */*)
48 remoteurl="${remoteurl%/*}"
50 *:*)
51 remoteurl="${remoteurl%:*}"
52 sep=:
55 die "cannot strip one component off url '$remoteurl'"
57 esac
59 ./*)
60 url="${url#./}"
63 break;;
64 esac
65 done
66 echo "$remoteurl$sep${url%/}"
70 # Get submodule info for registered submodules
71 # $@ = path to limit submodule list
73 module_list()
75 git ls-files --error-unmatch --stage -- "$@" |
76 perl -e '
77 my %unmerged = ();
78 my ($null_sha1) = ("0" x 40);
79 while (<STDIN>) {
80 chomp;
81 my ($mode, $sha1, $stage, $path) =
82 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
83 next unless $mode eq "160000";
84 if ($stage ne "0") {
85 if (!$unmerged{$path}++) {
86 print "$mode $null_sha1 U\t$path\n";
88 next;
90 print "$_\n";
96 # Map submodule path to submodule name
98 # $1 = path
100 module_name()
102 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
103 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
104 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
105 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
106 test -z "$name" &&
107 die "No submodule mapping found in .gitmodules for path '$path'"
108 echo "$name"
112 # Clone a submodule
114 # Prior to calling, cmd_update checks that a possibly existing
115 # path is not a git repository.
116 # Likewise, cmd_add checks that path does not exist at all,
117 # since it is the location of a new submodule.
119 module_clone()
121 path=$1
122 url=$2
123 reference="$3"
124 quiet=
125 if test -n "$GIT_QUIET"
126 then
127 quiet=-q
130 if test -n "$reference"
131 then
132 git-clone $quiet "$reference" -n "$url" "$path"
133 else
134 git-clone $quiet -n "$url" "$path"
135 fi ||
136 die "Clone of '$url' into submodule path '$path' failed"
140 # Add a new submodule to the working tree, .gitmodules and the index
142 # $@ = repo path
144 # optional branch is stored in global branch variable
146 cmd_add()
148 # parse $args after "submodule ... add".
149 while test $# -ne 0
151 case "$1" in
152 -b | --branch)
153 case "$2" in '') usage ;; esac
154 branch=$2
155 shift
157 -f | --force)
158 force=$1
160 -q|--quiet)
161 GIT_QUIET=1
163 --reference)
164 case "$2" in '') usage ;; esac
165 reference="--reference=$2"
166 shift
168 --reference=*)
169 reference="$1"
170 shift
173 shift
174 break
177 usage
180 break
182 esac
183 shift
184 done
186 repo=$1
187 path=$2
189 if test -z "$path"; then
190 path=$(echo "$repo" |
191 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
194 if test -z "$repo" -o -z "$path"; then
195 usage
198 # assure repo is absolute or relative to parent
199 case "$repo" in
200 ./*|../*)
201 # dereference source url relative to parent's url
202 realrepo=$(resolve_relative_url "$repo") || exit
204 *:*|/*)
205 # absolute url
206 realrepo=$repo
209 die "repo URL: '$repo' must be absolute or begin with ./|../"
211 esac
213 # normalize path:
214 # multiple //; leading ./; /./; /../; trailing /
215 path=$(printf '%s/\n' "$path" |
216 sed -e '
217 s|//*|/|g
218 s|^\(\./\)*||
219 s|/\./|/|g
220 :start
221 s|\([^/]*\)/\.\./||
222 tstart
223 s|/*$||
225 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
226 die "'$path' already exists in the index"
228 if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
229 then
230 echo >&2 "The following path is ignored by one of your .gitignore files:" &&
231 echo >&2 $path &&
232 echo >&2 "Use -f if you really want to add it."
233 exit 1
236 # perhaps the path exists and is already a git repo, else clone it
237 if test -e "$path"
238 then
239 if test -d "$path"/.git -o -f "$path"/.git
240 then
241 echo "Adding existing repo at '$path' to the index"
242 else
243 die "'$path' already exists and is not a valid git repo"
246 else
248 module_clone "$path" "$realrepo" "$reference" || exit
250 clear_local_git_env
251 cd "$path" &&
252 # ash fails to wordsplit ${branch:+-b "$branch"...}
253 case "$branch" in
254 '') git checkout -f -q ;;
255 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
256 esac
257 ) || die "Unable to checkout submodule '$path'"
259 git config submodule."$path".url "$realrepo"
261 git add $force "$path" ||
262 die "Failed to add submodule '$path'"
264 git config -f .gitmodules submodule."$path".path "$path" &&
265 git config -f .gitmodules submodule."$path".url "$repo" &&
266 git add --force .gitmodules ||
267 die "Failed to register submodule '$path'"
271 # Execute an arbitrary command sequence in each checked out
272 # submodule
274 # $@ = command to execute
276 cmd_foreach()
278 # parse $args after "submodule ... foreach".
279 while test $# -ne 0
281 case "$1" in
282 -q|--quiet)
283 GIT_QUIET=1
285 --recursive)
286 recursive=1
289 usage
292 break
294 esac
295 shift
296 done
298 toplevel=$(pwd)
300 # dup stdin so that it can be restored when running the external
301 # command in the subshell (and a recursive call to this function)
302 exec 3<&0
304 module_list |
305 while read mode sha1 stage path
307 if test -e "$path"/.git
308 then
309 say "Entering '$prefix$path'"
310 name=$(module_name "$path")
312 prefix="$prefix$path/"
313 clear_local_git_env
314 cd "$path" &&
315 eval "$@" &&
316 if test -n "$recursive"
317 then
318 cmd_foreach "--recursive" "$@"
320 ) <&3 3<&- ||
321 die "Stopping at '$path'; script returned non-zero status."
323 done
327 # Register submodules in .git/config
329 # $@ = requested paths (default to all)
331 cmd_init()
333 # parse $args after "submodule ... init".
334 while test $# -ne 0
336 case "$1" in
337 -q|--quiet)
338 GIT_QUIET=1
341 shift
342 break
345 usage
348 break
350 esac
351 shift
352 done
354 module_list "$@" |
355 while read mode sha1 stage path
357 # Skip already registered paths
358 name=$(module_name "$path") || exit
359 if test -z "$(git config "submodule.$name.url")"
360 then
361 url=$(git config -f .gitmodules submodule."$name".url)
362 test -z "$url" &&
363 die "No url found for submodule path '$path' in .gitmodules"
365 # Possibly a url relative to parent
366 case "$url" in
367 ./*|../*)
368 url=$(resolve_relative_url "$url") || exit
370 esac
371 git config submodule."$name".url "$url" ||
372 die "Failed to register url for submodule path '$path'"
375 # Copy "update" setting when it is not set yet
376 upd="$(git config -f .gitmodules submodule."$name".update)"
377 test -z "$upd" ||
378 test -n "$(git config submodule."$name".update)" ||
379 git config submodule."$name".update "$upd" ||
380 die "Failed to register update mode for submodule path '$path'"
382 say "Submodule '$name' ($url) registered for path '$path'"
383 done
387 # Update each submodule path to correct revision, using clone and checkout as needed
389 # $@ = requested paths (default to all)
391 cmd_update()
393 # parse $args after "submodule ... update".
394 orig_flags=
395 while test $# -ne 0
397 case "$1" in
398 -q|--quiet)
399 GIT_QUIET=1
401 -i|--init)
402 init=1
404 -N|--no-fetch)
405 nofetch=1
407 -f|--force)
408 force=$1
410 -r|--rebase)
411 update="rebase"
413 --reference)
414 case "$2" in '') usage ;; esac
415 reference="--reference=$2"
416 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
417 shift
419 --reference=*)
420 reference="$1"
422 -m|--merge)
423 update="merge"
425 --recursive)
426 recursive=1
429 shift
430 break
433 usage
436 break
438 esac
439 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
440 shift
441 done
443 if test -n "$init"
444 then
445 cmd_init "--" "$@" || return
448 cloned_modules=
449 module_list "$@" |
450 while read mode sha1 stage path
452 if test "$stage" = U
453 then
454 echo >&2 "Skipping unmerged submodule $path"
455 continue
457 name=$(module_name "$path") || exit
458 url=$(git config submodule."$name".url)
459 update_module=$(git config submodule."$name".update)
460 if test -z "$url"
461 then
462 # Only mention uninitialized submodules when its
463 # path have been specified
464 test "$#" != "0" &&
465 say "Submodule path '$path' not initialized" &&
466 say "Maybe you want to use 'update --init'?"
467 continue
470 if ! test -d "$path"/.git -o -f "$path"/.git
471 then
472 module_clone "$path" "$url" "$reference"|| exit
473 cloned_modules="$cloned_modules;$name"
474 subsha1=
475 else
476 subsha1=$(clear_local_git_env; cd "$path" &&
477 git rev-parse --verify HEAD) ||
478 die "Unable to find current revision in submodule path '$path'"
481 if ! test -z "$update"
482 then
483 update_module=$update
486 if test "$subsha1" != "$sha1"
487 then
488 subforce=$force
489 # If we don't already have a -f flag and the submodule has never been checked out
490 if test -z "$subsha1" -a -z "$force"
491 then
492 subforce="-f"
495 if test -z "$nofetch"
496 then
497 # Run fetch only if $sha1 isn't present or it
498 # is not reachable from a ref.
499 (clear_local_git_env; cd "$path" &&
500 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
501 test -z "$rev") || git-fetch)) ||
502 die "Unable to fetch in submodule path '$path'"
505 # Is this something we just cloned?
506 case ";$cloned_modules;" in
507 *";$name;"*)
508 # then there is no local change to integrate
509 update_module= ;;
510 esac
512 case "$update_module" in
513 rebase)
514 command="git rebase"
515 action="rebase"
516 msg="rebased onto"
518 merge)
519 command="git merge"
520 action="merge"
521 msg="merged in"
524 command="git checkout $subforce -q"
525 action="checkout"
526 msg="checked out"
528 esac
530 (clear_local_git_env; cd "$path" && $command "$sha1") ||
531 die "Unable to $action '$sha1' in submodule path '$path'"
532 say "Submodule path '$path': $msg '$sha1'"
535 if test -n "$recursive"
536 then
537 (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags") ||
538 die "Failed to recurse into submodule path '$path'"
540 done
543 set_name_rev () {
544 revname=$( (
545 clear_local_git_env
546 cd "$1" && {
547 git describe "$2" 2>/dev/null ||
548 git describe --tags "$2" 2>/dev/null ||
549 git describe --contains "$2" 2>/dev/null ||
550 git describe --all --always "$2"
553 test -z "$revname" || revname=" ($revname)"
556 # Show commit summary for submodules in index or working tree
558 # If '--cached' is given, show summary between index and given commit,
559 # or between working tree and given commit
561 # $@ = [commit (default 'HEAD'),] requested paths (default all)
563 cmd_summary() {
564 summary_limit=-1
565 for_status=
566 diff_cmd=diff-index
568 # parse $args after "submodule ... summary".
569 while test $# -ne 0
571 case "$1" in
572 --cached)
573 cached="$1"
575 --files)
576 files="$1"
578 --for-status)
579 for_status="$1"
581 -n|--summary-limit)
582 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
583 then
585 else
586 usage
588 shift
591 shift
592 break
595 usage
598 break
600 esac
601 shift
602 done
604 test $summary_limit = 0 && return
606 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
607 then
608 head=$rev
609 test $# = 0 || shift
610 elif test -z "$1" -o "$1" = "HEAD"
611 then
612 # before the first commit: compare with an empty tree
613 head=$(git hash-object -w -t tree --stdin </dev/null)
614 test -z "$1" || shift
615 else
616 head="HEAD"
619 if [ -n "$files" ]
620 then
621 test -n "$cached" &&
622 die "--cached cannot be used with --files"
623 diff_cmd=diff-files
624 head=
627 cd_to_toplevel
628 # Get modified modules cared by user
629 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
630 sane_egrep '^:([0-7]* )?160000' |
631 while read mod_src mod_dst sha1_src sha1_dst status name
633 # Always show modules deleted or type-changed (blob<->module)
634 test $status = D -o $status = T && echo "$name" && continue
635 # Also show added or modified modules which are checked out
636 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
637 echo "$name"
638 done
641 test -z "$modules" && return
643 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
644 sane_egrep '^:([0-7]* )?160000' |
645 cut -c2- |
646 while read mod_src mod_dst sha1_src sha1_dst status name
648 if test -z "$cached" &&
649 test $sha1_dst = 0000000000000000000000000000000000000000
650 then
651 case "$mod_dst" in
652 160000)
653 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
655 100644 | 100755 | 120000)
656 sha1_dst=$(git hash-object $name)
658 000000)
659 ;; # removed
661 # unexpected type
662 echo >&2 "unexpected mode $mod_dst"
663 continue ;;
664 esac
666 missing_src=
667 missing_dst=
669 test $mod_src = 160000 &&
670 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
671 missing_src=t
673 test $mod_dst = 160000 &&
674 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
675 missing_dst=t
677 total_commits=
678 case "$missing_src,$missing_dst" in
680 errmsg=" Warn: $name doesn't contain commit $sha1_src"
683 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
685 t,t)
686 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
689 errmsg=
690 total_commits=$(
691 if test $mod_src = 160000 -a $mod_dst = 160000
692 then
693 range="$sha1_src...$sha1_dst"
694 elif test $mod_src = 160000
695 then
696 range=$sha1_src
697 else
698 range=$sha1_dst
700 GIT_DIR="$name/.git" \
701 git rev-list --first-parent $range -- | wc -l
703 total_commits=" ($(($total_commits + 0)))"
705 esac
707 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
708 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
709 if test $status = T
710 then
711 if test $mod_dst = 160000
712 then
713 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
714 else
715 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
717 else
718 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
720 if test -n "$errmsg"
721 then
722 # Don't give error msg for modification whose dst is not submodule
723 # i.e. deleted or changed to blob
724 test $mod_dst = 160000 && echo "$errmsg"
725 else
726 if test $mod_src = 160000 -a $mod_dst = 160000
727 then
728 limit=
729 test $summary_limit -gt 0 && limit="-$summary_limit"
730 GIT_DIR="$name/.git" \
731 git log $limit --pretty='format: %m %s' \
732 --first-parent $sha1_src...$sha1_dst
733 elif test $mod_dst = 160000
734 then
735 GIT_DIR="$name/.git" \
736 git log --pretty='format: > %s' -1 $sha1_dst
737 else
738 GIT_DIR="$name/.git" \
739 git log --pretty='format: < %s' -1 $sha1_src
741 echo
743 echo
744 done |
745 if test -n "$for_status"; then
746 if [ -n "$files" ]; then
747 echo "# Submodules changed but not updated:"
748 else
749 echo "# Submodule changes to be committed:"
751 echo "#"
752 sed -e 's|^|# |' -e 's|^# $|#|'
753 else
758 # List all submodules, prefixed with:
759 # - submodule not initialized
760 # + different revision checked out
762 # If --cached was specified the revision in the index will be printed
763 # instead of the currently checked out revision.
765 # $@ = requested paths (default to all)
767 cmd_status()
769 # parse $args after "submodule ... status".
770 orig_flags=
771 while test $# -ne 0
773 case "$1" in
774 -q|--quiet)
775 GIT_QUIET=1
777 --cached)
778 cached=1
780 --recursive)
781 recursive=1
784 shift
785 break
788 usage
791 break
793 esac
794 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
795 shift
796 done
798 module_list "$@" |
799 while read mode sha1 stage path
801 name=$(module_name "$path") || exit
802 url=$(git config submodule."$name".url)
803 displaypath="$prefix$path"
804 if test "$stage" = U
805 then
806 say "U$sha1 $displaypath"
807 continue
809 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
810 then
811 say "-$sha1 $displaypath"
812 continue;
814 set_name_rev "$path" "$sha1"
815 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
816 then
817 say " $sha1 $displaypath$revname"
818 else
819 if test -z "$cached"
820 then
821 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
822 set_name_rev "$path" "$sha1"
824 say "+$sha1 $displaypath$revname"
827 if test -n "$recursive"
828 then
830 prefix="$displaypath/"
831 clear_local_git_env
832 cd "$path" &&
833 eval cmd_status "$orig_args"
834 ) ||
835 die "Failed to recurse into submodule path '$path'"
837 done
840 # Sync remote urls for submodules
841 # This makes the value for remote.$remote.url match the value
842 # specified in .gitmodules.
844 cmd_sync()
846 while test $# -ne 0
848 case "$1" in
849 -q|--quiet)
850 GIT_QUIET=1
851 shift
854 shift
855 break
858 usage
861 break
863 esac
864 done
865 cd_to_toplevel
866 module_list "$@" |
867 while read mode sha1 stage path
869 name=$(module_name "$path")
870 url=$(git config -f .gitmodules --get submodule."$name".url)
872 # Possibly a url relative to parent
873 case "$url" in
874 ./*|../*)
875 url=$(resolve_relative_url "$url") || exit
877 esac
879 if git config "submodule.$name.url" >/dev/null 2>/dev/null
880 then
881 say "Synchronizing submodule url for '$name'"
882 git config submodule."$name".url "$url"
884 if test -e "$path"/.git
885 then
887 clear_local_git_env
888 cd "$path"
889 remote=$(get_default_remote)
890 git config remote."$remote".url "$url"
894 done
897 # This loop parses the command line arguments to find the
898 # subcommand name to dispatch. Parsing of the subcommand specific
899 # options are primarily done by the subcommand implementations.
900 # Subcommand specific options such as --branch and --cached are
901 # parsed here as well, for backward compatibility.
903 while test $# != 0 && test -z "$command"
905 case "$1" in
906 add | foreach | init | update | status | summary | sync)
907 command=$1
909 -q|--quiet)
910 GIT_QUIET=1
912 -b|--branch)
913 case "$2" in
915 usage
917 esac
918 branch="$2"; shift
920 --cached)
921 cached="$1"
924 break
927 usage
930 break
932 esac
933 shift
934 done
936 # No command word defaults to "status"
937 test -n "$command" || command=status
939 # "-b branch" is accepted only by "add"
940 if test -n "$branch" && test "$command" != add
941 then
942 usage
945 # "--cached" is accepted only by "status" and "summary"
946 if test -n "$cached" && test "$command" != status -a "$command" != summary
947 then
948 usage
951 "cmd_$command" "$@"