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] [--recursive] [--] [<path>...]
12 or: $dashless [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
13 or: $dashless [--quiet] foreach [--recursive] <command>
14 or: $dashless [--quiet] sync [--] [<path>...]"
28 # Resolve relative url by appending to parent's url
29 resolve_relative_url
()
31 remote
=$
(get_default_remote
)
32 remoteurl
=$
(git config
"remote.$remote.url") ||
33 die
"remote ($remote) does not have a url defined in .git/config"
35 remoteurl
=${remoteurl%/}
41 remoteurl
="${remoteurl%/*}"
50 echo "$remoteurl/${url%/}"
54 # Get submodule info for registered submodules
55 # $@ = path to limit submodule list
59 git ls-files
--error-unmatch --stage -- "$@" |
grep '^160000 '
63 # Map submodule path to submodule name
69 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
70 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
71 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
72 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
74 die
"No submodule mapping found in .gitmodules for path '$path'"
81 # Prior to calling, cmd_update checks that a possibly existing
82 # path is not a git repository.
83 # Likewise, cmd_add checks that path does not exist at all,
84 # since it is the location of a new submodule.
92 # If there already is a directory at the submodule path,
93 # expect it to be empty (since that is the default checkout
94 # action) and try to remove it.
95 # Note: if $path is a symlink to a directory the test will
96 # succeed but the rmdir will fail. We might want to fix this.
99 rmdir "$path" 2>/dev
/null ||
100 die
"Directory '$path' exist, but is neither empty nor a git repository"
104 die
"A file already exist at path '$path'"
106 if test -n "$reference"
108 git-clone
"$reference" -n "$url" "$path"
110 git-clone
-n "$url" "$path"
112 die
"Clone of '$url' into submodule path '$path' failed"
116 # Add a new submodule to the working tree, .gitmodules and the index
120 # optional branch is stored in global branch variable
124 # parse $args after "submodule ... add".
129 case "$2" in '') usage
;; esac
137 case "$2" in '') usage
;; esac
138 reference
="--reference=$2"
162 if test -z "$repo" -o -z "$path"; then
166 # assure repo is absolute or relative to parent
169 # dereference source url relative to parent's url
170 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
177 die
"repo URL: '$repo' must be absolute or begin with ./|../"
182 # multiple //; leading ./; /./; /../; trailing /
183 path
=$
(printf '%s/\n' "$path" |
193 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
194 die
"'$path' already exists in the index"
196 # perhaps the path exists and is already a git repo, else clone it
199 if test -d "$path"/.git
-o -f "$path"/.git
201 echo "Adding existing repo at '$path' to the index"
203 die
"'$path' already exists and is not a valid git repo"
208 url
=$
(resolve_relative_url
"$repo") ||
exit
214 git config submodule.
"$path".url
"$url"
217 module_clone
"$path" "$realrepo" "$reference" ||
exit
221 # ash fails to wordsplit ${branch:+-b "$branch"...}
223 '') git checkout
-f -q ;;
224 ?
*) git checkout
-f -q -b "$branch" "origin/$branch" ;;
226 ) || die
"Unable to checkout submodule '$path'"
230 die
"Failed to add submodule '$path'"
232 git config
-f .gitmodules submodule.
"$path".path
"$path" &&
233 git config
-f .gitmodules submodule.
"$path".url
"$repo" &&
234 git add .gitmodules ||
235 die
"Failed to register submodule '$path'"
239 # Execute an arbitrary command sequence in each checked out
242 # $@ = command to execute
246 # parse $args after "submodule ... foreach".
267 while read mode sha1 stage path
269 if test -e "$path"/.git
271 say
"Entering '$prefix$path'"
272 name
=$
(module_name
"$path")
274 prefix
="$prefix$path/"
278 if test -n "$recursive"
280 cmd_foreach
"--recursive" "$@"
283 die
"Stopping at '$path'; script returned non-zero status."
289 # Register submodules in .git/config
291 # $@ = requested paths (default to all)
295 # parse $args after "submodule ... init".
317 while read mode sha1 stage path
319 # Skip already registered paths
320 name
=$
(module_name
"$path") ||
exit
321 url
=$
(git config submodule.
"$name".url
)
322 test -z "$url" ||
continue
324 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
326 die
"No url found for submodule path '$path' in .gitmodules"
328 # Possibly a url relative to parent
331 url
=$
(resolve_relative_url
"$url") ||
exit
335 git config submodule.
"$name".url
"$url" ||
336 die
"Failed to register url for submodule path '$path'"
338 upd
="$(git config -f .gitmodules submodule."$name".update)"
340 git config submodule.
"$name".update
"$upd" ||
341 die
"Failed to register update mode for submodule path '$path'"
343 say
"Submodule '$name' ($url) registered for path '$path'"
348 # Update each submodule path to correct revision, using clone and checkout as needed
350 # $@ = requested paths (default to all)
354 # parse $args after "submodule ... update".
376 case "$2" in '') usage
;; esac
377 reference
="--reference=$2"
407 cmd_init
"--" "$@" ||
return
411 while read mode sha1 stage path
413 name
=$
(module_name
"$path") ||
exit
414 url
=$
(git config submodule.
"$name".url
)
415 update_module
=$
(git config submodule.
"$name".update
)
418 # Only mention uninitialized submodules when its
419 # path have been specified
421 say
"Submodule path '$path' not initialized" &&
422 say
"Maybe you want to use 'update --init'?"
426 if ! test -d "$path"/.git
-o -f "$path"/.git
428 module_clone
"$path" "$url" "$reference"||
exit
431 subsha1
=$
(unset GIT_DIR
; cd "$path" &&
432 git rev-parse
--verify HEAD
) ||
433 die
"Unable to find current revision in submodule path '$path'"
436 if ! test -z "$update"
438 update_module
=$update
441 if test "$subsha1" != "$sha1"
444 if test -z "$subsha1"
449 if test -z "$nofetch"
451 (unset GIT_DIR
; cd "$path" &&
453 die
"Unable to fetch in submodule path '$path'"
456 case "$update_module" in
468 command="git checkout $force -q"
474 (unset GIT_DIR
; cd "$path" && $command "$sha1") ||
475 die
"Unable to $action '$sha1' in submodule path '$path'"
476 say
"Submodule path '$path': $msg '$sha1'"
479 if test -n "$recursive"
481 (unset GIT_DIR
; cd "$path" && cmd_update
$orig_args) ||
482 die
"Failed to recurse into submodule path '$path'"
491 git describe
"$2" 2>/dev
/null ||
492 git describe
--tags "$2" 2>/dev
/null ||
493 git describe
--contains "$2" 2>/dev
/null ||
494 git describe
--all --always "$2"
497 test -z "$revname" || revname
=" ($revname)"
500 # Show commit summary for submodules in index or working tree
502 # If '--cached' is given, show summary between index and given commit,
503 # or between working tree and given commit
505 # $@ = [commit (default 'HEAD'),] requested paths (default all)
511 # parse $args after "submodule ... summary".
522 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
544 test $summary_limit = 0 && return
546 if rev=$
(git rev-parse
-q --verify "$1^0")
555 # Get modified modules cared by user
556 modules
=$
(git diff-index
$cached --raw $head -- "$@" |
557 egrep '^:([0-7]* )?160000' |
558 while read mod_src mod_dst sha1_src sha1_dst status name
560 # Always show modules deleted or type-changed (blob<->module)
561 test $status = D
-o $status = T
&& echo "$name" && continue
562 # Also show added or modified modules which are checked out
563 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
568 test -z "$modules" && return
570 git diff-index
$cached --raw $head -- $modules |
571 egrep '^:([0-7]* )?160000' |
573 while read mod_src mod_dst sha1_src sha1_dst status name
575 if test -z "$cached" &&
576 test $sha1_dst = 0000000000000000000000000000000000000000
580 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
582 100644 |
100755 |
120000)
583 sha1_dst
=$
(git hash-object
$name)
589 echo >&2 "unexpected mode $mod_dst"
596 test $mod_src = 160000 &&
597 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
600 test $mod_dst = 160000 &&
601 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
605 case "$missing_src,$missing_dst" in
607 errmsg
=" Warn: $name doesn't contain commit $sha1_src"
610 errmsg
=" Warn: $name doesn't contain commit $sha1_dst"
613 errmsg
=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
618 if test $mod_src = 160000 -a $mod_dst = 160000
620 range
="$sha1_src...$sha1_dst"
621 elif test $mod_src = 160000
627 GIT_DIR
="$name/.git" \
628 git log
--pretty=oneline
--first-parent $range |
wc -l
630 total_commits
=" ($(($total_commits + 0)))"
634 sha1_abbr_src
=$
(echo $sha1_src | cut
-c1-7)
635 sha1_abbr_dst
=$
(echo $sha1_dst | cut
-c1-7)
638 if test $mod_dst = 160000
640 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
642 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
645 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
649 # Don't give error msg for modification whose dst is not submodule
650 # i.e. deleted or changed to blob
651 test $mod_dst = 160000 && echo "$errmsg"
653 if test $mod_src = 160000 -a $mod_dst = 160000
656 test $summary_limit -gt 0 && limit
="-$summary_limit"
657 GIT_DIR
="$name/.git" \
658 git log
$limit --pretty='format: %m %s' \
659 --first-parent $sha1_src...
$sha1_dst
660 elif test $mod_dst = 160000
662 GIT_DIR
="$name/.git" \
663 git log
--pretty='format: > %s' -1 $sha1_dst
665 GIT_DIR
="$name/.git" \
666 git log
--pretty='format: < %s' -1 $sha1_src
672 if test -n "$for_status"; then
673 echo "# Modified submodules:"
675 sed -e 's|^|# |' -e 's|^# $|#|'
681 # List all submodules, prefixed with:
682 # - submodule not initialized
683 # + different revision checked out
685 # If --cached was specified the revision in the index will be printed
686 # instead of the currently checked out revision.
688 # $@ = requested paths (default to all)
692 # parse $args after "submodule ... status".
717 while read mode sha1 stage path
719 name
=$
(module_name
"$path") ||
exit
720 url
=$
(git config submodule.
"$name".url
)
721 if test -z "$url" ||
! test -d "$path"/.git
-o -f "$path"/.git
726 set_name_rev
"$path" "$sha1"
727 if git diff-files
--quiet -- "$path"
729 say
" $sha1 $path$revname"
733 sha1
=$
(unset GIT_DIR
; cd "$path" && git rev-parse
--verify HEAD
)
734 set_name_rev
"$path" "$sha1"
736 say
"+$sha1 $path$revname"
741 # Sync remote urls for submodules
742 # This makes the value for remote.$remote.url match the value
743 # specified in .gitmodules.
768 while read mode sha1 stage path
770 name
=$
(module_name
"$path")
771 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
773 # Possibly a url relative to parent
776 url
=$
(resolve_relative_url
"$url") ||
exit
780 if test -e "$path"/.git
785 remote
=$
(get_default_remote
)
786 say
"Synchronizing submodule url for '$name'"
787 git config remote.
"$remote".url
"$url"
793 # This loop parses the command line arguments to find the
794 # subcommand name to dispatch. Parsing of the subcommand specific
795 # options are primarily done by the subcommand implementations.
796 # Subcommand specific options such as --branch and --cached are
797 # parsed here as well, for backward compatibility.
799 while test $# != 0 && test -z "$command"
802 add | foreach | init | update | status | summary | sync
)
832 # No command word defaults to "status"
833 test -n "$command" ||
command=status
835 # "-b branch" is accepted only by "add"
836 if test -n "$branch" && test "$command" != add
841 # "--cached" is accepted only by "status" and "summary"
842 if test -n "$cached" && test "$command" != status
-a "$command" != summary