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