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] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--] [<path>...]
12 or: $dashless [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
13 or: $dashless [--quiet] foreach <command>
14 or: $dashless [--quiet] sync [--] [<path>...]"
27 # Resolve relative url by appending to parent's url
28 resolve_relative_url
()
30 remote
=$
(get_default_remote
)
31 remoteurl
=$
(git config
"remote.$remote.url") ||
32 die
"remote ($remote) does not have a url defined in .git/config"
34 remoteurl
=${remoteurl%/}
40 remoteurl
="${remoteurl%/*}"
49 echo "$remoteurl/${url%/}"
53 # Get submodule info for registered submodules
54 # $@ = path to limit submodule list
58 git ls-files
--error-unmatch --stage -- "$@" |
grep '^160000 '
62 # Map submodule path to submodule name
68 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
69 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
70 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
71 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
73 die
"No submodule mapping found in .gitmodules for path '$path'"
80 # Prior to calling, cmd_update checks that a possibly existing
81 # path is not a git repository.
82 # Likewise, cmd_add checks that path does not exist at all,
83 # since it is the location of a new submodule.
91 # If there already is a directory at the submodule path,
92 # expect it to be empty (since that is the default checkout
93 # action) and try to remove it.
94 # Note: if $path is a symlink to a directory the test will
95 # succeed but the rmdir will fail. We might want to fix this.
98 rmdir "$path" 2>/dev
/null ||
99 die
"Directory '$path' exist, but is neither empty nor a git repository"
103 die
"A file already exist at path '$path'"
105 if test -n "$reference"
107 git-clone
"$reference" -n "$url" "$path"
109 git-clone
-n "$url" "$path"
111 die
"Clone of '$url' into submodule path '$path' failed"
115 # Add a new submodule to the working tree, .gitmodules and the index
119 # optional branch is stored in global branch variable
123 # parse $args after "submodule ... add".
128 case "$2" in '') usage
;; esac
136 case "$2" in '') usage
;; esac
137 reference
="--reference=$2"
161 if test -z "$repo" -o -z "$path"; then
165 # assure repo is absolute or relative to parent
168 # dereference source url relative to parent's url
169 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
176 die
"repo URL: '$repo' must be absolute or begin with ./|../"
181 # multiple //; leading ./; /./; /../; trailing /
182 path
=$
(printf '%s/\n' "$path" |
192 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
193 die
"'$path' already exists in the index"
195 # perhaps the path exists and is already a git repo, else clone it
198 if test -d "$path"/.git
-o -f "$path"/.git
200 echo "Adding existing repo at '$path' to the index"
202 die
"'$path' already exists and is not a valid git repo"
207 url
=$
(resolve_relative_url
"$repo") ||
exit
213 git config submodule.
"$path".url
"$url"
216 module_clone
"$path" "$realrepo" "$reference" ||
exit
220 # ash fails to wordsplit ${branch:+-b "$branch"...}
222 '') git checkout
-f -q ;;
223 ?
*) git checkout
-f -q -b "$branch" "origin/$branch" ;;
225 ) || die
"Unable to checkout submodule '$path'"
229 die
"Failed to add submodule '$path'"
231 git config
-f .gitmodules submodule.
"$path".path
"$path" &&
232 git config
-f .gitmodules submodule.
"$path".url
"$repo" &&
233 git add .gitmodules ||
234 die
"Failed to register submodule '$path'"
238 # Execute an arbitrary command sequence in each checked out
241 # $@ = command to execute
245 # parse $args after "submodule ... foreach".
263 while read mode sha1 stage path
265 if test -e "$path"/.git
267 say
"Entering '$path'"
268 name
=$
(module_name
"$path")
269 (cd "$path" && eval "$@") ||
270 die
"Stopping at '$path'; script returned non-zero status."
276 # Register submodules in .git/config
278 # $@ = requested paths (default to all)
282 # parse $args after "submodule ... init".
304 while read mode sha1 stage path
306 # Skip already registered paths
307 name
=$
(module_name
"$path") ||
exit
308 url
=$
(git config submodule.
"$name".url
)
309 test -z "$url" ||
continue
311 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
313 die
"No url found for submodule path '$path' in .gitmodules"
315 # Possibly a url relative to parent
318 url
=$
(resolve_relative_url
"$url") ||
exit
322 git config submodule.
"$name".url
"$url" ||
323 die
"Failed to register url for submodule path '$path'"
325 upd
="$(git config -f .gitmodules submodule."$name".update)"
327 git config submodule.
"$name".update
"$upd" ||
328 die
"Failed to register update mode for submodule path '$path'"
330 say
"Submodule '$name' ($url) registered for path '$path'"
335 # Update each submodule path to correct revision, using clone and checkout as needed
337 # $@ = requested paths (default to all)
341 # parse $args after "submodule ... update".
362 case "$2" in '') usage
;; esac
363 reference
="--reference=$2"
389 cmd_init
"--" "$@" ||
return
393 while read mode sha1 stage path
395 name
=$
(module_name
"$path") ||
exit
396 url
=$
(git config submodule.
"$name".url
)
397 update_module
=$
(git config submodule.
"$name".update
)
400 # Only mention uninitialized submodules when its
401 # path have been specified
403 say
"Submodule path '$path' not initialized" &&
404 say
"Maybe you want to use 'update --init'?"
408 if ! test -d "$path"/.git
-o -f "$path"/.git
410 module_clone
"$path" "$url" "$reference"||
exit
413 subsha1
=$
(unset GIT_DIR
; cd "$path" &&
414 git rev-parse
--verify HEAD
) ||
415 die
"Unable to find current revision in submodule path '$path'"
418 if ! test -z "$update"
420 update_module
=$update
423 if test "$subsha1" != "$sha1"
426 if test -z "$subsha1"
431 if test -z "$nofetch"
433 (unset GIT_DIR
; cd "$path" &&
435 die
"Unable to fetch in submodule path '$path'"
438 case "$update_module" in
450 command="git checkout $force -q"
456 (unset GIT_DIR
; cd "$path" && $command "$sha1") ||
457 die
"Unable to $action '$sha1' in submodule path '$path'"
458 say
"Submodule path '$path': $msg '$sha1'"
467 git describe
"$2" 2>/dev
/null ||
468 git describe
--tags "$2" 2>/dev
/null ||
469 git describe
--contains "$2" 2>/dev
/null ||
470 git describe
--all --always "$2"
473 test -z "$revname" || revname
=" ($revname)"
476 # Show commit summary for submodules in index or working tree
478 # If '--cached' is given, show summary between index and given commit,
479 # or between working tree and given commit
481 # $@ = [commit (default 'HEAD'),] requested paths (default all)
487 # parse $args after "submodule ... summary".
498 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
520 test $summary_limit = 0 && return
522 if rev=$
(git rev-parse
-q --verify "$1^0")
531 # Get modified modules cared by user
532 modules
=$
(git diff-index
$cached --raw $head -- "$@" |
533 egrep '^:([0-7]* )?160000' |
534 while read mod_src mod_dst sha1_src sha1_dst status name
536 # Always show modules deleted or type-changed (blob<->module)
537 test $status = D
-o $status = T
&& echo "$name" && continue
538 # Also show added or modified modules which are checked out
539 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
544 test -z "$modules" && return
546 git diff-index
$cached --raw $head -- $modules |
547 egrep '^:([0-7]* )?160000' |
549 while read mod_src mod_dst sha1_src sha1_dst status name
551 if test -z "$cached" &&
552 test $sha1_dst = 0000000000000000000000000000000000000000
556 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
558 100644 |
100755 |
120000)
559 sha1_dst
=$
(git hash-object
$name)
565 echo >&2 "unexpected mode $mod_dst"
572 test $mod_src = 160000 &&
573 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
576 test $mod_dst = 160000 &&
577 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
581 case "$missing_src,$missing_dst" in
583 errmsg
=" Warn: $name doesn't contain commit $sha1_src"
586 errmsg
=" Warn: $name doesn't contain commit $sha1_dst"
589 errmsg
=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
594 if test $mod_src = 160000 -a $mod_dst = 160000
596 range
="$sha1_src...$sha1_dst"
597 elif test $mod_src = 160000
603 GIT_DIR
="$name/.git" \
604 git log
--pretty=oneline
--first-parent $range |
wc -l
606 total_commits
=" ($(($total_commits + 0)))"
610 sha1_abbr_src
=$
(echo $sha1_src | cut
-c1-7)
611 sha1_abbr_dst
=$
(echo $sha1_dst | cut
-c1-7)
614 if test $mod_dst = 160000
616 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
618 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
621 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
625 # Don't give error msg for modification whose dst is not submodule
626 # i.e. deleted or changed to blob
627 test $mod_dst = 160000 && echo "$errmsg"
629 if test $mod_src = 160000 -a $mod_dst = 160000
632 test $summary_limit -gt 0 && limit
="-$summary_limit"
633 GIT_DIR
="$name/.git" \
634 git log
$limit --pretty='format: %m %s' \
635 --first-parent $sha1_src...
$sha1_dst
636 elif test $mod_dst = 160000
638 GIT_DIR
="$name/.git" \
639 git log
--pretty='format: > %s' -1 $sha1_dst
641 GIT_DIR
="$name/.git" \
642 git log
--pretty='format: < %s' -1 $sha1_src
648 if test -n "$for_status"; then
649 echo "# Modified submodules:"
651 sed -e 's|^|# |' -e 's|^# $|#|'
657 # List all submodules, prefixed with:
658 # - submodule not initialized
659 # + different revision checked out
661 # If --cached was specified the revision in the index will be printed
662 # instead of the currently checked out revision.
664 # $@ = requested paths (default to all)
668 # parse $args after "submodule ... status".
693 while read mode sha1 stage path
695 name
=$
(module_name
"$path") ||
exit
696 url
=$
(git config submodule.
"$name".url
)
697 if test -z "$url" ||
! test -d "$path"/.git
-o -f "$path"/.git
702 set_name_rev
"$path" "$sha1"
703 if git diff-files
--quiet -- "$path"
705 say
" $sha1 $path$revname"
709 sha1
=$
(unset GIT_DIR
; cd "$path" && git rev-parse
--verify HEAD
)
710 set_name_rev
"$path" "$sha1"
712 say
"+$sha1 $path$revname"
717 # Sync remote urls for submodules
718 # This makes the value for remote.$remote.url match the value
719 # specified in .gitmodules.
744 while read mode sha1 stage path
746 name
=$
(module_name
"$path")
747 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
749 # Possibly a url relative to parent
752 url
=$
(resolve_relative_url
"$url") ||
exit
756 if test -e "$path"/.git
761 remote
=$
(get_default_remote
)
762 say
"Synchronizing submodule url for '$name'"
763 git config remote.
"$remote".url
"$url"
769 # This loop parses the command line arguments to find the
770 # subcommand name to dispatch. Parsing of the subcommand specific
771 # options are primarily done by the subcommand implementations.
772 # Subcommand specific options such as --branch and --cached are
773 # parsed here as well, for backward compatibility.
775 while test $# != 0 && test -z "$command"
778 add | foreach | init | update | status | summary | sync
)
808 # No command word defaults to "status"
809 test -n "$command" ||
command=status
811 # "-b branch" is accepted only by "add"
812 if test -n "$branch" && test "$command" != add
817 # "--cached" is accepted only by "status" and "summary"
818 if test -n "$cached" && test "$command" != status
-a "$command" != summary