1#!/bin/sh
2#
3# git-submodule.sh: add, init, update or list git submodules
4#
5# Copyright (c) 2007 Lars Hjemli
6
7dashless=$(basename "$0" | sed -e 's/-/ /')
8USAGE="[--quiet] add [-b branch] [-f|--force] [--name <name>] [--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>...]"
15OPTIONS_SPEC=
16. git-sh-setup
17. git-sh-i18n
18. git-parse-remote
19require_work_tree
20
21command=
22branch=
23force=
24reference=
25cached=
26recursive=
27init=
28files=
29nofetch=
30update=
31prefix=
32custom_name=
33
34# The function takes at most 2 arguments. The first argument is the
35# URL that navigates to the submodule origin repo. When relative, this URL
36# is relative to the superproject origin URL repo. The second up_path
37# argument, if specified, is the relative path that navigates
38# from the submodule working tree to the superproject working tree.
39#
40# The output of the function is the origin URL of the submodule.
41#
42# The output will either be an absolute URL or filesystem path (if the
43# superproject origin URL is an absolute URL or filesystem path,
44# respectively) or a relative file system path (if the superproject
45# origin URL is a relative file system path).
46#
47# When the output is a relative file system path, the path is either
48# relative to the submodule working tree, if up_path is specified, or to
49# the superproject working tree otherwise.
50resolve_relative_url ()
51{
52 remote=$(get_default_remote)
53 remoteurl=$(git config "remote.$remote.url") ||
54 remoteurl=$(pwd) # the repository is its own authoritative upstream
55 url="$1"
56 remoteurl=${remoteurl%/}
57 sep=/
58 up_path="$2"
59
60 case "$remoteurl" in
61 *:*|/*)
62 is_relative=
63 ;;
64 ./*|../*)
65 is_relative=t
66 ;;
67 *)
68 is_relative=t
69 remoteurl="./$remoteurl"
70 ;;
71 esac
72
73 while test -n "$url"
74 do
75 case "$url" in
76 ../*)
77 url="${url#../}"
78 case "$remoteurl" in
79 */*)
80 remoteurl="${remoteurl%/*}"
81 ;;
82 *:*)
83 remoteurl="${remoteurl%:*}"
84 sep=:
85 ;;
86 *)
87 if test -z "$is_relative" || test "." = "$remoteurl"
88 then
89 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
90 else
91 remoteurl=.
92 fi
93 ;;
94 esac
95 ;;
96 ./*)
97 url="${url#./}"
98 ;;
99 *)
100 break;;
101 esac
102 done
103 remoteurl="$remoteurl$sep${url%/}"
104 echo "${is_relative:+${up_path}}${remoteurl#./}"
105}
106
107#
108# Get submodule info for registered submodules
109# $@ = path to limit submodule list
110#
111module_list()
112{
113 (
114 git ls-files --error-unmatch --stage -- "$@" ||
115 echo "unmatched pathspec exists"
116 ) |
117 perl -e '
118 my %unmerged = ();
119 my ($null_sha1) = ("0" x 40);
120 my @out = ();
121 my $unmatched = 0;
122 while (<STDIN>) {
123 if (/^unmatched pathspec/) {
124 $unmatched = 1;
125 next;
126 }
127 chomp;
128 my ($mode, $sha1, $stage, $path) =
129 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
130 next unless $mode eq "160000";
131 if ($stage ne "0") {
132 if (!$unmerged{$path}++) {
133 push @out, "$mode $null_sha1 U\t$path\n";
134 }
135 next;
136 }
137 push @out, "$_\n";
138 }
139 if ($unmatched) {
140 print "#unmatched\n";
141 } else {
142 print for (@out);
143 }
144 '
145}
146
147die_if_unmatched ()
148{
149 if test "$1" = "#unmatched"
150 then
151 exit 1
152 fi
153}
154
155#
156# Map submodule path to submodule name
157#
158# $1 = path
159#
160module_name()
161{
162 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
163 sm_path="$1"
164 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
165 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
166 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
167 test -z "$name" &&
168 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
169 echo "$name"
170}
171
172#
173# Clone a submodule
174#
175# Prior to calling, cmd_update checks that a possibly existing
176# path is not a git repository.
177# Likewise, cmd_add checks that path does not exist at all,
178# since it is the location of a new submodule.
179#
180module_clone()
181{
182 sm_path=$1
183 name=$2
184 url=$3
185 reference="$4"
186 quiet=
187 if test -n "$GIT_QUIET"
188 then
189 quiet=-q
190 fi
191
192 gitdir=
193 gitdir_base=
194 base_name=$(dirname "$name")
195
196 gitdir=$(git rev-parse --git-dir)
197 gitdir_base="$gitdir/modules/$base_name"
198 gitdir="$gitdir/modules/$name"
199
200 if test -d "$gitdir"
201 then
202 mkdir -p "$sm_path"
203 rm -f "$gitdir/index"
204 else
205 mkdir -p "$gitdir_base"
206 (
207 clear_local_git_env
208 git clone $quiet -n ${reference:+"$reference"} \
209 --separate-git-dir "$gitdir" "$url" "$sm_path"
210 ) ||
211 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
212 fi
213
214 # We already are at the root of the work tree but cd_to_toplevel will
215 # resolve any symlinks that might be present in $PWD
216 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
217 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
218 # normalize Windows-style absolute paths to POSIX-style absolute paths
219 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
220 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
221 # Remove all common leading directories after a sanity check
222 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
223 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
224 fi
225 while test "${a%%/*}" = "${b%%/*}"
226 do
227 a=${a#*/}
228 b=${b#*/}
229 done
230 # Now chop off the trailing '/'s that were added in the beginning
231 a=${a%/}
232 b=${b%/}
233
234 # Turn each leading "*/" component into "../"
235 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
236 echo "gitdir: $rel/$a" >"$sm_path/.git"
237
238 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
239 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
240}
241
242#
243# Add a new submodule to the working tree, .gitmodules and the index
244#
245# $@ = repo path
246#
247# optional branch is stored in global branch variable
248#
249cmd_add()
250{
251 # parse $args after "submodule ... add".
252 while test $# -ne 0
253 do
254 case "$1" in
255 -b | --branch)
256 case "$2" in '') usage ;; esac
257 branch=$2
258 shift
259 ;;
260 -f | --force)
261 force=$1
262 ;;
263 -q|--quiet)
264 GIT_QUIET=1
265 ;;
266 --reference)
267 case "$2" in '') usage ;; esac
268 reference="--reference=$2"
269 shift
270 ;;
271 --reference=*)
272 reference="$1"
273 shift
274 ;;
275 --name)
276 case "$2" in '') usage ;; esac
277 custom_name=$2
278 shift
279 ;;
280 --)
281 shift
282 break
283 ;;
284 -*)
285 usage
286 ;;
287 *)
288 break
289 ;;
290 esac
291 shift
292 done
293
294 repo=$1
295 sm_path=$2
296
297 if test -z "$sm_path"; then
298 sm_path=$(echo "$repo" |
299 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
300 fi
301
302 if test -z "$repo" -o -z "$sm_path"; then
303 usage
304 fi
305
306 # assure repo is absolute or relative to parent
307 case "$repo" in
308 ./*|../*)
309 # dereference source url relative to parent's url
310 realrepo=$(resolve_relative_url "$repo") || exit
311 ;;
312 *:*|/*)
313 # absolute url
314 realrepo=$repo
315 ;;
316 *)
317 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
318 ;;
319 esac
320
321 # normalize path:
322 # multiple //; leading ./; /./; /../; trailing /
323 sm_path=$(printf '%s/\n' "$sm_path" |
324 sed -e '
325 s|//*|/|g
326 s|^\(\./\)*||
327 s|/\./|/|g
328 :start
329 s|\([^/]*\)/\.\./||
330 tstart
331 s|/*$||
332 ')
333 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
334 die "$(eval_gettext "'\$sm_path' already exists in the index")"
335
336 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
337 then
338 eval_gettextln "The following path is ignored by one of your .gitignore files:
339\$sm_path
340Use -f if you really want to add it." >&2
341 exit 1
342 fi
343
344 if test -n "$custom_name"
345 then
346 sm_name="$custom_name"
347 else
348 sm_name="$sm_path"
349 fi
350
351 # perhaps the path exists and is already a git repo, else clone it
352 if test -e "$sm_path"
353 then
354 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
355 then
356 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
357 else
358 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
359 fi
360
361 else
362 if test -d ".git/modules/$sm_name"
363 then
364 if test -z "$force"
365 then
366 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
367 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
368 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
369 echo >&2 " $realrepo"
370 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
371 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
372 else
373 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
374 fi
375 fi
376 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
377 (
378 clear_local_git_env
379 cd "$sm_path" &&
380 # ash fails to wordsplit ${branch:+-b "$branch"...}
381 case "$branch" in
382 '') git checkout -f -q ;;
383 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
384 esac
385 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
386 fi
387 git config submodule."$sm_name".url "$realrepo"
388
389 git add $force "$sm_path" ||
390 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
391
392 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
393 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
394 git add --force .gitmodules ||
395 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
396}
397
398#
399# Execute an arbitrary command sequence in each checked out
400# submodule
401#
402# $@ = command to execute
403#
404cmd_foreach()
405{
406 # parse $args after "submodule ... foreach".
407 while test $# -ne 0
408 do
409 case "$1" in
410 -q|--quiet)
411 GIT_QUIET=1
412 ;;
413 --recursive)
414 recursive=1
415 ;;
416 -*)
417 usage
418 ;;
419 *)
420 break
421 ;;
422 esac
423 shift
424 done
425
426 toplevel=$(pwd)
427
428 # dup stdin so that it can be restored when running the external
429 # command in the subshell (and a recursive call to this function)
430 exec 3<&0
431
432 module_list |
433 while read mode sha1 stage sm_path
434 do
435 die_if_unmatched "$mode"
436 if test -e "$sm_path"/.git
437 then
438 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
439 name=$(module_name "$sm_path")
440 (
441 prefix="$prefix$sm_path/"
442 clear_local_git_env
443 # we make $path available to scripts ...
444 path=$sm_path
445 cd "$sm_path" &&
446 eval "$@" &&
447 if test -n "$recursive"
448 then
449 cmd_foreach "--recursive" "$@"
450 fi
451 ) <&3 3<&- ||
452 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
453 fi
454 done
455}
456
457#
458# Register submodules in .git/config
459#
460# $@ = requested paths (default to all)
461#
462cmd_init()
463{
464 # parse $args after "submodule ... init".
465 while test $# -ne 0
466 do
467 case "$1" in
468 -q|--quiet)
469 GIT_QUIET=1
470 ;;
471 --)
472 shift
473 break
474 ;;
475 -*)
476 usage
477 ;;
478 *)
479 break
480 ;;
481 esac
482 shift
483 done
484
485 module_list "$@" |
486 while read mode sha1 stage sm_path
487 do
488 die_if_unmatched "$mode"
489 name=$(module_name "$sm_path") || exit
490
491 # Copy url setting when it is not set yet
492 if test -z "$(git config "submodule.$name.url")"
493 then
494 url=$(git config -f .gitmodules submodule."$name".url)
495 test -z "$url" &&
496 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
497
498 # Possibly a url relative to parent
499 case "$url" in
500 ./*|../*)
501 url=$(resolve_relative_url "$url") || exit
502 ;;
503 esac
504 git config submodule."$name".url "$url" ||
505 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
506
507 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
508 fi
509
510 # Copy "update" setting when it is not set yet
511 upd="$(git config -f .gitmodules submodule."$name".update)"
512 test -z "$upd" ||
513 test -n "$(git config submodule."$name".update)" ||
514 git config submodule."$name".update "$upd" ||
515 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
516 done
517}
518
519#
520# Update each submodule path to correct revision, using clone and checkout as needed
521#
522# $@ = requested paths (default to all)
523#
524cmd_update()
525{
526 # parse $args after "submodule ... update".
527 orig_flags=
528 while test $# -ne 0
529 do
530 case "$1" in
531 -q|--quiet)
532 GIT_QUIET=1
533 ;;
534 -i|--init)
535 init=1
536 ;;
537 -N|--no-fetch)
538 nofetch=1
539 ;;
540 -f|--force)
541 force=$1
542 ;;
543 -r|--rebase)
544 update="rebase"
545 ;;
546 --reference)
547 case "$2" in '') usage ;; esac
548 reference="--reference=$2"
549 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
550 shift
551 ;;
552 --reference=*)
553 reference="$1"
554 ;;
555 -m|--merge)
556 update="merge"
557 ;;
558 --recursive)
559 recursive=1
560 ;;
561 --checkout)
562 update="checkout"
563 ;;
564 --)
565 shift
566 break
567 ;;
568 -*)
569 usage
570 ;;
571 *)
572 break
573 ;;
574 esac
575 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
576 shift
577 done
578
579 if test -n "$init"
580 then
581 cmd_init "--" "$@" || return
582 fi
583
584 cloned_modules=
585 module_list "$@" | {
586 err=
587 while read mode sha1 stage sm_path
588 do
589 die_if_unmatched "$mode"
590 if test "$stage" = U
591 then
592 echo >&2 "Skipping unmerged submodule $sm_path"
593 continue
594 fi
595 name=$(module_name "$sm_path") || exit
596 url=$(git config submodule."$name".url)
597 if ! test -z "$update"
598 then
599 update_module=$update
600 else
601 update_module=$(git config submodule."$name".update)
602 fi
603
604 if test "$update_module" = "none"
605 then
606 echo "Skipping submodule '$sm_path'"
607 continue
608 fi
609
610 if test -z "$url"
611 then
612 # Only mention uninitialized submodules when its
613 # path have been specified
614 test "$#" != "0" &&
615 say "$(eval_gettext "Submodule path '\$sm_path' not initialized
616Maybe you want to use 'update --init'?")"
617 continue
618 fi
619
620 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
621 then
622 module_clone "$sm_path" "$name" "$url" "$reference" || exit
623 cloned_modules="$cloned_modules;$name"
624 subsha1=
625 else
626 subsha1=$(clear_local_git_env; cd "$sm_path" &&
627 git rev-parse --verify HEAD) ||
628 die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
629 fi
630
631 if test "$subsha1" != "$sha1" -o -n "$force"
632 then
633 subforce=$force
634 # If we don't already have a -f flag and the submodule has never been checked out
635 if test -z "$subsha1" -a -z "$force"
636 then
637 subforce="-f"
638 fi
639
640 if test -z "$nofetch"
641 then
642 # Run fetch only if $sha1 isn't present or it
643 # is not reachable from a ref.
644 (clear_local_git_env; cd "$sm_path" &&
645 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
646 test -z "$rev") || git-fetch)) ||
647 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
648 fi
649
650 # Is this something we just cloned?
651 case ";$cloned_modules;" in
652 *";$name;"*)
653 # then there is no local change to integrate
654 update_module= ;;
655 esac
656
657 must_die_on_failure=
658 case "$update_module" in
659 rebase)
660 command="git rebase"
661 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
662 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
663 must_die_on_failure=yes
664 ;;
665 merge)
666 command="git merge"
667 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
668 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
669 must_die_on_failure=yes
670 ;;
671 *)
672 command="git checkout $subforce -q"
673 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
674 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
675 ;;
676 esac
677
678 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
679 then
680 say "$say_msg"
681 elif test -n "$must_die_on_failure"
682 then
683 die_with_status 2 "$die_msg"
684 else
685 err="${err};$die_msg"
686 continue
687 fi
688 fi
689
690 if test -n "$recursive"
691 then
692 (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
693 res=$?
694 if test $res -gt 0
695 then
696 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
697 if test $res -eq 1
698 then
699 err="${err};$die_msg"
700 continue
701 else
702 die_with_status $res "$die_msg"
703 fi
704 fi
705 fi
706 done
707
708 if test -n "$err"
709 then
710 OIFS=$IFS
711 IFS=';'
712 for e in $err
713 do
714 if test -n "$e"
715 then
716 echo >&2 "$e"
717 fi
718 done
719 IFS=$OIFS
720 exit 1
721 fi
722 }
723}
724
725set_name_rev () {
726 revname=$( (
727 clear_local_git_env
728 cd "$1" && {
729 git describe "$2" 2>/dev/null ||
730 git describe --tags "$2" 2>/dev/null ||
731 git describe --contains "$2" 2>/dev/null ||
732 git describe --all --always "$2"
733 }
734 ) )
735 test -z "$revname" || revname=" ($revname)"
736}
737#
738# Show commit summary for submodules in index or working tree
739#
740# If '--cached' is given, show summary between index and given commit,
741# or between working tree and given commit
742#
743# $@ = [commit (default 'HEAD'),] requested paths (default all)
744#
745cmd_summary() {
746 summary_limit=-1
747 for_status=
748 diff_cmd=diff-index
749
750 # parse $args after "submodule ... summary".
751 while test $# -ne 0
752 do
753 case "$1" in
754 --cached)
755 cached="$1"
756 ;;
757 --files)
758 files="$1"
759 ;;
760 --for-status)
761 for_status="$1"
762 ;;
763 -n|--summary-limit)
764 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
765 then
766 :
767 else
768 usage
769 fi
770 shift
771 ;;
772 --)
773 shift
774 break
775 ;;
776 -*)
777 usage
778 ;;
779 *)
780 break
781 ;;
782 esac
783 shift
784 done
785
786 test $summary_limit = 0 && return
787
788 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
789 then
790 head=$rev
791 test $# = 0 || shift
792 elif test -z "$1" -o "$1" = "HEAD"
793 then
794 # before the first commit: compare with an empty tree
795 head=$(git hash-object -w -t tree --stdin </dev/null)
796 test -z "$1" || shift
797 else
798 head="HEAD"
799 fi
800
801 if [ -n "$files" ]
802 then
803 test -n "$cached" &&
804 die "$(gettext "The --cached option cannot be used with the --files option")"
805 diff_cmd=diff-files
806 head=
807 fi
808
809 cd_to_toplevel
810 # Get modified modules cared by user
811 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
812 sane_egrep '^:([0-7]* )?160000' |
813 while read mod_src mod_dst sha1_src sha1_dst status name
814 do
815 # Always show modules deleted or type-changed (blob<->module)
816 test $status = D -o $status = T && echo "$name" && continue
817 # Also show added or modified modules which are checked out
818 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
819 echo "$name"
820 done
821 )
822
823 test -z "$modules" && return
824
825 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
826 sane_egrep '^:([0-7]* )?160000' |
827 cut -c2- |
828 while read mod_src mod_dst sha1_src sha1_dst status name
829 do
830 if test -z "$cached" &&
831 test $sha1_dst = 0000000000000000000000000000000000000000
832 then
833 case "$mod_dst" in
834 160000)
835 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
836 ;;
837 100644 | 100755 | 120000)
838 sha1_dst=$(git hash-object $name)
839 ;;
840 000000)
841 ;; # removed
842 *)
843 # unexpected type
844 eval_gettextln "unexpected mode \$mod_dst" >&2
845 continue ;;
846 esac
847 fi
848 missing_src=
849 missing_dst=
850
851 test $mod_src = 160000 &&
852 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
853 missing_src=t
854
855 test $mod_dst = 160000 &&
856 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
857 missing_dst=t
858
859 total_commits=
860 case "$missing_src,$missing_dst" in
861 t,)
862 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
863 ;;
864 ,t)
865 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
866 ;;
867 t,t)
868 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
869 ;;
870 *)
871 errmsg=
872 total_commits=$(
873 if test $mod_src = 160000 -a $mod_dst = 160000
874 then
875 range="$sha1_src...$sha1_dst"
876 elif test $mod_src = 160000
877 then
878 range=$sha1_src
879 else
880 range=$sha1_dst
881 fi
882 GIT_DIR="$name/.git" \
883 git rev-list --first-parent $range -- | wc -l
884 )
885 total_commits=" ($(($total_commits + 0)))"
886 ;;
887 esac
888
889 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
890 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
891 if test $status = T
892 then
893 blob="$(gettext "blob")"
894 submodule="$(gettext "submodule")"
895 if test $mod_dst = 160000
896 then
897 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
898 else
899 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
900 fi
901 else
902 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
903 fi
904 if test -n "$errmsg"
905 then
906 # Don't give error msg for modification whose dst is not submodule
907 # i.e. deleted or changed to blob
908 test $mod_dst = 160000 && echo "$errmsg"
909 else
910 if test $mod_src = 160000 -a $mod_dst = 160000
911 then
912 limit=
913 test $summary_limit -gt 0 && limit="-$summary_limit"
914 GIT_DIR="$name/.git" \
915 git log $limit --pretty='format: %m %s' \
916 --first-parent $sha1_src...$sha1_dst
917 elif test $mod_dst = 160000
918 then
919 GIT_DIR="$name/.git" \
920 git log --pretty='format: > %s' -1 $sha1_dst
921 else
922 GIT_DIR="$name/.git" \
923 git log --pretty='format: < %s' -1 $sha1_src
924 fi
925 echo
926 fi
927 echo
928 done |
929 if test -n "$for_status"; then
930 if [ -n "$files" ]; then
931 gettextln "# Submodules changed but not updated:"
932 else
933 gettextln "# Submodule changes to be committed:"
934 fi
935 echo "#"
936 sed -e 's|^|# |' -e 's|^# $|#|'
937 else
938 cat
939 fi
940}
941#
942# List all submodules, prefixed with:
943# - submodule not initialized
944# + different revision checked out
945#
946# If --cached was specified the revision in the index will be printed
947# instead of the currently checked out revision.
948#
949# $@ = requested paths (default to all)
950#
951cmd_status()
952{
953 # parse $args after "submodule ... status".
954 orig_flags=
955 while test $# -ne 0
956 do
957 case "$1" in
958 -q|--quiet)
959 GIT_QUIET=1
960 ;;
961 --cached)
962 cached=1
963 ;;
964 --recursive)
965 recursive=1
966 ;;
967 --)
968 shift
969 break
970 ;;
971 -*)
972 usage
973 ;;
974 *)
975 break
976 ;;
977 esac
978 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
979 shift
980 done
981
982 module_list "$@" |
983 while read mode sha1 stage sm_path
984 do
985 die_if_unmatched "$mode"
986 name=$(module_name "$sm_path") || exit
987 url=$(git config submodule."$name".url)
988 displaypath="$prefix$sm_path"
989 if test "$stage" = U
990 then
991 say "U$sha1 $displaypath"
992 continue
993 fi
994 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
995 then
996 say "-$sha1 $displaypath"
997 continue;
998 fi
999 set_name_rev "$sm_path" "$sha1"
1000 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1001 then
1002 say " $sha1 $displaypath$revname"
1003 else
1004 if test -z "$cached"
1005 then
1006 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1007 set_name_rev "$sm_path" "$sha1"
1008 fi
1009 say "+$sha1 $displaypath$revname"
1010 fi
1011
1012 if test -n "$recursive"
1013 then
1014 (
1015 prefix="$displaypath/"
1016 clear_local_git_env
1017 cd "$sm_path" &&
1018 eval cmd_status "$orig_args"
1019 ) ||
1020 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1021 fi
1022 done
1023}
1024#
1025# Sync remote urls for submodules
1026# This makes the value for remote.$remote.url match the value
1027# specified in .gitmodules.
1028#
1029cmd_sync()
1030{
1031 while test $# -ne 0
1032 do
1033 case "$1" in
1034 -q|--quiet)
1035 GIT_QUIET=1
1036 shift
1037 ;;
1038 --)
1039 shift
1040 break
1041 ;;
1042 -*)
1043 usage
1044 ;;
1045 *)
1046 break
1047 ;;
1048 esac
1049 done
1050 cd_to_toplevel
1051 module_list "$@" |
1052 while read mode sha1 stage sm_path
1053 do
1054 die_if_unmatched "$mode"
1055 name=$(module_name "$sm_path")
1056 url=$(git config -f .gitmodules --get submodule."$name".url)
1057
1058 # Possibly a url relative to parent
1059 case "$url" in
1060 ./*|../*)
1061 # rewrite foo/bar as ../.. to find path from
1062 # submodule work tree to superproject work tree
1063 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1064 # guarantee a trailing /
1065 up_path=${up_path%/}/ &&
1066 # path from submodule work tree to submodule origin repo
1067 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1068 # path from superproject work tree to submodule origin repo
1069 super_config_url=$(resolve_relative_url "$url") || exit
1070 ;;
1071 *)
1072 sub_origin_url="$url"
1073 super_config_url="$url"
1074 ;;
1075 esac
1076
1077 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1078 then
1079 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
1080 git config submodule."$name".url "$super_config_url"
1081
1082 if test -e "$sm_path"/.git
1083 then
1084 (
1085 clear_local_git_env
1086 cd "$sm_path"
1087 remote=$(get_default_remote)
1088 git config remote."$remote".url "$sub_origin_url"
1089 )
1090 fi
1091 fi
1092 done
1093}
1094
1095# This loop parses the command line arguments to find the
1096# subcommand name to dispatch. Parsing of the subcommand specific
1097# options are primarily done by the subcommand implementations.
1098# Subcommand specific options such as --branch and --cached are
1099# parsed here as well, for backward compatibility.
1100
1101while test $# != 0 && test -z "$command"
1102do
1103 case "$1" in
1104 add | foreach | init | update | status | summary | sync)
1105 command=$1
1106 ;;
1107 -q|--quiet)
1108 GIT_QUIET=1
1109 ;;
1110 -b|--branch)
1111 case "$2" in
1112 '')
1113 usage
1114 ;;
1115 esac
1116 branch="$2"; shift
1117 ;;
1118 --cached)
1119 cached="$1"
1120 ;;
1121 --)
1122 break
1123 ;;
1124 -*)
1125 usage
1126 ;;
1127 *)
1128 break
1129 ;;
1130 esac
1131 shift
1132done
1133
1134# No command word defaults to "status"
1135if test -z "$command"
1136then
1137 if test $# = 0
1138 then
1139 command=status
1140 else
1141 usage
1142 fi
1143fi
1144
1145# "-b branch" is accepted only by "add"
1146if test -n "$branch" && test "$command" != add
1147then
1148 usage
1149fi
1150
1151# "--cached" is accepted only by "status" and "summary"
1152if test -n "$cached" && test "$command" != status -a "$command" != summary
1153then
1154 usage
1155fi
1156
1157"cmd_$command" "$@"