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>]|[sync [--] [<path>...]]"
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"
38 remoteurl
=${remoteurl%/}
44 remoteurl
="${remoteurl%/*}"
53 echo "$remoteurl/${url%/}"
57 # Get submodule info for registered submodules
58 # $@ = path to limit submodule list
62 git ls-files
--stage -- "$@" |
grep '^160000 '
66 # Map submodule path to submodule name
72 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
73 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
74 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
75 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
77 die
"No submodule mapping found in .gitmodules for path '$path'"
84 # Prior to calling, cmd_update checks that a possibly existing
85 # path is not a git repository.
86 # Likewise, cmd_add checks that path does not exist at all,
87 # since it is the location of a new submodule.
94 # If there already is a directory at the submodule path,
95 # expect it to be empty (since that is the default checkout
96 # action) and try to remove it.
97 # Note: if $path is a symlink to a directory the test will
98 # succeed but the rmdir will fail. We might want to fix this.
101 rmdir "$path" 2>/dev
/null ||
102 die
"Directory '$path' exist, but is neither empty nor a git repository"
106 die
"A file already exist at path '$path'"
108 git-clone
-n "$url" "$path" ||
109 die
"Clone of '$url' into submodule path '$path' failed"
113 # Add a new submodule to the working tree, .gitmodules and the index
117 # optional branch is stored in global branch variable
121 # parse $args after "submodule ... add".
126 case "$2" in '') usage
;; esac
150 if test -z "$repo" -o -z "$path"; then
154 # assure repo is absolute or relative to parent
157 # dereference source url relative to parent's url
158 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
165 die
"repo URL: '$repo' must be absolute or begin with ./|../"
170 # multiple //; leading ./; /./; /../; trailing /
171 path
=$
(printf '%s/\n' "$path" |
181 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
182 die
"'$path' already exists in the index"
184 # perhaps the path exists and is already a git repo, else clone it
187 if test -d "$path"/.git
-o -f "$path"/.git
189 echo "Adding existing repo at '$path' to the index"
191 die
"'$path' already exists and is not a valid git repo"
196 url
=$
(resolve_relative_url
"$repo") ||
exit
202 git config submodule.
"$path".url
"$url"
205 module_clone
"$path" "$realrepo" ||
exit
206 (unset GIT_DIR
; cd "$path" && git checkout
-f -q ${branch:+-b "$branch" "origin/$branch"}) ||
207 die
"Unable to checkout submodule '$path'"
211 die
"Failed to add submodule '$path'"
213 git config
-f .gitmodules submodule.
"$path".path
"$path" &&
214 git config
-f .gitmodules submodule.
"$path".url
"$repo" &&
215 git add .gitmodules ||
216 die
"Failed to register submodule '$path'"
220 # Execute an arbitrary command sequence in each checked out
223 # $@ = command to execute
228 while read mode sha1 stage path
230 if test -e "$path"/.git
232 say
"Entering '$path'"
233 (cd "$path" && eval "$@") ||
234 die
"Stopping at '$path'; script returned non-zero status."
240 # Register submodules in .git/config
242 # $@ = requested paths (default to all)
246 # parse $args after "submodule ... init".
268 while read mode sha1 stage path
270 # Skip already registered paths
271 name
=$
(module_name
"$path") ||
exit
272 url
=$
(git config submodule.
"$name".url
)
273 test -z "$url" ||
continue
275 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
277 die
"No url found for submodule path '$path' in .gitmodules"
279 # Possibly a url relative to parent
282 url
=$
(resolve_relative_url
"$url") ||
exit
286 git config submodule.
"$name".url
"$url" ||
287 die
"Failed to register url for submodule path '$path'"
289 say
"Submodule '$name' ($url) registered for path '$path'"
294 # Update each submodule path to correct revision, using clone and checkout as needed
296 # $@ = requested paths (default to all)
300 # parse $args after "submodule ... update".
310 cmd_init
"$@" ||
return
326 while read mode sha1 stage path
328 name
=$
(module_name
"$path") ||
exit
329 url
=$
(git config submodule.
"$name".url
)
332 # Only mention uninitialized submodules when its
333 # path have been specified
335 say
"Submodule path '$path' not initialized" &&
336 say
"Maybe you want to use 'update --init'?"
340 if ! test -d "$path"/.git
-o -f "$path"/.git
342 module_clone
"$path" "$url" ||
exit
345 subsha1
=$
(unset GIT_DIR
; cd "$path" &&
346 git rev-parse
--verify HEAD
) ||
347 die
"Unable to find current revision in submodule path '$path'"
350 if test "$subsha1" != "$sha1"
353 if test -z "$subsha1"
357 (unset GIT_DIR
; cd "$path" && git-fetch
&&
358 git-checkout
$force -q "$sha1") ||
359 die
"Unable to checkout '$sha1' in submodule path '$path'"
361 say
"Submodule path '$path': checked out '$sha1'"
370 git describe
"$2" 2>/dev
/null ||
371 git describe
--tags "$2" 2>/dev
/null ||
372 git describe
--contains "$2" 2>/dev
/null ||
373 git describe
--all --always "$2"
376 test -z "$revname" || revname
=" ($revname)"
379 # Show commit summary for submodules in index or working tree
381 # If '--cached' is given, show summary between index and given commit,
382 # or between working tree and given commit
384 # $@ = [commit (default 'HEAD'),] requested paths (default all)
390 # parse $args after "submodule ... summary".
401 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
423 test $summary_limit = 0 && return
425 if rev=$
(git rev-parse
-q --verify "$1^0")
434 # Get modified modules cared by user
435 modules
=$
(git diff-index
$cached --raw $head -- "$@" |
436 egrep '^:([0-7]* )?160000' |
437 while read mod_src mod_dst sha1_src sha1_dst status name
439 # Always show modules deleted or type-changed (blob<->module)
440 test $status = D
-o $status = T
&& echo "$name" && continue
441 # Also show added or modified modules which are checked out
442 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
447 test -z "$modules" && return
449 git diff-index
$cached --raw $head -- $modules |
450 egrep '^:([0-7]* )?160000' |
452 while read mod_src mod_dst sha1_src sha1_dst status name
454 if test -z "$cached" &&
455 test $sha1_dst = 0000000000000000000000000000000000000000
459 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
461 100644 |
100755 |
120000)
462 sha1_dst
=$
(git hash-object
$name)
468 echo >&2 "unexpected mode $mod_dst"
475 test $mod_src = 160000 &&
476 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
479 test $mod_dst = 160000 &&
480 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
484 case "$missing_src,$missing_dst" in
486 errmsg
=" Warn: $name doesn't contain commit $sha1_src"
489 errmsg
=" Warn: $name doesn't contain commit $sha1_dst"
492 errmsg
=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
497 if test $mod_src = 160000 -a $mod_dst = 160000
499 range
="$sha1_src...$sha1_dst"
500 elif test $mod_src = 160000
506 GIT_DIR
="$name/.git" \
507 git log
--pretty=oneline
--first-parent $range |
wc -l
509 total_commits
=" ($(($total_commits + 0)))"
513 sha1_abbr_src
=$
(echo $sha1_src | cut
-c1-7)
514 sha1_abbr_dst
=$
(echo $sha1_dst | cut
-c1-7)
517 if test $mod_dst = 160000
519 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
521 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
524 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
528 # Don't give error msg for modification whose dst is not submodule
529 # i.e. deleted or changed to blob
530 test $mod_dst = 160000 && echo "$errmsg"
532 if test $mod_src = 160000 -a $mod_dst = 160000
535 test $summary_limit -gt 0 && limit
="-$summary_limit"
536 GIT_DIR
="$name/.git" \
537 git log
$limit --pretty='format: %m %s' \
538 --first-parent $sha1_src...
$sha1_dst
539 elif test $mod_dst = 160000
541 GIT_DIR
="$name/.git" \
542 git log
--pretty='format: > %s' -1 $sha1_dst
544 GIT_DIR
="$name/.git" \
545 git log
--pretty='format: < %s' -1 $sha1_src
551 if test -n "$for_status"; then
552 echo "# Modified submodules:"
554 sed -e 's|^|# |' -e 's|^# $|#|'
560 # List all submodules, prefixed with:
561 # - submodule not initialized
562 # + different revision checked out
564 # If --cached was specified the revision in the index will be printed
565 # instead of the currently checked out revision.
567 # $@ = requested paths (default to all)
571 # parse $args after "submodule ... status".
596 while read mode sha1 stage path
598 name
=$
(module_name
"$path") ||
exit
599 url
=$
(git config submodule.
"$name".url
)
600 if test -z "$url" ||
! test -d "$path"/.git
-o -f "$path"/.git
605 set_name_rev
"$path" "$sha1"
606 if git diff-files
--quiet -- "$path"
608 say
" $sha1 $path$revname"
612 sha1
=$
(unset GIT_DIR
; cd "$path" && git rev-parse
--verify HEAD
)
613 set_name_rev
"$path" "$sha1"
615 say
"+$sha1 $path$revname"
620 # Sync remote urls for submodules
621 # This makes the value for remote.$remote.url match the value
622 # specified in .gitmodules.
647 while read mode sha1 stage path
649 name
=$
(module_name
"$path")
650 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
652 # Possibly a url relative to parent
655 url
=$
(resolve_relative_url
"$url") ||
exit
659 if test -e "$path"/.git
664 remote
=$
(get_default_remote
)
665 say
"Synchronizing submodule url for '$name'"
666 git config remote.
"$remote".url
"$url"
672 # This loop parses the command line arguments to find the
673 # subcommand name to dispatch. Parsing of the subcommand specific
674 # options are primarily done by the subcommand implementations.
675 # Subcommand specific options such as --branch and --cached are
676 # parsed here as well, for backward compatibility.
678 while test $# != 0 && test -z "$command"
681 add | foreach | init | update | status | summary | sync
)
711 # No command word defaults to "status"
712 test -n "$command" ||
command=status
714 # "-b branch" is accepted only by "add"
715 if test -n "$branch" && test "$command" != add
720 # "--cached" is accepted only by "status" and "summary"
721 if test -n "$cached" && test "$command" != status
-a "$command" != summary