Partly revert "Work around funny CR issue"
[git/dscho.git] / git-submodule.sh
blobfd0324234e32aa6d9eec478d3107aa85ef66997d
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 cat >&2 <<EOF
226 The following path is ignored by one of your .gitignore files:
227 $path
228 Use -f if you really want to add it.
230 exit 1
233 # perhaps the path exists and is already a git repo, else clone it
234 if test -e "$path"
235 then
236 if test -d "$path"/.git -o -f "$path"/.git
237 then
238 echo "Adding existing repo at '$path' to the index"
239 else
240 die "'$path' already exists and is not a valid git repo"
243 case "$repo" in
244 ./*|../*)
245 url=$(resolve_relative_url "$repo") || exit
248 url="$repo"
250 esac
251 git config submodule."$path".url "$url"
252 else
254 module_clone "$path" "$realrepo" "$reference" || exit
256 clear_local_git_env
257 cd "$path" &&
258 # ash fails to wordsplit ${branch:+-b "$branch"...}
259 case "$branch" in
260 '') git checkout -f -q ;;
261 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
262 esac
263 ) || die "Unable to checkout submodule '$path'"
266 git add $force "$path" ||
267 die "Failed to add submodule '$path'"
269 git config -f .gitmodules submodule."$path".path "$path" &&
270 git config -f .gitmodules submodule."$path".url "$repo" &&
271 git add --force .gitmodules ||
272 die "Failed to register submodule '$path'"
276 # Execute an arbitrary command sequence in each checked out
277 # submodule
279 # $@ = command to execute
281 cmd_foreach()
283 # parse $args after "submodule ... foreach".
284 while test $# -ne 0
286 case "$1" in
287 -q|--quiet)
288 GIT_QUIET=1
290 --recursive)
291 recursive=1
294 usage
297 break
299 esac
300 shift
301 done
303 toplevel=$(pwd)
305 module_list |
306 while read mode sha1 stage path
308 if test -e "$path"/.git
309 then
310 say "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 ) ||
322 die "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 url=$(git config submodule."$name".url)
361 test -z "$url" || continue
363 url=$(git config -f .gitmodules submodule."$name".url)
364 test -z "$url" &&
365 die "No url found for submodule path '$path' in .gitmodules"
367 # Possibly a url relative to parent
368 case "$url" in
369 ./*|../*)
370 url=$(resolve_relative_url "$url") || exit
372 esac
374 git config submodule."$name".url "$url" ||
375 die "Failed to register url for submodule path '$path'"
377 upd="$(git config -f .gitmodules submodule."$name".update)"
378 test -z "$upd" ||
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 status_msg="# Submodules changed but not updated:"
748 else
749 status_msg="# Submodule changes to be committed:"
751 status_sed=$(sed -e 's|^|# |' -e 's|^# $|#|')
752 cat <<EOF
753 $status_msg
755 $status_sed
757 else
762 # List all submodules, prefixed with:
763 # - submodule not initialized
764 # + different revision checked out
766 # If --cached was specified the revision in the index will be printed
767 # instead of the currently checked out revision.
769 # $@ = requested paths (default to all)
771 cmd_status()
773 # parse $args after "submodule ... status".
774 orig_flags=
775 while test $# -ne 0
777 case "$1" in
778 -q|--quiet)
779 GIT_QUIET=1
781 --cached)
782 cached=1
784 --recursive)
785 recursive=1
788 shift
789 break
792 usage
795 break
797 esac
798 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
799 shift
800 done
802 module_list "$@" |
803 while read mode sha1 stage path
805 name=$(module_name "$path") || exit
806 url=$(git config submodule."$name".url)
807 displaypath="$prefix$path"
808 if test "$stage" = U
809 then
810 say "U$sha1 $displaypath"
811 continue
813 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
814 then
815 say "-$sha1 $displaypath"
816 continue;
818 set_name_rev "$path" "$sha1"
819 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
820 then
821 say " $sha1 $displaypath$revname"
822 else
823 if test -z "$cached"
824 then
825 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
826 set_name_rev "$path" "$sha1"
828 say "+$sha1 $displaypath$revname"
831 if test -n "$recursive"
832 then
834 prefix="$displaypath/"
835 clear_local_git_env
836 cd "$path" &&
837 eval cmd_status "$orig_args"
838 ) ||
839 die "Failed to recurse into submodule path '$path'"
841 done
844 # Sync remote urls for submodules
845 # This makes the value for remote.$remote.url match the value
846 # specified in .gitmodules.
848 cmd_sync()
850 while test $# -ne 0
852 case "$1" in
853 -q|--quiet)
854 GIT_QUIET=1
855 shift
858 shift
859 break
862 usage
865 break
867 esac
868 done
869 cd_to_toplevel
870 module_list "$@" |
871 while read mode sha1 stage path
873 name=$(module_name "$path")
874 url=$(git config -f .gitmodules --get submodule."$name".url)
876 # Possibly a url relative to parent
877 case "$url" in
878 ./*|../*)
879 url=$(resolve_relative_url "$url") || exit
881 esac
883 say "Synchronizing submodule url for '$name'"
884 git config submodule."$name".url "$url"
886 if test -e "$path"/.git
887 then
889 clear_local_git_env
890 cd "$path"
891 remote=$(get_default_remote)
892 git config remote."$remote".url "$url"
895 done
898 # This loop parses the command line arguments to find the
899 # subcommand name to dispatch. Parsing of the subcommand specific
900 # options are primarily done by the subcommand implementations.
901 # Subcommand specific options such as --branch and --cached are
902 # parsed here as well, for backward compatibility.
904 while test $# != 0 && test -z "$command"
906 case "$1" in
907 add | foreach | init | update | status | summary | sync)
908 command=$1
910 -q|--quiet)
911 GIT_QUIET=1
913 -b|--branch)
914 case "$2" in
916 usage
918 esac
919 branch="$2"; shift
921 --cached)
922 cached="$1"
925 break
928 usage
931 break
933 esac
934 shift
935 done
937 # No command word defaults to "status"
938 test -n "$command" || command=status
940 # "-b branch" is accepted only by "add"
941 if test -n "$branch" && test "$command" != add
942 then
943 usage
946 # "--cached" is accepted only by "status" and "summary"
947 if test -n "$cached" && test "$command" != status -a "$command" != summary
948 then
949 usage
952 "cmd_$command" "$@"