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] [--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>...]"
32 # Resolve relative url by appending to parent's url
33 resolve_relative_url
()
35 remote
=$
(get_default_remote
)
36 remoteurl
=$
(git config
"remote.$remote.url") ||
37 die
"remote ($remote) does not have a url defined in .git/config"
39 remoteurl
=${remoteurl%/}
45 remoteurl
="${remoteurl%/*}"
54 echo "$remoteurl/${url%/}"
58 # Get submodule info for registered submodules
59 # $@ = path to limit submodule list
63 git ls-files
--error-unmatch --stage -- "$@" | sane_grep
'^160000 '
67 # Map submodule path to submodule name
73 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
74 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
75 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
76 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
78 die
"No submodule mapping found in .gitmodules for path '$path'"
85 # Prior to calling, cmd_update checks that a possibly existing
86 # path is not a git repository.
87 # Likewise, cmd_add checks that path does not exist at all,
88 # since it is the location of a new submodule.
96 if test -n "$reference"
98 git-clone
"$reference" -n "$url" "$path"
100 git-clone
-n "$url" "$path"
102 die
"Clone of '$url' into submodule path '$path' failed"
106 # Add a new submodule to the working tree, .gitmodules and the index
110 # optional branch is stored in global branch variable
114 # parse $args after "submodule ... add".
119 case "$2" in '') usage
;; esac
130 case "$2" in '') usage
;; esac
131 reference
="--reference=$2"
155 if test -z "$path"; then
156 path
=$
(echo "$repo" |
157 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
160 if test -z "$repo" -o -z "$path"; then
164 # assure repo is absolute or relative to parent
167 # dereference source url relative to parent's url
168 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
175 die
"repo URL: '$repo' must be absolute or begin with ./|../"
180 # multiple //; leading ./; /./; /../; trailing /
181 path
=$
(printf '%s/\n' "$path" |
191 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
192 die
"'$path' already exists in the index"
194 if test -z "$force" && ! git add
--dry-run --ignore-missing "$path" > /dev
/null
2>&1
196 echo >&2 "The following path is ignored by one of your .gitignore files:" &&
198 echo >&2 "Use -f if you really want to add it."
202 # perhaps the path exists and is already a git repo, else clone it
205 if test -d "$path"/.git
-o -f "$path"/.git
207 echo "Adding existing repo at '$path' to the index"
209 die
"'$path' already exists and is not a valid git repo"
214 url
=$
(resolve_relative_url
"$repo") ||
exit
220 git config submodule.
"$path".url
"$url"
223 module_clone
"$path" "$realrepo" "$reference" ||
exit
227 # ash fails to wordsplit ${branch:+-b "$branch"...}
229 '') git checkout
-f -q ;;
230 ?
*) git checkout
-f -q -B "$branch" "origin/$branch" ;;
232 ) || die
"Unable to checkout submodule '$path'"
235 git add
$force "$path" ||
236 die
"Failed to add submodule '$path'"
238 git config
-f .gitmodules submodule.
"$path".path
"$path" &&
239 git config
-f .gitmodules submodule.
"$path".url
"$repo" &&
240 git add
--force .gitmodules ||
241 die
"Failed to register submodule '$path'"
245 # Execute an arbitrary command sequence in each checked out
248 # $@ = command to execute
252 # parse $args after "submodule ... foreach".
275 while read mode sha1 stage path
277 if test -e "$path"/.git
279 say
"Entering '$prefix$path'"
280 name
=$
(module_name
"$path")
282 prefix
="$prefix$path/"
286 if test -n "$recursive"
288 cmd_foreach
"--recursive" "$@"
291 die
"Stopping at '$path'; script returned non-zero status."
297 # Register submodules in .git/config
299 # $@ = requested paths (default to all)
303 # parse $args after "submodule ... init".
325 while read mode sha1 stage path
327 # Skip already registered paths
328 name
=$
(module_name
"$path") ||
exit
329 url
=$
(git config submodule.
"$name".url
)
330 test -z "$url" ||
continue
332 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
334 die
"No url found for submodule path '$path' in .gitmodules"
336 # Possibly a url relative to parent
339 url
=$
(resolve_relative_url
"$url") ||
exit
343 git config submodule.
"$name".url
"$url" ||
344 die
"Failed to register url for submodule path '$path'"
346 upd
="$(git config -f .gitmodules submodule."$name".update)"
348 git config submodule.
"$name".update
"$upd" ||
349 die
"Failed to register update mode for submodule path '$path'"
351 say
"Submodule '$name' ($url) registered for path '$path'"
356 # Update each submodule path to correct revision, using clone and checkout as needed
358 # $@ = requested paths (default to all)
362 # parse $args after "submodule ... update".
380 case "$2" in '') usage
;; esac
381 reference
="--reference=$2"
382 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
405 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
411 cmd_init
"--" "$@" ||
return
415 while read mode sha1 stage path
417 name
=$
(module_name
"$path") ||
exit
418 url
=$
(git config submodule.
"$name".url
)
419 update_module
=$
(git config submodule.
"$name".update
)
422 # Only mention uninitialized submodules when its
423 # path have been specified
425 say
"Submodule path '$path' not initialized" &&
426 say
"Maybe you want to use 'update --init'?"
430 if ! test -d "$path"/.git
-o -f "$path"/.git
432 module_clone
"$path" "$url" "$reference"||
exit
435 subsha1
=$
(clear_local_git_env
; cd "$path" &&
436 git rev-parse
--verify HEAD
) ||
437 die
"Unable to find current revision in submodule path '$path'"
440 if ! test -z "$update"
442 update_module
=$update
445 if test "$subsha1" != "$sha1"
448 if test -z "$subsha1"
453 if test -z "$nofetch"
455 (clear_local_git_env
; cd "$path" &&
457 die
"Unable to fetch in submodule path '$path'"
460 case "$update_module" in
472 command="git checkout $force -q"
478 (clear_local_git_env
; cd "$path" && $command "$sha1") ||
479 die
"Unable to $action '$sha1' in submodule path '$path'"
480 say
"Submodule path '$path': $msg '$sha1'"
483 if test -n "$recursive"
485 (clear_local_git_env
; cd "$path" && eval cmd_update
"$orig_flags") ||
486 die
"Failed to recurse into submodule path '$path'"
495 git describe
"$2" 2>/dev
/null ||
496 git describe
--tags "$2" 2>/dev
/null ||
497 git describe
--contains "$2" 2>/dev
/null ||
498 git describe
--all --always "$2"
501 test -z "$revname" || revname
=" ($revname)"
504 # Show commit summary for submodules in index or working tree
506 # If '--cached' is given, show summary between index and given commit,
507 # or between working tree and given commit
509 # $@ = [commit (default 'HEAD'),] requested paths (default all)
516 # parse $args after "submodule ... summary".
530 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
552 test $summary_limit = 0 && return
554 if rev=$
(git rev-parse
-q --verify --default HEAD
${1+"$1"})
558 elif test -z "$1" -o "$1" = "HEAD"
560 # before the first commit: compare with an empty tree
561 head=$
(git hash-object
-w -t tree
--stdin </dev
/null
)
562 test -z "$1" ||
shift
570 die
"--cached cannot be used with --files"
576 # Get modified modules cared by user
577 modules
=$
(git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- "$@" |
578 sane_egrep
'^:([0-7]* )?160000' |
579 while read mod_src mod_dst sha1_src sha1_dst status name
581 # Always show modules deleted or type-changed (blob<->module)
582 test $status = D
-o $status = T
&& echo "$name" && continue
583 # Also show added or modified modules which are checked out
584 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
589 test -z "$modules" && return
591 git
$diff_cmd $cached --ignore-submodules=dirty
--raw $head -- $modules |
592 sane_egrep
'^:([0-7]* )?160000' |
594 while read mod_src mod_dst sha1_src sha1_dst status name
596 if test -z "$cached" &&
597 test $sha1_dst = 0000000000000000000000000000000000000000
601 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
603 100644 |
100755 |
120000)
604 sha1_dst
=$
(git hash-object
$name)
610 echo >&2 "unexpected mode $mod_dst"
617 test $mod_src = 160000 &&
618 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
621 test $mod_dst = 160000 &&
622 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
626 case "$missing_src,$missing_dst" in
628 errmsg
=" Warn: $name doesn't contain commit $sha1_src"
631 errmsg
=" Warn: $name doesn't contain commit $sha1_dst"
634 errmsg
=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
639 if test $mod_src = 160000 -a $mod_dst = 160000
641 range
="$sha1_src...$sha1_dst"
642 elif test $mod_src = 160000
648 GIT_DIR
="$name/.git" \
649 git rev-list
--first-parent $range -- |
wc -l
651 total_commits
=" ($(($total_commits + 0)))"
655 sha1_abbr_src
=$
(echo $sha1_src | cut
-c1-7)
656 sha1_abbr_dst
=$
(echo $sha1_dst | cut
-c1-7)
659 if test $mod_dst = 160000
661 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
663 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
666 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
670 # Don't give error msg for modification whose dst is not submodule
671 # i.e. deleted or changed to blob
672 test $mod_dst = 160000 && echo "$errmsg"
674 if test $mod_src = 160000 -a $mod_dst = 160000
677 test $summary_limit -gt 0 && limit
="-$summary_limit"
678 GIT_DIR
="$name/.git" \
679 git log
$limit --pretty='format: %m %s' \
680 --first-parent $sha1_src...
$sha1_dst
681 elif test $mod_dst = 160000
683 GIT_DIR
="$name/.git" \
684 git log
--pretty='format: > %s' -1 $sha1_dst
686 GIT_DIR
="$name/.git" \
687 git log
--pretty='format: < %s' -1 $sha1_src
693 if test -n "$for_status"; then
694 if [ -n "$files" ]; then
695 echo "# Submodules changed but not updated:"
697 echo "# Submodule changes to be committed:"
700 sed -e 's|^|# |' -e 's|^# $|#|'
706 # List all submodules, prefixed with:
707 # - submodule not initialized
708 # + different revision checked out
710 # If --cached was specified the revision in the index will be printed
711 # instead of the currently checked out revision.
713 # $@ = requested paths (default to all)
717 # parse $args after "submodule ... status".
742 orig_flags
="$orig_flags $(git rev-parse --sq-quote "$1")"
747 while read mode sha1 stage path
749 name
=$
(module_name
"$path") ||
exit
750 url
=$
(git config submodule.
"$name".url
)
751 displaypath
="$prefix$path"
752 if test -z "$url" ||
! test -d "$path"/.git
-o -f "$path"/.git
754 say
"-$sha1 $displaypath"
757 set_name_rev
"$path" "$sha1"
758 if git diff-files
--ignore-submodules=dirty
--quiet -- "$path"
760 say
" $sha1 $displaypath$revname"
764 sha1
=$
(clear_local_git_env
; cd "$path" && git rev-parse
--verify HEAD
)
765 set_name_rev
"$path" "$sha1"
767 say
"+$sha1 $displaypath$revname"
770 if test -n "$recursive"
773 prefix
="$displaypath/"
776 eval cmd_status
"$orig_args"
778 die
"Failed to recurse into submodule path '$path'"
783 # Sync remote urls for submodules
784 # This makes the value for remote.$remote.url match the value
785 # specified in .gitmodules.
810 while read mode sha1 stage path
812 name
=$
(module_name
"$path")
813 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
815 # Possibly a url relative to parent
818 url
=$
(resolve_relative_url
"$url") ||
exit
822 say
"Synchronizing submodule url for '$name'"
823 git config submodule.
"$name".url
"$url"
825 if test -e "$path"/.git
830 remote
=$
(get_default_remote
)
831 git config remote.
"$remote".url
"$url"
837 # This loop parses the command line arguments to find the
838 # subcommand name to dispatch. Parsing of the subcommand specific
839 # options are primarily done by the subcommand implementations.
840 # Subcommand specific options such as --branch and --cached are
841 # parsed here as well, for backward compatibility.
843 while test $# != 0 && test -z "$command"
846 add | foreach | init | update | status | summary | sync
)
876 # No command word defaults to "status"
877 test -n "$command" ||
command=status
879 # "-b branch" is accepted only by "add"
880 if test -n "$branch" && test "$command" != add
885 # "--cached" is accepted only by "status" and "summary"
886 if test -n "$cached" && test "$command" != status
-a "$command" != summary