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] [--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 [--] [<path>...]"
33 # The function takes at most 2 arguments. The first argument is the
34 # URL that navigates to the submodule origin repo. When relative, this URL
35 # is relative to the superproject origin URL repo. The second up_path
36 # argument, if specified, is the relative path that navigates
37 # from the submodule working tree to the superproject working tree.
39 # The output of the function is the origin URL of the submodule.
41 # The output will either be an absolute URL or filesystem path (if the
42 # superproject origin URL is an absolute URL or filesystem path,
43 # respectively) or a relative file system path (if the superproject
44 # origin URL is a relative file system path).
46 # When the output is a relative file system path, the path is either
47 # relative to the submodule working tree, if up_path is specified, or to
48 # the superproject working tree otherwise.
49 resolve_relative_url
()
51 remote
=$
(get_default_remote
)
52 remoteurl
=$
(git config
"remote.$remote.url") ||
53 remoteurl
=$
(pwd) # the repository is its own authoritative upstream
55 remoteurl
=${remoteurl%/}
68 remoteurl
="./$remoteurl"
79 remoteurl
="${remoteurl%/*}"
82 remoteurl
="${remoteurl%:*}"
86 if test -z "$is_relative" ||
test "." = "$remoteurl"
88 die
"$(eval_gettext "cannot strip one component off url
'\$remoteurl'")"
102 remoteurl
="$remoteurl$sep${url%/}"
103 echo "${is_relative:+${up_path}}${remoteurl#./}"
107 # Get submodule info for registered submodules
108 # $@ = path to limit submodule list
112 git ls-files
--error-unmatch --stage -- "$@" |
115 my ($null_sha1) = ("0" x 40);
118 my ($mode, $sha1, $stage, $path) =
119 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
120 next unless $mode eq "160000";
122 if (!$unmerged{$path}++) {
123 print "$mode $null_sha1 U\t$path\n";
133 # Map submodule path to submodule name
139 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
141 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
142 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
143 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
145 die
"$(eval_gettext "No submodule mapping found
in .gitmodules
for path
'\$sm_path'")"
152 # Prior to calling, cmd_update checks that a possibly existing
153 # path is not a git repository.
154 # Likewise, cmd_add checks that path does not exist at all,
155 # since it is the location of a new submodule.
163 if test -n "$GIT_QUIET"
170 name
=$
(module_name
"$sm_path" 2>/dev
/null
)
171 test -n "$name" || name
="$sm_path"
172 base_name
=$
(dirname "$name")
174 gitdir
=$
(git rev-parse
--git-dir)
175 gitdir_base
="$gitdir/modules/$base_name"
176 gitdir
="$gitdir/modules/$name"
181 rm -f "$gitdir/index"
183 mkdir
-p "$gitdir_base"
186 git clone
$quiet -n ${reference:+"$reference"} \
187 --separate-git-dir "$gitdir" "$url" "$sm_path"
189 die
"$(eval_gettext "Clone of
'\$url' into submodule path
'\$sm_path' failed
")"
192 # We already are at the root of the work tree but cd_to_toplevel will
193 # resolve any symlinks that might be present in $PWD
194 a
=$
(cd_to_toplevel
&& cd "$gitdir" && pwd)/
195 b
=$
(cd_to_toplevel
&& cd "$sm_path" && pwd)/
196 # normalize Windows-style absolute paths to POSIX-style absolute paths
197 case $a in [a-zA-Z
]:/*) a
=/${a%%:*}${a#*:} ;; esac
198 case $b in [a-zA-Z
]:/*) b
=/${b%%:*}${b#*:} ;; esac
199 # Remove all common leading directories after a sanity check
200 if test "${a#$b}" != "$a" ||
test "${b#$a}" != "$b"; then
201 die
"$(eval_gettext "Gitdir
'\$a' is part of the submodule path
'\$b' or vice versa
")"
203 while test "${a%%/*}" = "${b%%/*}"
208 # Now chop off the trailing '/'s that were added in the beginning
212 # Turn each leading "*/" component into "../"
213 rel
=$
(echo $b |
sed -e 's|[^/][^/]*|..|g')
214 echo "gitdir: $rel/$a" >"$sm_path/.git"
216 rel
=$
(echo $a |
sed -e 's|[^/][^/]*|..|g')
217 (clear_local_git_env
; cd "$sm_path" && GIT_WORK_TREE
=. git config core.worktree
"$rel/$b")
221 # Add a new submodule to the working tree, .gitmodules and the index
225 # optional branch is stored in global branch variable
229 # parse $args after "submodule ... add".
234 case "$2" in '') usage
;; esac
245 case "$2" in '') usage
;; esac
246 reference
="--reference=$2"
270 if test -z "$sm_path"; then
271 sm_path
=$
(echo "$repo" |
272 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
275 if test -z "$repo" -o -z "$sm_path"; then
279 # assure repo is absolute or relative to parent
282 # dereference source url relative to parent's url
283 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
290 die
"$(eval_gettext "repo URL
: '\$repo' must be absolute or begin with .
/|..
/")"
295 # multiple //; leading ./; /./; /../; trailing /
296 sm_path
=$
(printf '%s/\n' "$sm_path" |
306 git ls-files
--error-unmatch "$sm_path" > /dev
/null
2>&1 &&
307 die
"$(eval_gettext "'\$sm_path' already exists
in the index
")"
309 if test -z "$force" && ! git add
--dry-run --ignore-missing "$sm_path" > /dev
/null
2>&1
311 eval_gettextln
"The following path is ignored by one of your .gitignore files:
313 Use -f if you really want to add it." >&2
317 # perhaps the path exists and is already a git repo, else clone it
318 if test -e "$sm_path"
320 if test -d "$sm_path"/.git
-o -f "$sm_path"/.git
322 eval_gettextln
"Adding existing repo at '\$sm_path' to the index"
324 die
"$(eval_gettext "'\$sm_path' already exists and is not a valid git repo
")"
329 module_clone
"$sm_path" "$realrepo" "$reference" ||
exit
333 # ash fails to wordsplit ${branch:+-b "$branch"...}
335 '') git checkout
-f -q ;;
336 ?
*) git checkout
-f -q -B "$branch" "origin/$branch" ;;
338 ) || die
"$(eval_gettext "Unable to checkout submodule
'\$sm_path'")"
340 git config submodule.
"$sm_path".url
"$realrepo"
342 git add
$force "$sm_path" ||
343 die
"$(eval_gettext "Failed to add submodule
'\$sm_path'")"
345 git config
-f .gitmodules submodule.
"$sm_path".path
"$sm_path" &&
346 git config
-f .gitmodules submodule.
"$sm_path".url
"$repo" &&
347 git add
--force .gitmodules ||
348 die
"$(eval_gettext "Failed to register submodule
'\$sm_path'")"
352 # Execute an arbitrary command sequence in each checked out
355 # $@ = command to execute
359 # parse $args after "submodule ... foreach".
381 # dup stdin so that it can be restored when running the external
382 # command in the subshell (and a recursive call to this function)
386 while read mode sha1 stage sm_path
388 if test -e "$sm_path"/.git
390 say
"$(eval_gettext "Entering
'\$prefix\$sm_path'")"
391 name
=$
(module_name
"$sm_path")
393 prefix
="$prefix$sm_path/"
395 # we make $path available to scripts ...
399 if test -n "$recursive"
401 cmd_foreach
"--recursive" "$@"
404 die
"$(eval_gettext "Stopping
at '\$sm_path'; script returned non-zero status.
")"
410 # Register submodules in .git/config
412 # $@ = requested paths (default to all)
416 # parse $args after "submodule ... init".
438 while read mode sha1 stage sm_path
440 name
=$
(module_name
"$sm_path") ||
exit
442 # Copy url setting when it is not set yet
443 if test -z "$(git config "submodule.
$name.url
")"
445 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
447 die
"$(eval_gettext "No url found
for submodule path
'\$sm_path' in .gitmodules
")"
449 # Possibly a url relative to parent
452 url
=$
(resolve_relative_url
"$url") ||
exit
455 git config submodule.
"$name".url
"$url" ||
456 die
"$(eval_gettext "Failed to register url
for submodule path
'\$sm_path'")"
458 say
"$(eval_gettext "Submodule
'\$name' (\
$url) registered
for path
'\$sm_path'")"
461 # Copy "update" setting when it is not set yet
462 upd
="$(git config -f .gitmodules submodule."$name".update)"
464 test -n "$(git config submodule."$name".update)" ||
465 git config submodule.
"$name".update
"$upd" ||
466 die
"$(eval_gettext "Failed to register update mode
for submodule path
'\$sm_path'")"
471 # Update each submodule path to correct revision, using clone and checkout as needed
473 # $@ = requested paths (default to all)
477 # parse $args after "submodule ... update".
498 case "$2" in '') usage
;; esac
499 reference
="--reference=$2"
500 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
526 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
532 cmd_init
"--" "$@" ||
return
538 while read mode sha1 stage sm_path
542 echo >&2 "Skipping unmerged submodule $sm_path"
545 name
=$
(module_name
"$sm_path") ||
exit
546 url
=$
(git config submodule.
"$name".url
)
547 if ! test -z "$update"
549 update_module
=$update
551 update_module
=$
(git config submodule.
"$name".update
)
554 if test "$update_module" = "none"
556 echo "Skipping submodule '$sm_path'"
562 # Only mention uninitialized submodules when its
563 # path have been specified
565 say
"$(eval_gettext "Submodule path
'\$sm_path' not initialized
566 Maybe you want to use
'update --init'?
")"
570 if ! test -d "$sm_path"/.git
-o -f "$sm_path"/.git
572 module_clone
"$sm_path" "$url" "$reference"||
exit
573 cloned_modules
="$cloned_modules;$name"
576 subsha1
=$
(clear_local_git_env
; cd "$sm_path" &&
577 git rev-parse
--verify HEAD
) ||
578 die
"$(eval_gettext "Unable to
find current revision
in submodule path
'\$sm_path'")"
581 if test "$subsha1" != "$sha1"
584 # If we don't already have a -f flag and the submodule has never been checked out
585 if test -z "$subsha1" -a -z "$force"
590 if test -z "$nofetch"
592 # Run fetch only if $sha1 isn't present or it
593 # is not reachable from a ref.
594 (clear_local_git_env
; cd "$sm_path" &&
595 ( (rev=$
(git rev-list
-n 1 $sha1 --not --all 2>/dev
/null
) &&
596 test -z "$rev") || git-fetch
)) ||
597 die
"$(eval_gettext "Unable to fetch
in submodule path
'\$sm_path'")"
600 # Is this something we just cloned?
601 case ";$cloned_modules;" in
603 # then there is no local change to integrate
608 case "$update_module" in
611 die_msg
="$(eval_gettext "Unable to rebase
'\$sha1' in submodule path
'\$sm_path'")"
612 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': rebased into
'\$sha1'")"
613 must_die_on_failure
=yes
617 die_msg
="$(eval_gettext "Unable to merge
'\$sha1' in submodule path
'\$sm_path'")"
618 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': merged
in '\$sha1'")"
619 must_die_on_failure
=yes
622 command="git checkout $subforce -q"
623 die_msg
="$(eval_gettext "Unable to checkout
'\$sha1' in submodule path
'\$sm_path'")"
624 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': checked out
'\$sha1'")"
628 if (clear_local_git_env
; cd "$sm_path" && $command "$sha1")
631 elif test -n "$must_die_on_failure"
633 die_with_status
2 "$die_msg"
635 err
="${err};$die_msg"
640 if test -n "$recursive"
642 (clear_local_git_env
; cd "$sm_path" && eval cmd_update
"$orig_flags")
646 die_msg
="$(eval_gettext "Failed to recurse into submodule path
'\$sm_path'")"
649 err
="${err};$die_msg"
652 die_with_status
$res "$die_msg"
679 git describe
"$2" 2>/dev
/null ||
680 git describe
--tags "$2" 2>/dev
/null ||
681 git describe
--contains "$2" 2>/dev
/null ||
682 git describe
--all --always "$2"
685 test -z "$revname" || revname
=" ($revname)"
688 # Show commit summary for submodules in index or working tree
690 # If '--cached' is given, show summary between index and given commit,
691 # or between working tree and given commit
693 # $@ = [commit (default 'HEAD'),] requested paths (default all)
700 # parse $args after "submodule ... summary".
714 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
736 test $summary_limit = 0 && return
738 if rev=$
(git rev-parse
-q --verify --default HEAD
${1+"$1"})
742 elif test -z "$1" -o "$1" = "HEAD"
744 # before the first commit: compare with an empty tree
745 head=$
(git hash-object
-w -t tree
--stdin </dev
/null
)
746 test -z "$1" ||
shift
754 die
"$(gettext "The
--cached option cannot be used with the
--files option
")"
760 # Get modified modules cared by user
761 modules
=$
(git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- "$@" |
762 sane_egrep
'^:([0-7]* )?160000' |
763 while read mod_src mod_dst sha1_src sha1_dst status name
765 # Always show modules deleted or type-changed (blob<->module)
766 test $status = D
-o $status = T
&& echo "$name" && continue
767 # Also show added or modified modules which are checked out
768 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
773 test -z "$modules" && return
775 git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- $modules |
776 sane_egrep
'^:([0-7]* )?160000' |
778 while read mod_src mod_dst sha1_src sha1_dst status name
780 if test -z "$cached" &&
781 test $sha1_dst = 0000000000000000000000000000000000000000
785 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
787 100644 |
100755 |
120000)
788 sha1_dst
=$
(git hash-object
$name)
794 eval_gettextln
"unexpected mode \$mod_dst" >&2
801 test $mod_src = 160000 &&
802 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
805 test $mod_dst = 160000 &&
806 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
810 case "$missing_src,$missing_dst" in
812 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commit \$sha1_src")"
815 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \
$sha1_dst")"
818 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commits \$sha1_src and \$sha1_dst")"
823 if test $mod_src = 160000 -a $mod_dst = 160000
825 range="$sha1_src...$sha1_dst"
826 elif test $mod_src = 160000
832 GIT_DIR="$name/.git" \
833 git rev-list --first-parent $range -- | wc -l
835 total_commits=" ($(($total_commits + 0)))"
839 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
840 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
843 blob="$(gettext "blob")"
844 submodule="$(gettext "submodule")"
845 if test $mod_dst = 160000
847 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
849 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
852 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
856 # Don't give error msg
for modification whose dst is not submodule
857 # i.e. deleted or changed to blob
858 test $mod_dst = 160000 && echo "$errmsg"
860 if test $mod_src = 160000 -a $mod_dst = 160000
863 test $summary_limit -gt 0 && limit
="-$summary_limit"
864 GIT_DIR
="$name/.git" \
865 git log
$limit --pretty='format: %m %s' \
866 --first-parent $sha1_src...
$sha1_dst
867 elif test $mod_dst = 160000
869 GIT_DIR
="$name/.git" \
870 git log
--pretty='format: > %s' -1 $sha1_dst
872 GIT_DIR
="$name/.git" \
873 git log
--pretty='format: < %s' -1 $sha1_src
879 if test -n "$for_status"; then
880 if [ -n "$files" ]; then
881 gettextln
"# Submodules changed but not updated:"
883 gettextln
"# Submodule changes to be committed:"
886 sed -e 's|^|# |' -e 's|^# $|#|'
892 # List all submodules, prefixed with:
893 # - submodule not initialized
894 # + different revision checked out
896 # If --cached was specified the revision in the index will be printed
897 # instead of the currently checked out revision.
899 # $@ = requested paths (default to all)
903 # parse $args after "submodule ... status".
928 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
933 while read mode sha1 stage sm_path
935 name
=$
(module_name
"$sm_path") ||
exit
936 url
=$
(git config submodule.
"$name".url
)
937 displaypath
="$prefix$sm_path"
940 say
"U$sha1 $displaypath"
943 if test -z "$url" ||
! test -d "$sm_path"/.git
-o -f "$sm_path"/.git
945 say
"-$sha1 $displaypath"
948 set_name_rev
"$sm_path" "$sha1"
949 if git diff-files
--ignore-submodules=dirty
--quiet -- "$sm_path"
951 say
" $sha1 $displaypath$revname"
955 sha1
=$
(clear_local_git_env
; cd "$sm_path" && git rev-parse
--verify HEAD
)
956 set_name_rev
"$sm_path" "$sha1"
958 say
"+$sha1 $displaypath$revname"
961 if test -n "$recursive"
964 prefix
="$displaypath/"
967 eval cmd_status
"$orig_args"
969 die
"$(eval_gettext "Failed to recurse into submodule path
'\$sm_path'")"
974 # Sync remote urls for submodules
975 # This makes the value for remote.$remote.url match the value
976 # specified in .gitmodules.
1001 while read mode sha1 stage sm_path
1003 name
=$
(module_name
"$sm_path")
1004 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
1006 # Possibly a url relative to parent
1009 # rewrite foo/bar as ../.. to find path from
1010 # submodule work tree to superproject work tree
1011 up_path
="$(echo "$sm_path" | sed "s
/[^
/][^
/]*/..
/g
")" &&
1012 # guarantee a trailing /
1013 up_path
=${up_path%/}/ &&
1014 # path from submodule work tree to submodule origin repo
1015 sub_origin_url
=$
(resolve_relative_url
"$url" "$up_path") &&
1016 # path from superproject work tree to submodule origin repo
1017 super_config_url
=$
(resolve_relative_url
"$url") ||
exit
1020 sub_origin_url
="$url"
1021 super_config_url
="$url"
1025 if git config
"submodule.$name.url" >/dev
/null
2>/dev
/null
1027 say
"$(eval_gettext "Synchronizing submodule url
for '\$name'")"
1028 git config submodule.
"$name".url
"$super_config_url"
1030 if test -e "$sm_path"/.git
1035 remote
=$
(get_default_remote
)
1036 git config remote.
"$remote".url
"$sub_origin_url"
1043 # This loop parses the command line arguments to find the
1044 # subcommand name to dispatch. Parsing of the subcommand specific
1045 # options are primarily done by the subcommand implementations.
1046 # Subcommand specific options such as --branch and --cached are
1047 # parsed here as well, for backward compatibility.
1049 while test $# != 0 && test -z "$command"
1052 add | foreach | init | update | status | summary | sync
)
1082 # No command word defaults to "status"
1083 test -n "$command" ||
command=status
1085 # "-b branch" is accepted only by "add"
1086 if test -n "$branch" && test "$command" != add
1091 # "--cached" is accepted only by "status" and "summary"
1092 if test -n "$cached" && test "$command" != status
-a "$command" != summary