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] [--remote] [-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>...]"
35 # The function takes at most 2 arguments. The first argument is the
36 # URL that navigates to the submodule origin repo. When relative, this URL
37 # is relative to the superproject origin URL repo. The second up_path
38 # argument, if specified, is the relative path that navigates
39 # from the submodule working tree to the superproject working tree.
41 # The output of the function is the origin URL of the submodule.
43 # The output will either be an absolute URL or filesystem path (if the
44 # superproject origin URL is an absolute URL or filesystem path,
45 # respectively) or a relative file system path (if the superproject
46 # origin URL is a relative file system path).
48 # When the output is a relative file system path, the path is either
49 # relative to the submodule working tree, if up_path is specified, or to
50 # the superproject working tree otherwise.
51 resolve_relative_url
()
53 remote
=$
(get_default_remote
)
54 remoteurl
=$
(git config
"remote.$remote.url") ||
55 remoteurl
=$
(pwd) # the repository is its own authoritative upstream
57 remoteurl
=${remoteurl%/}
70 remoteurl
="./$remoteurl"
81 remoteurl
="${remoteurl%/*}"
84 remoteurl
="${remoteurl%:*}"
88 if test -z "$is_relative" ||
test "." = "$remoteurl"
90 die
"$(eval_gettext "cannot strip one component off url
'\$remoteurl'")"
104 remoteurl
="$remoteurl$sep${url%/}"
105 echo "${is_relative:+${up_path}}${remoteurl#./}"
109 # Get submodule info for registered submodules
110 # $@ = path to limit submodule list
115 git ls-files
--error-unmatch --stage -- "$@" ||
116 echo "unmatched pathspec exists"
120 my ($null_sha1) = ("0" x 40);
124 if (/^unmatched pathspec/) {
129 my ($mode, $sha1, $stage, $path) =
130 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
131 next unless $mode eq "160000";
133 if (!$unmerged{$path}++) {
134 push @out, "$mode $null_sha1 U\t$path\n";
141 print "#unmatched\n";
150 if test "$1" = "#unmatched"
157 # Print a submodule configuration setting
159 # $1 = submodule name
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
() {
173 value
=$
(git config submodule.
"$name".
"$option")
176 value
=$
(git config
-f .gitmodules submodule.
"$name".
"$option")
178 printf '%s' "${value:-$default}"
183 # Map submodule path to submodule name
189 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
191 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
192 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
193 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
195 die
"$(eval_gettext "No submodule mapping found
in .gitmodules
for path
'\$sm_path'")"
202 # Prior to calling, cmd_update checks that a possibly existing
203 # path is not a git repository.
204 # Likewise, cmd_add checks that path does not exist at all,
205 # since it is the location of a new submodule.
214 if test -n "$GIT_QUIET"
221 base_name
=$
(dirname "$name")
223 gitdir
=$
(git rev-parse
--git-dir)
224 gitdir_base
="$gitdir/modules/$base_name"
225 gitdir
="$gitdir/modules/$name"
230 rm -f "$gitdir/index"
232 mkdir
-p "$gitdir_base"
235 git clone
$quiet -n ${reference:+"$reference"} \
236 --separate-git-dir "$gitdir" "$url" "$sm_path"
238 die
"$(eval_gettext "Clone of
'\$url' into submodule path
'\$sm_path' failed
")"
241 # We already are at the root of the work tree but cd_to_toplevel will
242 # resolve any symlinks that might be present in $PWD
243 a
=$
(cd_to_toplevel
&& cd "$gitdir" && pwd)/
244 b
=$
(cd_to_toplevel
&& cd "$sm_path" && pwd)/
245 # normalize Windows-style absolute paths to POSIX-style absolute paths
246 case $a in [a-zA-Z
]:/*) a
=/${a%%:*}${a#*:} ;; esac
247 case $b in [a-zA-Z
]:/*) b
=/${b%%:*}${b#*:} ;; esac
248 # Remove all common leading directories after a sanity check
249 if test "${a#$b}" != "$a" ||
test "${b#$a}" != "$b"; then
250 die
"$(eval_gettext "Gitdir
'\$a' is part of the submodule path
'\$b' or vice versa
")"
252 while test "${a%%/*}" = "${b%%/*}"
257 # Now chop off the trailing '/'s that were added in the beginning
261 # Turn each leading "*/" component into "../"
262 rel
=$
(echo $b |
sed -e 's|[^/][^/]*|..|g')
263 echo "gitdir: $rel/$a" >"$sm_path/.git"
265 rel
=$
(echo $a |
sed -e 's|[^/][^/]*|..|g')
266 (clear_local_git_env
; cd "$sm_path" && GIT_WORK_TREE
=. git config core.worktree
"$rel/$b")
270 # Add a new submodule to the working tree, .gitmodules and the index
274 # optional branch is stored in global branch variable
278 # parse $args after "submodule ... add".
283 case "$2" in '') usage
;; esac
294 case "$2" in '') usage
;; esac
295 reference
="--reference=$2"
302 case "$2" in '') usage
;; esac
323 if test -z "$sm_path"; then
324 sm_path
=$
(echo "$repo" |
325 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
328 if test -z "$repo" -o -z "$sm_path"; then
332 # assure repo is absolute or relative to parent
335 # dereference source url relative to parent's url
336 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
343 die
"$(eval_gettext "repo URL
: '\$repo' must be absolute or begin with .
/|..
/")"
348 # multiple //; leading ./; /./; /../; trailing /
349 sm_path
=$
(printf '%s/\n' "$sm_path" |
359 git ls-files
--error-unmatch "$sm_path" > /dev
/null
2>&1 &&
360 die
"$(eval_gettext "'\$sm_path' already exists
in the index
")"
362 if test -z "$force" && ! git add
--dry-run --ignore-missing "$sm_path" > /dev
/null
2>&1
364 eval_gettextln
"The following path is ignored by one of your .gitignore files:
366 Use -f if you really want to add it." >&2
370 if test -n "$custom_name"
372 sm_name
="$custom_name"
377 # perhaps the path exists and is already a git repo, else clone it
378 if test -e "$sm_path"
380 if test -d "$sm_path"/.git
-o -f "$sm_path"/.git
382 eval_gettextln
"Adding existing repo at '\$sm_path' to the index"
384 die
"$(eval_gettext "'\$sm_path' already exists and is not a valid git repo
")"
388 if test -d ".git/modules/$sm_name"
392 echo >&2 "$(eval_gettext "A git directory
for '\$sm_name' is found locally with remote
(s
):")"
393 GIT_DIR
=".git/modules/$sm_name" GIT_WORK_TREE
=. git remote
-v |
grep '(fetch)' |
sed -e s
,^
," ", -e s
,' (fetch)',, >&2
394 echo >&2 "$(eval_gettext "If you want to reuse this
local git directory instead of cloning again from
")"
395 echo >&2 " $realrepo"
396 echo >&2 "$(eval_gettext "use the
'--force' option. If the
local git directory is not the correct repo
")"
397 die
"$(eval_gettext "or you are unsure what this means choose another name with the
'--name' option.
")"
399 echo "$(eval_gettext "Reactivating
local git directory
for submodule
'\$sm_name'.
")"
402 module_clone
"$sm_path" "$sm_name" "$realrepo" "$reference" ||
exit
406 # ash fails to wordsplit ${branch:+-b "$branch"...}
408 '') git checkout
-f -q ;;
409 ?
*) git checkout
-f -q -B "$branch" "origin/$branch" ;;
411 ) || die
"$(eval_gettext "Unable to checkout submodule
'\$sm_path'")"
413 git config submodule.
"$sm_name".url
"$realrepo"
415 git add
$force "$sm_path" ||
416 die
"$(eval_gettext "Failed to add submodule
'\$sm_path'")"
418 git config
-f .gitmodules submodule.
"$sm_name".path
"$sm_path" &&
419 git config
-f .gitmodules submodule.
"$sm_name".url
"$repo" &&
422 git config
-f .gitmodules submodule.
"$sm_name".branch
"$branch"
424 git add
--force .gitmodules ||
425 die
"$(eval_gettext "Failed to register submodule
'\$sm_path'")"
429 # Execute an arbitrary command sequence in each checked out
432 # $@ = command to execute
436 # parse $args after "submodule ... foreach".
458 # dup stdin so that it can be restored when running the external
459 # command in the subshell (and a recursive call to this function)
463 while read mode sha1 stage sm_path
465 die_if_unmatched
"$mode"
466 if test -e "$sm_path"/.git
468 say
"$(eval_gettext "Entering
'\$prefix\$sm_path'")"
469 name
=$
(module_name
"$sm_path")
471 prefix
="$prefix$sm_path/"
473 # we make $path available to scripts ...
477 if test -n "$recursive"
479 cmd_foreach
"--recursive" "$@"
482 die
"$(eval_gettext "Stopping
at '\$sm_path'; script returned non-zero status.
")"
488 # Register submodules in .git/config
490 # $@ = requested paths (default to all)
494 # parse $args after "submodule ... init".
516 while read mode sha1 stage sm_path
518 die_if_unmatched
"$mode"
519 name
=$
(module_name
"$sm_path") ||
exit
521 # Copy url setting when it is not set yet
522 if test -z "$(git config "submodule.
$name.url
")"
524 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
526 die
"$(eval_gettext "No url found
for submodule path
'\$sm_path' in .gitmodules
")"
528 # Possibly a url relative to parent
531 url
=$
(resolve_relative_url
"$url") ||
exit
534 git config submodule.
"$name".url
"$url" ||
535 die
"$(eval_gettext "Failed to register url
for submodule path
'\$sm_path'")"
537 say
"$(eval_gettext "Submodule
'\$name' (\
$url) registered
for path
'\$sm_path'")"
540 # Copy "update" setting when it is not set yet
541 upd
="$(git config -f .gitmodules submodule."$name".update)"
543 test -n "$(git config submodule."$name".update)" ||
544 git config submodule.
"$name".update
"$upd" ||
545 die
"$(eval_gettext "Failed to register update mode
for submodule path
'\$sm_path'")"
550 # Update each submodule path to correct revision, using clone and checkout as needed
552 # $@ = requested paths (default to all)
556 # parse $args after "submodule ... update".
580 case "$2" in '') usage
;; esac
581 reference
="--reference=$2"
582 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
608 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
614 cmd_init
"--" "$@" ||
return
620 while read mode sha1 stage sm_path
622 die_if_unmatched
"$mode"
625 echo >&2 "Skipping unmerged submodule $sm_path"
628 name
=$
(module_name
"$sm_path") ||
exit
629 url
=$
(git config submodule.
"$name".url
)
630 branch
=$
(get_submodule_config
"$name" branch master
)
631 if ! test -z "$update"
633 update_module
=$update
635 update_module
=$
(git config submodule.
"$name".update
)
638 if test "$update_module" = "none"
640 echo "Skipping submodule '$sm_path'"
646 # Only mention uninitialized submodules when its
647 # path have been specified
649 say
"$(eval_gettext "Submodule path
'\$sm_path' not initialized
650 Maybe you want to use
'update --init'?
")"
654 if ! test -d "$sm_path"/.git
-o -f "$sm_path"/.git
656 module_clone
"$sm_path" "$name" "$url" "$reference" ||
exit
657 cloned_modules
="$cloned_modules;$name"
660 subsha1
=$
(clear_local_git_env
; cd "$sm_path" &&
661 git rev-parse
--verify HEAD
) ||
662 die
"$(eval_gettext "Unable to
find current revision
in submodule path
'\$sm_path'")"
667 if test -z "$nofetch"
669 # Fetch remote before determining tracking $sha1
670 (clear_local_git_env
; cd "$sm_path" && git-fetch
) ||
671 die
"$(eval_gettext "Unable to fetch
in submodule path
'\$sm_path'")"
673 remote_name
=$
(clear_local_git_env
; cd "$sm_path" && get_default_remote
)
674 sha1
=$
(clear_local_git_env
; cd "$sm_path" &&
675 git rev-parse
--verify "${remote_name}/${branch}") ||
676 die
"$(eval_gettext "Unable to
find current
${remote_name}/${branch} revision
in submodule path
'\$sm_path'")"
679 if test "$subsha1" != "$sha1" -o -n "$force"
682 # If we don't already have a -f flag and the submodule has never been checked out
683 if test -z "$subsha1" -a -z "$force"
688 if test -z "$nofetch"
690 # Run fetch only if $sha1 isn't present or it
691 # is not reachable from a ref.
692 (clear_local_git_env
; cd "$sm_path" &&
693 ( (rev=$
(git rev-list
-n 1 $sha1 --not --all 2>/dev
/null
) &&
694 test -z "$rev") || git-fetch
)) ||
695 die
"$(eval_gettext "Unable to fetch
in submodule path
'\$sm_path'")"
698 # Is this something we just cloned?
699 case ";$cloned_modules;" in
701 # then there is no local change to integrate
706 case "$update_module" in
709 die_msg
="$(eval_gettext "Unable to rebase
'\$sha1' in submodule path
'\$sm_path'")"
710 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': rebased into
'\$sha1'")"
711 must_die_on_failure
=yes
715 die_msg
="$(eval_gettext "Unable to merge
'\$sha1' in submodule path
'\$sm_path'")"
716 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': merged
in '\$sha1'")"
717 must_die_on_failure
=yes
720 command="git checkout $subforce -q"
721 die_msg
="$(eval_gettext "Unable to checkout
'\$sha1' in submodule path
'\$sm_path'")"
722 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': checked out
'\$sha1'")"
726 if (clear_local_git_env
; cd "$sm_path" && $command "$sha1")
729 elif test -n "$must_die_on_failure"
731 die_with_status
2 "$die_msg"
733 err
="${err};$die_msg"
738 if test -n "$recursive"
740 (clear_local_git_env
; cd "$sm_path" && eval cmd_update
"$orig_flags")
744 die_msg
="$(eval_gettext "Failed to recurse into submodule path
'\$sm_path'")"
747 err
="${err};$die_msg"
750 die_with_status
$res "$die_msg"
777 git describe
"$2" 2>/dev
/null ||
778 git describe
--tags "$2" 2>/dev
/null ||
779 git describe
--contains "$2" 2>/dev
/null ||
780 git describe
--all --always "$2"
783 test -z "$revname" || revname
=" ($revname)"
786 # Show commit summary for submodules in index or working tree
788 # If '--cached' is given, show summary between index and given commit,
789 # or between working tree and given commit
791 # $@ = [commit (default 'HEAD'),] requested paths (default all)
798 # parse $args after "submodule ... summary".
812 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
834 test $summary_limit = 0 && return
836 if rev=$
(git rev-parse
-q --verify --default HEAD
${1+"$1"})
840 elif test -z "$1" -o "$1" = "HEAD"
842 # before the first commit: compare with an empty tree
843 head=$
(git hash-object
-w -t tree
--stdin </dev
/null
)
844 test -z "$1" ||
shift
852 die
"$(gettext "The
--cached option cannot be used with the
--files option
")"
858 # Get modified modules cared by user
859 modules
=$
(git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- "$@" |
860 sane_egrep
'^:([0-7]* )?160000' |
861 while read mod_src mod_dst sha1_src sha1_dst status name
863 # Always show modules deleted or type-changed (blob<->module)
864 test $status = D
-o $status = T
&& echo "$name" && continue
865 # Also show added or modified modules which are checked out
866 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
871 test -z "$modules" && return
873 git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- $modules |
874 sane_egrep
'^:([0-7]* )?160000' |
876 while read mod_src mod_dst sha1_src sha1_dst status name
878 if test -z "$cached" &&
879 test $sha1_dst = 0000000000000000000000000000000000000000
883 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
885 100644 |
100755 |
120000)
886 sha1_dst
=$
(git hash-object
$name)
892 eval_gettextln
"unexpected mode \$mod_dst" >&2
899 test $mod_src = 160000 &&
900 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
903 test $mod_dst = 160000 &&
904 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
908 case "$missing_src,$missing_dst" in
910 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commit \$sha1_src")"
913 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \
$sha1_dst")"
916 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commits \$sha1_src and \$sha1_dst")"
921 if test $mod_src = 160000 -a $mod_dst = 160000
923 range="$sha1_src...$sha1_dst"
924 elif test $mod_src = 160000
930 GIT_DIR="$name/.git" \
931 git rev-list --first-parent $range -- | wc -l
933 total_commits=" ($(($total_commits + 0)))"
937 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
938 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
941 blob="$(gettext "blob")"
942 submodule="$(gettext "submodule")"
943 if test $mod_dst = 160000
945 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
947 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
950 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
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"
958 if test $mod_src = 160000 -a $mod_dst = 160000
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
967 GIT_DIR
="$name/.git" \
968 git log
--pretty='format: > %s' -1 $sha1_dst
970 GIT_DIR
="$name/.git" \
971 git log
--pretty='format: < %s' -1 $sha1_src
977 if test -n "$for_status"; then
978 if [ -n "$files" ]; then
979 gettextln
"# Submodules changed but not updated:"
981 gettextln
"# Submodule changes to be committed:"
984 sed -e 's|^|# |' -e 's|^# $|#|'
990 # List all submodules, prefixed with:
991 # - submodule not initialized
992 # + different revision checked out
994 # If --cached was specified the revision in the index will be printed
995 # instead of the currently checked out revision.
997 # $@ = requested paths (default to all)
1001 # parse $args after "submodule ... status".
1029 while read mode sha1 stage sm_path
1031 die_if_unmatched
"$mode"
1032 name
=$
(module_name
"$sm_path") ||
exit
1033 url
=$
(git config submodule.
"$name".url
)
1034 displaypath
="$prefix$sm_path"
1035 if test "$stage" = U
1037 say
"U$sha1 $displaypath"
1040 if test -z "$url" ||
! test -d "$sm_path"/.git
-o -f "$sm_path"/.git
1042 say
"-$sha1 $displaypath"
1045 set_name_rev
"$sm_path" "$sha1"
1046 if git diff-files
--ignore-submodules=dirty
--quiet -- "$sm_path"
1048 say
" $sha1 $displaypath$revname"
1050 if test -z "$cached"
1052 sha1
=$
(clear_local_git_env
; cd "$sm_path" && git rev-parse
--verify HEAD
)
1053 set_name_rev
"$sm_path" "$sha1"
1055 say
"+$sha1 $displaypath$revname"
1058 if test -n "$recursive"
1061 prefix
="$displaypath/"
1066 die
"$(eval_gettext "Failed to recurse into submodule path
'\$sm_path'")"
1071 # Sync remote urls for submodules
1072 # This makes the value for remote.$remote.url match the value
1073 # specified in .gitmodules.
1102 while read mode sha1 stage sm_path
1104 die_if_unmatched
"$mode"
1105 name
=$
(module_name
"$sm_path")
1106 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
1108 # Possibly a url relative to parent
1111 # rewrite foo/bar as ../.. to find path from
1112 # submodule work tree to superproject work tree
1113 up_path
="$(echo "$sm_path" | sed "s
/[^
/][^
/]*/..
/g
")" &&
1114 # guarantee a trailing /
1115 up_path
=${up_path%/}/ &&
1116 # path from submodule work tree to submodule origin repo
1117 sub_origin_url
=$
(resolve_relative_url
"$url" "$up_path") &&
1118 # path from superproject work tree to submodule origin repo
1119 super_config_url
=$
(resolve_relative_url
"$url") ||
exit
1122 sub_origin_url
="$url"
1123 super_config_url
="$url"
1127 if git config
"submodule.$name.url" >/dev
/null
2>/dev
/null
1129 say
"$(eval_gettext "Synchronizing submodule url
for '\$prefix\$sm_path'")"
1130 git config submodule.
"$name".url
"$super_config_url"
1132 if test -e "$sm_path"/.git
1137 remote
=$
(get_default_remote
)
1138 git config remote.
"$remote".url
"$sub_origin_url"
1140 if test -n "$recursive"
1142 prefix
="$prefix$sm_path/"
1151 # This loop parses the command line arguments to find the
1152 # subcommand name to dispatch. Parsing of the subcommand specific
1153 # options are primarily done by the subcommand implementations.
1154 # Subcommand specific options such as --branch and --cached are
1155 # parsed here as well, for backward compatibility.
1157 while test $# != 0 && test -z "$command"
1160 add | foreach | init | update | status | summary | sync
)
1190 # No command word defaults to "status"
1191 if test -z "$command"
1201 # "-b branch" is accepted only by "add"
1202 if test -n "$branch" && test "$command" != add
1207 # "--cached" is accepted only by "status" and "summary"
1208 if test -n "$cached" && test "$command" != status
-a "$command" != summary