3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 USAGE
="[--quiet] [--cached] \
8 [add <repo> [-b branch] <path>]|[status|init|update [-i|--init]|summary [-n|--summary-limit <n>] [<commit>]] \
9 [--] [<path>...]|[foreach <command>]"
21 # print stuff on stdout unless -q was specified
31 # Resolve relative url by appending to parent's url
32 resolve_relative_url
()
34 remote
=$
(get_default_remote
)
35 remoteurl
=$
(git config
"remote.$remote.url") ||
36 die
"remote ($remote) does not have a url defined in .git/config"
43 remoteurl
="${remoteurl%/*}"
52 echo "$remoteurl/$url"
56 # Get submodule info for registered submodules
57 # $@ = path to limit submodule list
61 git ls-files
--stage -- "$@" |
grep '^160000 '
65 # Map submodule path to submodule name
71 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
72 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
73 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
74 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
76 die
"No submodule mapping found in .gitmodules for path '$path'"
83 # Prior to calling, cmd_update checks that a possibly existing
84 # path is not a git repository.
85 # Likewise, cmd_add checks that path does not exist at all,
86 # 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' exist, but is neither empty nor a git repository"
105 die
"A file already exist at path '$path'"
107 git-clone
-n "$url" "$path" ||
108 die
"Clone of '$url' into submodule path '$path' failed"
112 # Add a new submodule to the working tree, .gitmodules and the index
116 # optional branch is stored in global branch variable
120 # parse $args after "submodule ... add".
125 case "$2" in '') usage
;; esac
149 if test -z "$repo" -o -z "$path"; then
153 # assure repo is absolute or relative to parent
156 # dereference source url relative to parent's url
157 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
164 die
"repo URL: '$repo' must be absolute or begin with ./|../"
168 # strip trailing slashes from path
169 path
=$
(echo "$path" |
sed -e 's|/*$||')
171 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
172 die
"'$path' already exists in the index"
174 # perhaps the path exists and is already a git repo, else clone it
177 if test -d "$path"/.git
-o -f "$path"/.git
179 echo "Adding existing repo at '$path' to the index"
181 die
"'$path' already exists and is not a valid git repo"
186 url
=$
(resolve_relative_url
"$repo") ||
exit
192 git config submodule.
"$path".url
"$url"
195 module_clone
"$path" "$realrepo" ||
exit
196 (unset GIT_DIR
; cd "$path" && git checkout
-q ${branch:+-b "$branch" "origin/$branch"}) ||
197 die
"Unable to checkout submodule '$path'"
201 die
"Failed to add submodule '$path'"
203 git config
-f .gitmodules submodule.
"$path".path
"$path" &&
204 git config
-f .gitmodules submodule.
"$path".url
"$repo" &&
205 git add .gitmodules ||
206 die
"Failed to register submodule '$path'"
210 # Execute an arbitrary command sequence in each checked out
213 # $@ = command to execute
218 while read mode sha1 stage path
220 if test -e "$path"/.git
222 say
"Entering '$path'"
223 (cd "$path" && eval "$@") ||
224 die
"Stopping at '$path'; script returned non-zero status."
230 # Register submodules in .git/config
232 # $@ = requested paths (default to all)
236 # parse $args after "submodule ... init".
258 while read mode sha1 stage path
260 # Skip already registered paths
261 name
=$
(module_name
"$path") ||
exit
262 url
=$
(git config submodule.
"$name".url
)
263 test -z "$url" ||
continue
265 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
267 die
"No url found for submodule path '$path' in .gitmodules"
269 # Possibly a url relative to parent
272 url
=$
(resolve_relative_url
"$url") ||
exit
276 git config submodule.
"$name".url
"$url" ||
277 die
"Failed to register url for submodule path '$path'"
279 say
"Submodule '$name' ($url) registered for path '$path'"
284 # Update each submodule path to correct revision, using clone and checkout as needed
286 # $@ = requested paths (default to all)
290 # parse $args after "submodule ... update".
300 cmd_init
"$@" ||
return
316 while read mode sha1 stage path
318 name
=$
(module_name
"$path") ||
exit
319 url
=$
(git config submodule.
"$name".url
)
322 # Only mention uninitialized submodules when its
323 # path have been specified
325 say
"Submodule path '$path' not initialized"
326 say
"Maybe you want to use 'update --init'?"
330 if ! test -d "$path"/.git
-o -f "$path"/.git
332 module_clone
"$path" "$url" ||
exit
335 subsha1
=$
(unset GIT_DIR
; cd "$path" &&
336 git rev-parse
--verify HEAD
) ||
337 die
"Unable to find current revision in submodule path '$path'"
340 if test "$subsha1" != "$sha1"
342 (unset GIT_DIR
; cd "$path" && git-fetch
&&
343 git-checkout
-q "$sha1") ||
344 die
"Unable to checkout '$sha1' in submodule path '$path'"
346 say
"Submodule path '$path': checked out '$sha1'"
355 git describe
"$2" 2>/dev
/null ||
356 git describe
--tags "$2" 2>/dev
/null ||
357 git describe
--contains "$2" 2>/dev
/null ||
358 git describe
--all --always "$2"
361 test -z "$revname" || revname
=" ($revname)"
364 # Show commit summary for submodules in index or working tree
366 # If '--cached' is given, show summary between index and given commit,
367 # or between working tree and given commit
369 # $@ = [commit (default 'HEAD'),] requested paths (default all)
375 # parse $args after "submodule ... summary".
386 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
408 test $summary_limit = 0 && return
410 if rev=$
(git rev-parse
--verify "$1^0" 2>/dev
/null
)
419 # Get modified modules cared by user
420 modules
=$
(git diff-index
$cached --raw $head -- "$@" |
421 grep -e '^:160000' -e '^:[0-7]* 160000' |
422 while read mod_src mod_dst sha1_src sha1_dst status name
424 # Always show modules deleted or type-changed (blob<->module)
425 test $status = D
-o $status = T
&& echo "$name" && continue
426 # Also show added or modified modules which are checked out
427 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
432 test -z "$modules" && return
434 git diff-index
$cached --raw $head -- $modules |
435 grep -e '^:160000' -e '^:[0-7]* 160000' |
437 while read mod_src mod_dst sha1_src sha1_dst status name
439 if test -z "$cached" &&
440 test $sha1_dst = 0000000000000000000000000000000000000000
444 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
446 100644 |
100755 |
120000)
447 sha1_dst
=$
(git hash-object
$name)
453 echo >&2 "unexpected mode $mod_dst"
460 test $mod_src = 160000 &&
461 ! GIT_DIR
="$name/.git" git-rev-parse
--verify $sha1_src^
0 >/dev
/null
2>&1 &&
464 test $mod_dst = 160000 &&
465 ! GIT_DIR
="$name/.git" git-rev-parse
--verify $sha1_dst^
0 >/dev
/null
2>&1 &&
469 case "$missing_src,$missing_dst" in
471 errmsg
=" Warn: $name doesn't contain commit $sha1_src"
474 errmsg
=" Warn: $name doesn't contain commit $sha1_dst"
477 errmsg
=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
482 if test $mod_src = 160000 -a $mod_dst = 160000
484 range
="$sha1_src...$sha1_dst"
485 elif test $mod_src = 160000
491 GIT_DIR
="$name/.git" \
492 git log
--pretty=oneline
--first-parent $range |
wc -l
494 total_commits
=" ($(($total_commits + 0)))"
498 sha1_abbr_src
=$
(echo $sha1_src | cut
-c1-7)
499 sha1_abbr_dst
=$
(echo $sha1_dst | cut
-c1-7)
502 if test $mod_dst = 160000
504 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
506 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
509 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
513 # Don't give error msg for modification whose dst is not submodule
514 # i.e. deleted or changed to blob
515 test $mod_dst = 160000 && echo "$errmsg"
517 if test $mod_src = 160000 -a $mod_dst = 160000
520 test $summary_limit -gt 0 && limit
="-$summary_limit"
521 GIT_DIR
="$name/.git" \
522 git log
$limit --pretty='format: %m %s' \
523 --first-parent $sha1_src...
$sha1_dst
524 elif test $mod_dst = 160000
526 GIT_DIR
="$name/.git" \
527 git log
--pretty='format: > %s' -1 $sha1_dst
529 GIT_DIR
="$name/.git" \
530 git log
--pretty='format: < %s' -1 $sha1_src
536 if test -n "$for_status"; then
537 echo "# Modified submodules:"
539 sed -e 's|^|# |' -e 's|^# $|#|'
545 # List all submodules, prefixed with:
546 # - submodule not initialized
547 # + different revision checked out
549 # If --cached was specified the revision in the index will be printed
550 # instead of the currently checked out revision.
552 # $@ = requested paths (default to all)
556 # parse $args after "submodule ... status".
581 while read mode sha1 stage path
583 name
=$
(module_name
"$path") ||
exit
584 url
=$
(git config submodule.
"$name".url
)
585 if test -z "$url" ||
! test -d "$path"/.git
-o -f "$path"/.git
590 set_name_rev
"$path" "$sha1"
591 if git diff-files
--quiet -- "$path"
593 say
" $sha1 $path$revname"
597 sha1
=$
(unset GIT_DIR
; cd "$path" && git rev-parse
--verify HEAD
)
598 set_name_rev
"$path" "$sha1"
600 say
"+$sha1 $path$revname"
605 # This loop parses the command line arguments to find the
606 # subcommand name to dispatch. Parsing of the subcommand specific
607 # options are primarily done by the subcommand implementations.
608 # Subcommand specific options such as --branch and --cached are
609 # parsed here as well, for backward compatibility.
611 while test $# != 0 && test -z "$command"
614 add | foreach | init | update | status | summary
)
644 # No command word defaults to "status"
645 test -n "$command" ||
command=status
647 # "-b branch" is accepted only by "add"
648 if test -n "$branch" && test "$command" != add
653 # "--cached" is accepted only by "status" and "summary"
654 if test -n "$cached" && test "$command" != status
-a "$command" != summary