1 #!/bin/sh
2 #
3 # git-submodules.sh: add, init, update or list git submodules
4 #
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] [--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 ()
34 {
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"
42 do
43 case "$url" in
44 ../*)
45 url="${url#../}"
46 case "$remoteurl" in
47 */*)
48 remoteurl="${remoteurl%/*}"
49 ;;
50 *:*)
51 remoteurl="${remoteurl%:*}"
52 sep=:
53 ;;
54 *)
55 die "cannot strip one component off url '$remoteurl'"
56 ;;
57 esac
58 ;;
59 ./*)
60 url="${url#./}"
61 ;;
62 *)
63 break;;
64 esac
65 done
66 echo "$remoteurl$sep${url%/}"
67 }
69 #
70 # Get submodule info for registered submodules
71 # $@ = path to limit submodule list
72 #
73 module_list()
74 {
75 git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 '
76 }
78 #
79 # Map submodule path to submodule name
80 #
81 # $1 = path
82 #
83 module_name()
84 {
85 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
86 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
87 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
88 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
89 test -z "$name" &&
90 die "No submodule mapping found in .gitmodules for path '$path'"
91 echo "$name"
92 }
94 #
95 # Clone a submodule
96 #
97 # Prior to calling, cmd_update checks that a possibly existing
98 # path is not a git repository.
99 # Likewise, cmd_add checks that path does not exist at all,
100 # since it is the location of a new submodule.
101 #
102 module_clone()
103 {
104 path=$1
105 url=$2
106 reference="$3"
108 if test -n "$reference"
109 then
110 git-clone "$reference" -n "$url" "$path"
111 else
112 git-clone -n "$url" "$path"
113 fi ||
114 die "Clone of '$url' into submodule path '$path' failed"
115 }
117 #
118 # Add a new submodule to the working tree, .gitmodules and the index
119 #
120 # $@ = repo path
121 #
122 # optional branch is stored in global branch variable
123 #
124 cmd_add()
125 {
126 # parse $args after "submodule ... add".
127 while test $# -ne 0
128 do
129 case "$1" in
130 -b | --branch)
131 case "$2" in '') usage ;; esac
132 branch=$2
133 shift
134 ;;
135 -f | --force)
136 force=$1
137 ;;
138 -q|--quiet)
139 GIT_QUIET=1
140 ;;
141 --reference)
142 case "$2" in '') usage ;; esac
143 reference="--reference=$2"
144 shift
145 ;;
146 --reference=*)
147 reference="$1"
148 shift
149 ;;
150 --)
151 shift
152 break
153 ;;
154 -*)
155 usage
156 ;;
157 *)
158 break
159 ;;
160 esac
161 shift
162 done
164 repo=$1
165 path=$2
167 if test -z "$path"; then
168 path=$(echo "$repo" |
169 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
170 fi
172 if test -z "$repo" -o -z "$path"; then
173 usage
174 fi
176 # assure repo is absolute or relative to parent
177 case "$repo" in
178 ./*|../*)
179 # dereference source url relative to parent's url
180 realrepo=$(resolve_relative_url "$repo") || exit
181 ;;
182 *:*|/*)
183 # absolute url
184 realrepo=$repo
185 ;;
186 *)
187 die "repo URL: '$repo' must be absolute or begin with ./|../"
188 ;;
189 esac
191 # normalize path:
192 # multiple //; leading ./; /./; /../; trailing /
193 path=$(printf '%s/\n' "$path" |
194 sed -e '
195 s|//*|/|g
196 s|^\(\./\)*||
197 s|/\./|/|g
198 :start
199 s|\([^/]*\)/\.\./||
200 tstart
201 s|/*$||
202 ')
203 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
204 die "'$path' already exists in the index"
206 if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
207 then
208 echo >&2 "The following path is ignored by one of your .gitignore files:" &&
209 echo >&2 $path &&
210 echo >&2 "Use -f if you really want to add it."
211 exit 1
212 fi
214 # perhaps the path exists and is already a git repo, else clone it
215 if test -e "$path"
216 then
217 if test -d "$path"/.git -o -f "$path"/.git
218 then
219 echo "Adding existing repo at '$path' to the index"
220 else
221 die "'$path' already exists and is not a valid git repo"
222 fi
224 case "$repo" in
225 ./*|../*)
226 url=$(resolve_relative_url "$repo") || exit
227 ;;
228 *)
229 url="$repo"
230 ;;
231 esac
232 git config submodule."$path".url "$url"
233 else
235 module_clone "$path" "$realrepo" "$reference" || exit
236 (
237 clear_local_git_env
238 cd "$path" &&
239 # ash fails to wordsplit ${branch:+-b "$branch"...}
240 case "$branch" in
241 '') git checkout -f -q ;;
242 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
243 esac
244 ) || die "Unable to checkout submodule '$path'"
245 fi
247 git add $force "$path" ||
248 die "Failed to add submodule '$path'"
250 git config -f .gitmodules submodule."$path".path "$path" &&
251 git config -f .gitmodules submodule."$path".url "$repo" &&
252 git add --force .gitmodules ||
253 die "Failed to register submodule '$path'"
254 }
256 #
257 # Execute an arbitrary command sequence in each checked out
258 # submodule
259 #
260 # $@ = command to execute
261 #
262 cmd_foreach()
263 {
264 # parse $args after "submodule ... foreach".
265 while test $# -ne 0
266 do
267 case "$1" in
268 -q|--quiet)
269 GIT_QUIET=1
270 ;;
271 --recursive)
272 recursive=1
273 ;;
274 -*)
275 usage
276 ;;
277 *)
278 break
279 ;;
280 esac
281 shift
282 done
284 toplevel=$(pwd)
286 module_list |
287 while read mode sha1 stage path
288 do
289 if test -e "$path"/.git
290 then
291 say "Entering '$prefix$path'"
292 name=$(module_name "$path")
293 (
294 prefix="$prefix$path/"
295 clear_local_git_env
296 cd "$path" &&
297 eval "$@" &&
298 if test -n "$recursive"
299 then
300 cmd_foreach "--recursive" "$@"
301 fi
302 ) ||
303 die "Stopping at '$path'; script returned non-zero status."
304 fi
305 done
306 }
308 #
309 # Register submodules in .git/config
310 #
311 # $@ = requested paths (default to all)
312 #
313 cmd_init()
314 {
315 # parse $args after "submodule ... init".
316 while test $# -ne 0
317 do
318 case "$1" in
319 -q|--quiet)
320 GIT_QUIET=1
321 ;;
322 --)
323 shift
324 break
325 ;;
326 -*)
327 usage
328 ;;
329 *)
330 break
331 ;;
332 esac
333 shift
334 done
336 module_list "$@" |
337 while read mode sha1 stage path
338 do
339 # Skip already registered paths
340 name=$(module_name "$path") || exit
341 url=$(git config submodule."$name".url)
342 test -z "$url" || continue
344 url=$(git config -f .gitmodules submodule."$name".url)
345 test -z "$url" &&
346 die "No url found for submodule path '$path' in .gitmodules"
348 # Possibly a url relative to parent
349 case "$url" in
350 ./*|../*)
351 url=$(resolve_relative_url "$url") || exit
352 ;;
353 esac
355 git config submodule."$name".url "$url" ||
356 die "Failed to register url for submodule path '$path'"
358 upd="$(git config -f .gitmodules submodule."$name".update)"
359 test -z "$upd" ||
360 git config submodule."$name".update "$upd" ||
361 die "Failed to register update mode for submodule path '$path'"
363 say "Submodule '$name' ($url) registered for path '$path'"
364 done
365 }
367 #
368 # Update each submodule path to correct revision, using clone and checkout as needed
369 #
370 # $@ = requested paths (default to all)
371 #
372 cmd_update()
373 {
374 # parse $args after "submodule ... update".
375 orig_flags=
376 while test $# -ne 0
377 do
378 case "$1" in
379 -q|--quiet)
380 GIT_QUIET=1
381 ;;
382 -i|--init)
383 init=1
384 ;;
385 -N|--no-fetch)
386 nofetch=1
387 ;;
388 -r|--rebase)
389 update="rebase"
390 ;;
391 --reference)
392 case "$2" in '') usage ;; esac
393 reference="--reference=$2"
394 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
395 shift
396 ;;
397 --reference=*)
398 reference="$1"
399 ;;
400 -m|--merge)
401 update="merge"
402 ;;
403 --recursive)
404 recursive=1
405 ;;
406 --)
407 shift
408 break
409 ;;
410 -*)
411 usage
412 ;;
413 *)
414 break
415 ;;
416 esac
417 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
418 shift
419 done
421 if test -n "$init"
422 then
423 cmd_init "--" "$@" || return
424 fi
426 module_list "$@" |
427 while read mode sha1 stage path
428 do
429 name=$(module_name "$path") || exit
430 url=$(git config submodule."$name".url)
431 update_module=$(git config submodule."$name".update)
432 if test -z "$url"
433 then
434 # Only mention uninitialized submodules when its
435 # path have been specified
436 test "$#" != "0" &&
437 say "Submodule path '$path' not initialized" &&
438 say "Maybe you want to use 'update --init'?"
439 continue
440 fi
442 if ! test -d "$path"/.git -o -f "$path"/.git
443 then
444 module_clone "$path" "$url" "$reference"|| exit
445 subsha1=
446 else
447 subsha1=$(clear_local_git_env; cd "$path" &&
448 git rev-parse --verify HEAD) ||
449 die "Unable to find current revision in submodule path '$path'"
450 fi
452 if ! test -z "$update"
453 then
454 update_module=$update
455 fi
457 if test "$subsha1" != "$sha1"
458 then
459 force=
460 if test -z "$subsha1"
461 then
462 force="-f"
463 fi
465 if test -z "$nofetch"
466 then
467 (clear_local_git_env; cd "$path" &&
468 git-fetch) ||
469 die "Unable to fetch in submodule path '$path'"
470 fi
472 case "$update_module" in
473 rebase)
474 command="git rebase"
475 action="rebase"
476 msg="rebased onto"
477 ;;
478 merge)
479 command="git merge"
480 action="merge"
481 msg="merged in"
482 ;;
483 *)
484 command="git checkout $force -q"
485 action="checkout"
486 msg="checked out"
487 ;;
488 esac
490 (clear_local_git_env; cd "$path" && $command "$sha1") ||
491 die "Unable to $action '$sha1' in submodule path '$path'"
492 say "Submodule path '$path': $msg '$sha1'"
493 fi
495 if test -n "$recursive"
496 then
497 (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags") ||
498 die "Failed to recurse into submodule path '$path'"
499 fi
500 done
501 }
503 set_name_rev () {
504 revname=$( (
505 clear_local_git_env
506 cd "$1" && {
507 git describe "$2" 2>/dev/null ||
508 git describe --tags "$2" 2>/dev/null ||
509 git describe --contains "$2" 2>/dev/null ||
510 git describe --all --always "$2"
511 }
512 ) )
513 test -z "$revname" || revname=" ($revname)"
514 }
515 #
516 # Show commit summary for submodules in index or working tree
517 #
518 # If '--cached' is given, show summary between index and given commit,
519 # or between working tree and given commit
520 #
521 # $@ = [commit (default 'HEAD'),] requested paths (default all)
522 #
523 cmd_summary() {
524 summary_limit=-1
525 for_status=
526 diff_cmd=diff-index
528 # parse $args after "submodule ... summary".
529 while test $# -ne 0
530 do
531 case "$1" in
532 --cached)
533 cached="$1"
534 ;;
535 --files)
536 files="$1"
537 ;;
538 --for-status)
539 for_status="$1"
540 ;;
541 -n|--summary-limit)
542 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
543 then
544 :
545 else
546 usage
547 fi
548 shift
549 ;;
550 --)
551 shift
552 break
553 ;;
554 -*)
555 usage
556 ;;
557 *)
558 break
559 ;;
560 esac
561 shift
562 done
564 test $summary_limit = 0 && return
566 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
567 then
568 head=$rev
569 test $# = 0 || shift
570 elif test -z "$1" -o "$1" = "HEAD"
571 then
572 # before the first commit: compare with an empty tree
573 head=$(git hash-object -w -t tree --stdin </dev/null)
574 test -z "$1" || shift
575 else
576 head="HEAD"
577 fi
579 if [ -n "$files" ]
580 then
581 test -n "$cached" &&
582 die "--cached cannot be used with --files"
583 diff_cmd=diff-files
584 head=
585 fi
587 cd_to_toplevel
588 # Get modified modules cared by user
589 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
590 sane_egrep '^:([0-7]* )?160000' |
591 while read mod_src mod_dst sha1_src sha1_dst status name
592 do
593 # Always show modules deleted or type-changed (blob<->module)
594 test $status = D -o $status = T && echo "$name" && continue
595 # Also show added or modified modules which are checked out
596 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
597 echo "$name"
598 done
599 )
601 test -z "$modules" && return
603 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
604 sane_egrep '^:([0-7]* )?160000' |
605 cut -c2- |
606 while read mod_src mod_dst sha1_src sha1_dst status name
607 do
608 if test -z "$cached" &&
609 test $sha1_dst = 0000000000000000000000000000000000000000
610 then
611 case "$mod_dst" in
612 160000)
613 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
614 ;;
615 100644 | 100755 | 120000)
616 sha1_dst=$(git hash-object $name)
617 ;;
618 000000)
619 ;; # removed
620 *)
621 # unexpected type
622 echo >&2 "unexpected mode $mod_dst"
623 continue ;;
624 esac
625 fi
626 missing_src=
627 missing_dst=
629 test $mod_src = 160000 &&
630 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
631 missing_src=t
633 test $mod_dst = 160000 &&
634 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
635 missing_dst=t
637 total_commits=
638 case "$missing_src,$missing_dst" in
639 t,)
640 errmsg=" Warn: $name doesn't contain commit $sha1_src"
641 ;;
642 ,t)
643 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
644 ;;
645 t,t)
646 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
647 ;;
648 *)
649 errmsg=
650 total_commits=$(
651 if test $mod_src = 160000 -a $mod_dst = 160000
652 then
653 range="$sha1_src...$sha1_dst"
654 elif test $mod_src = 160000
655 then
656 range=$sha1_src
657 else
658 range=$sha1_dst
659 fi
660 GIT_DIR="$name/.git" \
661 git rev-list --first-parent $range -- | wc -l
662 )
663 total_commits=" ($(($total_commits + 0)))"
664 ;;
665 esac
667 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
668 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
669 if test $status = T
670 then
671 if test $mod_dst = 160000
672 then
673 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
674 else
675 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
676 fi
677 else
678 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
679 fi
680 if test -n "$errmsg"
681 then
682 # Don't give error msg for modification whose dst is not submodule
683 # i.e. deleted or changed to blob
684 test $mod_dst = 160000 && echo "$errmsg"
685 else
686 if test $mod_src = 160000 -a $mod_dst = 160000
687 then
688 limit=
689 test $summary_limit -gt 0 && limit="-$summary_limit"
690 GIT_DIR="$name/.git" \
691 git log $limit --pretty='format: %m %s' \
692 --first-parent $sha1_src...$sha1_dst
693 elif test $mod_dst = 160000
694 then
695 GIT_DIR="$name/.git" \
696 git log --pretty='format: > %s' -1 $sha1_dst
697 else
698 GIT_DIR="$name/.git" \
699 git log --pretty='format: < %s' -1 $sha1_src
700 fi
701 echo
702 fi
703 echo
704 done |
705 if test -n "$for_status"; then
706 if [ -n "$files" ]; then
707 echo "# Submodules changed but not updated:"
708 else
709 echo "# Submodule changes to be committed:"
710 fi
711 echo "#"
712 sed -e 's|^|# |' -e 's|^# $|#|'
713 else
714 cat
715 fi
716 }
717 #
718 # List all submodules, prefixed with:
719 # - submodule not initialized
720 # + different revision checked out
721 #
722 # If --cached was specified the revision in the index will be printed
723 # instead of the currently checked out revision.
724 #
725 # $@ = requested paths (default to all)
726 #
727 cmd_status()
728 {
729 # parse $args after "submodule ... status".
730 orig_flags=
731 while test $# -ne 0
732 do
733 case "$1" in
734 -q|--quiet)
735 GIT_QUIET=1
736 ;;
737 --cached)
738 cached=1
739 ;;
740 --recursive)
741 recursive=1
742 ;;
743 --)
744 shift
745 break
746 ;;
747 -*)
748 usage
749 ;;
750 *)
751 break
752 ;;
753 esac
754 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
755 shift
756 done
758 module_list "$@" |
759 while read mode sha1 stage path
760 do
761 name=$(module_name "$path") || exit
762 url=$(git config submodule."$name".url)
763 displaypath="$prefix$path"
764 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
765 then
766 say "-$sha1 $displaypath"
767 continue;
768 fi
769 set_name_rev "$path" "$sha1"
770 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
771 then
772 say " $sha1 $displaypath$revname"
773 else
774 if test -z "$cached"
775 then
776 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
777 set_name_rev "$path" "$sha1"
778 fi
779 say "+$sha1 $displaypath$revname"
780 fi
782 if test -n "$recursive"
783 then
784 (
785 prefix="$displaypath/"
786 clear_local_git_env
787 cd "$path" &&
788 eval cmd_status "$orig_args"
789 ) ||
790 die "Failed to recurse into submodule path '$path'"
791 fi
792 done
793 }
794 #
795 # Sync remote urls for submodules
796 # This makes the value for remote.$remote.url match the value
797 # specified in .gitmodules.
798 #
799 cmd_sync()
800 {
801 while test $# -ne 0
802 do
803 case "$1" in
804 -q|--quiet)
805 GIT_QUIET=1
806 shift
807 ;;
808 --)
809 shift
810 break
811 ;;
812 -*)
813 usage
814 ;;
815 *)
816 break
817 ;;
818 esac
819 done
820 cd_to_toplevel
821 module_list "$@" |
822 while read mode sha1 stage path
823 do
824 name=$(module_name "$path")
825 url=$(git config -f .gitmodules --get submodule."$name".url)
827 # Possibly a url relative to parent
828 case "$url" in
829 ./*|../*)
830 url=$(resolve_relative_url "$url") || exit
831 ;;
832 esac
834 say "Synchronizing submodule url for '$name'"
835 git config submodule."$name".url "$url"
837 if test -e "$path"/.git
838 then
839 (
840 clear_local_git_env
841 cd "$path"
842 remote=$(get_default_remote)
843 git config remote."$remote".url "$url"
844 )
845 fi
846 done
847 }
849 # This loop parses the command line arguments to find the
850 # subcommand name to dispatch. Parsing of the subcommand specific
851 # options are primarily done by the subcommand implementations.
852 # Subcommand specific options such as --branch and --cached are
853 # parsed here as well, for backward compatibility.
855 while test $# != 0 && test -z "$command"
856 do
857 case "$1" in
858 add | foreach | init | update | status | summary | sync)
859 command=$1
860 ;;
861 -q|--quiet)
862 GIT_QUIET=1
863 ;;
864 -b|--branch)
865 case "$2" in
866 '')
867 usage
868 ;;
869 esac
870 branch="$2"; shift
871 ;;
872 --cached)
873 cached="$1"
874 ;;
875 --)
876 break
877 ;;
878 -*)
879 usage
880 ;;
881 *)
882 break
883 ;;
884 esac
885 shift
886 done
888 # No command word defaults to "status"
889 test -n "$command" || command=status
891 # "-b branch" is accepted only by "add"
892 if test -n "$branch" && test "$command" != add
893 then
894 usage
895 fi
897 # "--cached" is accepted only by "status" and "summary"
898 if test -n "$cached" && test "$command" != status -a "$command" != summary
899 then
900 usage
901 fi
903 "cmd_$command" "$@"