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 # Resolve relative url by appending to parent's url
34 resolve_relative_url
()
36 remote
=$
(get_default_remote
)
37 remoteurl
=$
(git config
"remote.$remote.url") ||
38 remoteurl
=$
(pwd) # the repository is its own authoritative upstream
40 remoteurl
=${remoteurl%/}
49 remoteurl
="${remoteurl%/*}"
52 remoteurl
="${remoteurl%:*}"
56 die
"$(eval_gettext "cannot strip one component off url
'\$remoteurl'")"
67 echo "$remoteurl$sep${url%/}"
71 # Get submodule info for registered submodules
72 # $@ = path to limit submodule list
76 git ls-files
--error-unmatch --stage -- "$@" |
79 my ($null_sha1) = ("0" x 40);
82 my ($mode, $sha1, $stage, $path) =
83 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
84 next unless $mode eq "160000";
86 if (!$unmerged{$path}++) {
87 print "$mode $null_sha1 U\t$path\n";
97 # Map submodule path to submodule name
103 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
104 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
105 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
106 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
108 die
"$(eval_gettext "No submodule mapping found
in .gitmodules
for path
'\$path'")"
115 # Prior to calling, cmd_update checks that a possibly existing
116 # path is not a git repository.
117 # Likewise, cmd_add checks that path does not exist at all,
118 # since it is the location of a new submodule.
126 if test -n "$GIT_QUIET"
133 name
=$
(module_name
"$path" 2>/dev
/null
)
134 test -n "$name" || name
="$path"
135 base_path
=$
(dirname "$path")
137 gitdir
=$
(git rev-parse
--git-dir)
138 gitdir_base
="$gitdir/modules/$base_path"
139 gitdir
="$gitdir/modules/$path"
143 a
="$(cd_to_toplevel && pwd)/"
145 while [ "$b" ] && [ "${a%%/*}" = "${b%%/*}" ]
151 rel
=`echo $rel | sed -e 's|[^/]*|..|g'`
155 rel
=`echo $name | sed -e 's|[^/]*|..|g'`
156 rel_gitdir
="$rel/$gitdir"
163 rm -f "$gitdir/index"
165 mkdir
-p "$gitdir_base"
166 git clone
$quiet -n ${reference:+"$reference"} \
167 --separate-git-dir "$gitdir" "$url" "$path" ||
168 die
"$(eval_gettext "Clone of
'\$url' into submodule path
'\$path' failed
")"
171 echo "gitdir: $rel_gitdir" >"$path/.git"
173 a
=$
(cd "$gitdir" && pwd)/
174 b
=$
(cd "$path" && pwd)/
175 # Remove all common leading directories after a sanity check
176 if test "${a#$b}" != "$a" ||
test "${b#$a}" != "$b"; then
177 die
"$(eval_gettext "Gitdir
'\$a' is part of the submodule path
'\$b' or vice versa
")"
179 while test "${a%%/*}" = "${b%%/*}"
184 # Now chop off the trailing '/'s that were added in the beginning
188 rel
=$
(echo $a |
sed -e 's|[^/]*|..|g')
189 (clear_local_git_env
; cd "$path" && GIT_WORK_TREE
=. git config core.worktree
"$rel/$b")
193 # Add a new submodule to the working tree, .gitmodules and the index
197 # optional branch is stored in global branch variable
201 # parse $args after "submodule ... add".
206 case "$2" in '') usage
;; esac
217 case "$2" in '') usage
;; esac
218 reference
="--reference=$2"
242 if test -z "$path"; then
243 path
=$
(echo "$repo" |
244 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
247 if test -z "$repo" -o -z "$path"; then
251 # assure repo is absolute or relative to parent
254 # dereference source url relative to parent's url
255 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
262 die
"$(eval_gettext "repo URL
: '\$repo' must be absolute or begin with .
/|..
/")"
267 # multiple //; leading ./; /./; /../; trailing /
268 path
=$
(printf '%s/\n' "$path" |
278 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
279 die
"$(eval_gettext "'\$path' already exists
in the index
")"
281 if test -z "$force" && ! git add
--dry-run --ignore-missing "$path" > /dev
/null
2>&1
283 eval_gettextln
"The following path is ignored by one of your .gitignore files:
285 Use -f if you really want to add it." >&2
289 # perhaps the path exists and is already a git repo, else clone it
292 if test -d "$path"/.git
-o -f "$path"/.git
294 eval_gettextln
"Adding existing repo at '\$path' to the index"
296 die
"$(eval_gettext "'\$path' already exists and is not a valid git repo
")"
301 module_clone
"$path" "$realrepo" "$reference" ||
exit
305 # ash fails to wordsplit ${branch:+-b "$branch"...}
307 '') git checkout
-f -q ;;
308 ?
*) git checkout
-f -q -B "$branch" "origin/$branch" ;;
310 ) || die
"$(eval_gettext "Unable to checkout submodule
'\$path'")"
312 git config submodule.
"$path".url
"$realrepo"
314 git add
$force "$path" ||
315 die
"$(eval_gettext "Failed to add submodule
'\$path'")"
317 git config
-f .gitmodules submodule.
"$path".path
"$path" &&
318 git config
-f .gitmodules submodule.
"$path".url
"$repo" &&
319 git add
--force .gitmodules ||
320 die
"$(eval_gettext "Failed to register submodule
'\$path'")"
324 # Execute an arbitrary command sequence in each checked out
327 # $@ = command to execute
331 # parse $args after "submodule ... foreach".
353 # dup stdin so that it can be restored when running the external
354 # command in the subshell (and a recursive call to this function)
358 while read mode sha1 stage path
360 if test -e "$path"/.git
362 say
"$(eval_gettext "Entering
'\$prefix\$path'")"
363 name
=$
(module_name
"$path")
365 prefix
="$prefix$path/"
369 if test -n "$recursive"
371 cmd_foreach
"--recursive" "$@"
374 die
"$(eval_gettext "Stopping
at '\$path'; script returned non-zero status.
")"
380 # Register submodules in .git/config
382 # $@ = requested paths (default to all)
386 # parse $args after "submodule ... init".
408 while read mode sha1 stage path
410 # Skip already registered paths
411 name
=$
(module_name
"$path") ||
exit
412 if test -z "$(git config "submodule.
$name.url
")"
414 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
416 die
"$(eval_gettext "No url found
for submodule path
'\$path' in .gitmodules
")"
418 # Possibly a url relative to parent
421 url
=$
(resolve_relative_url
"$url") ||
exit
424 git config submodule.
"$name".url
"$url" ||
425 die
"$(eval_gettext "Failed to register url
for submodule path
'\$path'")"
428 # Copy "update" setting when it is not set yet
429 upd
="$(git config -f .gitmodules submodule."$name".update)"
431 test -n "$(git config submodule."$name".update)" ||
432 git config submodule.
"$name".update
"$upd" ||
433 die
"$(eval_gettext "Failed to register update mode
for submodule path
'\$path'")"
435 say
"$(eval_gettext "Submodule
'\$name' (\
$url) registered
for path
'\$path'")"
440 # Update each submodule path to correct revision, using clone and checkout as needed
442 # $@ = requested paths (default to all)
446 # parse $args after "submodule ... update".
467 case "$2" in '') usage
;; esac
468 reference
="--reference=$2"
469 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
495 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
501 cmd_init
"--" "$@" ||
return
507 while read mode sha1 stage path
511 echo >&2 "Skipping unmerged submodule $path"
514 name
=$
(module_name
"$path") ||
exit
515 url
=$
(git config submodule.
"$name".url
)
516 if ! test -z "$update"
518 update_module
=$update
520 update_module
=$
(git config submodule.
"$name".update
)
523 if test "$update_module" = "none"
525 echo "Skipping submodule '$path'"
531 # Only mention uninitialized submodules when its
532 # path have been specified
534 say
"$(eval_gettext "Submodule path
'\$path' not initialized
535 Maybe you want to use
'update --init'?
")"
539 if ! test -d "$path"/.git
-o -f "$path"/.git
541 module_clone
"$path" "$url" "$reference"||
exit
542 cloned_modules
="$cloned_modules;$name"
545 subsha1
=$
(clear_local_git_env
; cd "$path" &&
546 git rev-parse
--verify HEAD
) ||
547 die
"$(eval_gettext "Unable to
find current revision
in submodule path
'\$path'")"
550 if test "$subsha1" != "$sha1"
553 # If we don't already have a -f flag and the submodule has never been checked out
554 if test -z "$subsha1" -a -z "$force"
559 if test -z "$nofetch"
561 # Run fetch only if $sha1 isn't present or it
562 # is not reachable from a ref.
563 (clear_local_git_env
; cd "$path" &&
564 ( (rev=$
(git rev-list
-n 1 $sha1 --not --all 2>/dev
/null
) &&
565 test -z "$rev") || git-fetch
)) ||
566 die
"$(eval_gettext "Unable to fetch
in submodule path
'\$path'")"
569 # Is this something we just cloned?
570 case ";$cloned_modules;" in
572 # then there is no local change to integrate
577 case "$update_module" in
580 die_msg
="$(eval_gettext "Unable to rebase
'\$sha1' in submodule path
'\$path'")"
581 say_msg
="$(eval_gettext "Submodule path
'\$path': rebased into
'\$sha1'")"
582 must_die_on_failure
=yes
586 die_msg
="$(eval_gettext "Unable to merge
'\$sha1' in submodule path
'\$path'")"
587 say_msg
="$(eval_gettext "Submodule path
'\$path': merged
in '\$sha1'")"
588 must_die_on_failure
=yes
591 command="git checkout $subforce -q"
592 die_msg
="$(eval_gettext "Unable to checkout
'\$sha1' in submodule path
'\$path'")"
593 say_msg
="$(eval_gettext "Submodule path
'\$path': checked out
'\$sha1'")"
597 if (clear_local_git_env
; cd "$path" && $command "$sha1")
600 elif test -n "$must_die_on_failure"
602 die_with_status
2 "$die_msg"
604 err
="${err};$die_msg"
609 if test -n "$recursive"
611 (clear_local_git_env
; cd "$path" && eval cmd_update
"$orig_flags")
615 die_msg
="$(eval_gettext "Failed to recurse into submodule path
'\$path'")"
618 err
="${err};$die_msg"
621 die_with_status
$res "$die_msg"
648 git describe
"$2" 2>/dev
/null ||
649 git describe
--tags "$2" 2>/dev
/null ||
650 git describe
--contains "$2" 2>/dev
/null ||
651 git describe
--all --always "$2"
654 test -z "$revname" || revname
=" ($revname)"
657 # Show commit summary for submodules in index or working tree
659 # If '--cached' is given, show summary between index and given commit,
660 # or between working tree and given commit
662 # $@ = [commit (default 'HEAD'),] requested paths (default all)
669 # parse $args after "submodule ... summary".
683 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
705 test $summary_limit = 0 && return
707 if rev=$
(git rev-parse
-q --verify --default HEAD
${1+"$1"})
711 elif test -z "$1" -o "$1" = "HEAD"
713 # before the first commit: compare with an empty tree
714 head=$
(git hash-object
-w -t tree
--stdin </dev
/null
)
715 test -z "$1" ||
shift
723 die
"$(gettext -- "--cached cannot be used with
--files")"
729 # Get modified modules cared by user
730 modules
=$
(git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- "$@" |
731 sane_egrep
'^:([0-7]* )?160000' |
732 while read mod_src mod_dst sha1_src sha1_dst status name
734 # Always show modules deleted or type-changed (blob<->module)
735 test $status = D
-o $status = T
&& echo "$name" && continue
736 # Also show added or modified modules which are checked out
737 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
742 test -z "$modules" && return
744 git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- $modules |
745 sane_egrep
'^:([0-7]* )?160000' |
747 while read mod_src mod_dst sha1_src sha1_dst status name
749 if test -z "$cached" &&
750 test $sha1_dst = 0000000000000000000000000000000000000000
754 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
756 100644 |
100755 |
120000)
757 sha1_dst
=$
(git hash-object
$name)
763 eval_gettextln
"unexpected mode \$mod_dst" >&2
770 test $mod_src = 160000 &&
771 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
774 test $mod_dst = 160000 &&
775 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
779 case "$missing_src,$missing_dst" in
781 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commit \$sha1_src")"
784 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \
$sha1_dst")"
787 errmsg
="$(eval_gettext " Warn
: \
$name doesn
't contain commits \$sha1_src and \$sha1_dst")"
792 if test $mod_src = 160000 -a $mod_dst = 160000
794 range="$sha1_src...$sha1_dst"
795 elif test $mod_src = 160000
801 GIT_DIR="$name/.git" \
802 git rev-list --first-parent $range -- | wc -l
804 total_commits=" ($(($total_commits + 0)))"
808 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
809 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
812 blob="$(gettext "blob")"
813 submodule="$(gettext "submodule")"
814 if test $mod_dst = 160000
816 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
818 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
821 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
825 # Don't give error msg
for modification whose dst is not submodule
826 # i.e. deleted or changed to blob
827 test $mod_dst = 160000 && echo "$errmsg"
829 if test $mod_src = 160000 -a $mod_dst = 160000
832 test $summary_limit -gt 0 && limit
="-$summary_limit"
833 GIT_DIR
="$name/.git" \
834 git log
$limit --pretty='format: %m %s' \
835 --first-parent $sha1_src...
$sha1_dst
836 elif test $mod_dst = 160000
838 GIT_DIR
="$name/.git" \
839 git log
--pretty='format: > %s' -1 $sha1_dst
841 GIT_DIR
="$name/.git" \
842 git log
--pretty='format: < %s' -1 $sha1_src
848 if test -n "$for_status"; then
849 if [ -n "$files" ]; then
850 gettextln
"# Submodules changed but not updated:"
852 gettextln
"# Submodule changes to be committed:"
855 sed -e 's|^|# |' -e 's|^# $|#|'
861 # List all submodules, prefixed with:
862 # - submodule not initialized
863 # + different revision checked out
865 # If --cached was specified the revision in the index will be printed
866 # instead of the currently checked out revision.
868 # $@ = requested paths (default to all)
872 # parse $args after "submodule ... status".
897 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
902 while read mode sha1 stage path
904 name
=$
(module_name
"$path") ||
exit
905 url
=$
(git config submodule.
"$name".url
)
906 displaypath
="$prefix$path"
909 say
"U$sha1 $displaypath"
912 if test -z "$url" ||
! test -d "$path"/.git
-o -f "$path"/.git
914 say
"-$sha1 $displaypath"
917 set_name_rev
"$path" "$sha1"
918 if git diff-files
--ignore-submodules=dirty
--quiet -- "$path"
920 say
" $sha1 $displaypath$revname"
924 sha1
=$
(clear_local_git_env
; cd "$path" && git rev-parse
--verify HEAD
)
925 set_name_rev
"$path" "$sha1"
927 say
"+$sha1 $displaypath$revname"
930 if test -n "$recursive"
933 prefix
="$displaypath/"
936 eval cmd_status
"$orig_args"
938 die
"$(eval_gettext "Failed to recurse into submodule path
'\$path'")"
943 # Sync remote urls for submodules
944 # This makes the value for remote.$remote.url match the value
945 # specified in .gitmodules.
970 while read mode sha1 stage path
972 name
=$
(module_name
"$path")
973 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
975 # Possibly a url relative to parent
978 url
=$
(resolve_relative_url
"$url") ||
exit
982 if git config
"submodule.$name.url" >/dev
/null
2>/dev
/null
984 say
"$(eval_gettext "Synchronizing submodule url
for '\$name'")"
985 git config submodule.
"$name".url
"$url"
987 if test -e "$path"/.git
992 remote
=$
(get_default_remote
)
993 git config remote.
"$remote".url
"$url"
1000 # This loop parses the command line arguments to find the
1001 # subcommand name to dispatch. Parsing of the subcommand specific
1002 # options are primarily done by the subcommand implementations.
1003 # Subcommand specific options such as --branch and --cached are
1004 # parsed here as well, for backward compatibility.
1006 while test $# != 0 && test -z "$command"
1009 add | foreach | init | update | status | summary | sync
)
1039 # No command word defaults to "status"
1040 test -n "$command" ||
command=status
1042 # "-b branch" is accepted only by "add"
1043 if test -n "$branch" && test "$command" != add
1048 # "--cached" is accepted only by "status" and "summary"
1049 if test -n "$cached" && test "$command" != status
-a "$command" != summary