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
335 The following path is ignored by one of your .gitignore files:
336 $(eval_gettextln $sm_path)
337 Use -f if you really want to add it.
342 if test -n "$custom_name"
344 sm_name
="$custom_name"
349 # perhaps the path exists and is already a git repo, else clone it
350 if test -e "$sm_path"
352 if test -d "$sm_path"/.git
-o -f "$sm_path"/.git
354 eval_gettextln
"Adding existing repo at '\$sm_path' to the index"
356 die
"$(eval_gettext "'\$sm_path' already exists and is not a valid git repo
")"
360 if test -d ".git/modules/$sm_name"
364 echo >&2 "$(eval_gettext "A git directory
for '\$sm_name' is found locally with remote
(s
):")"
365 GIT_DIR
=".git/modules/$sm_name" GIT_WORK_TREE
=. git remote
-v |
grep '(fetch)' |
sed -e s
,^
," ", -e s
,' (fetch)',, >&2
366 echo >&2 "$(eval_gettext "If you want to reuse this
local git directory instead of cloning again from
")"
367 echo >&2 " $realrepo"
368 echo >&2 "$(eval_gettext "use the
'--force' option. If the
local git directory is not the correct repo
")"
369 die
"$(eval_gettext "or you are unsure what this means choose another name with the
'--name' option.
")"
371 echo "$(eval_gettext "Reactivating
local git directory
for submodule
'\$sm_name'.
")"
374 module_clone
"$sm_path" "$sm_name" "$realrepo" "$reference" ||
exit
378 # ash fails to wordsplit ${branch:+-b "$branch"...}
380 '') git checkout
-f -q ;;
381 ?
*) git checkout
-f -q -B "$branch" "origin/$branch" ;;
383 ) || die
"$(eval_gettext "Unable to checkout submodule
'\$sm_path'")"
385 git config submodule.
"$sm_name".url
"$realrepo"
387 git add
$force "$sm_path" ||
388 die
"$(eval_gettext "Failed to add submodule
'\$sm_path'")"
390 git config
-f .gitmodules submodule.
"$sm_name".path
"$sm_path" &&
391 git config
-f .gitmodules submodule.
"$sm_name".url
"$repo" &&
392 git add
--force .gitmodules ||
393 die
"$(eval_gettext "Failed to register submodule
'\$sm_path'")"
397 # Execute an arbitrary command sequence in each checked out
400 # $@ = command to execute
404 # parse $args after "submodule ... foreach".
426 # dup stdin so that it can be restored when running the external
427 # command in the subshell (and a recursive call to this function)
431 while read mode sha1 stage sm_path
433 die_if_unmatched
"$mode"
434 if test -e "$sm_path"/.git
436 say
"$(eval_gettext "Entering
'\$prefix\$sm_path'")"
437 name
=$
(module_name
"$sm_path")
439 prefix
="$prefix$sm_path/"
441 # we make $path available to scripts ...
445 if test -n "$recursive"
447 cmd_foreach
"--recursive" "$@"
450 die
"$(eval_gettext "Stopping
at '\$sm_path'; script returned non-zero status.
")"
456 # Register submodules in .git/config
458 # $@ = requested paths (default to all)
462 # parse $args after "submodule ... init".
484 while read mode sha1 stage sm_path
486 die_if_unmatched
"$mode"
487 name
=$
(module_name
"$sm_path") ||
exit
489 # Copy url setting when it is not set yet
490 if test -z "$(git config "submodule.
$name.url
")"
492 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
494 die
"$(eval_gettext "No url found
for submodule path
'\$sm_path' in .gitmodules
")"
496 # Possibly a url relative to parent
499 url
=$
(resolve_relative_url
"$url") ||
exit
502 git config submodule.
"$name".url
"$url" ||
503 die
"$(eval_gettext "Failed to register url
for submodule path
'\$sm_path'")"
505 say
"$(eval_gettext "Submodule
'\$name' (\
$url) registered
for path
'\$sm_path'")"
508 # Copy "update" setting when it is not set yet
509 upd
="$(git config -f .gitmodules submodule."$name".update)"
511 test -n "$(git config submodule."$name".update)" ||
512 git config submodule.
"$name".update
"$upd" ||
513 die
"$(eval_gettext "Failed to register update mode
for submodule path
'\$sm_path'")"
518 # Update each submodule path to correct revision, using clone and checkout as needed
520 # $@ = requested paths (default to all)
524 # parse $args after "submodule ... update".
545 case "$2" in '') usage
;; esac
546 reference
="--reference=$2"
547 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
573 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
579 cmd_init
"--" "$@" ||
return
585 while read mode sha1 stage sm_path
587 die_if_unmatched
"$mode"
590 echo >&2 "Skipping unmerged submodule $sm_path"
593 name
=$
(module_name
"$sm_path") ||
exit
594 url
=$
(git config submodule.
"$name".url
)
595 if ! test -z "$update"
597 update_module
=$update
599 update_module
=$
(git config submodule.
"$name".update
)
602 if test "$update_module" = "none"
604 echo "Skipping submodule '$sm_path'"
610 # Only mention uninitialized submodules when its
611 # path have been specified
613 say
"$(eval_gettext "Submodule path
'\$sm_path' not initialized
614 Maybe you want to use
'update --init'?
")"
618 if ! test -d "$sm_path"/.git
-o -f "$sm_path"/.git
620 module_clone
"$sm_path" "$name" "$url" "$reference" ||
exit
621 cloned_modules
="$cloned_modules;$name"
624 subsha1
=$
(clear_local_git_env
; cd "$sm_path" &&
625 git rev-parse
--verify HEAD
) ||
626 die
"$(eval_gettext "Unable to
find current revision
in submodule path
'\$sm_path'")"
629 if test "$subsha1" != "$sha1" -o -n "$force"
632 # If we don't already have a -f flag and the submodule has never been checked out
633 if test -z "$subsha1" -a -z "$force"
638 if test -z "$nofetch"
640 # Run fetch only if $sha1 isn't present or it
641 # is not reachable from a ref.
642 (clear_local_git_env
; cd "$sm_path" &&
643 ( (rev=$
(git rev-list
-n 1 $sha1 --not --all 2>/dev
/null
) &&
644 test -z "$rev") || git-fetch
)) ||
645 die
"$(eval_gettext "Unable to fetch
in submodule path
'\$sm_path'")"
648 # Is this something we just cloned?
649 case ";$cloned_modules;" in
651 # then there is no local change to integrate
656 case "$update_module" in
659 die_msg
="$(eval_gettext "Unable to rebase
'\$sha1' in submodule path
'\$sm_path'")"
660 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': rebased into
'\$sha1'")"
661 must_die_on_failure
=yes
665 die_msg
="$(eval_gettext "Unable to merge
'\$sha1' in submodule path
'\$sm_path'")"
666 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': merged
in '\$sha1'")"
667 must_die_on_failure
=yes
670 command="git checkout $subforce -q"
671 die_msg
="$(eval_gettext "Unable to checkout
'\$sha1' in submodule path
'\$sm_path'")"
672 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': checked out
'\$sha1'")"
676 if (clear_local_git_env
; cd "$sm_path" && $command "$sha1")
679 elif test -n "$must_die_on_failure"
681 die_with_status
2 "$die_msg"
683 err
="${err};$die_msg"
688 if test -n "$recursive"
690 (clear_local_git_env
; cd "$sm_path" && eval cmd_update
"$orig_flags")
694 die_msg
="$(eval_gettext "Failed to recurse into submodule path
'\$sm_path'")"
697 err
="${err};$die_msg"
700 die_with_status
$res "$die_msg"
727 git describe
"$2" 2>/dev
/null ||
728 git describe
--tags "$2" 2>/dev
/null ||
729 git describe
--contains "$2" 2>/dev
/null ||
730 git describe
--all --always "$2"
733 test -z "$revname" || revname
=" ($revname)"
736 # Show commit summary for submodules in index or working tree
738 # If '--cached' is given, show summary between index and given commit,
739 # or between working tree and given commit
741 # $@ = [commit (default 'HEAD'),] requested paths (default all)
748 # parse $args after "submodule ... summary".
762 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
784 test $summary_limit = 0 && return
786 if rev=$
(git rev-parse
-q --verify --default HEAD
${1+"$1"})
790 elif test -z "$1" -o "$1" = "HEAD"
792 # before the first commit: compare with an empty tree
793 head=$
(git hash-object
-w -t tree
--stdin </dev
/null
)
794 test -z "$1" ||
shift
802 die
"$(gettext "The
--cached option cannot be used with the
--files option
")"
808 # Get modified modules cared by user
809 modules
=$
(git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- "$@" |
810 sane_egrep
'^:([0-7]* )?160000' |
811 while read mod_src mod_dst sha1_src sha1_dst status name
813 # Always show modules deleted or type-changed (blob<->module)
814 test $status = D
-o $status = T
&& echo "$name" && continue
815 # Also show added or modified modules which are checked out
816 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
821 test -z "$modules" && return
823 git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- $modules |
824 sane_egrep
'^:([0-7]* )?160000' |
826 while read mod_src mod_dst sha1_src sha1_dst status name
828 if test -z "$cached" &&
829 test $sha1_dst = 0000000000000000000000000000000000000000
833 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
835 100644 |
100755 |
120000)
836 sha1_dst
=$
(git hash-object
$name)
842 eval_gettextln
"unexpected mode \$mod_dst" >&2
849 test $mod_src = 160000 &&
850 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
853 test $mod_dst = 160000 &&
854 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
858 case "$missing_src,$missing_dst" in
860 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commit \$sha1_src")"
863 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \
$sha1_dst")"
866 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commits \$sha1_src and \$sha1_dst")"
871 if test $mod_src = 160000 -a $mod_dst = 160000
873 range="$sha1_src...$sha1_dst"
874 elif test $mod_src = 160000
880 GIT_DIR="$name/.git" \
881 git rev-list --first-parent $range -- | wc -l
883 total_commits=" ($(($total_commits + 0)))"
887 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
888 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
891 blob="$(gettext "blob")"
892 submodule="$(gettext "submodule")"
893 if test $mod_dst = 160000
895 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
897 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
900 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
904 # Don't give error msg
for modification whose dst is not submodule
905 # i.e. deleted or changed to blob
906 test $mod_dst = 160000 && echo "$errmsg"
908 if test $mod_src = 160000 -a $mod_dst = 160000
911 test $summary_limit -gt 0 && limit
="-$summary_limit"
912 GIT_DIR
="$name/.git" \
913 git log
$limit --pretty='format: %m %s' \
914 --first-parent $sha1_src...
$sha1_dst
915 elif test $mod_dst = 160000
917 GIT_DIR
="$name/.git" \
918 git log
--pretty='format: > %s' -1 $sha1_dst
920 GIT_DIR
="$name/.git" \
921 git log
--pretty='format: < %s' -1 $sha1_src
927 if test -n "$for_status"; then
928 if [ -n "$files" ]; then
929 status_msg
="$(gettextln "# Submodules changed but not updated:")"
931 status_msg
="$(gettextln "# Submodule changes to be committed:")"
933 status_sed
=$
(sed -e 's|^|# |' -e 's|^# $|#|')
944 # List all submodules, prefixed with:
945 # - submodule not initialized
946 # + different revision checked out
948 # If --cached was specified the revision in the index will be printed
949 # instead of the currently checked out revision.
951 # $@ = requested paths (default to all)
955 # parse $args after "submodule ... status".
983 while read mode sha1 stage sm_path
985 die_if_unmatched
"$mode"
986 name
=$
(module_name
"$sm_path") ||
exit
987 url
=$
(git config submodule.
"$name".url
)
988 displaypath
="$prefix$sm_path"
991 say
"U$sha1 $displaypath"
994 if test -z "$url" ||
! test -d "$sm_path"/.git
-o -f "$sm_path"/.git
996 say
"-$sha1 $displaypath"
999 set_name_rev
"$sm_path" "$sha1"
1000 if git diff-files
--ignore-submodules=dirty
--quiet -- "$sm_path"
1002 say
" $sha1 $displaypath$revname"
1004 if test -z "$cached"
1006 sha1
=$
(clear_local_git_env
; cd "$sm_path" && git rev-parse
--verify HEAD
)
1007 set_name_rev
"$sm_path" "$sha1"
1009 say
"+$sha1 $displaypath$revname"
1012 if test -n "$recursive"
1015 prefix
="$displaypath/"
1020 die
"$(eval_gettext "Failed to recurse into submodule path
'\$sm_path'")"
1025 # Sync remote urls for submodules
1026 # This makes the value for remote.$remote.url match the value
1027 # specified in .gitmodules.
1056 while read mode sha1 stage sm_path
1058 die_if_unmatched
"$mode"
1059 name
=$
(module_name
"$sm_path")
1060 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
1062 # Possibly a url relative to parent
1065 # rewrite foo/bar as ../.. to find path from
1066 # submodule work tree to superproject work tree
1067 up_path
="$(echo "$sm_path" | sed "s
/[^
/][^
/]*/..
/g
")" &&
1068 # guarantee a trailing /
1069 up_path
=${up_path%/}/ &&
1070 # path from submodule work tree to submodule origin repo
1071 sub_origin_url
=$
(resolve_relative_url
"$url" "$up_path") &&
1072 # path from superproject work tree to submodule origin repo
1073 super_config_url
=$
(resolve_relative_url
"$url") ||
exit
1076 sub_origin_url
="$url"
1077 super_config_url
="$url"
1081 if git config
"submodule.$name.url" >/dev
/null
2>/dev
/null
1083 say
"$(eval_gettext "Synchronizing submodule url
for '\$prefix\$sm_path'")"
1084 git config submodule.
"$name".url
"$super_config_url"
1086 if test -e "$sm_path"/.git
1091 remote
=$
(get_default_remote
)
1092 git config remote.
"$remote".url
"$sub_origin_url"
1094 if test -n "$recursive"
1096 prefix
="$prefix$sm_path/"
1105 # This loop parses the command line arguments to find the
1106 # subcommand name to dispatch. Parsing of the subcommand specific
1107 # options are primarily done by the subcommand implementations.
1108 # Subcommand specific options such as --branch and --cached are
1109 # parsed here as well, for backward compatibility.
1111 while test $# != 0 && test -z "$command"
1114 add | foreach | init | update | status | summary | sync
)
1144 # No command word defaults to "status"
1145 if test -z "$command"
1155 # "-b branch" is accepted only by "add"
1156 if test -n "$branch" && test "$command" != add
1161 # "--cached" is accepted only by "status" and "summary"
1162 if test -n "$cached" && test "$command" != status
-a "$command" != summary