3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 USAGE
="[--quiet] [--cached] \
8 [add [-b branch] <repo> <path>]|[status|init|update [-i|--init] [-N|--no-fetch]|summary [-n|--summary-limit <n>] [<commit>]] \
9 [--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
22 # print stuff on stdout unless -q was specified
32 # Resolve relative url by appending to parent's url
33 resolve_relative_url
()
35 remote
=$
(get_default_remote
)
36 remoteurl
=$
(git config
"remote.$remote.url") ||
37 die
"remote ($remote) does not have a url defined in .git/config"
39 remoteurl
=${remoteurl%/}
45 remoteurl
="${remoteurl%/*}"
54 echo "$remoteurl/${url%/}"
58 # Get submodule info for registered submodules
59 # $@ = path to limit submodule list
63 git ls-files
--error-unmatch --stage -- "$@" |
grep '^160000 '
67 # Map submodule path to submodule name
73 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
74 re
=$
(printf '%s\n' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
75 name
=$
( git config
-f .gitmodules
--get-regexp '^submodule\..*\.path$' |
76 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
78 die
"No submodule mapping found in .gitmodules for path '$path'"
85 # Prior to calling, cmd_update checks that a possibly existing
86 # path is not a git repository.
87 # Likewise, cmd_add checks that path does not exist at all,
88 # since it is the location of a new submodule.
95 # If there already is a directory at the submodule path,
96 # expect it to be empty (since that is the default checkout
97 # action) and try to remove it.
98 # Note: if $path is a symlink to a directory the test will
99 # succeed but the rmdir will fail. We might want to fix this.
102 rmdir "$path" 2>/dev
/null ||
103 die
"Directory '$path' exist, but is neither empty nor a git repository"
107 die
"A file already exist at path '$path'"
109 git-clone
-n "$url" "$path" ||
110 die
"Clone of '$url' into submodule path '$path' failed"
114 # Add a new submodule to the working tree, .gitmodules and the index
118 # optional branch is stored in global branch variable
122 # parse $args after "submodule ... add".
127 case "$2" in '') usage
;; esac
151 if test -z "$repo" -o -z "$path"; then
155 # assure repo is absolute or relative to parent
158 # dereference source url relative to parent's url
159 realrepo
=$
(resolve_relative_url
"$repo") ||
exit
166 die
"repo URL: '$repo' must be absolute or begin with ./|../"
171 # multiple //; leading ./; /./; /../; trailing /
172 path
=$
(printf '%s/\n' "$path" |
182 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
183 die
"'$path' already exists in the index"
185 # perhaps the path exists and is already a git repo, else clone it
188 if test -d "$path"/.git
-o -f "$path"/.git
190 echo "Adding existing repo at '$path' to the index"
192 die
"'$path' already exists and is not a valid git repo"
197 url
=$
(resolve_relative_url
"$repo") ||
exit
203 git config submodule.
"$path".url
"$url"
206 module_clone
"$path" "$realrepo" ||
exit
210 # ash fails to wordsplit ${branch:+-b "$branch"...}
212 '') git checkout
-f -q ;;
213 ?
*) git checkout
-f -q -b "$branch" "origin/$branch" ;;
215 ) || die
"Unable to checkout submodule '$path'"
219 die
"Failed to add submodule '$path'"
221 git config
-f .gitmodules submodule.
"$path".path
"$path" &&
222 git config
-f .gitmodules submodule.
"$path".url
"$repo" &&
223 git add .gitmodules ||
224 die
"Failed to register submodule '$path'"
228 # Execute an arbitrary command sequence in each checked out
231 # $@ = command to execute
236 while read mode sha1 stage path
238 if test -e "$path"/.git
240 say
"Entering '$path'"
241 (cd "$path" && eval "$@") ||
242 die
"Stopping at '$path'; script returned non-zero status."
248 # Register submodules in .git/config
250 # $@ = requested paths (default to all)
254 # parse $args after "submodule ... init".
276 while read mode sha1 stage path
278 # Skip already registered paths
279 name
=$
(module_name
"$path") ||
exit
280 url
=$
(git config submodule.
"$name".url
)
281 test -z "$url" ||
continue
283 url
=$
(git config
-f .gitmodules submodule.
"$name".url
)
285 die
"No url found for submodule path '$path' in .gitmodules"
287 # Possibly a url relative to parent
290 url
=$
(resolve_relative_url
"$url") ||
exit
294 git config submodule.
"$name".url
"$url" ||
295 die
"Failed to register url for submodule path '$path'"
297 say
"Submodule '$name' ($url) registered for path '$path'"
302 # Update each submodule path to correct revision, using clone and checkout as needed
304 # $@ = requested paths (default to all)
308 # parse $args after "submodule ... update".
318 cmd_init
"$@" ||
return
338 while read mode sha1 stage path
340 name
=$
(module_name
"$path") ||
exit
341 url
=$
(git config submodule.
"$name".url
)
344 # Only mention uninitialized submodules when its
345 # path have been specified
347 say
"Submodule path '$path' not initialized" &&
348 say
"Maybe you want to use 'update --init'?"
352 if ! test -d "$path"/.git
-o -f "$path"/.git
354 module_clone
"$path" "$url" ||
exit
357 subsha1
=$
(unset GIT_DIR
; cd "$path" &&
358 git rev-parse
--verify HEAD
) ||
359 die
"Unable to find current revision in submodule path '$path'"
362 if test "$subsha1" != "$sha1"
365 if test -z "$subsha1"
370 if test -z "$nofetch"
372 (unset GIT_DIR
; cd "$path" &&
374 die
"Unable to fetch in submodule path '$path'"
377 (unset GIT_DIR
; cd "$path" &&
378 git-checkout
$force -q "$sha1") ||
379 die
"Unable to checkout '$sha1' in submodule path '$path'"
381 say
"Submodule path '$path': checked out '$sha1'"
390 git describe
"$2" 2>/dev
/null ||
391 git describe
--tags "$2" 2>/dev
/null ||
392 git describe
--contains "$2" 2>/dev
/null ||
393 git describe
--all --always "$2"
396 test -z "$revname" || revname
=" ($revname)"
399 # Show commit summary for submodules in index or working tree
401 # If '--cached' is given, show summary between index and given commit,
402 # or between working tree and given commit
404 # $@ = [commit (default 'HEAD'),] requested paths (default all)
410 # parse $args after "submodule ... summary".
421 if summary_limit
=$
(($2 + 0)) 2>/dev
/null
&& test "$summary_limit" = "$2"
443 test $summary_limit = 0 && return
445 if rev=$
(git rev-parse
-q --verify "$1^0")
454 # Get modified modules cared by user
455 modules
=$
(git diff-index
$cached --raw $head -- "$@" |
456 egrep '^:([0-7]* )?160000' |
457 while read mod_src mod_dst sha1_src sha1_dst status name
459 # Always show modules deleted or type-changed (blob<->module)
460 test $status = D
-o $status = T
&& echo "$name" && continue
461 # Also show added or modified modules which are checked out
462 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
467 test -z "$modules" && return
469 git diff-index
$cached --raw $head -- $modules |
470 egrep '^:([0-7]* )?160000' |
472 while read mod_src mod_dst sha1_src sha1_dst status name
474 if test -z "$cached" &&
475 test $sha1_dst = 0000000000000000000000000000000000000000
479 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
481 100644 |
100755 |
120000)
482 sha1_dst
=$
(git hash-object
$name)
488 echo >&2 "unexpected mode $mod_dst"
495 test $mod_src = 160000 &&
496 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_src^
0 >/dev
/null
&&
499 test $mod_dst = 160000 &&
500 ! GIT_DIR
="$name/.git" git-rev-parse
-q --verify $sha1_dst^
0 >/dev
/null
&&
504 case "$missing_src,$missing_dst" in
506 errmsg
=" Warn: $name doesn't contain commit $sha1_src"
509 errmsg
=" Warn: $name doesn't contain commit $sha1_dst"
512 errmsg
=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
517 if test $mod_src = 160000 -a $mod_dst = 160000
519 range
="$sha1_src...$sha1_dst"
520 elif test $mod_src = 160000
526 GIT_DIR
="$name/.git" \
527 git log
--pretty=oneline
--first-parent $range |
wc -l
529 total_commits
=" ($(($total_commits + 0)))"
533 sha1_abbr_src
=$
(echo $sha1_src | cut
-c1-7)
534 sha1_abbr_dst
=$
(echo $sha1_dst | cut
-c1-7)
537 if test $mod_dst = 160000
539 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
541 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
544 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
548 # Don't give error msg for modification whose dst is not submodule
549 # i.e. deleted or changed to blob
550 test $mod_dst = 160000 && echo "$errmsg"
552 if test $mod_src = 160000 -a $mod_dst = 160000
555 test $summary_limit -gt 0 && limit
="-$summary_limit"
556 GIT_DIR
="$name/.git" \
557 git log
$limit --pretty='format: %m %s' \
558 --first-parent $sha1_src...
$sha1_dst
559 elif test $mod_dst = 160000
561 GIT_DIR
="$name/.git" \
562 git log
--pretty='format: > %s' -1 $sha1_dst
564 GIT_DIR
="$name/.git" \
565 git log
--pretty='format: < %s' -1 $sha1_src
571 if test -n "$for_status"; then
572 echo "# Modified submodules:"
574 sed -e 's|^|# |' -e 's|^# $|#|'
580 # List all submodules, prefixed with:
581 # - submodule not initialized
582 # + different revision checked out
584 # If --cached was specified the revision in the index will be printed
585 # instead of the currently checked out revision.
587 # $@ = requested paths (default to all)
591 # parse $args after "submodule ... status".
616 while read mode sha1 stage path
618 name
=$
(module_name
"$path") ||
exit
619 url
=$
(git config submodule.
"$name".url
)
620 if test -z "$url" ||
! test -d "$path"/.git
-o -f "$path"/.git
625 set_name_rev
"$path" "$sha1"
626 if git diff-files
--quiet -- "$path"
628 say
" $sha1 $path$revname"
632 sha1
=$
(unset GIT_DIR
; cd "$path" && git rev-parse
--verify HEAD
)
633 set_name_rev
"$path" "$sha1"
635 say
"+$sha1 $path$revname"
640 # Sync remote urls for submodules
641 # This makes the value for remote.$remote.url match the value
642 # specified in .gitmodules.
667 while read mode sha1 stage path
669 name
=$
(module_name
"$path")
670 url
=$
(git config
-f .gitmodules
--get submodule.
"$name".url
)
672 # Possibly a url relative to parent
675 url
=$
(resolve_relative_url
"$url") ||
exit
679 if test -e "$path"/.git
684 remote
=$
(get_default_remote
)
685 say
"Synchronizing submodule url for '$name'"
686 git config remote.
"$remote".url
"$url"
692 # This loop parses the command line arguments to find the
693 # subcommand name to dispatch. Parsing of the subcommand specific
694 # options are primarily done by the subcommand implementations.
695 # Subcommand specific options such as --branch and --cached are
696 # parsed here as well, for backward compatibility.
698 while test $# != 0 && test -z "$command"
701 add | foreach | init | update | status | summary | sync
)
731 # No command word defaults to "status"
732 test -n "$command" ||
command=status
734 # "-b branch" is accepted only by "add"
735 if test -n "$branch" && test "$command" != add
740 # "--cached" is accepted only by "status" and "summary"
741 if test -n "$cached" && test "$command" != status
-a "$command" != summary