Add --depth to submodule update/add
[git.git] / git-submodule.sh
blob2458e1fcfba9b5615f26e45fd03c9ec558a1be96
1 #!/bin/sh
3 # git-submodule.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] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] deinit [-f|--force] [--] <path>...
12 or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
13 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
14 or: $dashless [--quiet] foreach [--recursive] <command>
15 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
16 OPTIONS_SPEC=
17 . git-sh-setup
18 . git-sh-i18n
19 . git-parse-remote
20 require_work_tree
22 command=
23 branch=
24 force=
25 reference=
26 cached=
27 recursive=
28 init=
29 files=
30 remote=
31 nofetch=
32 update=
33 prefix=
34 custom_name=
35 depth=
37 # The function takes at most 2 arguments. The first argument is the
38 # URL that navigates to the submodule origin repo. When relative, this URL
39 # is relative to the superproject origin URL repo. The second up_path
40 # argument, if specified, is the relative path that navigates
41 # from the submodule working tree to the superproject working tree.
43 # The output of the function is the origin URL of the submodule.
45 # The output will either be an absolute URL or filesystem path (if the
46 # superproject origin URL is an absolute URL or filesystem path,
47 # respectively) or a relative file system path (if the superproject
48 # origin URL is a relative file system path).
50 # When the output is a relative file system path, the path is either
51 # relative to the submodule working tree, if up_path is specified, or to
52 # the superproject working tree otherwise.
53 resolve_relative_url ()
55 remote=$(get_default_remote)
56 remoteurl=$(git config "remote.$remote.url") ||
57 remoteurl=$(pwd) # the repository is its own authoritative upstream
58 url="$1"
59 remoteurl=${remoteurl%/}
60 sep=/
61 up_path="$2"
63 case "$remoteurl" in
64 *:*|/*)
65 is_relative=
67 ./*|../*)
68 is_relative=t
71 is_relative=t
72 remoteurl="./$remoteurl"
74 esac
76 while test -n "$url"
78 case "$url" in
79 ../*)
80 url="${url#../}"
81 case "$remoteurl" in
82 */*)
83 remoteurl="${remoteurl%/*}"
85 *:*)
86 remoteurl="${remoteurl%:*}"
87 sep=:
90 if test -z "$is_relative" || test "." = "$remoteurl"
91 then
92 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
93 else
94 remoteurl=.
97 esac
99 ./*)
100 url="${url#./}"
103 break;;
104 esac
105 done
106 remoteurl="$remoteurl$sep${url%/}"
107 echo "${is_relative:+${up_path}}${remoteurl#./}"
111 # Get submodule info for registered submodules
112 # $@ = path to limit submodule list
114 module_list()
117 git ls-files --error-unmatch --stage -- "$@" ||
118 echo "unmatched pathspec exists"
120 perl -e '
121 my %unmerged = ();
122 my ($null_sha1) = ("0" x 40);
123 my @out = ();
124 my $unmatched = 0;
125 while (<STDIN>) {
126 if (/^unmatched pathspec/) {
127 $unmatched = 1;
128 next;
130 chomp;
131 my ($mode, $sha1, $stage, $path) =
132 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
133 next unless $mode eq "160000";
134 if ($stage ne "0") {
135 if (!$unmerged{$path}++) {
136 push @out, "$mode $null_sha1 U\t$path\n";
138 next;
140 push @out, "$_\n";
142 if ($unmatched) {
143 print "#unmatched\n";
144 } else {
145 print for (@out);
150 die_if_unmatched ()
152 if test "$1" = "#unmatched"
153 then
154 exit 1
159 # Print a submodule configuration setting
161 # $1 = submodule name
162 # $2 = option name
163 # $3 = default value
165 # Checks in the usual git-config places first (for overrides),
166 # otherwise it falls back on .gitmodules. This allows you to
167 # distribute project-wide defaults in .gitmodules, while still
168 # customizing individual repositories if necessary. If the option is
169 # not in .gitmodules either, print a default value.
171 get_submodule_config () {
172 name="$1"
173 option="$2"
174 default="$3"
175 value=$(git config submodule."$name"."$option")
176 if test -z "$value"
177 then
178 value=$(git config -f .gitmodules submodule."$name"."$option")
180 printf '%s' "${value:-$default}"
185 # Map submodule path to submodule name
187 # $1 = path
189 module_name()
191 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
192 sm_path="$1"
193 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
194 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
195 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
196 test -z "$name" &&
197 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
198 echo "$name"
202 # Clone a submodule
204 # Prior to calling, cmd_update checks that a possibly existing
205 # path is not a git repository.
206 # Likewise, cmd_add checks that path does not exist at all,
207 # since it is the location of a new submodule.
209 module_clone()
211 sm_path=$1
212 name=$2
213 url=$3
214 reference="$4"
215 depth="$5"
216 quiet=
217 if test -n "$GIT_QUIET"
218 then
219 quiet=-q
222 gitdir=
223 gitdir_base=
224 base_name=$(dirname "$name")
226 gitdir=$(git rev-parse --git-dir)
227 gitdir_base="$gitdir/modules/$base_name"
228 gitdir="$gitdir/modules/$name"
230 if test -d "$gitdir"
231 then
232 mkdir -p "$sm_path"
233 rm -f "$gitdir/index"
234 else
235 mkdir -p "$gitdir_base"
237 clear_local_git_env
238 git clone $quiet ${depth:+"$depth"} -n ${reference:+"$reference"} \
239 --separate-git-dir "$gitdir" "$url" "$sm_path"
240 ) ||
241 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
244 # We already are at the root of the work tree but cd_to_toplevel will
245 # resolve any symlinks that might be present in $PWD
246 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
247 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
248 # normalize Windows-style absolute paths to POSIX-style absolute paths
249 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
250 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
251 # Remove all common leading directories after a sanity check
252 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
253 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
255 while test "${a%%/*}" = "${b%%/*}"
257 a=${a#*/}
258 b=${b#*/}
259 done
260 # Now chop off the trailing '/'s that were added in the beginning
261 a=${a%/}
262 b=${b%/}
264 # Turn each leading "*/" component into "../"
265 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
266 echo "gitdir: $rel/$a" >"$sm_path/.git"
268 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
269 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
272 isnumber()
274 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
278 # Add a new submodule to the working tree, .gitmodules and the index
280 # $@ = repo path
282 # optional branch is stored in global branch variable
284 cmd_add()
286 # parse $args after "submodule ... add".
287 while test $# -ne 0
289 case "$1" in
290 -b | --branch)
291 case "$2" in '') usage ;; esac
292 branch=$2
293 shift
295 -f | --force)
296 force=$1
298 -q|--quiet)
299 GIT_QUIET=1
301 --reference)
302 case "$2" in '') usage ;; esac
303 reference="--reference=$2"
304 shift
306 --reference=*)
307 reference="$1"
309 --name)
310 case "$2" in '') usage ;; esac
311 custom_name=$2
312 shift
314 --depth)
315 case "$2" in '') usage ;; esac
316 depth="--depth=$2"
317 shift
319 --depth=*)
320 depth=$1
323 shift
324 break
327 usage
330 break
332 esac
333 shift
334 done
336 repo=$1
337 sm_path=$2
339 if test -z "$sm_path"; then
340 sm_path=$(echo "$repo" |
341 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
344 if test -z "$repo" -o -z "$sm_path"; then
345 usage
348 # assure repo is absolute or relative to parent
349 case "$repo" in
350 ./*|../*)
351 # dereference source url relative to parent's url
352 realrepo=$(resolve_relative_url "$repo") || exit
354 *:*|/*)
355 # absolute url
356 realrepo=$repo
359 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
361 esac
363 # normalize path:
364 # multiple //; leading ./; /./; /../; trailing /
365 sm_path=$(printf '%s/\n' "$sm_path" |
366 sed -e '
367 s|//*|/|g
368 s|^\(\./\)*||
369 s|/\./|/|g
370 :start
371 s|\([^/]*\)/\.\./||
372 tstart
373 s|/*$||
375 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
376 die "$(eval_gettext "'\$sm_path' already exists in the index")"
378 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
379 then
380 eval_gettextln "The following path is ignored by one of your .gitignore files:
381 \$sm_path
382 Use -f if you really want to add it." >&2
383 exit 1
386 if test -n "$custom_name"
387 then
388 sm_name="$custom_name"
389 else
390 sm_name="$sm_path"
393 # perhaps the path exists and is already a git repo, else clone it
394 if test -e "$sm_path"
395 then
396 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
397 then
398 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
399 else
400 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
403 else
404 if test -d ".git/modules/$sm_name"
405 then
406 if test -z "$force"
407 then
408 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
409 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
410 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
411 echo >&2 " $realrepo"
412 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
413 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
414 else
415 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
418 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" || exit
420 clear_local_git_env
421 cd "$sm_path" &&
422 # ash fails to wordsplit ${branch:+-b "$branch"...}
423 case "$branch" in
424 '') git checkout -f -q ;;
425 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
426 esac
427 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
429 git config submodule."$sm_name".url "$realrepo"
431 git add $force "$sm_path" ||
432 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
434 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
435 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
436 if test -n "$branch"
437 then
438 git config -f .gitmodules submodule."$sm_name".branch "$branch"
439 fi &&
440 git add --force .gitmodules ||
441 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
445 # Execute an arbitrary command sequence in each checked out
446 # submodule
448 # $@ = command to execute
450 cmd_foreach()
452 # parse $args after "submodule ... foreach".
453 while test $# -ne 0
455 case "$1" in
456 -q|--quiet)
457 GIT_QUIET=1
459 --recursive)
460 recursive=1
463 usage
466 break
468 esac
469 shift
470 done
472 toplevel=$(pwd)
474 # dup stdin so that it can be restored when running the external
475 # command in the subshell (and a recursive call to this function)
476 exec 3<&0
478 module_list |
479 while read mode sha1 stage sm_path
481 die_if_unmatched "$mode"
482 if test -e "$sm_path"/.git
483 then
484 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
485 name=$(module_name "$sm_path")
487 prefix="$prefix$sm_path/"
488 clear_local_git_env
489 # we make $path available to scripts ...
490 path=$sm_path
491 cd "$sm_path" &&
492 eval "$@" &&
493 if test -n "$recursive"
494 then
495 cmd_foreach "--recursive" "$@"
497 ) <&3 3<&- ||
498 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
500 done
504 # Register submodules in .git/config
506 # $@ = requested paths (default to all)
508 cmd_init()
510 # parse $args after "submodule ... init".
511 while test $# -ne 0
513 case "$1" in
514 -q|--quiet)
515 GIT_QUIET=1
518 shift
519 break
522 usage
525 break
527 esac
528 shift
529 done
531 module_list "$@" |
532 while read mode sha1 stage sm_path
534 die_if_unmatched "$mode"
535 name=$(module_name "$sm_path") || exit
537 # Copy url setting when it is not set yet
538 if test -z "$(git config "submodule.$name.url")"
539 then
540 url=$(git config -f .gitmodules submodule."$name".url)
541 test -z "$url" &&
542 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
544 # Possibly a url relative to parent
545 case "$url" in
546 ./*|../*)
547 url=$(resolve_relative_url "$url") || exit
549 esac
550 git config submodule."$name".url "$url" ||
551 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
553 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
556 # Copy "update" setting when it is not set yet
557 upd="$(git config -f .gitmodules submodule."$name".update)"
558 test -z "$upd" ||
559 test -n "$(git config submodule."$name".update)" ||
560 git config submodule."$name".update "$upd" ||
561 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
562 done
566 # Unregister submodules from .git/config and remove their work tree
568 # $@ = requested paths (use '.' to deinit all submodules)
570 cmd_deinit()
572 # parse $args after "submodule ... deinit".
573 while test $# -ne 0
575 case "$1" in
576 -f|--force)
577 force=$1
579 -q|--quiet)
580 GIT_QUIET=1
583 shift
584 break
587 usage
590 break
592 esac
593 shift
594 done
596 if test $# = 0
597 then
598 die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
601 module_list "$@" |
602 while read mode sha1 stage sm_path
604 die_if_unmatched "$mode"
605 name=$(module_name "$sm_path") || exit
607 # Remove the submodule work tree (unless the user already did it)
608 if test -d "$sm_path"
609 then
610 # Protect submodules containing a .git directory
611 if test -d "$sm_path/.git"
612 then
613 echo >&2 "$(eval_gettext "Submodule work tree '\$sm_path' contains a .git directory")"
614 die "$(eval_gettext "(use 'rm -rf' if you really want to remove it including all of its history)")"
617 if test -z "$force"
618 then
619 git rm -qn "$sm_path" ||
620 die "$(eval_gettext "Submodule work tree '\$sm_path' contains local modifications; use '-f' to discard them")"
622 rm -rf "$sm_path" &&
623 say "$(eval_gettext "Cleared directory '\$sm_path'")" ||
624 say "$(eval_gettext "Could not remove submodule work tree '\$sm_path'")"
627 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$sm_path'")"
629 # Remove the .git/config entries (unless the user already did it)
630 if test -n "$(git config --get-regexp submodule."$name\.")"
631 then
632 # Remove the whole section so we have a clean state when
633 # the user later decides to init this submodule again
634 url=$(git config submodule."$name".url)
635 git config --remove-section submodule."$name" 2>/dev/null &&
636 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$sm_path'")"
638 done
642 # Update each submodule path to correct revision, using clone and checkout as needed
644 # $@ = requested paths (default to all)
646 cmd_update()
648 # parse $args after "submodule ... update".
649 orig_flags=
650 while test $# -ne 0
652 case "$1" in
653 -q|--quiet)
654 GIT_QUIET=1
656 -i|--init)
657 init=1
659 --remote)
660 remote=1
662 -N|--no-fetch)
663 nofetch=1
665 -f|--force)
666 force=$1
668 -r|--rebase)
669 update="rebase"
671 --reference)
672 case "$2" in '') usage ;; esac
673 reference="--reference=$2"
674 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
675 shift
677 --reference=*)
678 reference="$1"
680 -m|--merge)
681 update="merge"
683 --recursive)
684 recursive=1
686 --checkout)
687 update="checkout"
689 --depth)
690 case "$2" in '') usage ;; esac
691 depth="--depth=$2"
692 shift
694 --depth=*)
695 depth=$1
698 shift
699 break
702 usage
705 break
707 esac
708 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
709 shift
710 done
712 if test -n "$init"
713 then
714 cmd_init "--" "$@" || return
717 cloned_modules=
718 module_list "$@" | {
719 err=
720 while read mode sha1 stage sm_path
722 die_if_unmatched "$mode"
723 if test "$stage" = U
724 then
725 echo >&2 "Skipping unmerged submodule $prefix$sm_path"
726 continue
728 name=$(module_name "$sm_path") || exit
729 url=$(git config submodule."$name".url)
730 branch=$(get_submodule_config "$name" branch master)
731 if ! test -z "$update"
732 then
733 update_module=$update
734 else
735 update_module=$(git config submodule."$name".update)
738 if test "$update_module" = "none"
739 then
740 echo "Skipping submodule '$prefix$sm_path'"
741 continue
744 if test -z "$url"
745 then
746 # Only mention uninitialized submodules when its
747 # path have been specified
748 test "$#" != "0" &&
749 say "$(eval_gettext "Submodule path '\$prefix\$sm_path' not initialized
750 Maybe you want to use 'update --init'?")"
751 continue
754 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
755 then
756 module_clone "$sm_path" "$name" "$url" "$reference" "$depth" || exit
757 cloned_modules="$cloned_modules;$name"
758 subsha1=
759 else
760 subsha1=$(clear_local_git_env; cd "$sm_path" &&
761 git rev-parse --verify HEAD) ||
762 die "$(eval_gettext "Unable to find current revision in submodule path '\$prefix\$sm_path'")"
765 if test -n "$remote"
766 then
767 if test -z "$nofetch"
768 then
769 # Fetch remote before determining tracking $sha1
770 (clear_local_git_env; cd "$sm_path" && git-fetch) ||
771 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
773 remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
774 sha1=$(clear_local_git_env; cd "$sm_path" &&
775 git rev-parse --verify "${remote_name}/${branch}") ||
776 die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
779 if test "$subsha1" != "$sha1" -o -n "$force"
780 then
781 subforce=$force
782 # If we don't already have a -f flag and the submodule has never been checked out
783 if test -z "$subsha1" -a -z "$force"
784 then
785 subforce="-f"
788 if test -z "$nofetch"
789 then
790 # Run fetch only if $sha1 isn't present or it
791 # is not reachable from a ref.
792 (clear_local_git_env; cd "$sm_path" &&
793 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
794 test -z "$rev") || git-fetch)) ||
795 die "$(eval_gettext "Unable to fetch in submodule path '\$prefix\$sm_path'")"
798 # Is this something we just cloned?
799 case ";$cloned_modules;" in
800 *";$name;"*)
801 # then there is no local change to integrate
802 update_module= ;;
803 esac
805 must_die_on_failure=
806 case "$update_module" in
807 rebase)
808 command="git rebase"
809 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$prefix\$sm_path'")"
810 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': rebased into '\$sha1'")"
811 must_die_on_failure=yes
813 merge)
814 command="git merge"
815 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$prefix\$sm_path'")"
816 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': merged in '\$sha1'")"
817 must_die_on_failure=yes
820 command="git checkout $subforce -q"
821 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$prefix\$sm_path'")"
822 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': checked out '\$sha1'")"
824 esac
826 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
827 then
828 say "$say_msg"
829 elif test -n "$must_die_on_failure"
830 then
831 die_with_status 2 "$die_msg"
832 else
833 err="${err};$die_msg"
834 continue
838 if test -n "$recursive"
839 then
841 prefix="$prefix$sm_path/"
842 clear_local_git_env
843 cd "$sm_path" &&
844 eval cmd_update "$orig_flags"
846 res=$?
847 if test $res -gt 0
848 then
849 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$prefix\$sm_path'")"
850 if test $res -eq 1
851 then
852 err="${err};$die_msg"
853 continue
854 else
855 die_with_status $res "$die_msg"
859 done
861 if test -n "$err"
862 then
863 OIFS=$IFS
864 IFS=';'
865 for e in $err
867 if test -n "$e"
868 then
869 echo >&2 "$e"
871 done
872 IFS=$OIFS
873 exit 1
878 set_name_rev () {
879 revname=$( (
880 clear_local_git_env
881 cd "$1" && {
882 git describe "$2" 2>/dev/null ||
883 git describe --tags "$2" 2>/dev/null ||
884 git describe --contains "$2" 2>/dev/null ||
885 git describe --all --always "$2"
888 test -z "$revname" || revname=" ($revname)"
891 # Show commit summary for submodules in index or working tree
893 # If '--cached' is given, show summary between index and given commit,
894 # or between working tree and given commit
896 # $@ = [commit (default 'HEAD'),] requested paths (default all)
898 cmd_summary() {
899 summary_limit=-1
900 for_status=
901 diff_cmd=diff-index
903 # parse $args after "submodule ... summary".
904 while test $# -ne 0
906 case "$1" in
907 --cached)
908 cached="$1"
910 --files)
911 files="$1"
913 --for-status)
914 for_status="$1"
916 -n|--summary-limit)
917 summary_limit="$2"
918 isnumber "$summary_limit" || usage
919 shift
921 --summary-limit=*)
922 summary_limit="${1#--summary-limit=}"
923 isnumber "$summary_limit" || usage
926 shift
927 break
930 usage
933 break
935 esac
936 shift
937 done
939 test $summary_limit = 0 && return
941 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
942 then
943 head=$rev
944 test $# = 0 || shift
945 elif test -z "$1" -o "$1" = "HEAD"
946 then
947 # before the first commit: compare with an empty tree
948 head=$(git hash-object -w -t tree --stdin </dev/null)
949 test -z "$1" || shift
950 else
951 head="HEAD"
954 if [ -n "$files" ]
955 then
956 test -n "$cached" &&
957 die "$(gettext "The --cached option cannot be used with the --files option")"
958 diff_cmd=diff-files
959 head=
962 cd_to_toplevel
963 # Get modified modules cared by user
964 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
965 sane_egrep '^:([0-7]* )?160000' |
966 while read mod_src mod_dst sha1_src sha1_dst status name
968 # Always show modules deleted or type-changed (blob<->module)
969 test $status = D -o $status = T && echo "$name" && continue
970 # Also show added or modified modules which are checked out
971 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
972 echo "$name"
973 done
976 test -z "$modules" && return
978 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
979 sane_egrep '^:([0-7]* )?160000' |
980 cut -c2- |
981 while read mod_src mod_dst sha1_src sha1_dst status name
983 if test -z "$cached" &&
984 test $sha1_dst = 0000000000000000000000000000000000000000
985 then
986 case "$mod_dst" in
987 160000)
988 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
990 100644 | 100755 | 120000)
991 sha1_dst=$(git hash-object $name)
993 000000)
994 ;; # removed
996 # unexpected type
997 eval_gettextln "unexpected mode \$mod_dst" >&2
998 continue ;;
999 esac
1001 missing_src=
1002 missing_dst=
1004 test $mod_src = 160000 &&
1005 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1006 missing_src=t
1008 test $mod_dst = 160000 &&
1009 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1010 missing_dst=t
1012 total_commits=
1013 case "$missing_src,$missing_dst" in
1015 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
1018 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
1020 t,t)
1021 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
1024 errmsg=
1025 total_commits=$(
1026 if test $mod_src = 160000 -a $mod_dst = 160000
1027 then
1028 range="$sha1_src...$sha1_dst"
1029 elif test $mod_src = 160000
1030 then
1031 range=$sha1_src
1032 else
1033 range=$sha1_dst
1035 GIT_DIR="$name/.git" \
1036 git rev-list --first-parent $range -- | wc -l
1038 total_commits=" ($(($total_commits + 0)))"
1040 esac
1042 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
1043 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
1044 if test $status = T
1045 then
1046 blob="$(gettext "blob")"
1047 submodule="$(gettext "submodule")"
1048 if test $mod_dst = 160000
1049 then
1050 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1051 else
1052 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1054 else
1055 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1057 if test -n "$errmsg"
1058 then
1059 # Don't give error msg for modification whose dst is not submodule
1060 # i.e. deleted or changed to blob
1061 test $mod_dst = 160000 && echo "$errmsg"
1062 else
1063 if test $mod_src = 160000 -a $mod_dst = 160000
1064 then
1065 limit=
1066 test $summary_limit -gt 0 && limit="-$summary_limit"
1067 GIT_DIR="$name/.git" \
1068 git log $limit --pretty='format: %m %s' \
1069 --first-parent $sha1_src...$sha1_dst
1070 elif test $mod_dst = 160000
1071 then
1072 GIT_DIR="$name/.git" \
1073 git log --pretty='format: > %s' -1 $sha1_dst
1074 else
1075 GIT_DIR="$name/.git" \
1076 git log --pretty='format: < %s' -1 $sha1_src
1078 echo
1080 echo
1081 done |
1082 if test -n "$for_status"; then
1083 if [ -n "$files" ]; then
1084 gettextln "Submodules changed but not updated:" | git stripspace -c
1085 else
1086 gettextln "Submodule changes to be committed:" | git stripspace -c
1088 printf "\n" | git stripspace -c
1089 git stripspace -c
1090 else
1095 # List all submodules, prefixed with:
1096 # - submodule not initialized
1097 # + different revision checked out
1099 # If --cached was specified the revision in the index will be printed
1100 # instead of the currently checked out revision.
1102 # $@ = requested paths (default to all)
1104 cmd_status()
1106 # parse $args after "submodule ... status".
1107 while test $# -ne 0
1109 case "$1" in
1110 -q|--quiet)
1111 GIT_QUIET=1
1113 --cached)
1114 cached=1
1116 --recursive)
1117 recursive=1
1120 shift
1121 break
1124 usage
1127 break
1129 esac
1130 shift
1131 done
1133 module_list "$@" |
1134 while read mode sha1 stage sm_path
1136 die_if_unmatched "$mode"
1137 name=$(module_name "$sm_path") || exit
1138 url=$(git config submodule."$name".url)
1139 displaypath="$prefix$sm_path"
1140 if test "$stage" = U
1141 then
1142 say "U$sha1 $displaypath"
1143 continue
1145 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
1146 then
1147 say "-$sha1 $displaypath"
1148 continue;
1150 set_name_rev "$sm_path" "$sha1"
1151 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1152 then
1153 say " $sha1 $displaypath$revname"
1154 else
1155 if test -z "$cached"
1156 then
1157 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1158 set_name_rev "$sm_path" "$sha1"
1160 say "+$sha1 $displaypath$revname"
1163 if test -n "$recursive"
1164 then
1166 prefix="$displaypath/"
1167 clear_local_git_env
1168 cd "$sm_path" &&
1169 eval cmd_status
1170 ) ||
1171 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1173 done
1176 # Sync remote urls for submodules
1177 # This makes the value for remote.$remote.url match the value
1178 # specified in .gitmodules.
1180 cmd_sync()
1182 while test $# -ne 0
1184 case "$1" in
1185 -q|--quiet)
1186 GIT_QUIET=1
1187 shift
1189 --recursive)
1190 recursive=1
1191 shift
1194 shift
1195 break
1198 usage
1201 break
1203 esac
1204 done
1205 cd_to_toplevel
1206 module_list "$@" |
1207 while read mode sha1 stage sm_path
1209 die_if_unmatched "$mode"
1210 name=$(module_name "$sm_path")
1211 url=$(git config -f .gitmodules --get submodule."$name".url)
1213 # Possibly a url relative to parent
1214 case "$url" in
1215 ./*|../*)
1216 # rewrite foo/bar as ../.. to find path from
1217 # submodule work tree to superproject work tree
1218 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1219 # guarantee a trailing /
1220 up_path=${up_path%/}/ &&
1221 # path from submodule work tree to submodule origin repo
1222 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1223 # path from superproject work tree to submodule origin repo
1224 super_config_url=$(resolve_relative_url "$url") || exit
1227 sub_origin_url="$url"
1228 super_config_url="$url"
1230 esac
1232 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1233 then
1234 say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")"
1235 git config submodule."$name".url "$super_config_url"
1237 if test -e "$sm_path"/.git
1238 then
1240 clear_local_git_env
1241 cd "$sm_path"
1242 remote=$(get_default_remote)
1243 git config remote."$remote".url "$sub_origin_url"
1245 if test -n "$recursive"
1246 then
1247 prefix="$prefix$sm_path/"
1248 eval cmd_sync
1253 done
1256 # This loop parses the command line arguments to find the
1257 # subcommand name to dispatch. Parsing of the subcommand specific
1258 # options are primarily done by the subcommand implementations.
1259 # Subcommand specific options such as --branch and --cached are
1260 # parsed here as well, for backward compatibility.
1262 while test $# != 0 && test -z "$command"
1264 case "$1" in
1265 add | foreach | init | deinit | update | status | summary | sync)
1266 command=$1
1268 -q|--quiet)
1269 GIT_QUIET=1
1271 -b|--branch)
1272 case "$2" in
1274 usage
1276 esac
1277 branch="$2"; shift
1279 --cached)
1280 cached="$1"
1283 break
1286 usage
1289 break
1291 esac
1292 shift
1293 done
1295 # No command word defaults to "status"
1296 if test -z "$command"
1297 then
1298 if test $# = 0
1299 then
1300 command=status
1301 else
1302 usage
1306 # "-b branch" is accepted only by "add"
1307 if test -n "$branch" && test "$command" != add
1308 then
1309 usage
1312 # "--cached" is accepted only by "status" and "summary"
1313 if test -n "$cached" && test "$command" != status -a "$command" != summary
1314 then
1315 usage
1318 "cmd_$command" "$@"