submodule: update and add must honor --quiet flag
[git/jrn.git] / git-submodule.sh
blob11eab508024037c92daab0af776579e4545a143e
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"
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 case "$repo" in
247 ./*|../*)
248 url=$(resolve_relative_url "$repo") || exit
251 url="$repo"
253 esac
254 git config submodule."$path".url "$url"
255 else
257 module_clone "$path" "$realrepo" "$reference" || exit
259 clear_local_git_env
260 cd "$path" &&
261 # ash fails to wordsplit ${branch:+-b "$branch"...}
262 case "$branch" in
263 '') git checkout -f -q ;;
264 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
265 esac
266 ) || die "Unable to checkout submodule '$path'"
269 git add $force "$path" ||
270 die "Failed to add submodule '$path'"
272 git config -f .gitmodules submodule."$path".path "$path" &&
273 git config -f .gitmodules submodule."$path".url "$repo" &&
274 git add --force .gitmodules ||
275 die "Failed to register submodule '$path'"
279 # Execute an arbitrary command sequence in each checked out
280 # submodule
282 # $@ = command to execute
284 cmd_foreach()
286 # parse $args after "submodule ... foreach".
287 while test $# -ne 0
289 case "$1" in
290 -q|--quiet)
291 GIT_QUIET=1
293 --recursive)
294 recursive=1
297 usage
300 break
302 esac
303 shift
304 done
306 toplevel=$(pwd)
308 module_list |
309 while read mode sha1 stage path
311 if test -e "$path"/.git
312 then
313 say "Entering '$prefix$path'"
314 name=$(module_name "$path")
316 prefix="$prefix$path/"
317 clear_local_git_env
318 cd "$path" &&
319 eval "$@" &&
320 if test -n "$recursive"
321 then
322 cmd_foreach "--recursive" "$@"
324 ) ||
325 die "Stopping at '$path'; script returned non-zero status."
327 done
331 # Register submodules in .git/config
333 # $@ = requested paths (default to all)
335 cmd_init()
337 # parse $args after "submodule ... init".
338 while test $# -ne 0
340 case "$1" in
341 -q|--quiet)
342 GIT_QUIET=1
345 shift
346 break
349 usage
352 break
354 esac
355 shift
356 done
358 module_list "$@" |
359 while read mode sha1 stage path
361 # Skip already registered paths
362 name=$(module_name "$path") || exit
363 url=$(git config submodule."$name".url)
364 test -z "$url" || continue
366 url=$(git config -f .gitmodules submodule."$name".url)
367 test -z "$url" &&
368 die "No url found for submodule path '$path' in .gitmodules"
370 # Possibly a url relative to parent
371 case "$url" in
372 ./*|../*)
373 url=$(resolve_relative_url "$url") || exit
375 esac
377 git config submodule."$name".url "$url" ||
378 die "Failed to register url for submodule path '$path'"
380 upd="$(git config -f .gitmodules submodule."$name".update)"
381 test -z "$upd" ||
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 say "Synchronizing submodule url for '$name'"
883 git config submodule."$name".url "$url"
885 if test -e "$path"/.git
886 then
888 clear_local_git_env
889 cd "$path"
890 remote=$(get_default_remote)
891 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" "$@"