i18n: submodule: join strings marked for translation
[git.git] / git-submodule.sh
blob5c7d25197774e1d48510bdafbdc6a50feec36443
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] (--all| [--] <path>...)
12 or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--reference <repository>] [--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 SUBDIRECTORY_OK=Yes
18 . git-sh-setup
19 . git-parse-remote
20 require_work_tree
21 wt_prefix=$(git rev-parse --show-prefix)
22 cd_to_toplevel
24 # Restrict ourselves to a vanilla subset of protocols; the URLs
25 # we get are under control of a remote repository, and we do not
26 # want them kicking off arbitrary git-remote-* programs.
28 # If the user has already specified a set of allowed protocols,
29 # we assume they know what they're doing and use that instead.
30 : ${GIT_ALLOW_PROTOCOL=file:git:http:https:ssh}
31 export GIT_ALLOW_PROTOCOL
33 command=
34 branch=
35 force=
36 reference=
37 cached=
38 recursive=
39 init=
40 files=
41 remote=
42 nofetch=
43 update=
44 prefix=
45 custom_name=
46 depth=
48 # Resolve a path to be relative to another path. This is intended for
49 # converting submodule paths when git-submodule is run in a subdirectory
50 # and only handles paths where the directory separator is '/'.
52 # The output is the first argument as a path relative to the second argument,
53 # which defaults to $wt_prefix if it is omitted.
54 relative_path ()
56 local target curdir result
57 target=$1
58 curdir=${2-$wt_prefix}
59 curdir=${curdir%/}
60 result=
62 while test -n "$curdir"
64 case "$target" in
65 "$curdir/"*)
66 target=${target#"$curdir"/}
67 break
69 esac
71 result="${result}../"
72 if test "$curdir" = "${curdir%/*}"
73 then
74 curdir=
75 else
76 curdir="${curdir%/*}"
78 done
80 echo "$result$target"
83 die_if_unmatched ()
85 if test "$1" = "#unmatched"
86 then
87 exit 1
92 # Print a submodule configuration setting
94 # $1 = submodule name
95 # $2 = option name
96 # $3 = default value
98 # Checks in the usual git-config places first (for overrides),
99 # otherwise it falls back on .gitmodules. This allows you to
100 # distribute project-wide defaults in .gitmodules, while still
101 # customizing individual repositories if necessary. If the option is
102 # not in .gitmodules either, print a default value.
104 get_submodule_config () {
105 name="$1"
106 option="$2"
107 default="$3"
108 value=$(git config submodule."$name"."$option")
109 if test -z "$value"
110 then
111 value=$(git config -f .gitmodules submodule."$name"."$option")
113 printf '%s' "${value:-$default}"
116 isnumber()
118 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
121 # Sanitize the local git environment for use within a submodule. We
122 # can't simply use clear_local_git_env since we want to preserve some
123 # of the settings from GIT_CONFIG_PARAMETERS.
124 sanitize_submodule_env()
126 save_config=$GIT_CONFIG_PARAMETERS
127 clear_local_git_env
128 GIT_CONFIG_PARAMETERS=$save_config
129 export GIT_CONFIG_PARAMETERS
133 # Add a new submodule to the working tree, .gitmodules and the index
135 # $@ = repo path
137 # optional branch is stored in global branch variable
139 cmd_add()
141 # parse $args after "submodule ... add".
142 reference_path=
143 while test $# -ne 0
145 case "$1" in
146 -b | --branch)
147 case "$2" in '') usage ;; esac
148 branch=$2
149 shift
151 -f | --force)
152 force=$1
154 -q|--quiet)
155 GIT_QUIET=1
157 --reference)
158 case "$2" in '') usage ;; esac
159 reference_path=$2
160 shift
162 --reference=*)
163 reference_path="${1#--reference=}"
165 --name)
166 case "$2" in '') usage ;; esac
167 custom_name=$2
168 shift
170 --depth)
171 case "$2" in '') usage ;; esac
172 depth="--depth=$2"
173 shift
175 --depth=*)
176 depth=$1
179 shift
180 break
183 usage
186 break
188 esac
189 shift
190 done
192 if test -n "$reference_path"
193 then
194 is_absolute_path "$reference_path" ||
195 reference_path="$wt_prefix$reference_path"
197 reference="--reference=$reference_path"
200 repo=$1
201 sm_path=$2
203 if test -z "$sm_path"; then
204 sm_path=$(printf '%s\n' "$repo" |
205 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
208 if test -z "$repo" || test -z "$sm_path"; then
209 usage
212 is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
214 # assure repo is absolute or relative to parent
215 case "$repo" in
216 ./*|../*)
217 test -z "$wt_prefix" ||
218 die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
220 # dereference source url relative to parent's url
221 realrepo=$(git submodule--helper resolve-relative-url "$repo") || exit
223 *:*|/*)
224 # absolute url
225 realrepo=$repo
228 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
230 esac
232 # normalize path:
233 # multiple //; leading ./; /./; /../; trailing /
234 sm_path=$(printf '%s/\n' "$sm_path" |
235 sed -e '
236 s|//*|/|g
237 s|^\(\./\)*||
238 s|/\(\./\)*|/|g
239 :start
240 s|\([^/]*\)/\.\./||
241 tstart
242 s|/*$||
244 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
245 die "$(eval_gettext "'\$sm_path' already exists in the index")"
247 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
248 then
249 eval_gettextln "The following path is ignored by one of your .gitignore files:
250 \$sm_path
251 Use -f if you really want to add it." >&2
252 exit 1
255 if test -n "$custom_name"
256 then
257 sm_name="$custom_name"
258 else
259 sm_name="$sm_path"
262 # perhaps the path exists and is already a git repo, else clone it
263 if test -e "$sm_path"
264 then
265 if test -d "$sm_path"/.git || test -f "$sm_path"/.git
266 then
267 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
268 else
269 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
272 else
273 if test -d ".git/modules/$sm_name"
274 then
275 if test -z "$force"
276 then
277 eval_gettextln >&2 "A git directory for '\$sm_name' is found locally with remote(s):"
278 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
279 die "$(eval_gettextln "\
280 If you want to reuse this local git directory instead of cloning again from
281 \$realrepo
282 use the '--force' option. If the local git directory is not the correct repo
283 or you are unsure what this means choose another name with the '--name' option.")"
284 else
285 eval_gettextln "Reactivating local git directory for submodule '\$sm_name'."
288 git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${depth:+"$depth"} || exit
290 sanitize_submodule_env
291 cd "$sm_path" &&
292 # ash fails to wordsplit ${branch:+-b "$branch"...}
293 case "$branch" in
294 '') git checkout -f -q ;;
295 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
296 esac
297 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
299 git config submodule."$sm_name".url "$realrepo"
301 git add $force "$sm_path" ||
302 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
304 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
305 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
306 if test -n "$branch"
307 then
308 git config -f .gitmodules submodule."$sm_name".branch "$branch"
309 fi &&
310 git add --force .gitmodules ||
311 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
315 # Execute an arbitrary command sequence in each checked out
316 # submodule
318 # $@ = command to execute
320 cmd_foreach()
322 # parse $args after "submodule ... foreach".
323 while test $# -ne 0
325 case "$1" in
326 -q|--quiet)
327 GIT_QUIET=1
329 --recursive)
330 recursive=1
333 usage
336 break
338 esac
339 shift
340 done
342 toplevel=$(pwd)
344 # dup stdin so that it can be restored when running the external
345 # command in the subshell (and a recursive call to this function)
346 exec 3<&0
348 git submodule--helper list --prefix "$wt_prefix"|
349 while read mode sha1 stage sm_path
351 die_if_unmatched "$mode"
352 if test -e "$sm_path"/.git
353 then
354 displaypath=$(relative_path "$prefix$sm_path")
355 say "$(eval_gettext "Entering '\$displaypath'")"
356 name=$(git submodule--helper name "$sm_path")
358 prefix="$prefix$sm_path/"
359 sanitize_submodule_env
360 cd "$sm_path" &&
361 sm_path=$(relative_path "$sm_path") &&
362 # we make $path available to scripts ...
363 path=$sm_path &&
364 if test $# -eq 1
365 then
366 eval "$1"
367 else
368 "$@"
369 fi &&
370 if test -n "$recursive"
371 then
372 cmd_foreach "--recursive" "$@"
374 ) <&3 3<&- ||
375 die "$(eval_gettext "Stopping at '\$displaypath'; script returned non-zero status.")"
377 done
381 # Register submodules in .git/config
383 # $@ = requested paths (default to all)
385 cmd_init()
387 # parse $args after "submodule ... init".
388 while test $# -ne 0
390 case "$1" in
391 -q|--quiet)
392 GIT_QUIET=1
395 shift
396 break
399 usage
402 break
404 esac
405 shift
406 done
408 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} "$@"
412 # Unregister submodules from .git/config and remove their work tree
414 # $@ = requested paths (use '.' to deinit all submodules)
416 cmd_deinit()
418 # parse $args after "submodule ... deinit".
419 deinit_all=
420 while test $# -ne 0
422 case "$1" in
423 -f|--force)
424 force=$1
426 -q|--quiet)
427 GIT_QUIET=1
429 --all)
430 deinit_all=t
433 shift
434 break
437 usage
440 break
442 esac
443 shift
444 done
446 if test -n "$deinit_all" && test "$#" -ne 0
447 then
448 echo >&2 "$(eval_gettext "pathspec and --all are incompatible")"
449 usage
451 if test $# = 0 && test -z "$deinit_all"
452 then
453 die "$(eval_gettext "Use '--all' if you really want to deinitialize all submodules")"
456 git submodule--helper list --prefix "$wt_prefix" "$@" |
457 while read mode sha1 stage sm_path
459 die_if_unmatched "$mode"
460 name=$(git submodule--helper name "$sm_path") || exit
462 displaypath=$(relative_path "$sm_path")
464 # Remove the submodule work tree (unless the user already did it)
465 if test -d "$sm_path"
466 then
467 # Protect submodules containing a .git directory
468 if test -d "$sm_path/.git"
469 then
470 die "$(eval_gettext "\
471 Submodule work tree '\$displaypath' contains a .git directory
472 (use 'rm -rf' if you really want to remove it including all of its history)")"
475 if test -z "$force"
476 then
477 git rm -qn "$sm_path" ||
478 die "$(eval_gettext "Submodule work tree '\$displaypath' contains local modifications; use '-f' to discard them")"
480 rm -rf "$sm_path" &&
481 say "$(eval_gettext "Cleared directory '\$displaypath'")" ||
482 say "$(eval_gettext "Could not remove submodule work tree '\$displaypath'")"
485 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$displaypath'")"
487 # Remove the .git/config entries (unless the user already did it)
488 if test -n "$(git config --get-regexp submodule."$name\.")"
489 then
490 # Remove the whole section so we have a clean state when
491 # the user later decides to init this submodule again
492 url=$(git config submodule."$name".url)
493 git config --remove-section submodule."$name" 2>/dev/null &&
494 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$displaypath'")"
496 done
499 is_tip_reachable () (
500 sanitize_submodule_env &&
501 cd "$1" &&
502 rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
503 test -z "$rev"
506 fetch_in_submodule () (
507 sanitize_submodule_env &&
508 cd "$1" &&
509 case "$2" in
511 git fetch ;;
513 git fetch $(get_default_remote) "$2" ;;
514 esac
518 # Update each submodule path to correct revision, using clone and checkout as needed
520 # $@ = requested paths (default to all)
522 cmd_update()
524 # parse $args after "submodule ... update".
525 while test $# -ne 0
527 case "$1" in
528 -q|--quiet)
529 GIT_QUIET=1
531 -i|--init)
532 init=1
534 --remote)
535 remote=1
537 -N|--no-fetch)
538 nofetch=1
540 -f|--force)
541 force=$1
543 -r|--rebase)
544 update="rebase"
546 --reference)
547 case "$2" in '') usage ;; esac
548 reference="--reference=$2"
549 shift
551 --reference=*)
552 reference="$1"
554 -m|--merge)
555 update="merge"
557 --recursive)
558 recursive=1
560 --checkout)
561 update="checkout"
563 --depth)
564 case "$2" in '') usage ;; esac
565 depth="--depth=$2"
566 shift
568 --depth=*)
569 depth=$1
571 -j|--jobs)
572 case "$2" in '') usage ;; esac
573 jobs="--jobs=$2"
574 shift
576 --jobs=*)
577 jobs=$1
580 shift
581 break
584 usage
587 break
589 esac
590 shift
591 done
593 if test -n "$init"
594 then
595 cmd_init "--" "$@" || return
599 git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
600 ${wt_prefix:+--prefix "$wt_prefix"} \
601 ${prefix:+--recursive-prefix "$prefix"} \
602 ${update:+--update "$update"} \
603 ${reference:+--reference "$reference"} \
604 ${depth:+--depth "$depth"} \
605 ${jobs:+$jobs} \
606 "$@" || echo "#unmatched"
607 } | {
608 err=
609 while read mode sha1 stage just_cloned sm_path
611 die_if_unmatched "$mode"
613 name=$(git submodule--helper name "$sm_path") || exit
614 url=$(git config submodule."$name".url)
615 branch=$(get_submodule_config "$name" branch master)
616 if ! test -z "$update"
617 then
618 update_module=$update
619 else
620 update_module=$(git config submodule."$name".update)
621 if test -z "$update_module"
622 then
623 update_module="checkout"
627 displaypath=$(relative_path "$prefix$sm_path")
629 if test $just_cloned -eq 1
630 then
631 subsha1=
632 update_module=checkout
633 else
634 subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
635 git rev-parse --verify HEAD) ||
636 die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
639 if test -n "$remote"
640 then
641 if test -z "$nofetch"
642 then
643 # Fetch remote before determining tracking $sha1
644 (sanitize_submodule_env; cd "$sm_path" && git-fetch) ||
645 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
647 remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote)
648 sha1=$(sanitize_submodule_env; cd "$sm_path" &&
649 git rev-parse --verify "${remote_name}/${branch}") ||
650 die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
653 if test "$subsha1" != "$sha1" || test -n "$force"
654 then
655 subforce=$force
656 # If we don't already have a -f flag and the submodule has never been checked out
657 if test -z "$subsha1" && test -z "$force"
658 then
659 subforce="-f"
662 if test -z "$nofetch"
663 then
664 # Run fetch only if $sha1 isn't present or it
665 # is not reachable from a ref.
666 is_tip_reachable "$sm_path" "$sha1" ||
667 fetch_in_submodule "$sm_path" ||
668 die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
670 # Now we tried the usual fetch, but $sha1 may
671 # not be reachable from any of the refs
672 is_tip_reachable "$sm_path" "$sha1" ||
673 fetch_in_submodule "$sm_path" "$sha1" ||
674 die "$(eval_gettext "Fetched in submodule path '\$displaypath', but it did not contain $sha1. Direct fetching of that commit failed.")"
677 must_die_on_failure=
678 case "$update_module" in
679 checkout)
680 command="git checkout $subforce -q"
681 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
682 say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
684 rebase)
685 command="git rebase"
686 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
687 say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
688 must_die_on_failure=yes
690 merge)
691 command="git merge"
692 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
693 say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
694 must_die_on_failure=yes
697 command="${update_module#!}"
698 die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$displaypath'")"
699 say_msg="$(eval_gettext "Submodule path '\$displaypath': '\$command \$sha1'")"
700 must_die_on_failure=yes
703 die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
704 esac
706 if (sanitize_submodule_env; cd "$sm_path" && $command "$sha1")
707 then
708 say "$say_msg"
709 elif test -n "$must_die_on_failure"
710 then
711 die_with_status 2 "$die_msg"
712 else
713 err="${err};$die_msg"
714 continue
718 if test -n "$recursive"
719 then
721 prefix=$(relative_path "$prefix$sm_path/")
722 wt_prefix=
723 sanitize_submodule_env
724 cd "$sm_path" &&
725 eval cmd_update
727 res=$?
728 if test $res -gt 0
729 then
730 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
731 if test $res -eq 1
732 then
733 err="${err};$die_msg"
734 continue
735 else
736 die_with_status $res "$die_msg"
740 done
742 if test -n "$err"
743 then
744 OIFS=$IFS
745 IFS=';'
746 for e in $err
748 if test -n "$e"
749 then
750 echo >&2 "$e"
752 done
753 IFS=$OIFS
754 exit 1
759 set_name_rev () {
760 revname=$( (
761 sanitize_submodule_env
762 cd "$1" && {
763 git describe "$2" 2>/dev/null ||
764 git describe --tags "$2" 2>/dev/null ||
765 git describe --contains "$2" 2>/dev/null ||
766 git describe --all --always "$2"
769 test -z "$revname" || revname=" ($revname)"
772 # Show commit summary for submodules in index or working tree
774 # If '--cached' is given, show summary between index and given commit,
775 # or between working tree and given commit
777 # $@ = [commit (default 'HEAD'),] requested paths (default all)
779 cmd_summary() {
780 summary_limit=-1
781 for_status=
782 diff_cmd=diff-index
784 # parse $args after "submodule ... summary".
785 while test $# -ne 0
787 case "$1" in
788 --cached)
789 cached="$1"
791 --files)
792 files="$1"
794 --for-status)
795 for_status="$1"
797 -n|--summary-limit)
798 summary_limit="$2"
799 isnumber "$summary_limit" || usage
800 shift
802 --summary-limit=*)
803 summary_limit="${1#--summary-limit=}"
804 isnumber "$summary_limit" || usage
807 shift
808 break
811 usage
814 break
816 esac
817 shift
818 done
820 test $summary_limit = 0 && return
822 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
823 then
824 head=$rev
825 test $# = 0 || shift
826 elif test -z "$1" || test "$1" = "HEAD"
827 then
828 # before the first commit: compare with an empty tree
829 head=$(git hash-object -w -t tree --stdin </dev/null)
830 test -z "$1" || shift
831 else
832 head="HEAD"
835 if [ -n "$files" ]
836 then
837 test -n "$cached" &&
838 die "$(gettext "The --cached option cannot be used with the --files option")"
839 diff_cmd=diff-files
840 head=
843 cd_to_toplevel
844 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
845 # Get modified modules cared by user
846 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
847 sane_egrep '^:([0-7]* )?160000' |
848 while read mod_src mod_dst sha1_src sha1_dst status sm_path
850 # Always show modules deleted or type-changed (blob<->module)
851 if test "$status" = D || test "$status" = T
852 then
853 printf '%s\n' "$sm_path"
854 continue
856 # Respect the ignore setting for --for-status.
857 if test -n "$for_status"
858 then
859 name=$(git submodule--helper name "$sm_path")
860 ignore_config=$(get_submodule_config "$name" ignore none)
861 test $status != A && test $ignore_config = all && continue
863 # Also show added or modified modules which are checked out
864 GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
865 printf '%s\n' "$sm_path"
866 done
869 test -z "$modules" && return
871 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
872 sane_egrep '^:([0-7]* )?160000' |
873 cut -c2- |
874 while read mod_src mod_dst sha1_src sha1_dst status name
876 if test -z "$cached" &&
877 test $sha1_dst = 0000000000000000000000000000000000000000
878 then
879 case "$mod_dst" in
880 160000)
881 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
883 100644 | 100755 | 120000)
884 sha1_dst=$(git hash-object $name)
886 000000)
887 ;; # removed
889 # unexpected type
890 eval_gettextln "unexpected mode \$mod_dst" >&2
891 continue ;;
892 esac
894 missing_src=
895 missing_dst=
897 test $mod_src = 160000 &&
898 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
899 missing_src=t
901 test $mod_dst = 160000 &&
902 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
903 missing_dst=t
905 display_name=$(relative_path "$name")
907 total_commits=
908 case "$missing_src,$missing_dst" in
910 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_src")"
913 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_dst")"
915 t,t)
916 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
919 errmsg=
920 total_commits=$(
921 if test $mod_src = 160000 && test $mod_dst = 160000
922 then
923 range="$sha1_src...$sha1_dst"
924 elif test $mod_src = 160000
925 then
926 range=$sha1_src
927 else
928 range=$sha1_dst
930 GIT_DIR="$name/.git" \
931 git rev-list --first-parent $range -- | wc -l
933 total_commits=" ($(($total_commits + 0)))"
935 esac
937 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
938 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
939 if test $status = T
940 then
941 blob="$(gettext "blob")"
942 submodule="$(gettext "submodule")"
943 if test $mod_dst = 160000
944 then
945 echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
946 else
947 echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
949 else
950 echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
952 if test -n "$errmsg"
953 then
954 # Don't give error msg for modification whose dst is not submodule
955 # i.e. deleted or changed to blob
956 test $mod_dst = 160000 && echo "$errmsg"
957 else
958 if test $mod_src = 160000 && test $mod_dst = 160000
959 then
960 limit=
961 test $summary_limit -gt 0 && limit="-$summary_limit"
962 GIT_DIR="$name/.git" \
963 git log $limit --pretty='format: %m %s' \
964 --first-parent $sha1_src...$sha1_dst
965 elif test $mod_dst = 160000
966 then
967 GIT_DIR="$name/.git" \
968 git log --pretty='format: > %s' -1 $sha1_dst
969 else
970 GIT_DIR="$name/.git" \
971 git log --pretty='format: < %s' -1 $sha1_src
973 echo
975 echo
976 done
979 # List all submodules, prefixed with:
980 # - submodule not initialized
981 # + different revision checked out
983 # If --cached was specified the revision in the index will be printed
984 # instead of the currently checked out revision.
986 # $@ = requested paths (default to all)
988 cmd_status()
990 # parse $args after "submodule ... status".
991 while test $# -ne 0
993 case "$1" in
994 -q|--quiet)
995 GIT_QUIET=1
997 --cached)
998 cached=1
1000 --recursive)
1001 recursive=1
1004 shift
1005 break
1008 usage
1011 break
1013 esac
1014 shift
1015 done
1017 git submodule--helper list --prefix "$wt_prefix" "$@" |
1018 while read mode sha1 stage sm_path
1020 die_if_unmatched "$mode"
1021 name=$(git submodule--helper name "$sm_path") || exit
1022 url=$(git config submodule."$name".url)
1023 displaypath=$(relative_path "$prefix$sm_path")
1024 if test "$stage" = U
1025 then
1026 say "U$sha1 $displaypath"
1027 continue
1029 if test -z "$url" ||
1031 ! test -d "$sm_path"/.git &&
1032 ! test -f "$sm_path"/.git
1034 then
1035 say "-$sha1 $displaypath"
1036 continue;
1038 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1039 then
1040 set_name_rev "$sm_path" "$sha1"
1041 say " $sha1 $displaypath$revname"
1042 else
1043 if test -z "$cached"
1044 then
1045 sha1=$(sanitize_submodule_env; cd "$sm_path" && git rev-parse --verify HEAD)
1047 set_name_rev "$sm_path" "$sha1"
1048 say "+$sha1 $displaypath$revname"
1051 if test -n "$recursive"
1052 then
1054 prefix="$displaypath/"
1055 sanitize_submodule_env
1056 wt_prefix=
1057 cd "$sm_path" &&
1058 eval cmd_status
1059 ) ||
1060 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1062 done
1065 # Sync remote urls for submodules
1066 # This makes the value for remote.$remote.url match the value
1067 # specified in .gitmodules.
1069 cmd_sync()
1071 while test $# -ne 0
1073 case "$1" in
1074 -q|--quiet)
1075 GIT_QUIET=1
1076 shift
1078 --recursive)
1079 recursive=1
1080 shift
1083 shift
1084 break
1087 usage
1090 break
1092 esac
1093 done
1094 cd_to_toplevel
1095 git submodule--helper list --prefix "$wt_prefix" "$@" |
1096 while read mode sha1 stage sm_path
1098 die_if_unmatched "$mode"
1099 name=$(git submodule--helper name "$sm_path")
1100 url=$(git config -f .gitmodules --get submodule."$name".url)
1102 # Possibly a url relative to parent
1103 case "$url" in
1104 ./*|../*)
1105 # rewrite foo/bar as ../.. to find path from
1106 # submodule work tree to superproject work tree
1107 up_path="$(printf '%s\n' "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1108 # guarantee a trailing /
1109 up_path=${up_path%/}/ &&
1110 # path from submodule work tree to submodule origin repo
1111 sub_origin_url=$(git submodule--helper resolve-relative-url "$url" "$up_path") &&
1112 # path from superproject work tree to submodule origin repo
1113 super_config_url=$(git submodule--helper resolve-relative-url "$url") || exit
1116 sub_origin_url="$url"
1117 super_config_url="$url"
1119 esac
1121 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1122 then
1123 displaypath=$(relative_path "$prefix$sm_path")
1124 say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
1125 git config submodule."$name".url "$super_config_url"
1127 if test -e "$sm_path"/.git
1128 then
1130 sanitize_submodule_env
1131 cd "$sm_path"
1132 remote=$(get_default_remote)
1133 git config remote."$remote".url "$sub_origin_url"
1135 if test -n "$recursive"
1136 then
1137 prefix="$prefix$sm_path/"
1138 eval cmd_sync
1143 done
1146 # This loop parses the command line arguments to find the
1147 # subcommand name to dispatch. Parsing of the subcommand specific
1148 # options are primarily done by the subcommand implementations.
1149 # Subcommand specific options such as --branch and --cached are
1150 # parsed here as well, for backward compatibility.
1152 while test $# != 0 && test -z "$command"
1154 case "$1" in
1155 add | foreach | init | deinit | update | status | summary | sync)
1156 command=$1
1158 -q|--quiet)
1159 GIT_QUIET=1
1161 -b|--branch)
1162 case "$2" in
1164 usage
1166 esac
1167 branch="$2"; shift
1169 --cached)
1170 cached="$1"
1173 break
1176 usage
1179 break
1181 esac
1182 shift
1183 done
1185 # No command word defaults to "status"
1186 if test -z "$command"
1187 then
1188 if test $# = 0
1189 then
1190 command=status
1191 else
1192 usage
1196 # "-b branch" is accepted only by "add"
1197 if test -n "$branch" && test "$command" != add
1198 then
1199 usage
1202 # "--cached" is accepted only by "status" and "summary"
1203 if test -n "$cached" && test "$command" != status && test "$command" != summary
1204 then
1205 usage
1208 "cmd_$command" "$@"