Merge branch 'mz/doc-rebase-abort' into maint
[git/jrn.git] / git-submodule.sh
blob20c9bec9709749239849e5e91df2a9ac40c77eae
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 die "remote ($remote) does not have a url defined in .git/config"
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"
125 if test -n "$reference"
126 then
127 git-clone "$reference" -n "$url" "$path"
128 else
129 git-clone -n "$url" "$path"
130 fi ||
131 die "Clone of '$url' into submodule path '$path' failed"
135 # Add a new submodule to the working tree, .gitmodules and the index
137 # $@ = repo path
139 # optional branch is stored in global branch variable
141 cmd_add()
143 # parse $args after "submodule ... add".
144 while test $# -ne 0
146 case "$1" in
147 -b | --branch)
148 case "$2" in '') usage ;; esac
149 branch=$2
150 shift
152 -f | --force)
153 force=$1
155 -q|--quiet)
156 GIT_QUIET=1
158 --reference)
159 case "$2" in '') usage ;; esac
160 reference="--reference=$2"
161 shift
163 --reference=*)
164 reference="$1"
165 shift
168 shift
169 break
172 usage
175 break
177 esac
178 shift
179 done
181 repo=$1
182 path=$2
184 if test -z "$path"; then
185 path=$(echo "$repo" |
186 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
189 if test -z "$repo" -o -z "$path"; then
190 usage
193 # assure repo is absolute or relative to parent
194 case "$repo" in
195 ./*|../*)
196 # dereference source url relative to parent's url
197 realrepo=$(resolve_relative_url "$repo") || exit
199 *:*|/*)
200 # absolute url
201 realrepo=$repo
204 die "repo URL: '$repo' must be absolute or begin with ./|../"
206 esac
208 # normalize path:
209 # multiple //; leading ./; /./; /../; trailing /
210 path=$(printf '%s/\n' "$path" |
211 sed -e '
212 s|//*|/|g
213 s|^\(\./\)*||
214 s|/\./|/|g
215 :start
216 s|\([^/]*\)/\.\./||
217 tstart
218 s|/*$||
220 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
221 die "'$path' already exists in the index"
223 if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
224 then
225 echo >&2 "The following path is ignored by one of your .gitignore files:" &&
226 echo >&2 $path &&
227 echo >&2 "Use -f if you really want to add it."
228 exit 1
231 # perhaps the path exists and is already a git repo, else clone it
232 if test -e "$path"
233 then
234 if test -d "$path"/.git -o -f "$path"/.git
235 then
236 echo "Adding existing repo at '$path' to the index"
237 else
238 die "'$path' already exists and is not a valid git repo"
241 case "$repo" in
242 ./*|../*)
243 url=$(resolve_relative_url "$repo") || exit
246 url="$repo"
248 esac
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 "Unable to checkout submodule '$path'"
262 git config submodule."$path".url "$url"
264 git add $force "$path" ||
265 die "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 "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 "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 "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 "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 "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 "Failed to register update mode for submodule path '$path'"
385 say "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 while read mode sha1 stage path
455 if test "$stage" = U
456 then
457 echo >&2 "Skipping unmerged submodule $path"
458 continue
460 name=$(module_name "$path") || exit
461 url=$(git config submodule."$name".url)
462 update_module=$(git config submodule."$name".update)
463 if test -z "$url"
464 then
465 # Only mention uninitialized submodules when its
466 # path have been specified
467 test "$#" != "0" &&
468 say "Submodule path '$path' not initialized" &&
469 say "Maybe you want to use 'update --init'?"
470 continue
473 if ! test -d "$path"/.git -o -f "$path"/.git
474 then
475 module_clone "$path" "$url" "$reference"|| exit
476 cloned_modules="$cloned_modules;$name"
477 subsha1=
478 else
479 subsha1=$(clear_local_git_env; cd "$path" &&
480 git rev-parse --verify HEAD) ||
481 die "Unable to find current revision in submodule path '$path'"
484 if ! test -z "$update"
485 then
486 update_module=$update
489 if test "$subsha1" != "$sha1"
490 then
491 subforce=$force
492 # If we don't already have a -f flag and the submodule has never been checked out
493 if test -z "$subsha1" -a -z "$force"
494 then
495 subforce="-f"
498 if test -z "$nofetch"
499 then
500 # Run fetch only if $sha1 isn't present or it
501 # is not reachable from a ref.
502 (clear_local_git_env; cd "$path" &&
503 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
504 test -z "$rev") || git-fetch)) ||
505 die "Unable to fetch in submodule path '$path'"
508 # Is this something we just cloned?
509 case ";$cloned_modules;" in
510 *";$name;"*)
511 # then there is no local change to integrate
512 update_module= ;;
513 esac
515 case "$update_module" in
516 rebase)
517 command="git rebase"
518 action="rebase"
519 msg="rebased onto"
521 merge)
522 command="git merge"
523 action="merge"
524 msg="merged in"
527 command="git checkout $subforce -q"
528 action="checkout"
529 msg="checked out"
531 esac
533 (clear_local_git_env; cd "$path" && $command "$sha1") ||
534 die "Unable to $action '$sha1' in submodule path '$path'"
535 say "Submodule path '$path': $msg '$sha1'"
538 if test -n "$recursive"
539 then
540 (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags") ||
541 die "Failed to recurse into submodule path '$path'"
543 done
546 set_name_rev () {
547 revname=$( (
548 clear_local_git_env
549 cd "$1" && {
550 git describe "$2" 2>/dev/null ||
551 git describe --tags "$2" 2>/dev/null ||
552 git describe --contains "$2" 2>/dev/null ||
553 git describe --all --always "$2"
556 test -z "$revname" || revname=" ($revname)"
559 # Show commit summary for submodules in index or working tree
561 # If '--cached' is given, show summary between index and given commit,
562 # or between working tree and given commit
564 # $@ = [commit (default 'HEAD'),] requested paths (default all)
566 cmd_summary() {
567 summary_limit=-1
568 for_status=
569 diff_cmd=diff-index
571 # parse $args after "submodule ... summary".
572 while test $# -ne 0
574 case "$1" in
575 --cached)
576 cached="$1"
578 --files)
579 files="$1"
581 --for-status)
582 for_status="$1"
584 -n|--summary-limit)
585 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
586 then
588 else
589 usage
591 shift
594 shift
595 break
598 usage
601 break
603 esac
604 shift
605 done
607 test $summary_limit = 0 && return
609 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
610 then
611 head=$rev
612 test $# = 0 || shift
613 elif test -z "$1" -o "$1" = "HEAD"
614 then
615 # before the first commit: compare with an empty tree
616 head=$(git hash-object -w -t tree --stdin </dev/null)
617 test -z "$1" || shift
618 else
619 head="HEAD"
622 if [ -n "$files" ]
623 then
624 test -n "$cached" &&
625 die "--cached cannot be used with --files"
626 diff_cmd=diff-files
627 head=
630 cd_to_toplevel
631 # Get modified modules cared by user
632 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
633 sane_egrep '^:([0-7]* )?160000' |
634 while read mod_src mod_dst sha1_src sha1_dst status name
636 # Always show modules deleted or type-changed (blob<->module)
637 test $status = D -o $status = T && echo "$name" && continue
638 # Also show added or modified modules which are checked out
639 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
640 echo "$name"
641 done
644 test -z "$modules" && return
646 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
647 sane_egrep '^:([0-7]* )?160000' |
648 cut -c2- |
649 while read mod_src mod_dst sha1_src sha1_dst status name
651 if test -z "$cached" &&
652 test $sha1_dst = 0000000000000000000000000000000000000000
653 then
654 case "$mod_dst" in
655 160000)
656 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
658 100644 | 100755 | 120000)
659 sha1_dst=$(git hash-object $name)
661 000000)
662 ;; # removed
664 # unexpected type
665 echo >&2 "unexpected mode $mod_dst"
666 continue ;;
667 esac
669 missing_src=
670 missing_dst=
672 test $mod_src = 160000 &&
673 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
674 missing_src=t
676 test $mod_dst = 160000 &&
677 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
678 missing_dst=t
680 total_commits=
681 case "$missing_src,$missing_dst" in
683 errmsg=" Warn: $name doesn't contain commit $sha1_src"
686 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
688 t,t)
689 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
692 errmsg=
693 total_commits=$(
694 if test $mod_src = 160000 -a $mod_dst = 160000
695 then
696 range="$sha1_src...$sha1_dst"
697 elif test $mod_src = 160000
698 then
699 range=$sha1_src
700 else
701 range=$sha1_dst
703 GIT_DIR="$name/.git" \
704 git rev-list --first-parent $range -- | wc -l
706 total_commits=" ($(($total_commits + 0)))"
708 esac
710 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
711 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
712 if test $status = T
713 then
714 if test $mod_dst = 160000
715 then
716 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
717 else
718 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
720 else
721 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
723 if test -n "$errmsg"
724 then
725 # Don't give error msg for modification whose dst is not submodule
726 # i.e. deleted or changed to blob
727 test $mod_dst = 160000 && echo "$errmsg"
728 else
729 if test $mod_src = 160000 -a $mod_dst = 160000
730 then
731 limit=
732 test $summary_limit -gt 0 && limit="-$summary_limit"
733 GIT_DIR="$name/.git" \
734 git log $limit --pretty='format: %m %s' \
735 --first-parent $sha1_src...$sha1_dst
736 elif test $mod_dst = 160000
737 then
738 GIT_DIR="$name/.git" \
739 git log --pretty='format: > %s' -1 $sha1_dst
740 else
741 GIT_DIR="$name/.git" \
742 git log --pretty='format: < %s' -1 $sha1_src
744 echo
746 echo
747 done |
748 if test -n "$for_status"; then
749 if [ -n "$files" ]; then
750 echo "# Submodules changed but not updated:"
751 else
752 echo "# Submodule changes to be committed:"
754 echo "#"
755 sed -e 's|^|# |' -e 's|^# $|#|'
756 else
761 # List all submodules, prefixed with:
762 # - submodule not initialized
763 # + different revision checked out
765 # If --cached was specified the revision in the index will be printed
766 # instead of the currently checked out revision.
768 # $@ = requested paths (default to all)
770 cmd_status()
772 # parse $args after "submodule ... status".
773 orig_flags=
774 while test $# -ne 0
776 case "$1" in
777 -q|--quiet)
778 GIT_QUIET=1
780 --cached)
781 cached=1
783 --recursive)
784 recursive=1
787 shift
788 break
791 usage
794 break
796 esac
797 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
798 shift
799 done
801 module_list "$@" |
802 while read mode sha1 stage path
804 name=$(module_name "$path") || exit
805 url=$(git config submodule."$name".url)
806 displaypath="$prefix$path"
807 if test "$stage" = U
808 then
809 say "U$sha1 $displaypath"
810 continue
812 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
813 then
814 say "-$sha1 $displaypath"
815 continue;
817 set_name_rev "$path" "$sha1"
818 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
819 then
820 say " $sha1 $displaypath$revname"
821 else
822 if test -z "$cached"
823 then
824 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
825 set_name_rev "$path" "$sha1"
827 say "+$sha1 $displaypath$revname"
830 if test -n "$recursive"
831 then
833 prefix="$displaypath/"
834 clear_local_git_env
835 cd "$path" &&
836 eval cmd_status "$orig_args"
837 ) ||
838 die "Failed to recurse into submodule path '$path'"
840 done
843 # Sync remote urls for submodules
844 # This makes the value for remote.$remote.url match the value
845 # specified in .gitmodules.
847 cmd_sync()
849 while test $# -ne 0
851 case "$1" in
852 -q|--quiet)
853 GIT_QUIET=1
854 shift
857 shift
858 break
861 usage
864 break
866 esac
867 done
868 cd_to_toplevel
869 module_list "$@" |
870 while read mode sha1 stage path
872 name=$(module_name "$path")
873 url=$(git config -f .gitmodules --get submodule."$name".url)
875 # Possibly a url relative to parent
876 case "$url" in
877 ./*|../*)
878 url=$(resolve_relative_url "$url") || exit
880 esac
882 if git config "submodule.$name.url" >/dev/null 2>/dev/null
883 then
884 say "Synchronizing submodule url for '$name'"
885 git config submodule."$name".url "$url"
887 if test -e "$path"/.git
888 then
890 clear_local_git_env
891 cd "$path"
892 remote=$(get_default_remote)
893 git config remote."$remote".url "$url"
897 done
900 # This loop parses the command line arguments to find the
901 # subcommand name to dispatch. Parsing of the subcommand specific
902 # options are primarily done by the subcommand implementations.
903 # Subcommand specific options such as --branch and --cached are
904 # parsed here as well, for backward compatibility.
906 while test $# != 0 && test -z "$command"
908 case "$1" in
909 add | foreach | init | update | status | summary | sync)
910 command=$1
912 -q|--quiet)
913 GIT_QUIET=1
915 -b|--branch)
916 case "$2" in
918 usage
920 esac
921 branch="$2"; shift
923 --cached)
924 cached="$1"
927 break
930 usage
933 break
935 esac
936 shift
937 done
939 # No command word defaults to "status"
940 test -n "$command" || command=status
942 # "-b branch" is accepted only by "add"
943 if test -n "$branch" && test "$command" != add
944 then
945 usage
948 # "--cached" is accepted only by "status" and "summary"
949 if test -n "$cached" && test "$command" != status -a "$command" != summary
950 then
951 usage
954 "cmd_$command" "$@"