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 ./|../"
169 # strip trailing slashes from path
170 path
=$
(echo "$path" |
sed -e 's|/*$||')
172 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
173 die
"'$path' already exists in the index"
175 # perhaps the path exists and is already a git repo, else clone it
178 if test -d "$path"/.git
-o -f "$path"/.git
180 echo "Adding existing repo at '$path' to the index"
182 die
"'$path' already exists and is not a valid git repo"
187 url
=$
(resolve_relative_url
"$repo") ||
exit
193 git config submodule.
"$path".url
"$url"
196 module_clone
"$path" "$realrepo" ||
exit
197 (unset GIT_DIR
; cd "$path" && git checkout
-q ${branch:+-b "$branch" "origin/$branch"}) ||
198 die
"Unable to checkout submodule '$path'"
202 die
"Failed to add submodule '$path'"
204 git config
-f .gitmodules submodule.
"$path".path
"$path" &&
205 git config
-f .gitmodules submodule.
"$path".url
"$repo" &&
206 git add .gitmodules ||
207 die
"Failed to register submodule '$path'"
211 # Execute an arbitrary command sequence in each checked out
214 # $@ = command to execute
219 while read mode sha1 stage path
221 if test -e "$path"/.git
223 say
"Entering '$path'"
224 (cd "$path" && eval "$@") ||
225 die
"Stopping at '$path'; script returned non-zero status."
231 # Register submodules in .git/config
233 # $@ = requested paths (default to all)
237 # parse $args after "submodule ... init".
259 while read mode sha1 stage path
261 # Skip already registered paths
262 name
=$
(module_name
"$path") ||
exit
263 url
=$
(git config submodule.
"$name".url
)
264 test -z "$url" ||
continue
266 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
268 die
"No url found for submodule path '$path' in .gitmodules"
270 # Possibly a url relative to parent
273 url
=$
(resolve_relative_url
"$url") ||
exit
277 git config submodule.
"$name".url
"$url" ||
278 die
"Failed to register url for submodule path '$path'"
280 say
"Submodule '$name' ($url) registered for path '$path'"
285 # Update each submodule path to correct revision, using clone and checkout as needed
287 # $@ = requested paths (default to all)
291 # parse $args after "submodule ... update".
301 cmd_init
"$@" ||
return
317 while read mode sha1 stage path
319 name
=$
(module_name
"$path") ||
exit
320 url
=$
(git config submodule.
"$name".url
)
323 # Only mention uninitialized submodules when its
324 # path have been specified
326 say
"Submodule path '$path' not initialized"
327 say
"Maybe you want to use 'update --init'?"
331 if ! test -d "$path"/.git
-o -f "$path"/.git
333 module_clone
"$path" "$url" ||
exit
336 subsha1
=$
(unset GIT_DIR
; cd "$path" &&
337 git rev-parse
--verify HEAD
) ||
338 die
"Unable to find current revision in submodule path '$path'"
341 if test "$subsha1" != "$sha1"
343 (unset GIT_DIR
; cd "$path" && git-fetch
&&
344 git-checkout
-q "$sha1") ||
345 die
"Unable to checkout '$sha1' in submodule path '$path'"
347 say
"Submodule path '$path': checked out '$sha1'"
356 git describe
"$2" 2>/dev
/null ||
357 git describe
--tags "$2" 2>/dev
/null ||
358 git describe
--contains "$2" 2>/dev
/null ||
359 git describe
--all --always "$2"
362 test -z "$revname" || revname
=" ($revname)"
365 # Show commit summary for submodules in index or working tree
367 # If '--cached' is given, show summary between index and given commit,
368 # or between working tree and given commit
370 # $@ = [commit (default 'HEAD'),] requested paths (default all)
376 # parse $args after "submodule ... summary".
387 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
409 test $summary_limit = 0 && return
411 if rev=$
(git rev-parse
--verify "$1^0" 2>/dev
/null
)
420 # Get modified modules cared by user
421 modules
=$
(git diff-index
$cached --raw $head -- "$@" |
422 grep -e '^:160000' -e '^:[0-7]* 160000' |
423 while read mod_src mod_dst sha1_src sha1_dst status name
425 # Always show modules deleted or type-changed (blob<->module)
426 test $status = D
-o $status = T
&& echo "$name" && continue
427 # Also show added or modified modules which are checked out
428 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
433 test -z "$modules" && return
435 git diff-index
$cached --raw $head -- $modules |
436 grep -e '^:160000' -e '^:[0-7]* 160000' |
438 while read mod_src mod_dst sha1_src sha1_dst status name
440 if test -z "$cached" &&
441 test $sha1_dst = 0000000000000000000000000000000000000000
445 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
447 100644 |
100755 |
120000)
448 sha1_dst
=$
(git hash-object
$name)
454 echo >&2 "unexpected mode $mod_dst"
461 test $mod_src = 160000 &&
462 ! GIT_DIR
="$name/.git" git-rev-parse
--verify $sha1_src^
0 >/dev
/null
2>&1 &&
465 test $mod_dst = 160000 &&
466 ! GIT_DIR
="$name/.git" git-rev-parse
--verify $sha1_dst^
0 >/dev
/null
2>&1 &&
470 case "$missing_src,$missing_dst" in
472 errmsg
=" Warn: $name doesn't contain commit $sha1_src"
475 errmsg
=" Warn: $name doesn't contain commit $sha1_dst"
478 errmsg
=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
483 if test $mod_src = 160000 -a $mod_dst = 160000
485 range
="$sha1_src...$sha1_dst"
486 elif test $mod_src = 160000
492 GIT_DIR
="$name/.git" \
493 git log
--pretty=oneline
--first-parent $range |
wc -l
495 total_commits
=" ($(($total_commits + 0)))"
499 sha1_abbr_src
=$
(echo $sha1_src | cut
-c1-7)
500 sha1_abbr_dst
=$
(echo $sha1_dst | cut
-c1-7)
503 if test $mod_dst = 160000
505 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
507 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
510 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
514 # Don't give error msg for modification whose dst is not submodule
515 # i.e. deleted or changed to blob
516 test $mod_dst = 160000 && echo "$errmsg"
518 if test $mod_src = 160000 -a $mod_dst = 160000
521 test $summary_limit -gt 0 && limit
="-$summary_limit"
522 GIT_DIR
="$name/.git" \
523 git log
$limit --pretty='format: %m %s' \
524 --first-parent $sha1_src...
$sha1_dst
525 elif test $mod_dst = 160000
527 GIT_DIR
="$name/.git" \
528 git log
--pretty='format: > %s' -1 $sha1_dst
530 GIT_DIR
="$name/.git" \
531 git log
--pretty='format: < %s' -1 $sha1_src
537 if test -n "$for_status"; then
538 echo "# Modified submodules:"
540 sed -e 's|^|# |' -e 's|^# $|#|'
546 # List all submodules, prefixed with:
547 # - submodule not initialized
548 # + different revision checked out
550 # If --cached was specified the revision in the index will be printed
551 # instead of the currently checked out revision.
553 # $@ = requested paths (default to all)
557 # parse $args after "submodule ... status".
582 while read mode sha1 stage path
584 name
=$
(module_name
"$path") ||
exit
585 url
=$
(git config submodule.
"$name".url
)
586 if test -z "$url" ||
! test -d "$path"/.git
-o -f "$path"/.git
591 set_name_rev
"$path" "$sha1"
592 if git diff-files
--quiet -- "$path"
594 say
" $sha1 $path$revname"
598 sha1
=$
(unset GIT_DIR
; cd "$path" && git rev-parse
--verify HEAD
)
599 set_name_rev
"$path" "$sha1"
601 say
"+$sha1 $path$revname"
606 # Sync remote urls for submodules
607 # This makes the value for remote.$remote.url match the value
608 # specified in .gitmodules.
633 while read mode sha1 stage path
635 name
=$
(module_name
"$path")
636 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
637 if test -e "$path"/.git
642 remote
=$
(get_default_remote
)
643 say
"Synchronizing submodule url for '$name'"
644 git config remote.
"$remote".url
"$url"
650 # This loop parses the command line arguments to find the
651 # subcommand name to dispatch. Parsing of the subcommand specific
652 # options are primarily done by the subcommand implementations.
653 # Subcommand specific options such as --branch and --cached are
654 # parsed here as well, for backward compatibility.
656 while test $# != 0 && test -z "$command"
659 add | foreach | init | update | status | summary | sync
)
689 # No command word defaults to "status"
690 test -n "$command" ||
command=status
692 # "-b branch" is accepted only by "add"
693 if test -n "$branch" && test "$command" != add
698 # "--cached" is accepted only by "status" and "summary"
699 if test -n "$cached" && test "$command" != status
-a "$command" != summary