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>...]'
21 # print stuff on stdout unless -q was specified
31 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
35 cd "$1" ||
cd "$1.git" &&
43 # Resolve relative url by appending to parent's url
44 resolve_relative_url
()
46 branch
="$(git symbolic-ref HEAD 2>/dev/null)"
47 remote
="$(git config branch.${branch#refs/heads/}.remote)"
48 remote
="${remote:-origin}"
49 remoteurl
="$(git config remote.$remote.url)" ||
50 die
"remote ($remote) does not have a url in .git/config"
57 remoteurl
="${remoteurl%/*}"
66 echo "$remoteurl/$url"
70 # Map submodule path to submodule name
76 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
77 re
=$
(printf '%s' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
78 name
=$
( GIT_CONFIG
=.gitmodules \
79 git config
--get-regexp '^submodule\..*\.path$' |
80 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
82 die
"No submodule mapping found in .gitmodules for path '$path'"
89 # Prior to calling, modules_update checks that a possibly existing
90 # path is not a git repository.
91 # Likewise, module_add checks that path does not exist at all,
92 # since it is the location of a new submodule.
99 # If there already is a directory at the submodule path,
100 # expect it to be empty (since that is the default checkout
101 # action) and try to remove it.
102 # Note: if $path is a symlink to a directory the test will
103 # succeed but the rmdir will fail. We might want to fix this.
106 rmdir "$path" 2>/dev
/null ||
107 die
"Directory '$path' exist, but is neither empty nor a git repository"
111 die
"A file already exist at path '$path'"
113 git-clone
-n "$url" "$path" ||
114 die
"Clone of '$url' into submodule path '$path' failed"
118 # Add a new submodule to the working tree, .gitmodules and the index
122 # optional branch is stored in global branch variable
129 if test -z "$repo"; then
135 # dereference source url relative to parent's url
136 realrepo
="$(resolve_relative_url $repo)" ;;
138 # Turn the source into an absolute path if
140 if base
=$
(get_repo_base
"$repo"); then
147 # Guess path from repo if not specified or strip trailing slashes
148 if test -z "$path"; then
149 path
=$
(echo "$repo" |
sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
151 path
=$
(echo "$path" |
sed -e 's|/*$||')
155 die
"'$path' already exists"
157 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
158 die
"'$path' already exists in the index"
160 module_clone
"$path" "$realrepo" ||
exit
161 (unset GIT_DIR
&& cd "$path" && git checkout
-q ${branch:+-b "$branch" "origin/$branch"}) ||
162 die
"Unable to checkout submodule '$path'"
164 die
"Failed to add submodule '$path'"
166 GIT_CONFIG
=.gitmodules git config submodule.
"$path".path
"$path" &&
167 GIT_CONFIG
=.gitmodules git config submodule.
"$path".url
"$repo" &&
168 git add .gitmodules ||
169 die
"Failed to register submodule '$path'"
173 # Register submodules in .git/config
175 # $@ = requested paths (default to all)
179 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
180 while read mode sha1 stage path
182 # Skip already registered paths
183 name
=$
(module_name
"$path") ||
exit
184 url
=$
(git config submodule.
"$name".url
)
185 test -z "$url" ||
continue
187 url
=$
(GIT_CONFIG
=.gitmodules git config submodule.
"$name".url
)
189 die
"No url found for submodule path '$path' in .gitmodules"
191 # Possibly a url relative to parent
194 url
="$(resolve_relative_url "$url")"
198 git config submodule.
"$name".url
"$url" ||
199 die
"Failed to register url for submodule path '$path'"
201 say
"Submodule '$name' ($url) registered for path '$path'"
206 # Update each submodule path to correct revision, using clone and checkout as needed
208 # $@ = requested paths (default to all)
212 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
213 while read mode sha1 stage path
215 name
=$
(module_name
"$path") ||
exit
216 url
=$
(git config submodule.
"$name".url
)
219 # Only mention uninitialized submodules when its
220 # path have been specified
222 say
"Submodule path '$path' not initialized"
226 if ! test -d "$path"/.git
228 module_clone
"$path" "$url" ||
exit
231 subsha1
=$
(unset GIT_DIR
&& cd "$path" &&
232 git rev-parse
--verify HEAD
) ||
233 die
"Unable to find current revision in submodule path '$path'"
236 if test "$subsha1" != "$sha1"
238 (unset GIT_DIR
&& cd "$path" && git-fetch
&&
239 git-checkout
-q "$sha1") ||
240 die
"Unable to checkout '$sha1' in submodule path '$path'"
242 say
"Submodule path '$path': checked out '$sha1'"
251 git describe
"$2" 2>/dev
/null ||
252 git describe
--tags "$2" 2>/dev
/null ||
253 git describe
--contains --tags "$2"
256 test -z "$revname" || revname
=" ($revname)"
260 # List all submodules, prefixed with:
261 # - submodule not initialized
262 # + different revision checked out
264 # If --cached was specified the revision in the index will be printed
265 # instead of the currently checked out revision.
267 # $@ = requested paths (default to all)
271 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
272 while read mode sha1 stage path
274 name
=$
(module_name
"$path") ||
exit
275 url
=$
(git config submodule.
"$name".url
)
276 if test -z "url" ||
! test -d "$path"/.git
281 set_name_rev
"$path" "$sha1"
282 if git diff-files
--quiet -- "$path"
284 say
" $sha1 $path$revname"
288 sha1
=$
(unset GIT_DIR
&& cd "$path" && git rev-parse
--verify HEAD
)
289 set_name_rev
"$path" "$sha1"
291 say
"+$sha1 $path$revname"
338 case "$add,$branch" in
348 case "$add,$init,$update,$status,$cached" in