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>...]'
20 # print stuff on stdout unless -q was specified
30 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
34 cd "$1" ||
cd "$1.git" &&
42 # Resolve relative url by appending to parent's url
43 resolve_relative_url
()
45 branch
="$(git symbolic-ref HEAD 2>/dev/null)"
46 remote
="$(git config branch.${branch#refs/heads/}.remote)"
47 remote
="${remote:-origin}"
48 remoteurl
="$(git config remote.$remote.url)" ||
49 die
"remote ($remote) does not have a url in .git/config"
56 remoteurl
="${remoteurl%/*}"
65 echo "$remoteurl/$url"
69 # Map submodule path to submodule name
75 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
76 re
=$
(printf '%s' "$1" |
sed -e 's/\([^a-zA-Z0-9_]\)/\\\1/g')
77 name
=$
( GIT_CONFIG
=.gitmodules \
78 git config
--get-regexp '^submodule\..*\.path$' |
79 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
81 die
"No submodule mapping found in .gitmodules for path '$path'"
88 # Prior to calling, modules_update checks that a possibly existing
89 # path is not a git repository.
90 # Likewise, module_add checks that path does not exist at all,
91 # since it is the location of a new submodule.
98 # If there already is a directory at the submodule path,
99 # expect it to be empty (since that is the default checkout
100 # action) and try to remove it.
101 # Note: if $path is a symlink to a directory the test will
102 # succeed but the rmdir will fail. We might want to fix this.
105 rmdir "$path" 2>/dev
/null ||
106 die
"Directory '$path' exist, but is neither empty nor a git repository"
110 die
"A file already exist at path '$path'"
112 git-clone
-n "$url" "$path" ||
113 die
"Clone of '$url' into submodule path '$path' failed"
117 # Add a new submodule to the working tree, .gitmodules and the index
121 # optional branch is stored in global branch variable
128 if test -z "$repo"; then
134 # dereference source url relative to parent's url
135 realrepo
="$(resolve_relative_url $repo)" ;;
137 # Turn the source into an absolute path if
139 if base
=$
(get_repo_base
"$repo"); then
146 # Guess path from repo if not specified or strip trailing slashes
147 if test -z "$path"; then
148 path
=$
(echo "$repo" |
sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
150 path
=$
(echo "$path" |
sed -e 's|/*$||')
154 die
"'$path' already exists"
156 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
157 die
"'$path' already exists in the index"
159 module_clone
"$path" "$realrepo" ||
exit
160 (unset GIT_DIR
&& cd "$path" && git checkout
-q ${branch:+-b "$branch" "origin/$branch"}) ||
161 die
"Unable to checkout submodule '$path'"
163 die
"Failed to add submodule '$path'"
165 GIT_CONFIG
=.gitmodules git config submodule.
"$path".path
"$path" &&
166 GIT_CONFIG
=.gitmodules git config submodule.
"$path".url
"$repo" &&
167 git add .gitmodules ||
168 die
"Failed to register submodule '$path'"
172 # Register submodules in .git/config
174 # $@ = requested paths (default to all)
178 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
179 while read mode sha1 stage path
181 # Skip already registered paths
182 name
=$
(module_name
"$path") ||
exit
183 url
=$
(git config submodule.
"$name".url
)
184 test -z "$url" ||
continue
186 url
=$
(GIT_CONFIG
=.gitmodules git config submodule.
"$name".url
)
188 die
"No url found for submodule path '$path' in .gitmodules"
190 # Possibly a url relative to parent
193 url
="$(resolve_relative_url "$url")"
197 git config submodule.
"$name".url
"$url" ||
198 die
"Failed to register url for submodule path '$path'"
200 say
"Submodule '$name' ($url) registered for path '$path'"
205 # Update each submodule path to correct revision, using clone and checkout as needed
207 # $@ = requested paths (default to all)
211 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
212 while read mode sha1 stage path
214 name
=$
(module_name
"$path") ||
exit
215 url
=$
(git config submodule.
"$name".url
)
218 # Only mention uninitialized submodules when its
219 # path have been specified
221 say
"Submodule path '$path' not initialized"
225 if ! test -d "$path"/.git
227 module_clone
"$path" "$url" ||
exit
230 subsha1
=$
(unset GIT_DIR
&& cd "$path" &&
231 git rev-parse
--verify HEAD
) ||
232 die
"Unable to find current revision in submodule path '$path'"
235 if test "$subsha1" != "$sha1"
237 (unset GIT_DIR
&& cd "$path" && git-fetch
&&
238 git-checkout
-q "$sha1") ||
239 die
"Unable to checkout '$sha1' in submodule path '$path'"
241 say
"Submodule path '$path': checked out '$sha1'"
250 git describe
"$2" 2>/dev
/null ||
251 git describe
--tags "$2" 2>/dev
/null ||
252 git describe
--contains --tags "$2"
255 test -z "$revname" || revname
=" ($revname)"
259 # List all submodules, prefixed with:
260 # - submodule not initialized
261 # + different revision checked out
263 # If --cached was specified the revision in the index will be printed
264 # instead of the currently checked out revision.
266 # $@ = requested paths (default to all)
270 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
271 while read mode sha1 stage path
273 name
=$
(module_name
"$path") ||
exit
274 url
=$
(git config submodule.
"$name".url
)
275 if test -z "url" ||
! test -d "$path"/.git
280 set_name_rev
"$path" "$sha1"
281 if git diff-files
--quiet -- "$path"
283 say
" $sha1 $path$revname"
287 sha1
=$
(unset GIT_DIR
&& cd "$path" && git rev-parse
--verify HEAD
)
288 set_name_rev
"$path" "$sha1"
290 say
"+$sha1 $path$revname"
337 case "$add,$branch" in
347 case "$add,$init,$update,$status,$cached" in