3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 USAGE
='[--quiet] [--cached] [add <repo> [-b branch]|status|init|update|summary [<commit>]] [--] [<path>...]'
18 # print stuff on stdout unless -q was specified
28 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
32 cd "$1" ||
cd "$1.git" &&
40 # Resolve relative url by appending to parent's url
41 resolve_relative_url
()
43 branch
="$(git symbolic-ref HEAD 2>/dev/null)"
44 remote
="$(git config branch.${branch#refs/heads/}.remote)"
45 remote
="${remote:-origin}"
46 remoteurl
="$(git config remote.$remote.url)" ||
47 die
"remote ($remote) does not have a url in .git/config"
54 remoteurl
="${remoteurl%/*}"
63 echo "$remoteurl/$url"
67 # Map submodule path to submodule name
73 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
74 re
=$
(printf '%s' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
75 name
=$
( GIT_CONFIG
=.gitmodules \
76 git config
--get-regexp '^submodule\..*\.path$' |
77 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
79 die
"No submodule mapping found in .gitmodules for path '$path'"
86 # Prior to calling, cmd_update checks that a possibly existing
87 # path is not a git repository.
88 # Likewise, cmd_add checks that path does not exist at all,
89 # since it is the location of a new submodule.
96 # If there already is a directory at the submodule path,
97 # expect it to be empty (since that is the default checkout
98 # action) and try to remove it.
99 # Note: if $path is a symlink to a directory the test will
100 # succeed but the rmdir will fail. We might want to fix this.
103 rmdir "$path" 2>/dev
/null ||
104 die
"Directory '$path' exist, but is neither empty nor a git repository"
108 die
"A file already exist at path '$path'"
110 git-clone
-n "$url" "$path" ||
111 die
"Clone of '$url' into submodule path '$path' failed"
115 # Add a new submodule to the working tree, .gitmodules and the index
119 # optional branch is stored in global branch variable
123 # parse $args after "submodule ... add".
128 case "$2" in '') usage
;; esac
152 if test -z "$repo"; then
158 # dereference source url relative to parent's url
159 realrepo
="$(resolve_relative_url $repo)" ;;
161 # Turn the source into an absolute path if
163 if base
=$
(get_repo_base
"$repo"); then
170 # Guess path from repo if not specified or strip trailing slashes
171 if test -z "$path"; then
172 path
=$
(echo "$repo" |
sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
174 path
=$
(echo "$path" |
sed -e 's|/*$||')
178 die
"'$path' already exists"
180 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
181 die
"'$path' already exists in the index"
183 module_clone
"$path" "$realrepo" ||
exit
184 (unset GIT_DIR
; cd "$path" && git checkout
-q ${branch:+-b "$branch" "origin/$branch"}) ||
185 die
"Unable to checkout submodule '$path'"
187 die
"Failed to add submodule '$path'"
189 GIT_CONFIG
=.gitmodules git config submodule.
"$path".path
"$path" &&
190 GIT_CONFIG
=.gitmodules git config submodule.
"$path".url
"$repo" &&
191 git add .gitmodules ||
192 die
"Failed to register submodule '$path'"
196 # Register submodules in .git/config
198 # $@ = requested paths (default to all)
202 # parse $args after "submodule ... init".
223 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
224 while read mode sha1 stage path
226 # Skip already registered paths
227 name
=$
(module_name
"$path") ||
exit
228 url
=$
(git config submodule.
"$name".url
)
229 test -z "$url" ||
continue
231 url
=$
(GIT_CONFIG
=.gitmodules git config submodule.
"$name".url
)
233 die
"No url found for submodule path '$path' in .gitmodules"
235 # Possibly a url relative to parent
238 url
="$(resolve_relative_url "$url")"
242 git config submodule.
"$name".url
"$url" ||
243 die
"Failed to register url for submodule path '$path'"
245 say
"Submodule '$name' ($url) registered for path '$path'"
250 # Update each submodule path to correct revision, using clone and checkout as needed
252 # $@ = requested paths (default to all)
256 # parse $args after "submodule ... update".
277 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
278 while read mode sha1 stage path
280 name
=$
(module_name
"$path") ||
exit
281 url
=$
(git config submodule.
"$name".url
)
284 # Only mention uninitialized submodules when its
285 # path have been specified
287 say
"Submodule path '$path' not initialized"
291 if ! test -d "$path"/.git
293 module_clone
"$path" "$url" ||
exit
296 subsha1
=$
(unset GIT_DIR
; cd "$path" &&
297 git rev-parse
--verify HEAD
) ||
298 die
"Unable to find current revision in submodule path '$path'"
301 if test "$subsha1" != "$sha1"
303 (unset GIT_DIR
; cd "$path" && git-fetch
&&
304 git-checkout
-q "$sha1") ||
305 die
"Unable to checkout '$sha1' in submodule path '$path'"
307 say
"Submodule path '$path': checked out '$sha1'"
316 git describe
"$2" 2>/dev
/null ||
317 git describe
--tags "$2" 2>/dev
/null ||
318 git describe
--contains --tags "$2"
321 test -z "$revname" || revname
=" ($revname)"
324 # Show commit summary for submodules in index or working tree
326 # If '--cached' is given, show summary between index and given commit,
327 # or between working tree and given commit
329 # $@ = [commit (default 'HEAD'),] requested paths (default all)
332 # parse $args after "submodule ... summary".
353 if rev=$
(git rev-parse
--verify "$1^0" 2>/dev
/null
)
362 # Get modified modules cared by user
363 modules
=$
(git diff-index
$cached --raw $head -- "$@" |
364 grep -e '^:160000' -e '^:[0-7]* 160000' |
365 while read mod_src mod_dst sha1_src sha1_dst status name
367 # Always show modules deleted or type-changed (blob<->module)
368 test $status = D
-o $status = T
&& echo "$name" && continue
369 # Also show added or modified modules which are checked out
370 GIT_DIR
="$name/.git" git-rev-parse
--git-dir >/dev
/null
2>&1 &&
375 test -n "$modules" &&
376 git diff-index
$cached --raw $head -- $modules |
377 grep -e '^:160000' -e '^:[0-7]* 160000' |
379 while read mod_src mod_dst sha1_src sha1_dst status name
381 if test -z "$cached" &&
382 test $sha1_dst = 0000000000000000000000000000000000000000
386 sha1_dst
=$
(GIT_DIR
="$name/.git" git rev-parse HEAD
)
388 100644 |
100755 |
120000)
389 sha1_dst
=$
(git hash-object
$name)
395 echo >&2 "unexpected mode $mod_dst"
402 test $mod_src = 160000 &&
403 ! GIT_DIR
="$name/.git" git-rev-parse
--verify $sha1_src^
0 >/dev
/null
2>&1 &&
406 test $mod_dst = 160000 &&
407 ! GIT_DIR
="$name/.git" git-rev-parse
--verify $sha1_dst^
0 >/dev
/null
2>&1 &&
411 case "$missing_src,$missing_dst" in
413 errmsg
=" Warn: $name doesn't contain commit $sha1_src"
416 errmsg
=" Warn: $name doesn't contain commit $sha1_dst"
419 errmsg
=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
424 if test $mod_src = 160000 -a $mod_dst = 160000
426 range
="$sha1_src...$sha1_dst"
427 elif test $mod_src = 160000
433 GIT_DIR
="$name/.git" \
434 git log
--pretty=oneline
--first-parent $range |
wc -l
436 total_commits
=" ($total_commits)"
440 sha1_abbr_src
=$
(echo $sha1_src | cut
-c1-7)
441 sha1_abbr_dst
=$
(echo $sha1_dst | cut
-c1-7)
444 if test $mod_dst = 160000
446 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
448 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
451 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
455 # Don't give error msg for modification whose dst is not submodule
456 # i.e. deleted or changed to blob
457 test $mod_dst = 160000 && echo "$errmsg"
459 if test $mod_src = 160000 -a $mod_dst = 160000
461 GIT_DIR
="$name/.git" \
462 git log
--pretty='format: %m %s' \
463 --first-parent $sha1_src...
$sha1_dst
464 elif test $mod_dst = 160000
466 GIT_DIR
="$name/.git" \
467 git log
--pretty='format: > %s' -1 $sha1_dst
469 GIT_DIR
="$name/.git" \
470 git log
--pretty='format: < %s' -1 $sha1_src
478 # List all submodules, prefixed with:
479 # - submodule not initialized
480 # + different revision checked out
482 # If --cached was specified the revision in the index will be printed
483 # instead of the currently checked out revision.
485 # $@ = requested paths (default to all)
489 # parse $args after "submodule ... status".
513 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
514 while read mode sha1 stage path
516 name
=$
(module_name
"$path") ||
exit
517 url
=$
(git config submodule.
"$name".url
)
518 if test -z "$url" ||
! test -d "$path"/.git
523 set_name_rev
"$path" "$sha1"
524 if git diff-files
--quiet -- "$path"
526 say
" $sha1 $path$revname"
530 sha1
=$
(unset GIT_DIR
; cd "$path" && git rev-parse
--verify HEAD
)
531 set_name_rev
"$path" "$sha1"
533 say
"+$sha1 $path$revname"
538 # This loop parses the command line arguments to find the
539 # subcommand name to dispatch. Parsing of the subcommand specific
540 # options are primarily done by the subcommand implementations.
541 # Subcommand specific options such as --branch and --cached are
542 # parsed here as well, for backward compatibility.
544 while test $# != 0 && test -z "$command"
547 add | init | update | status | summary
)
577 # No command word defaults to "status"
578 test -n "$command" ||
command=status
580 # "-b branch" is accepted only by "add"
581 if test -n "$branch" && test "$command" != add
586 # "--cached" is accepted only by "status" and "summary"
587 if test -n "$cached" && test "$command" != status
-a "$command" != summary