3 # git-submodules.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"
184 git clone
$quiet -n ${reference:+"$reference"} \
185 --separate-git-dir "$gitdir" "$url" "$sm_path" ||
186 die
"$(eval_gettext "Clone of
'\$url' into submodule path
'\$sm_path' failed
")"
189 a
=$
(cd "$gitdir" && pwd)/
190 b
=$
(cd "$sm_path" && pwd)/
191 # normalize Windows-style absolute paths to POSIX-style absolute paths
192 case $a in [a-zA-Z
]:/*) a
=/${a%%:*}${a#*:} ;; esac
193 case $b in [a-zA-Z
]:/*) b
=/${b%%:*}${b#*:} ;; esac
194 # Remove all common leading directories after a sanity check
195 if test "${a#$b}" != "$a" ||
test "${b#$a}" != "$b"; then
196 die
"$(eval_gettext "Gitdir
'\$a' is part of the submodule path
'\$b' or vice versa
")"
198 while test "${a%%/*}" = "${b%%/*}"
203 # Now chop off the trailing '/'s that were added in the beginning
207 # Turn each leading "*/" component into "../"
208 rel
=$
(echo $b |
sed -e 's|[^/][^/]*|..|g')
209 echo "gitdir: $rel/$a" >"$sm_path/.git"
211 rel
=$
(echo $a |
sed -e 's|[^/][^/]*|..|g')
212 (clear_local_git_env
; cd "$sm_path" && GIT_WORK_TREE
=. git config core.worktree
"$rel/$b")
216 # Add a new submodule to the working tree, .gitmodules and the index
220 # optional branch is stored in global branch variable
224 # parse $args after "submodule ... add".
229 case "$2" in '') usage
;; esac
240 case "$2" in '') usage
;; esac
241 reference
="--reference=$2"
265 if test -z "$sm_path"; then
266 sm_path
=$
(echo "$repo" |
267 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
270 if test -z "$repo" -o -z "$sm_path"; then
274 # assure repo is absolute or relative to parent
277 # dereference source url relative to parent's url
278 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
285 die
"$(eval_gettext "repo URL
: '\$repo' must be absolute or begin with .
/|..
/")"
290 # multiple //; leading ./; /./; /../; trailing /
291 sm_path
=$
(printf '%s/\n' "$sm_path" |
301 git ls-files
--error-unmatch "$sm_path" > /dev
/null
2>&1 &&
302 die
"$(eval_gettext "'\$sm_path' already exists
in the index
")"
304 if test -z "$force" && ! git add
--dry-run --ignore-missing "$sm_path" > /dev
/null
2>&1
306 eval_gettextln
"The following path is ignored by one of your .gitignore files:
308 Use -f if you really want to add it." >&2
312 # perhaps the path exists and is already a git repo, else clone it
313 if test -e "$sm_path"
315 if test -d "$sm_path"/.git
-o -f "$sm_path"/.git
317 eval_gettextln
"Adding existing repo at '\$sm_path' to the index"
319 die
"$(eval_gettext "'\$sm_path' already exists and is not a valid git repo
")"
324 module_clone
"$sm_path" "$realrepo" "$reference" ||
exit
328 # ash fails to wordsplit ${branch:+-b "$branch"...}
330 '') git checkout
-f -q ;;
331 ?
*) git checkout
-f -q -B "$branch" "origin/$branch" ;;
333 ) || die
"$(eval_gettext "Unable to checkout submodule
'\$sm_path'")"
335 git config submodule.
"$sm_path".url
"$realrepo"
337 git add
$force "$sm_path" ||
338 die
"$(eval_gettext "Failed to add submodule
'\$sm_path'")"
340 git config
-f .gitmodules submodule.
"$sm_path".path
"$sm_path" &&
341 git config
-f .gitmodules submodule.
"$sm_path".url
"$repo" &&
342 git add
--force .gitmodules ||
343 die
"$(eval_gettext "Failed to register submodule
'\$sm_path'")"
347 # Execute an arbitrary command sequence in each checked out
350 # $@ = command to execute
354 # parse $args after "submodule ... foreach".
376 # dup stdin so that it can be restored when running the external
377 # command in the subshell (and a recursive call to this function)
381 while read mode sha1 stage sm_path
383 if test -e "$sm_path"/.git
385 say
"$(eval_gettext "Entering
'\$prefix\$sm_path'")"
386 name
=$
(module_name
"$sm_path")
388 prefix
="$prefix$sm_path/"
390 # we make $path available to scripts ...
394 if test -n "$recursive"
396 cmd_foreach
"--recursive" "$@"
399 die
"$(eval_gettext "Stopping
at '\$sm_path'; script returned non-zero status.
")"
405 # Register submodules in .git/config
407 # $@ = requested paths (default to all)
411 # parse $args after "submodule ... init".
433 while read mode sha1 stage sm_path
435 # Skip already registered paths
436 name
=$
(module_name
"$sm_path") ||
exit
437 if test -z "$(git config "submodule.
$name.url
")"
439 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
441 die
"$(eval_gettext "No url found
for submodule path
'\$sm_path' in .gitmodules
")"
443 # Possibly a url relative to parent
446 url
=$
(resolve_relative_url
"$url") ||
exit
449 git config submodule.
"$name".url
"$url" ||
450 die
"$(eval_gettext "Failed to register url
for submodule path
'\$sm_path'")"
453 # Copy "update" setting when it is not set yet
454 upd
="$(git config -f .gitmodules submodule."$name".update)"
456 test -n "$(git config submodule."$name".update)" ||
457 git config submodule.
"$name".update
"$upd" ||
458 die
"$(eval_gettext "Failed to register update mode
for submodule path
'\$sm_path'")"
460 say
"$(eval_gettext "Submodule
'\$name' (\
$url) registered
for path
'\$sm_path'")"
465 # Update each submodule path to correct revision, using clone and checkout as needed
467 # $@ = requested paths (default to all)
471 # parse $args after "submodule ... update".
492 case "$2" in '') usage
;; esac
493 reference
="--reference=$2"
494 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
520 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
526 cmd_init
"--" "$@" ||
return
532 while read mode sha1 stage sm_path
536 echo >&2 "Skipping unmerged submodule $sm_path"
539 name
=$
(module_name
"$sm_path") ||
exit
540 url
=$
(git config submodule.
"$name".url
)
541 if ! test -z "$update"
543 update_module
=$update
545 update_module
=$
(git config submodule.
"$name".update
)
548 if test "$update_module" = "none"
550 echo "Skipping submodule '$sm_path'"
556 # Only mention uninitialized submodules when its
557 # path have been specified
559 say
"$(eval_gettext "Submodule path
'\$sm_path' not initialized
560 Maybe you want to use
'update --init'?
")"
564 if ! test -d "$sm_path"/.git
-o -f "$sm_path"/.git
566 module_clone
"$sm_path" "$url" "$reference"||
exit
567 cloned_modules
="$cloned_modules;$name"
570 subsha1
=$
(clear_local_git_env
; cd "$sm_path" &&
571 git rev-parse
--verify HEAD
) ||
572 die
"$(eval_gettext "Unable to
find current revision
in submodule path
'\$sm_path'")"
575 if test "$subsha1" != "$sha1"
578 # If we don't already have a -f flag and the submodule has never been checked out
579 if test -z "$subsha1" -a -z "$force"
584 if test -z "$nofetch"
586 # Run fetch only if $sha1 isn't present or it
587 # is not reachable from a ref.
588 (clear_local_git_env
; cd "$sm_path" &&
589 ( (rev=$
(git rev-list
-n 1 $sha1 --not --all 2>/dev
/null
) &&
590 test -z "$rev") || git-fetch
)) ||
591 die
"$(eval_gettext "Unable to fetch
in submodule path
'\$sm_path'")"
594 # Is this something we just cloned?
595 case ";$cloned_modules;" in
597 # then there is no local change to integrate
602 case "$update_module" in
605 die_msg
="$(eval_gettext "Unable to rebase
'\$sha1' in submodule path
'\$sm_path'")"
606 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': rebased into
'\$sha1'")"
607 must_die_on_failure
=yes
611 die_msg
="$(eval_gettext "Unable to merge
'\$sha1' in submodule path
'\$sm_path'")"
612 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': merged
in '\$sha1'")"
613 must_die_on_failure
=yes
616 command="git checkout $subforce -q"
617 die_msg
="$(eval_gettext "Unable to checkout
'\$sha1' in submodule path
'\$sm_path'")"
618 say_msg
="$(eval_gettext "Submodule path
'\$sm_path': checked out
'\$sha1'")"
622 if (clear_local_git_env
; cd "$sm_path" && $command "$sha1")
625 elif test -n "$must_die_on_failure"
627 die_with_status
2 "$die_msg"
629 err
="${err};$die_msg"
634 if test -n "$recursive"
636 (clear_local_git_env
; cd "$sm_path" && eval cmd_update
"$orig_flags")
640 die_msg
="$(eval_gettext "Failed to recurse into submodule path
'\$sm_path'")"
643 err
="${err};$die_msg"
646 die_with_status
$res "$die_msg"
673 git describe
"$2" 2>/dev
/null ||
674 git describe
--tags "$2" 2>/dev
/null ||
675 git describe
--contains "$2" 2>/dev
/null ||
676 git describe
--all --always "$2"
679 test -z "$revname" || revname
=" ($revname)"
682 # Show commit summary for submodules in index or working tree
684 # If '--cached' is given, show summary between index and given commit,
685 # or between working tree and given commit
687 # $@ = [commit (default 'HEAD'),] requested paths (default all)
694 # parse $args after "submodule ... summary".
708 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
730 test $summary_limit = 0 && return
732 if rev=$
(git rev-parse
-q --verify --default HEAD
${1+"$1"})
736 elif test -z "$1" -o "$1" = "HEAD"
738 # before the first commit: compare with an empty tree
739 head=$
(git hash-object
-w -t tree
--stdin </dev
/null
)
740 test -z "$1" ||
shift
748 die
"$(gettext -- "--cached cannot be used with
--files")"
754 # Get modified modules cared by user
755 modules
=$
(git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- "$@" |
756 sane_egrep
'^:([0-7]* )?160000' |
757 while read mod_src mod_dst sha1_src sha1_dst status name
759 # Always show modules deleted or type-changed (blob<->module)
760 test $status = D
-o $status = T
&& echo "$name" && continue
761 # Also show added or modified modules which are checked out
762 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
767 test -z "$modules" && return
769 git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- $modules |
770 sane_egrep
'^:([0-7]* )?160000' |
772 while read mod_src mod_dst sha1_src sha1_dst status name
774 if test -z "$cached" &&
775 test $sha1_dst = 0000000000000000000000000000000000000000
779 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
781 100644 |
100755 |
120000)
782 sha1_dst
=$
(git hash-object
$name)
788 eval_gettextln
"unexpected mode \$mod_dst" >&2
795 test $mod_src = 160000 &&
796 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
799 test $mod_dst = 160000 &&
800 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
804 case "$missing_src,$missing_dst" in
806 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commit \$sha1_src")"
809 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \
$sha1_dst")"
812 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commits \$sha1_src and \$sha1_dst")"
817 if test $mod_src = 160000 -a $mod_dst = 160000
819 range="$sha1_src...$sha1_dst"
820 elif test $mod_src = 160000
826 GIT_DIR="$name/.git" \
827 git rev-list --first-parent $range -- | wc -l
829 total_commits=" ($(($total_commits + 0)))"
833 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
834 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
837 blob="$(gettext "blob")"
838 submodule="$(gettext "submodule")"
839 if test $mod_dst = 160000
841 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
843 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
846 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
850 # Don't give error msg
for modification whose dst is not submodule
851 # i.e. deleted or changed to blob
852 test $mod_dst = 160000 && echo "$errmsg"
854 if test $mod_src = 160000 -a $mod_dst = 160000
857 test $summary_limit -gt 0 && limit
="-$summary_limit"
858 GIT_DIR
="$name/.git" \
859 git log
$limit --pretty='format: %m %s' \
860 --first-parent $sha1_src...
$sha1_dst
861 elif test $mod_dst = 160000
863 GIT_DIR
="$name/.git" \
864 git log
--pretty='format: > %s' -1 $sha1_dst
866 GIT_DIR
="$name/.git" \
867 git log
--pretty='format: < %s' -1 $sha1_src
873 if test -n "$for_status"; then
874 if [ -n "$files" ]; then
875 gettextln
"# Submodules changed but not updated:"
877 gettextln
"# Submodule changes to be committed:"
880 sed -e 's|^|# |' -e 's|^# $|#|'
886 # List all submodules, prefixed with:
887 # - submodule not initialized
888 # + different revision checked out
890 # If --cached was specified the revision in the index will be printed
891 # instead of the currently checked out revision.
893 # $@ = requested paths (default to all)
897 # parse $args after "submodule ... status".
922 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
927 while read mode sha1 stage sm_path
929 name
=$
(module_name
"$sm_path") ||
exit
930 url
=$
(git config submodule.
"$name".url
)
931 displaypath
="$prefix$sm_path"
934 say
"U$sha1 $displaypath"
937 if test -z "$url" ||
! test -d "$sm_path"/.git
-o -f "$sm_path"/.git
939 say
"-$sha1 $displaypath"
942 set_name_rev
"$sm_path" "$sha1"
943 if git diff-files
--ignore-submodules=dirty
--quiet -- "$sm_path"
945 say
" $sha1 $displaypath$revname"
949 sha1
=$
(clear_local_git_env
; cd "$sm_path" && git rev-parse
--verify HEAD
)
950 set_name_rev
"$sm_path" "$sha1"
952 say
"+$sha1 $displaypath$revname"
955 if test -n "$recursive"
958 prefix
="$displaypath/"
961 eval cmd_status
"$orig_args"
963 die
"$(eval_gettext "Failed to recurse into submodule path
'\$sm_path'")"
968 # Sync remote urls for submodules
969 # This makes the value for remote.$remote.url match the value
970 # specified in .gitmodules.
995 while read mode sha1 stage sm_path
997 name
=$
(module_name
"$sm_path")
998 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
1000 # Possibly a url relative to parent
1003 # rewrite foo/bar as ../.. to find path from
1004 # submodule work tree to superproject work tree
1005 up_path
="$(echo "$sm_path" | sed "s
/[^
/][^
/]*/..
/g
")" &&
1006 # guarantee a trailing /
1007 up_path
=${up_path%/}/ &&
1008 # path from submodule work tree to submodule origin repo
1009 sub_origin_url
=$
(resolve_relative_url
"$url" "$up_path") &&
1010 # path from superproject work tree to submodule origin repo
1011 super_config_url
=$
(resolve_relative_url
"$url") ||
exit
1014 sub_origin_url
="$url"
1015 super_config_url
="$url"
1019 if git config
"submodule.$name.url" >/dev
/null
2>/dev
/null
1021 say
"$(eval_gettext "Synchronizing submodule url
for '\$name'")"
1022 git config submodule.
"$name".url
"$super_config_url"
1024 if test -e "$sm_path"/.git
1029 remote
=$
(get_default_remote
)
1030 git config remote.
"$remote".url
"$sub_origin_url"
1037 # This loop parses the command line arguments to find the
1038 # subcommand name to dispatch. Parsing of the subcommand specific
1039 # options are primarily done by the subcommand implementations.
1040 # Subcommand specific options such as --branch and --cached are
1041 # parsed here as well, for backward compatibility.
1043 while test $# != 0 && test -z "$command"
1046 add | foreach | init | update | status | summary | sync
)
1076 # No command word defaults to "status"
1077 test -n "$command" ||
command=status
1079 # "-b branch" is accepted only by "add"
1080 if test -n "$branch" && test "$command" != add
1085 # "--cached" is accepted only by "status" and "summary"
1086 if test -n "$cached" && test "$command" != status
-a "$command" != summary