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] [--] [<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
156 # Guess path from repo if not specified or strip trailing slashes
157 if test -z "$path"; then
158 path
=$
(echo "$repo" |
sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
160 path
=$
(echo "$path" |
sed -e 's|/*$||')
163 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
164 die
"'$path' already exists in the index"
166 # perhaps the path exists and is already a git repo, else clone it
169 if test -d "$path/.git" &&
170 test "$(unset GIT_DIR; cd $path; git rev-parse --git-dir)" = ".git"
172 echo "Adding existing repo at '$path' to the index"
174 die
"'$path' already exists and is not a valid git repo"
179 # dereference source url relative to parent's url
180 realrepo
="$(resolve_relative_url $repo)" ;;
182 # Turn the source into an absolute path if
184 if base
=$
(get_repo_base
"$repo"); then
191 module_clone
"$path" "$realrepo" ||
exit
192 (unset GIT_DIR
; cd "$path" && git checkout
-q ${branch:+-b "$branch" "origin/$branch"}) ||
193 die
"Unable to checkout submodule '$path'"
197 die
"Failed to add submodule '$path'"
199 GIT_CONFIG
=.gitmodules git config submodule.
"$path".path
"$path" &&
200 GIT_CONFIG
=.gitmodules git config submodule.
"$path".url
"$repo" &&
201 git add .gitmodules ||
202 die
"Failed to register submodule '$path'"
206 # Register submodules in .git/config
208 # $@ = requested paths (default to all)
212 # parse $args after "submodule ... init".
233 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
234 while read mode sha1 stage path
236 # Skip already registered paths
237 name
=$
(module_name
"$path") ||
exit
238 url
=$
(git config submodule.
"$name".url
)
239 test -z "$url" ||
continue
241 url
=$
(GIT_CONFIG
=.gitmodules git config submodule.
"$name".url
)
243 die
"No url found for submodule path '$path' in .gitmodules"
245 # Possibly a url relative to parent
248 url
="$(resolve_relative_url "$url")"
252 git config submodule.
"$name".url
"$url" ||
253 die
"Failed to register url for submodule path '$path'"
255 say
"Submodule '$name' ($url) registered for path '$path'"
260 # Update each submodule path to correct revision, using clone and checkout as needed
262 # $@ = requested paths (default to all)
266 # parse $args after "submodule ... update".
287 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
288 while read mode sha1 stage path
290 name
=$
(module_name
"$path") ||
exit
291 url
=$
(git config submodule.
"$name".url
)
294 # Only mention uninitialized submodules when its
295 # path have been specified
297 say
"Submodule path '$path' not initialized"
301 if ! test -d "$path"/.git
303 module_clone
"$path" "$url" ||
exit
306 subsha1
=$
(unset GIT_DIR
; cd "$path" &&
307 git rev-parse
--verify HEAD
) ||
308 die
"Unable to find current revision in submodule path '$path'"
311 if test "$subsha1" != "$sha1"
313 (unset GIT_DIR
; cd "$path" && git-fetch
&&
314 git-checkout
-q "$sha1") ||
315 die
"Unable to checkout '$sha1' in submodule path '$path'"
317 say
"Submodule path '$path': checked out '$sha1'"
326 git describe
"$2" 2>/dev
/null ||
327 git describe
--tags "$2" 2>/dev
/null ||
328 git describe
--contains --tags "$2"
331 test -z "$revname" || revname
=" ($revname)"
335 # List all submodules, prefixed with:
336 # - submodule not initialized
337 # + different revision checked out
339 # If --cached was specified the revision in the index will be printed
340 # instead of the currently checked out revision.
342 # $@ = requested paths (default to all)
346 # parse $args after "submodule ... status".
370 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
371 while read mode sha1 stage path
373 name
=$
(module_name
"$path") ||
exit
374 url
=$
(git config submodule.
"$name".url
)
375 if test -z "$url" ||
! test -d "$path"/.git
380 set_name_rev
"$path" "$sha1"
381 if git diff-files
--quiet -- "$path"
383 say
" $sha1 $path$revname"
387 sha1
=$
(unset GIT_DIR
; cd "$path" && git rev-parse
--verify HEAD
)
388 set_name_rev
"$path" "$sha1"
390 say
"+$sha1 $path$revname"
395 # This loop parses the command line arguments to find the
396 # subcommand name to dispatch. Parsing of the subcommand specific
397 # options are primarily done by the subcommand implementations.
398 # Subcommand specific options such as --branch and --cached are
399 # parsed here as well, for backward compatibility.
401 while test $# != 0 && test -z "$command"
404 add | init | update | status
)
434 # No command word defaults to "status"
435 test -n "$command" ||
command=status
437 # "-b branch" is accepted only by "add"
438 if test -n "$branch" && test "$command" != add
443 # "--cached" is accepted only by "status"
444 if test -n "$cached" && test "$command" != status