submodule: rewrite `module_name` shell function in C
[git.git] / git-submodule.sh
blob2be8da2396eb11dbac887049dae1c8f0dea849d6
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] [--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-sh-i18n
20 . git-parse-remote
21 require_work_tree
22 wt_prefix=$(git rev-parse --show-prefix)
23 cd_to_toplevel
25 command=
26 branch=
27 force=
28 reference=
29 cached=
30 recursive=
31 init=
32 files=
33 remote=
34 nofetch=
35 update=
36 prefix=
37 custom_name=
38 depth=
40 # The function takes at most 2 arguments. The first argument is the
41 # URL that navigates to the submodule origin repo. When relative, this URL
42 # is relative to the superproject origin URL repo. The second up_path
43 # argument, if specified, is the relative path that navigates
44 # from the submodule working tree to the superproject working tree.
46 # The output of the function is the origin URL of the submodule.
48 # The output will either be an absolute URL or filesystem path (if the
49 # superproject origin URL is an absolute URL or filesystem path,
50 # respectively) or a relative file system path (if the superproject
51 # origin URL is a relative file system path).
53 # When the output is a relative file system path, the path is either
54 # relative to the submodule working tree, if up_path is specified, or to
55 # the superproject working tree otherwise.
56 resolve_relative_url ()
58 remote=$(get_default_remote)
59 remoteurl=$(git config "remote.$remote.url") ||
60 remoteurl=$(pwd) # the repository is its own authoritative upstream
61 url="$1"
62 remoteurl=${remoteurl%/}
63 sep=/
64 up_path="$2"
66 case "$remoteurl" in
67 *:*|/*)
68 is_relative=
70 ./*|../*)
71 is_relative=t
74 is_relative=t
75 remoteurl="./$remoteurl"
77 esac
79 while test -n "$url"
81 case "$url" in
82 ../*)
83 url="${url#../}"
84 case "$remoteurl" in
85 */*)
86 remoteurl="${remoteurl%/*}"
88 *:*)
89 remoteurl="${remoteurl%:*}"
90 sep=:
93 if test -z "$is_relative" || test "." = "$remoteurl"
94 then
95 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
96 else
97 remoteurl=.
100 esac
102 ./*)
103 url="${url#./}"
106 break;;
107 esac
108 done
109 remoteurl="$remoteurl$sep${url%/}"
110 echo "${is_relative:+${up_path}}${remoteurl#./}"
113 # Resolve a path to be relative to another path. This is intended for
114 # converting submodule paths when git-submodule is run in a subdirectory
115 # and only handles paths where the directory separator is '/'.
117 # The output is the first argument as a path relative to the second argument,
118 # which defaults to $wt_prefix if it is omitted.
119 relative_path ()
121 local target curdir result
122 target=$1
123 curdir=${2-$wt_prefix}
124 curdir=${curdir%/}
125 result=
127 while test -n "$curdir"
129 case "$target" in
130 "$curdir/"*)
131 target=${target#"$curdir"/}
132 break
134 esac
136 result="${result}../"
137 if test "$curdir" = "${curdir%/*}"
138 then
139 curdir=
140 else
141 curdir="${curdir%/*}"
143 done
145 echo "$result$target"
148 die_if_unmatched ()
150 if test "$1" = "#unmatched"
151 then
152 exit 1
157 # Print a submodule configuration setting
159 # $1 = submodule name
160 # $2 = option name
161 # $3 = default value
163 # Checks in the usual git-config places first (for overrides),
164 # otherwise it falls back on .gitmodules. This allows you to
165 # distribute project-wide defaults in .gitmodules, while still
166 # customizing individual repositories if necessary. If the option is
167 # not in .gitmodules either, print a default value.
169 get_submodule_config () {
170 name="$1"
171 option="$2"
172 default="$3"
173 value=$(git config submodule."$name"."$option")
174 if test -z "$value"
175 then
176 value=$(git config -f .gitmodules submodule."$name"."$option")
178 printf '%s' "${value:-$default}"
182 # Clone a submodule
184 # $1 = submodule path
185 # $2 = submodule name
186 # $3 = URL to clone
187 # $4 = reference repository to reuse (empty for independent)
188 # $5 = depth argument for shallow clones (empty for deep)
190 # Prior to calling, cmd_update checks that a possibly existing
191 # path is not a git repository.
192 # Likewise, cmd_add checks that path does not exist at all,
193 # since it is the location of a new submodule.
195 module_clone()
197 sm_path=$1
198 name=$2
199 url=$3
200 reference="$4"
201 depth="$5"
202 quiet=
203 if test -n "$GIT_QUIET"
204 then
205 quiet=-q
208 gitdir=
209 gitdir_base=
210 base_name=$(dirname "$name")
212 gitdir=$(git rev-parse --git-dir)
213 gitdir_base="$gitdir/modules/$base_name"
214 gitdir="$gitdir/modules/$name"
216 if test -d "$gitdir"
217 then
218 mkdir -p "$sm_path"
219 rm -f "$gitdir/index"
220 else
221 mkdir -p "$gitdir_base"
223 clear_local_git_env
224 git clone $quiet ${depth:+"$depth"} -n ${reference:+"$reference"} \
225 --separate-git-dir "$gitdir" "$url" "$sm_path"
226 ) ||
227 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
230 # We already are at the root of the work tree but cd_to_toplevel will
231 # resolve any symlinks that might be present in $PWD
232 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
233 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
234 # Remove all common leading directories after a sanity check
235 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
236 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
238 while test "${a%%/*}" = "${b%%/*}"
240 a=${a#*/}
241 b=${b#*/}
242 done
243 # Now chop off the trailing '/'s that were added in the beginning
244 a=${a%/}
245 b=${b%/}
247 # Turn each leading "*/" component into "../"
248 rel=$(printf '%s\n' "$b" | sed -e 's|[^/][^/]*|..|g')
249 printf '%s\n' "gitdir: $rel/$a" >"$sm_path/.git"
251 rel=$(printf '%s\n' "$a" | sed -e 's|[^/][^/]*|..|g')
252 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
255 isnumber()
257 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
261 # Add a new submodule to the working tree, .gitmodules and the index
263 # $@ = repo path
265 # optional branch is stored in global branch variable
267 cmd_add()
269 # parse $args after "submodule ... add".
270 reference_path=
271 while test $# -ne 0
273 case "$1" in
274 -b | --branch)
275 case "$2" in '') usage ;; esac
276 branch=$2
277 shift
279 -f | --force)
280 force=$1
282 -q|--quiet)
283 GIT_QUIET=1
285 --reference)
286 case "$2" in '') usage ;; esac
287 reference_path=$2
288 shift
290 --reference=*)
291 reference_path="${1#--reference=}"
293 --name)
294 case "$2" in '') usage ;; esac
295 custom_name=$2
296 shift
298 --depth)
299 case "$2" in '') usage ;; esac
300 depth="--depth=$2"
301 shift
303 --depth=*)
304 depth=$1
307 shift
308 break
311 usage
314 break
316 esac
317 shift
318 done
320 if test -n "$reference_path"
321 then
322 is_absolute_path "$reference_path" ||
323 reference_path="$wt_prefix$reference_path"
325 reference="--reference=$reference_path"
328 repo=$1
329 sm_path=$2
331 if test -z "$sm_path"; then
332 sm_path=$(printf '%s\n' "$repo" |
333 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
336 if test -z "$repo" || test -z "$sm_path"; then
337 usage
340 is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
342 # assure repo is absolute or relative to parent
343 case "$repo" in
344 ./*|../*)
345 test -z "$wt_prefix" ||
346 die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
348 # dereference source url relative to parent's url
349 realrepo=$(resolve_relative_url "$repo") || exit
351 *:*|/*)
352 # absolute url
353 realrepo=$repo
356 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
358 esac
360 # normalize path:
361 # multiple //; leading ./; /./; /../; trailing /
362 sm_path=$(printf '%s/\n' "$sm_path" |
363 sed -e '
364 s|//*|/|g
365 s|^\(\./\)*||
366 s|/\(\./\)*|/|g
367 :start
368 s|\([^/]*\)/\.\./||
369 tstart
370 s|/*$||
372 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
373 die "$(eval_gettext "'\$sm_path' already exists in the index")"
375 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
376 then
377 eval_gettextln "The following path is ignored by one of your .gitignore files:
378 \$sm_path
379 Use -f if you really want to add it." >&2
380 exit 1
383 if test -n "$custom_name"
384 then
385 sm_name="$custom_name"
386 else
387 sm_name="$sm_path"
390 # perhaps the path exists and is already a git repo, else clone it
391 if test -e "$sm_path"
392 then
393 if test -d "$sm_path"/.git || test -f "$sm_path"/.git
394 then
395 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
396 else
397 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
400 else
401 if test -d ".git/modules/$sm_name"
402 then
403 if test -z "$force"
404 then
405 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
406 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
407 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
408 echo >&2 " $realrepo"
409 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
410 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
411 else
412 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
415 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" || exit
417 clear_local_git_env
418 cd "$sm_path" &&
419 # ash fails to wordsplit ${branch:+-b "$branch"...}
420 case "$branch" in
421 '') git checkout -f -q ;;
422 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
423 esac
424 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
426 git config submodule."$sm_name".url "$realrepo"
428 git add $force "$sm_path" ||
429 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
431 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
432 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
433 if test -n "$branch"
434 then
435 git config -f .gitmodules submodule."$sm_name".branch "$branch"
436 fi &&
437 git add --force .gitmodules ||
438 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
442 # Execute an arbitrary command sequence in each checked out
443 # submodule
445 # $@ = command to execute
447 cmd_foreach()
449 # parse $args after "submodule ... foreach".
450 while test $# -ne 0
452 case "$1" in
453 -q|--quiet)
454 GIT_QUIET=1
456 --recursive)
457 recursive=1
460 usage
463 break
465 esac
466 shift
467 done
469 toplevel=$(pwd)
471 # dup stdin so that it can be restored when running the external
472 # command in the subshell (and a recursive call to this function)
473 exec 3<&0
475 git submodule--helper list --prefix "$wt_prefix"|
476 while read mode sha1 stage sm_path
478 die_if_unmatched "$mode"
479 if test -e "$sm_path"/.git
480 then
481 displaypath=$(relative_path "$sm_path")
482 say "$(eval_gettext "Entering '\$prefix\$displaypath'")"
483 name=$(git submodule--helper name "$sm_path")
485 prefix="$prefix$sm_path/"
486 clear_local_git_env
487 cd "$sm_path" &&
488 sm_path=$(relative_path "$sm_path") &&
489 # we make $path available to scripts ...
490 path=$sm_path &&
491 if test $# -eq 1
492 then
493 eval "$1"
494 else
495 "$@"
496 fi &&
497 if test -n "$recursive"
498 then
499 cmd_foreach "--recursive" "$@"
501 ) <&3 3<&- ||
502 die "$(eval_gettext "Stopping at '\$prefix\$displaypath'; script returned non-zero status.")"
504 done
508 # Register submodules in .git/config
510 # $@ = requested paths (default to all)
512 cmd_init()
514 # parse $args after "submodule ... init".
515 while test $# -ne 0
517 case "$1" in
518 -q|--quiet)
519 GIT_QUIET=1
522 shift
523 break
526 usage
529 break
531 esac
532 shift
533 done
535 git submodule--helper list --prefix "$wt_prefix" "$@" |
536 while read mode sha1 stage sm_path
538 die_if_unmatched "$mode"
539 name=$(git submodule--helper name "$sm_path") || exit
541 displaypath=$(relative_path "$sm_path")
543 # Copy url setting when it is not set yet
544 if test -z "$(git config "submodule.$name.url")"
545 then
546 url=$(git config -f .gitmodules submodule."$name".url)
547 test -z "$url" &&
548 die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")"
550 # Possibly a url relative to parent
551 case "$url" in
552 ./*|../*)
553 url=$(resolve_relative_url "$url") || exit
555 esac
556 git config submodule."$name".url "$url" ||
557 die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")"
559 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")"
562 # Copy "update" setting when it is not set yet
563 if upd="$(git config -f .gitmodules submodule."$name".update)" &&
564 test -n "$upd" &&
565 test -z "$(git config submodule."$name".update)"
566 then
567 case "$upd" in
568 checkout | rebase | merge | none)
569 ;; # known modes of updating
571 echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'"
572 upd=none
574 esac
575 git config submodule."$name".update "$upd" ||
576 die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")"
578 done
582 # Unregister submodules from .git/config and remove their work tree
584 # $@ = requested paths (use '.' to deinit all submodules)
586 cmd_deinit()
588 # parse $args after "submodule ... deinit".
589 while test $# -ne 0
591 case "$1" in
592 -f|--force)
593 force=$1
595 -q|--quiet)
596 GIT_QUIET=1
599 shift
600 break
603 usage
606 break
608 esac
609 shift
610 done
612 if test $# = 0
613 then
614 die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
617 git submodule--helper list --prefix "$wt_prefix" "$@" |
618 while read mode sha1 stage sm_path
620 die_if_unmatched "$mode"
621 name=$(git submodule--helper name "$sm_path") || exit
623 displaypath=$(relative_path "$sm_path")
625 # Remove the submodule work tree (unless the user already did it)
626 if test -d "$sm_path"
627 then
628 # Protect submodules containing a .git directory
629 if test -d "$sm_path/.git"
630 then
631 echo >&2 "$(eval_gettext "Submodule work tree '\$displaypath' contains a .git directory")"
632 die "$(eval_gettext "(use 'rm -rf' if you really want to remove it including all of its history)")"
635 if test -z "$force"
636 then
637 git rm -qn "$sm_path" ||
638 die "$(eval_gettext "Submodule work tree '\$displaypath' contains local modifications; use '-f' to discard them")"
640 rm -rf "$sm_path" &&
641 say "$(eval_gettext "Cleared directory '\$displaypath'")" ||
642 say "$(eval_gettext "Could not remove submodule work tree '\$displaypath'")"
645 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$displaypath'")"
647 # Remove the .git/config entries (unless the user already did it)
648 if test -n "$(git config --get-regexp submodule."$name\.")"
649 then
650 # Remove the whole section so we have a clean state when
651 # the user later decides to init this submodule again
652 url=$(git config submodule."$name".url)
653 git config --remove-section submodule."$name" 2>/dev/null &&
654 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$displaypath'")"
656 done
660 # Update each submodule path to correct revision, using clone and checkout as needed
662 # $@ = requested paths (default to all)
664 cmd_update()
666 # parse $args after "submodule ... update".
667 while test $# -ne 0
669 case "$1" in
670 -q|--quiet)
671 GIT_QUIET=1
673 -i|--init)
674 init=1
676 --remote)
677 remote=1
679 -N|--no-fetch)
680 nofetch=1
682 -f|--force)
683 force=$1
685 -r|--rebase)
686 update="rebase"
688 --reference)
689 case "$2" in '') usage ;; esac
690 reference="--reference=$2"
691 shift
693 --reference=*)
694 reference="$1"
696 -m|--merge)
697 update="merge"
699 --recursive)
700 recursive=1
702 --checkout)
703 update="checkout"
705 --depth)
706 case "$2" in '') usage ;; esac
707 depth="--depth=$2"
708 shift
710 --depth=*)
711 depth=$1
714 shift
715 break
718 usage
721 break
723 esac
724 shift
725 done
727 if test -n "$init"
728 then
729 cmd_init "--" "$@" || return
732 cloned_modules=
733 git submodule--helper list --prefix "$wt_prefix" "$@" | {
734 err=
735 while read mode sha1 stage sm_path
737 die_if_unmatched "$mode"
738 if test "$stage" = U
739 then
740 echo >&2 "Skipping unmerged submodule $prefix$sm_path"
741 continue
743 name=$(git submodule--helper name "$sm_path") || exit
744 url=$(git config submodule."$name".url)
745 branch=$(get_submodule_config "$name" branch master)
746 if ! test -z "$update"
747 then
748 update_module=$update
749 else
750 update_module=$(git config submodule."$name".update)
751 if test -z "$update_module"
752 then
753 update_module="checkout"
757 displaypath=$(relative_path "$prefix$sm_path")
759 if test "$update_module" = "none"
760 then
761 echo "Skipping submodule '$displaypath'"
762 continue
765 if test -z "$url"
766 then
767 # Only mention uninitialized submodules when its
768 # path have been specified
769 test "$#" != "0" &&
770 say "$(eval_gettext "Submodule path '\$displaypath' not initialized
771 Maybe you want to use 'update --init'?")"
772 continue
775 if ! test -d "$sm_path"/.git && ! test -f "$sm_path"/.git
776 then
777 module_clone "$sm_path" "$name" "$url" "$reference" "$depth" || exit
778 cloned_modules="$cloned_modules;$name"
779 subsha1=
780 else
781 subsha1=$(clear_local_git_env; cd "$sm_path" &&
782 git rev-parse --verify HEAD) ||
783 die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
786 if test -n "$remote"
787 then
788 if test -z "$nofetch"
789 then
790 # Fetch remote before determining tracking $sha1
791 (clear_local_git_env; cd "$sm_path" && git-fetch) ||
792 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
794 remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
795 sha1=$(clear_local_git_env; cd "$sm_path" &&
796 git rev-parse --verify "${remote_name}/${branch}") ||
797 die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
800 if test "$subsha1" != "$sha1" || test -n "$force"
801 then
802 subforce=$force
803 # If we don't already have a -f flag and the submodule has never been checked out
804 if test -z "$subsha1" && test -z "$force"
805 then
806 subforce="-f"
809 if test -z "$nofetch"
810 then
811 # Run fetch only if $sha1 isn't present or it
812 # is not reachable from a ref.
813 (clear_local_git_env; cd "$sm_path" &&
814 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
815 test -z "$rev") || git-fetch)) ||
816 die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
819 # Is this something we just cloned?
820 case ";$cloned_modules;" in
821 *";$name;"*)
822 # then there is no local change to integrate
823 update_module=checkout ;;
824 esac
826 must_die_on_failure=
827 case "$update_module" in
828 checkout)
829 command="git checkout $subforce -q"
830 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
831 say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
833 rebase)
834 command="git rebase"
835 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
836 say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
837 must_die_on_failure=yes
839 merge)
840 command="git merge"
841 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
842 say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
843 must_die_on_failure=yes
846 command="${update_module#!}"
847 die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$prefix\$sm_path'")"
848 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': '\$command \$sha1'")"
849 must_die_on_failure=yes
852 die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
853 esac
855 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
856 then
857 say "$say_msg"
858 elif test -n "$must_die_on_failure"
859 then
860 die_with_status 2 "$die_msg"
861 else
862 err="${err};$die_msg"
863 continue
867 if test -n "$recursive"
868 then
870 prefix="$prefix$sm_path/"
871 clear_local_git_env
872 cd "$sm_path" &&
873 eval cmd_update
875 res=$?
876 if test $res -gt 0
877 then
878 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
879 if test $res -eq 1
880 then
881 err="${err};$die_msg"
882 continue
883 else
884 die_with_status $res "$die_msg"
888 done
890 if test -n "$err"
891 then
892 OIFS=$IFS
893 IFS=';'
894 for e in $err
896 if test -n "$e"
897 then
898 echo >&2 "$e"
900 done
901 IFS=$OIFS
902 exit 1
907 set_name_rev () {
908 revname=$( (
909 clear_local_git_env
910 cd "$1" && {
911 git describe "$2" 2>/dev/null ||
912 git describe --tags "$2" 2>/dev/null ||
913 git describe --contains "$2" 2>/dev/null ||
914 git describe --all --always "$2"
917 test -z "$revname" || revname=" ($revname)"
920 # Show commit summary for submodules in index or working tree
922 # If '--cached' is given, show summary between index and given commit,
923 # or between working tree and given commit
925 # $@ = [commit (default 'HEAD'),] requested paths (default all)
927 cmd_summary() {
928 summary_limit=-1
929 for_status=
930 diff_cmd=diff-index
932 # parse $args after "submodule ... summary".
933 while test $# -ne 0
935 case "$1" in
936 --cached)
937 cached="$1"
939 --files)
940 files="$1"
942 --for-status)
943 for_status="$1"
945 -n|--summary-limit)
946 summary_limit="$2"
947 isnumber "$summary_limit" || usage
948 shift
950 --summary-limit=*)
951 summary_limit="${1#--summary-limit=}"
952 isnumber "$summary_limit" || usage
955 shift
956 break
959 usage
962 break
964 esac
965 shift
966 done
968 test $summary_limit = 0 && return
970 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
971 then
972 head=$rev
973 test $# = 0 || shift
974 elif test -z "$1" || test "$1" = "HEAD"
975 then
976 # before the first commit: compare with an empty tree
977 head=$(git hash-object -w -t tree --stdin </dev/null)
978 test -z "$1" || shift
979 else
980 head="HEAD"
983 if [ -n "$files" ]
984 then
985 test -n "$cached" &&
986 die "$(gettext "The --cached option cannot be used with the --files option")"
987 diff_cmd=diff-files
988 head=
991 cd_to_toplevel
992 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
993 # Get modified modules cared by user
994 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
995 sane_egrep '^:([0-7]* )?160000' |
996 while read mod_src mod_dst sha1_src sha1_dst status sm_path
998 # Always show modules deleted or type-changed (blob<->module)
999 if test "$status" = D || test "$status" = T
1000 then
1001 printf '%s\n' "$sm_path"
1002 continue
1004 # Respect the ignore setting for --for-status.
1005 if test -n "$for_status"
1006 then
1007 name=$(git submodule--helper name "$sm_path")
1008 ignore_config=$(get_submodule_config "$name" ignore none)
1009 test $status != A && test $ignore_config = all && continue
1011 # Also show added or modified modules which are checked out
1012 GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
1013 printf '%s\n' "$sm_path"
1014 done
1017 test -z "$modules" && return
1019 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
1020 sane_egrep '^:([0-7]* )?160000' |
1021 cut -c2- |
1022 while read mod_src mod_dst sha1_src sha1_dst status name
1024 if test -z "$cached" &&
1025 test $sha1_dst = 0000000000000000000000000000000000000000
1026 then
1027 case "$mod_dst" in
1028 160000)
1029 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
1031 100644 | 100755 | 120000)
1032 sha1_dst=$(git hash-object $name)
1034 000000)
1035 ;; # removed
1037 # unexpected type
1038 eval_gettextln "unexpected mode \$mod_dst" >&2
1039 continue ;;
1040 esac
1042 missing_src=
1043 missing_dst=
1045 test $mod_src = 160000 &&
1046 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1047 missing_src=t
1049 test $mod_dst = 160000 &&
1050 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1051 missing_dst=t
1053 display_name=$(relative_path "$name")
1055 total_commits=
1056 case "$missing_src,$missing_dst" in
1058 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_src")"
1061 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_dst")"
1063 t,t)
1064 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
1067 errmsg=
1068 total_commits=$(
1069 if test $mod_src = 160000 && test $mod_dst = 160000
1070 then
1071 range="$sha1_src...$sha1_dst"
1072 elif test $mod_src = 160000
1073 then
1074 range=$sha1_src
1075 else
1076 range=$sha1_dst
1078 GIT_DIR="$name/.git" \
1079 git rev-list --first-parent $range -- | wc -l
1081 total_commits=" ($(($total_commits + 0)))"
1083 esac
1085 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
1086 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
1087 if test $status = T
1088 then
1089 blob="$(gettext "blob")"
1090 submodule="$(gettext "submodule")"
1091 if test $mod_dst = 160000
1092 then
1093 echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1094 else
1095 echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1097 else
1098 echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1100 if test -n "$errmsg"
1101 then
1102 # Don't give error msg for modification whose dst is not submodule
1103 # i.e. deleted or changed to blob
1104 test $mod_dst = 160000 && echo "$errmsg"
1105 else
1106 if test $mod_src = 160000 && test $mod_dst = 160000
1107 then
1108 limit=
1109 test $summary_limit -gt 0 && limit="-$summary_limit"
1110 GIT_DIR="$name/.git" \
1111 git log $limit --pretty='format: %m %s' \
1112 --first-parent $sha1_src...$sha1_dst
1113 elif test $mod_dst = 160000
1114 then
1115 GIT_DIR="$name/.git" \
1116 git log --pretty='format: > %s' -1 $sha1_dst
1117 else
1118 GIT_DIR="$name/.git" \
1119 git log --pretty='format: < %s' -1 $sha1_src
1121 echo
1123 echo
1124 done
1127 # List all submodules, prefixed with:
1128 # - submodule not initialized
1129 # + different revision checked out
1131 # If --cached was specified the revision in the index will be printed
1132 # instead of the currently checked out revision.
1134 # $@ = requested paths (default to all)
1136 cmd_status()
1138 # parse $args after "submodule ... status".
1139 while test $# -ne 0
1141 case "$1" in
1142 -q|--quiet)
1143 GIT_QUIET=1
1145 --cached)
1146 cached=1
1148 --recursive)
1149 recursive=1
1152 shift
1153 break
1156 usage
1159 break
1161 esac
1162 shift
1163 done
1165 git submodule--helper list --prefix "$wt_prefix" "$@" |
1166 while read mode sha1 stage sm_path
1168 die_if_unmatched "$mode"
1169 name=$(git submodule--helper name "$sm_path") || exit
1170 url=$(git config submodule."$name".url)
1171 displaypath=$(relative_path "$prefix$sm_path")
1172 if test "$stage" = U
1173 then
1174 say "U$sha1 $displaypath"
1175 continue
1177 if test -z "$url" ||
1179 ! test -d "$sm_path"/.git &&
1180 ! test -f "$sm_path"/.git
1182 then
1183 say "-$sha1 $displaypath"
1184 continue;
1186 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1187 then
1188 set_name_rev "$sm_path" "$sha1"
1189 say " $sha1 $displaypath$revname"
1190 else
1191 if test -z "$cached"
1192 then
1193 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1195 set_name_rev "$sm_path" "$sha1"
1196 say "+$sha1 $displaypath$revname"
1199 if test -n "$recursive"
1200 then
1202 prefix="$displaypath/"
1203 clear_local_git_env
1204 cd "$sm_path" &&
1205 eval cmd_status
1206 ) ||
1207 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1209 done
1212 # Sync remote urls for submodules
1213 # This makes the value for remote.$remote.url match the value
1214 # specified in .gitmodules.
1216 cmd_sync()
1218 while test $# -ne 0
1220 case "$1" in
1221 -q|--quiet)
1222 GIT_QUIET=1
1223 shift
1225 --recursive)
1226 recursive=1
1227 shift
1230 shift
1231 break
1234 usage
1237 break
1239 esac
1240 done
1241 cd_to_toplevel
1242 git submodule--helper list --prefix "$wt_prefix" "$@" |
1243 while read mode sha1 stage sm_path
1245 die_if_unmatched "$mode"
1246 name=$(git submodule--helper name "$sm_path")
1247 url=$(git config -f .gitmodules --get submodule."$name".url)
1249 # Possibly a url relative to parent
1250 case "$url" in
1251 ./*|../*)
1252 # rewrite foo/bar as ../.. to find path from
1253 # submodule work tree to superproject work tree
1254 up_path="$(printf '%s\n' "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1255 # guarantee a trailing /
1256 up_path=${up_path%/}/ &&
1257 # path from submodule work tree to submodule origin repo
1258 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1259 # path from superproject work tree to submodule origin repo
1260 super_config_url=$(resolve_relative_url "$url") || exit
1263 sub_origin_url="$url"
1264 super_config_url="$url"
1266 esac
1268 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1269 then
1270 displaypath=$(relative_path "$prefix$sm_path")
1271 say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
1272 git config submodule."$name".url "$super_config_url"
1274 if test -e "$sm_path"/.git
1275 then
1277 clear_local_git_env
1278 cd "$sm_path"
1279 remote=$(get_default_remote)
1280 git config remote."$remote".url "$sub_origin_url"
1282 if test -n "$recursive"
1283 then
1284 prefix="$prefix$sm_path/"
1285 eval cmd_sync
1290 done
1293 # This loop parses the command line arguments to find the
1294 # subcommand name to dispatch. Parsing of the subcommand specific
1295 # options are primarily done by the subcommand implementations.
1296 # Subcommand specific options such as --branch and --cached are
1297 # parsed here as well, for backward compatibility.
1299 while test $# != 0 && test -z "$command"
1301 case "$1" in
1302 add | foreach | init | deinit | update | status | summary | sync)
1303 command=$1
1305 -q|--quiet)
1306 GIT_QUIET=1
1308 -b|--branch)
1309 case "$2" in
1311 usage
1313 esac
1314 branch="$2"; shift
1316 --cached)
1317 cached="$1"
1320 break
1323 usage
1326 break
1328 esac
1329 shift
1330 done
1332 # No command word defaults to "status"
1333 if test -z "$command"
1334 then
1335 if test $# = 0
1336 then
1337 command=status
1338 else
1339 usage
1343 # "-b branch" is accepted only by "add"
1344 if test -n "$branch" && test "$command" != add
1345 then
1346 usage
1349 # "--cached" is accepted only by "status" and "summary"
1350 if test -n "$cached" && test "$command" != status && test "$command" != summary
1351 then
1352 usage
1355 "cmd_$command" "$@"