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] [--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>...]"
29 # Resolve relative url by appending to parent's url
30 resolve_relative_url
()
32 remote
=$
(get_default_remote
)
33 remoteurl
=$
(git config
"remote.$remote.url") ||
34 die
"remote ($remote) does not have a url defined in .git/config"
36 remoteurl
=${remoteurl%/}
42 remoteurl
="${remoteurl%/*}"
51 echo "$remoteurl/${url%/}"
55 # Get submodule info for registered submodules
56 # $@ = path to limit submodule list
60 git ls-files
--error-unmatch --stage -- "$@" | sane_grep
'^160000 '
64 # Map submodule path to submodule name
70 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
71 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
72 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
73 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
75 die
"No submodule mapping found in .gitmodules for path '$path'"
82 # Prior to calling, cmd_update checks that a possibly existing
83 # path is not a git repository.
84 # Likewise, cmd_add checks that path does not exist at all,
85 # since it is the location of a new submodule.
93 # If there already is a directory at the submodule path,
94 # expect it to be empty (since that is the default checkout
95 # action) and try to remove it.
96 # Note: if $path is a symlink to a directory the test will
97 # succeed but the rmdir will fail. We might want to fix this.
100 rmdir "$path" 2>/dev
/null ||
101 die
"Directory '$path' exists, but is neither empty nor a git repository"
105 die
"A file already exist at path '$path'"
107 if test -n "$reference"
109 git-clone
"$reference" -n "$url" "$path"
111 git-clone
-n "$url" "$path"
113 die
"Clone of '$url' into submodule path '$path' failed"
117 # Add a new submodule to the working tree, .gitmodules and the index
121 # optional branch is stored in global branch variable
125 # parse $args after "submodule ... add".
130 case "$2" in '') usage
;; esac
138 case "$2" in '') usage
;; esac
139 reference
="--reference=$2"
163 if test -z "$path"; then
164 path
=$
(echo "$repo" |
165 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
168 if test -z "$repo" -o -z "$path"; then
172 # assure repo is absolute or relative to parent
175 # dereference source url relative to parent's url
176 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
183 die
"repo URL: '$repo' must be absolute or begin with ./|../"
188 # multiple //; leading ./; /./; /../; trailing /
189 path
=$
(printf '%s/\n' "$path" |
199 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
200 die
"'$path' already exists in the index"
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'"
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 .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".
273 while read mode sha1 stage path
275 if test -e "$path"/.git
277 say
"Entering '$prefix$path'"
278 name
=$
(module_name
"$path")
280 prefix
="$prefix$path/"
284 if test -n "$recursive"
286 cmd_foreach
"--recursive" "$@"
289 die
"Stopping at '$path'; script returned non-zero status."
295 # Register submodules in .git/config
297 # $@ = requested paths (default to all)
301 # parse $args after "submodule ... init".
323 while read mode sha1 stage path
325 # Skip already registered paths
326 name
=$
(module_name
"$path") ||
exit
327 url
=$
(git config submodule.
"$name".url
)
328 test -z "$url" ||
continue
330 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
332 die
"No url found for submodule path '$path' in .gitmodules"
334 # Possibly a url relative to parent
337 url
=$
(resolve_relative_url
"$url") ||
exit
341 git config submodule.
"$name".url
"$url" ||
342 die
"Failed to register url for submodule path '$path'"
344 upd
="$(git config -f .gitmodules submodule."$name".update)"
346 git config submodule.
"$name".update
"$upd" ||
347 die
"Failed to register update mode for submodule path '$path'"
349 say
"Submodule '$name' ($url) registered for path '$path'"
354 # Update each submodule path to correct revision, using clone and checkout as needed
356 # $@ = requested paths (default to all)
360 # parse $args after "submodule ... update".
382 case "$2" in '') usage
;; esac
383 reference
="--reference=$2"
413 cmd_init
"--" "$@" ||
return
417 while read mode sha1 stage path
419 name
=$
(module_name
"$path") ||
exit
420 url
=$
(git config submodule.
"$name".url
)
421 update_module
=$
(git config submodule.
"$name".update
)
424 # Only mention uninitialized submodules when its
425 # path have been specified
427 say
"Submodule path '$path' not initialized" &&
428 say
"Maybe you want to use 'update --init'?"
432 if ! test -d "$path"/.git
-o -f "$path"/.git
434 module_clone
"$path" "$url" "$reference"||
exit
437 subsha1
=$
(unset GIT_DIR
; cd "$path" &&
438 git rev-parse
--verify HEAD
) ||
439 die
"Unable to find current revision in submodule path '$path'"
442 if ! test -z "$update"
444 update_module
=$update
447 if test "$subsha1" != "$sha1"
450 if test -z "$subsha1"
455 if test -z "$nofetch"
457 (unset GIT_DIR
; cd "$path" &&
459 die
"Unable to fetch in submodule path '$path'"
462 case "$update_module" in
474 command="git checkout $force -q"
480 (unset GIT_DIR
; cd "$path" && $command "$sha1") ||
481 die
"Unable to $action '$sha1' in submodule path '$path'"
482 say
"Submodule path '$path': $msg '$sha1'"
485 if test -n "$recursive"
487 (unset GIT_DIR
; cd "$path" && cmd_update
$orig_args) ||
488 die
"Failed to recurse into submodule path '$path'"
497 git describe
"$2" 2>/dev
/null ||
498 git describe
--tags "$2" 2>/dev
/null ||
499 git describe
--contains "$2" 2>/dev
/null ||
500 git describe
--all --always "$2"
503 test -z "$revname" || revname
=" ($revname)"
506 # Show commit summary for submodules in index or working tree
508 # If '--cached' is given, show summary between index and given commit,
509 # or between working tree and given commit
511 # $@ = [commit (default 'HEAD'),] requested paths (default all)
518 # parse $args after "submodule ... summary".
532 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
554 test $summary_limit = 0 && return
556 if rev=$
(git rev-parse
-q --verify --default HEAD
${1+"$1"})
560 elif test -z "$1" -o "$1" = "HEAD"
562 # before the first commit: compare with an empty tree
563 head=$
(git hash-object
-w -t tree
--stdin </dev
/null
)
564 test -z "$1" ||
shift
572 die
"--cached cannot be used with --files"
578 # Get modified modules cared by user
579 modules
=$
(git
$diff_cmd $cached --raw $head -- "$@" |
580 sane_egrep
'^:([0-7]* )?160000' |
581 while read mod_src mod_dst sha1_src sha1_dst status name
583 # Always show modules deleted or type-changed (blob<->module)
584 test $status = D
-o $status = T
&& echo "$name" && continue
585 # Also show added or modified modules which are checked out
586 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
591 test -z "$modules" && return
593 git
$diff_cmd $cached --raw $head -- $modules |
594 sane_egrep
'^:([0-7]* )?160000' |
596 while read mod_src mod_dst sha1_src sha1_dst status name
598 if test -z "$cached" &&
599 test $sha1_dst = 0000000000000000000000000000000000000000
603 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
605 100644 |
100755 |
120000)
606 sha1_dst
=$
(git hash-object
$name)
612 echo >&2 "unexpected mode $mod_dst"
619 test $mod_src = 160000 &&
620 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
623 test $mod_dst = 160000 &&
624 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
628 case "$missing_src,$missing_dst" in
630 errmsg
=" Warn: $name doesn't contain commit $sha1_src"
633 errmsg
=" Warn: $name doesn't contain commit $sha1_dst"
636 errmsg
=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
641 if test $mod_src = 160000 -a $mod_dst = 160000
643 range
="$sha1_src...$sha1_dst"
644 elif test $mod_src = 160000
650 GIT_DIR
="$name/.git" \
651 git log
--pretty=oneline
--first-parent $range |
wc -l
653 total_commits
=" ($(($total_commits + 0)))"
657 sha1_abbr_src
=$
(echo $sha1_src | cut
-c1-7)
658 sha1_abbr_dst
=$
(echo $sha1_dst | cut
-c1-7)
661 if test $mod_dst = 160000
663 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
665 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
668 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
672 # Don't give error msg for modification whose dst is not submodule
673 # i.e. deleted or changed to blob
674 test $mod_dst = 160000 && echo "$errmsg"
676 if test $mod_src = 160000 -a $mod_dst = 160000
679 test $summary_limit -gt 0 && limit
="-$summary_limit"
680 GIT_DIR
="$name/.git" \
681 git log
$limit --pretty='format: %m %s' \
682 --first-parent $sha1_src...
$sha1_dst
683 elif test $mod_dst = 160000
685 GIT_DIR
="$name/.git" \
686 git log
--pretty='format: > %s' -1 $sha1_dst
688 GIT_DIR
="$name/.git" \
689 git log
--pretty='format: < %s' -1 $sha1_src
695 if test -n "$for_status"; then
696 if [ -n "$files" ]; then
697 echo "# Submodules changed but not updated:"
699 echo "# Submodule changes to be committed:"
702 sed -e 's|^|# |' -e 's|^# $|#|'
708 # List all submodules, prefixed with:
709 # - submodule not initialized
710 # + different revision checked out
712 # If --cached was specified the revision in the index will be printed
713 # instead of the currently checked out revision.
715 # $@ = requested paths (default to all)
719 # parse $args after "submodule ... status".
748 while read mode sha1 stage path
750 name
=$
(module_name
"$path") ||
exit
751 url
=$
(git config submodule.
"$name".url
)
752 displaypath
="$prefix$path"
753 if test -z "$url" ||
! test -d "$path"/.git
-o -f "$path"/.git
755 say
"-$sha1 $displaypath"
758 set_name_rev
"$path" "$sha1"
759 if git diff-files
--quiet -- "$path"
761 say
" $sha1 $displaypath$revname"
765 sha1
=$
(unset GIT_DIR
; cd "$path" && git rev-parse
--verify HEAD
)
766 set_name_rev
"$path" "$sha1"
768 say
"+$sha1 $displaypath$revname"
771 if test -n "$recursive"
774 prefix
="$displaypath/"
777 cmd_status
$orig_args
779 die
"Failed to recurse into submodule path '$path'"
784 # Sync remote urls for submodules
785 # This makes the value for remote.$remote.url match the value
786 # specified in .gitmodules.
811 while read mode sha1 stage path
813 name
=$
(module_name
"$path")
814 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
816 # Possibly a url relative to parent
819 url
=$
(resolve_relative_url
"$url") ||
exit
823 if test -e "$path"/.git
828 remote
=$
(get_default_remote
)
829 say
"Synchronizing submodule url for '$name'"
830 git config remote.
"$remote".url
"$url"
836 # This loop parses the command line arguments to find the
837 # subcommand name to dispatch. Parsing of the subcommand specific
838 # options are primarily done by the subcommand implementations.
839 # Subcommand specific options such as --branch and --cached are
840 # parsed here as well, for backward compatibility.
842 while test $# != 0 && test -z "$command"
845 add | foreach | init | update | status | summary | sync
)
875 # No command word defaults to "status"
876 test -n "$command" ||
command=status
878 # "-b branch" is accepted only by "add"
879 if test -n "$branch" && test "$command" != add
884 # "--cached" is accepted only by "status" and "summary"
885 if test -n "$cached" && test "$command" != status
-a "$command" != summary