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] 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 [--recursive] [--] [<path>...]"
34 # The function takes at most 2 arguments. The first argument is the
35 # URL that navigates to the submodule origin repo. When relative, this URL
36 # is relative to the superproject origin URL repo. The second up_path
37 # argument, if specified, is the relative path that navigates
38 # from the submodule working tree to the superproject working tree.
40 # The output of the function is the origin URL of the submodule.
42 # The output will either be an absolute URL or filesystem path (if the
43 # superproject origin URL is an absolute URL or filesystem path,
44 # respectively) or a relative file system path (if the superproject
45 # origin URL is a relative file system path).
47 # When the output is a relative file system path, the path is either
48 # relative to the submodule working tree, if up_path is specified, or to
49 # the superproject working tree otherwise.
50 resolve_relative_url
()
52 remote
=$
(get_default_remote
)
53 remoteurl
=$
(git config
"remote.$remote.url") ||
54 remoteurl
=$
(pwd) # the repository is its own authoritative upstream
56 remoteurl
=${remoteurl%/}
69 remoteurl
="./$remoteurl"
80 remoteurl
="${remoteurl%/*}"
83 remoteurl
="${remoteurl%:*}"
87 if test -z "$is_relative" ||
test "." = "$remoteurl"
89 die
"$(eval_gettext "cannot strip one component off url
'\$remoteurl'")"
103 remoteurl
="$remoteurl$sep${url%/}"
104 echo "${is_relative:+${up_path}}${remoteurl#./}"
108 # Get submodule info for registered submodules
109 # $@ = path to limit submodule list
114 git ls-files
--error-unmatch --stage -- "$@" ||
115 echo "unmatched pathspec exists"
119 my ($null_sha1) = ("0" x 40);
123 if (/^unmatched pathspec/) {
128 my ($mode, $sha1, $stage, $path) =
129 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
130 next unless $mode eq "160000";
132 if (!$unmerged{$path}++) {
133 push @out, "$mode $null_sha1 U\t$path\n";
140 print "#unmatched\n";
149 if test "$1" = "#unmatched"
156 # Map submodule path to submodule name
162 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
164 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
165 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
166 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
168 die
"$(eval_gettext "No submodule mapping found
in .gitmodules
for path
'\$sm_path'")"
175 # Prior to calling, cmd_update checks that a possibly existing
176 # path is not a git repository.
177 # Likewise, cmd_add checks that path does not exist at all,
178 # since it is the location of a new submodule.
187 if test -n "$GIT_QUIET"
194 base_name
=$
(dirname "$name")
196 gitdir
=$
(git rev-parse
--git-dir)
197 gitdir_base
="$gitdir/modules/$base_name"
198 gitdir
="$gitdir/modules/$name"
203 rm -f "$gitdir/index"
205 mkdir
-p "$gitdir_base"
208 git clone
$quiet -n ${reference:+"$reference"} \
209 --separate-git-dir "$gitdir" "$url" "$sm_path"
211 die
"$(eval_gettext "Clone of
'\$url' into submodule path
'\$sm_path' failed
")"
214 # We already are at the root of the work tree but cd_to_toplevel will
215 # resolve any symlinks that might be present in $PWD
216 a
=$
(cd_to_toplevel
&& cd "$gitdir" && pwd)/
217 b
=$
(cd_to_toplevel
&& cd "$sm_path" && pwd)/
218 # Remove all common leading directories after a sanity check
219 if test "${a#$b}" != "$a" ||
test "${b#$a}" != "$b"; then
220 die
"$(eval_gettext "Gitdir
'\$a' is part of the submodule path
'\$b' or vice versa
")"
222 while test "${a%%/*}" = "${b%%/*}"
227 # Now chop off the trailing '/'s that were added in the beginning
231 # Turn each leading "*/" component into "../"
232 rel
=$
(echo $b |
sed -e 's|[^/][^/]*|..|g')
233 echo "gitdir: $rel/$a" >"$sm_path/.git"
235 rel
=$
(echo $a |
sed -e 's|[^/][^/]*|..|g')
236 (clear_local_git_env
; cd "$sm_path" && GIT_WORK_TREE
=. git config core.worktree
"$rel/$b")
240 # Add a new submodule to the working tree, .gitmodules and the index
244 # optional branch is stored in global branch variable
248 # parse $args after "submodule ... add".
253 case "$2" in '') usage
;; esac
264 case "$2" in '') usage
;; esac
265 reference
="--reference=$2"
272 case "$2" in '') usage
;; esac
293 if test -z "$sm_path"; then
294 sm_path
=$
(echo "$repo" |
295 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
298 if test -z "$repo" -o -z "$sm_path"; then
302 # assure repo is absolute or relative to parent
305 # dereference source url relative to parent's url
306 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
313 die
"$(eval_gettext "repo URL
: '\$repo' must be absolute or begin with .
/|..
/")"
318 # multiple //; leading ./; /./; /../; trailing /
319 sm_path
=$
(printf '%s/\n' "$sm_path" |
329 git ls-files
--error-unmatch "$sm_path" > /dev
/null
2>&1 &&
330 die
"$(eval_gettext "'\$sm_path' already exists
in the index
")"
332 if test -z "$force" && ! git add
--dry-run --ignore-missing "$sm_path" > /dev
/null
2>&1
334 eval_gettextln
"The following path is ignored by one of your .gitignore files:
336 Use -f if you really want to add it." >&2
340 if test -n "$custom_name"
342 sm_name
="$custom_name"
347 # perhaps the path exists and is already a git repo, else clone it
348 if test -e "$sm_path"
350 if test -d "$sm_path"/.git
-o -f "$sm_path"/.git
352 eval_gettextln
"Adding existing repo at '\$sm_path' to the index"
354 die
"$(eval_gettext "'\$sm_path' already exists and is not a valid git repo
")"
358 if test -d ".git/modules/$sm_name"
362 echo >&2 "$(eval_gettext "A git directory
for '\$sm_name' is found locally with remote
(s
):")"
363 GIT_DIR
=".git/modules/$sm_name" GIT_WORK_TREE
=. git remote
-v |
grep '(fetch)' |
sed -e s
,^
," ", -e s
,' (fetch)',, >&2
364 echo >&2 "$(eval_gettext "If you want to reuse this
local git directory instead of cloning again from
")"
365 echo >&2 " $realrepo"
366 echo >&2 "$(eval_gettext "use the
'--force' option. If the
local git directory is not the correct repo
")"
367 die
"$(eval_gettext "or you are unsure what this means choose another name with the
'--name' option.
")"
369 echo "$(eval_gettext "Reactivating
local git directory
for submodule
'\$sm_name'.
")"
372 module_clone
"$sm_path" "$sm_name" "$realrepo" "$reference" ||
exit
376 # ash fails to wordsplit ${branch:+-b "$branch"...}
378 '') git checkout
-f -q ;;
379 ?
*) git checkout
-f -q -B "$branch" "origin/$branch" ;;
381 ) || die
"$(eval_gettext "Unable to checkout submodule
'\$sm_path'")"
383 git config submodule.
"$sm_name".url
"$realrepo"
385 git add
$force "$sm_path" ||
386 die
"$(eval_gettext "Failed to add submodule
'\$sm_path'")"
388 git config
-f .gitmodules submodule.
"$sm_name".path
"$sm_path" &&
389 git config
-f .gitmodules submodule.
"$sm_name".url
"$repo" &&
390 git add
--force .gitmodules ||
391 die
"$(eval_gettext "Failed to register submodule
'\$sm_path'")"
395 # Execute an arbitrary command sequence in each checked out
398 # $@ = command to execute
402 # parse $args after "submodule ... foreach".
424 # dup stdin so that it can be restored when running the external
425 # command in the subshell (and a recursive call to this function)
429 while read mode sha1 stage sm_path
431 die_if_unmatched
"$mode"
432 if test -e "$sm_path"/.git
434 say
"$(eval_gettext "Entering
'\$prefix\$sm_path'")"
435 name
=$
(module_name
"$sm_path")
437 prefix
="$prefix$sm_path/"
439 # we make $path available to scripts ...
443 if test -n "$recursive"
445 cmd_foreach
"--recursive" "$@"
448 die
"$(eval_gettext "Stopping
at '\$sm_path'; script returned non-zero status.
")"
454 # Register submodules in .git/config
456 # $@ = requested paths (default to all)
460 # parse $args after "submodule ... init".
482 while read mode sha1 stage sm_path
484 die_if_unmatched
"$mode"
485 name
=$
(module_name
"$sm_path") ||
exit
487 # Copy url setting when it is not set yet
488 if test -z "$(git config "submodule.
$name.url
")"
490 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
492 die
"$(eval_gettext "No url found
for submodule path
'\$sm_path' in .gitmodules
")"
494 # Possibly a url relative to parent
497 url
=$
(resolve_relative_url
"$url") ||
exit
500 git config submodule.
"$name".url
"$url" ||
501 die
"$(eval_gettext "Failed to register url
for submodule path
'\$sm_path'")"
503 say
"$(eval_gettext "Submodule
'\$name' (\
$url) registered
for path
'\$sm_path'")"
506 # Copy "update" setting when it is not set yet
507 upd
="$(git config -f .gitmodules submodule."$name".update)"
509 test -n "$(git config submodule."$name".update)" ||
510 git config submodule.
"$name".update
"$upd" ||
511 die
"$(eval_gettext "Failed to register update mode
for submodule path
'\$sm_path'")"
516 # Update each submodule path to correct revision, using clone and checkout as needed
518 # $@ = requested paths (default to all)
522 # parse $args after "submodule ... update".
543 case "$2" in '') usage
;; esac
544 reference
="--reference=$2"
545 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
571 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
577 cmd_init
"--" "$@" ||
return
583 while read mode sha1 stage sm_path
585 die_if_unmatched
"$mode"
588 echo >&2 "Skipping unmerged submodule $sm_path"
591 name
=$
(module_name
"$sm_path") ||
exit
592 url
=$
(git config submodule.
"$name".url
)
593 if ! test -z "$update"
595 update_module
=$update
597 update_module
=$
(git config submodule.
"$name".update
)
600 if test "$update_module" = "none"
602 echo "Skipping submodule '$sm_path'"
608 # Only mention uninitialized submodules when its
609 # path have been specified
611 say
"$(eval_gettext "Submodule path
'\$sm_path' not initialized
612 Maybe you want to use
'update --init'?
")"
616 if ! test -d "$sm_path"/.git
-o -f "$sm_path"/.git
618 module_clone
"$sm_path" "$name" "$url" "$reference" ||
exit
619 cloned_modules
="$cloned_modules;$name"
622 subsha1
=$
(clear_local_git_env
; cd "$sm_path" &&
623 git rev-parse
--verify HEAD
) ||
624 die
"$(eval_gettext "Unable to
find current revision
in submodule path
'\$sm_path'")"
627 if test "$subsha1" != "$sha1" -o -n "$force"
630 # If we don't already have a -f flag and the submodule has never been checked out
631 if test -z "$subsha1" -a -z "$force"
636 if test -z "$nofetch"
638 # Run fetch only if $sha1 isn't present or it
639 # is not reachable from a ref.
640 (clear_local_git_env
; cd "$sm_path" &&
641 ( (rev=$
(git rev-list
-n 1 $sha1 --not --all 2>/dev
/null
) &&
642 test -z "$rev") || git-fetch
)) ||
643 die
"$(eval_gettext "Unable to fetch
in submodule path
'\$sm_path'")"
646 # Is this something we just cloned?
647 case ";$cloned_modules;" in
649 # then there is no local change to integrate
654 case "$update_module" in
657 die_msg
="$(eval_gettext "Unable to rebase
'\$sha1' in submodule path
'\$sm_path'")"
658 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': rebased into
'\$sha1'")"
659 must_die_on_failure
=yes
663 die_msg
="$(eval_gettext "Unable to merge
'\$sha1' in submodule path
'\$sm_path'")"
664 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': merged
in '\$sha1'")"
665 must_die_on_failure
=yes
668 command="git checkout $subforce -q"
669 die_msg
="$(eval_gettext "Unable to checkout
'\$sha1' in submodule path
'\$sm_path'")"
670 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': checked out
'\$sha1'")"
674 if (clear_local_git_env
; cd "$sm_path" && $command "$sha1")
677 elif test -n "$must_die_on_failure"
679 die_with_status
2 "$die_msg"
681 err
="${err};$die_msg"
686 if test -n "$recursive"
688 (clear_local_git_env
; cd "$sm_path" && eval cmd_update
"$orig_flags")
692 die_msg
="$(eval_gettext "Failed to recurse into submodule path
'\$sm_path'")"
695 err
="${err};$die_msg"
698 die_with_status
$res "$die_msg"
725 git describe
"$2" 2>/dev
/null ||
726 git describe
--tags "$2" 2>/dev
/null ||
727 git describe
--contains "$2" 2>/dev
/null ||
728 git describe
--all --always "$2"
731 test -z "$revname" || revname
=" ($revname)"
734 # Show commit summary for submodules in index or working tree
736 # If '--cached' is given, show summary between index and given commit,
737 # or between working tree and given commit
739 # $@ = [commit (default 'HEAD'),] requested paths (default all)
746 # parse $args after "submodule ... summary".
760 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
782 test $summary_limit = 0 && return
784 if rev=$
(git rev-parse
-q --verify --default HEAD
${1+"$1"})
788 elif test -z "$1" -o "$1" = "HEAD"
790 # before the first commit: compare with an empty tree
791 head=$
(git hash-object
-w -t tree
--stdin </dev
/null
)
792 test -z "$1" ||
shift
800 die
"$(gettext "The
--cached option cannot be used with the
--files option
")"
806 # Get modified modules cared by user
807 modules
=$
(git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- "$@" |
808 sane_egrep
'^:([0-7]* )?160000' |
809 while read mod_src mod_dst sha1_src sha1_dst status name
811 # Always show modules deleted or type-changed (blob<->module)
812 test $status = D
-o $status = T
&& echo "$name" && continue
813 # Also show added or modified modules which are checked out
814 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
819 test -z "$modules" && return
821 git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- $modules |
822 sane_egrep
'^:([0-7]* )?160000' |
824 while read mod_src mod_dst sha1_src sha1_dst status name
826 if test -z "$cached" &&
827 test $sha1_dst = 0000000000000000000000000000000000000000
831 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
833 100644 |
100755 |
120000)
834 sha1_dst
=$
(git hash-object
$name)
840 eval_gettextln
"unexpected mode \$mod_dst" >&2
847 test $mod_src = 160000 &&
848 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
851 test $mod_dst = 160000 &&
852 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
856 case "$missing_src,$missing_dst" in
858 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commit \$sha1_src")"
861 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \
$sha1_dst")"
864 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commits \$sha1_src and \$sha1_dst")"
869 if test $mod_src = 160000 -a $mod_dst = 160000
871 range="$sha1_src...$sha1_dst"
872 elif test $mod_src = 160000
878 GIT_DIR="$name/.git" \
879 git rev-list --first-parent $range -- | wc -l
881 total_commits=" ($(($total_commits + 0)))"
885 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
886 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
889 blob="$(gettext "blob")"
890 submodule="$(gettext "submodule")"
891 if test $mod_dst = 160000
893 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
895 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
898 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
902 # Don't give error msg
for modification whose dst is not submodule
903 # i.e. deleted or changed to blob
904 test $mod_dst = 160000 && echo "$errmsg"
906 if test $mod_src = 160000 -a $mod_dst = 160000
909 test $summary_limit -gt 0 && limit
="-$summary_limit"
910 GIT_DIR
="$name/.git" \
911 git log
$limit --pretty='format: %m %s' \
912 --first-parent $sha1_src...
$sha1_dst
913 elif test $mod_dst = 160000
915 GIT_DIR
="$name/.git" \
916 git log
--pretty='format: > %s' -1 $sha1_dst
918 GIT_DIR
="$name/.git" \
919 git log
--pretty='format: < %s' -1 $sha1_src
925 if test -n "$for_status"; then
926 if [ -n "$files" ]; then
927 gettextln
"# Submodules changed but not updated:"
929 gettextln
"# Submodule changes to be committed:"
932 sed -e 's|^|# |' -e 's|^# $|#|'
938 # List all submodules, prefixed with:
939 # - submodule not initialized
940 # + different revision checked out
942 # If --cached was specified the revision in the index will be printed
943 # instead of the currently checked out revision.
945 # $@ = requested paths (default to all)
949 # parse $args after "submodule ... status".
977 while read mode sha1 stage sm_path
979 die_if_unmatched
"$mode"
980 name
=$
(module_name
"$sm_path") ||
exit
981 url
=$
(git config submodule.
"$name".url
)
982 displaypath
="$prefix$sm_path"
985 say
"U$sha1 $displaypath"
988 if test -z "$url" ||
! test -d "$sm_path"/.git
-o -f "$sm_path"/.git
990 say
"-$sha1 $displaypath"
993 set_name_rev
"$sm_path" "$sha1"
994 if git diff-files
--ignore-submodules=dirty
--quiet -- "$sm_path"
996 say
" $sha1 $displaypath$revname"
1000 sha1
=$
(clear_local_git_env
; cd "$sm_path" && git rev-parse
--verify HEAD
)
1001 set_name_rev
"$sm_path" "$sha1"
1003 say
"+$sha1 $displaypath$revname"
1006 if test -n "$recursive"
1009 prefix
="$displaypath/"
1014 die
"$(eval_gettext "Failed to recurse into submodule path
'\$sm_path'")"
1019 # Sync remote urls for submodules
1020 # This makes the value for remote.$remote.url match the value
1021 # specified in .gitmodules.
1050 while read mode sha1 stage sm_path
1052 die_if_unmatched
"$mode"
1053 name
=$
(module_name
"$sm_path")
1054 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
1056 # Possibly a url relative to parent
1059 # rewrite foo/bar as ../.. to find path from
1060 # submodule work tree to superproject work tree
1061 up_path
="$(echo "$sm_path" | sed "s
/[^
/][^
/]*/..
/g
")" &&
1062 # guarantee a trailing /
1063 up_path
=${up_path%/}/ &&
1064 # path from submodule work tree to submodule origin repo
1065 sub_origin_url
=$
(resolve_relative_url
"$url" "$up_path") &&
1066 # path from superproject work tree to submodule origin repo
1067 super_config_url
=$
(resolve_relative_url
"$url") ||
exit
1070 sub_origin_url
="$url"
1071 super_config_url
="$url"
1075 if git config
"submodule.$name.url" >/dev
/null
2>/dev
/null
1077 say
"$(eval_gettext "Synchronizing submodule url
for '\$prefix\$sm_path'")"
1078 git config submodule.
"$name".url
"$super_config_url"
1080 if test -e "$sm_path"/.git
1085 remote
=$
(get_default_remote
)
1086 git config remote.
"$remote".url
"$sub_origin_url"
1088 if test -n "$recursive"
1090 prefix
="$prefix$sm_path/"
1099 # This loop parses the command line arguments to find the
1100 # subcommand name to dispatch. Parsing of the subcommand specific
1101 # options are primarily done by the subcommand implementations.
1102 # Subcommand specific options such as --branch and --cached are
1103 # parsed here as well, for backward compatibility.
1105 while test $# != 0 && test -z "$command"
1108 add | foreach | init | update | status | summary | sync
)
1138 # No command word defaults to "status"
1139 if test -z "$command"
1149 # "-b branch" is accepted only by "add"
1150 if test -n "$branch" && test "$command" != add
1155 # "--cached" is accepted only by "status" and "summary"
1156 if test -n "$cached" && test "$command" != status
-a "$command" != summary