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" &&
43 # Map submodule path to submodule name
49 name
=$
(GIT_CONFIG
=.gitmodules git-config
--get-regexp '^submodule\..*\.path$' "$1" |
50 sed -nre 's/^submodule\.(.+)\.path .+$/\1/p')
52 die
"No submodule mapping found in .gitmodules for path '$path'"
59 # Prior to calling, modules_update checks that a possibly existing
60 # path is not a git repository.
61 # Likewise, module_add checks that path does not exist at all,
62 # since it is the location of a new submodule.
69 # If there already is a directory at the submodule path,
70 # expect it to be empty (since that is the default checkout
71 # action) and try to remove it.
72 # Note: if $path is a symlink to a directory the test will
73 # succeed but the rmdir will fail. We might want to fix this.
76 rmdir "$path" 2>/dev
/null ||
77 die
"Directory '$path' exist, but is neither empty nor a git repository"
81 die
"A file already exist at path '$path'"
83 git-clone
-n "$url" "$path" ||
84 die
"Clone of '$url' into submodule path '$path' failed"
88 # Add a new submodule to the working tree, .gitmodules and the index
92 # optional branch is stored in global branch variable
99 if test -z "$repo"; then
103 # Turn the source into an absolute path if
105 if base
=$
(get_repo_base
"$repo"); then
109 # Guess path from repo if not specified or strip trailing slashes
110 if test -z "$path"; then
111 path
=$
(echo "$repo" |
sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
113 path
=$
(echo "$path" |
sed -e 's|/*$||')
117 die
"'$path' already exists"
119 git-ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
120 die
"'$path' already exists in the index"
122 module_clone
"$path" "$repo" ||
exit
123 (unset GIT_DIR
&& cd "$path" && git checkout
-q ${branch:+-b "$branch" "origin/$branch"}) ||
124 die
"Unable to checkout submodule '$path'"
126 die
"Failed to add submodule '$path'"
128 GIT_CONFIG
=.gitmodules git config submodule.
"$path".path
"$path" &&
129 GIT_CONFIG
=.gitmodules git config submodule.
"$path".url
"$repo" &&
130 git add .gitmodules ||
131 die
"Failed to register submodule '$path'"
135 # Register submodules in .git/config
137 # $@ = requested paths (default to all)
141 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
142 while read mode sha1 stage path
144 # Skip already registered paths
145 name
=$
(module_name
"$path") ||
exit
146 url
=$
(git-config submodule.
"$name".url
)
147 test -z "$url" ||
continue
149 url
=$
(GIT_CONFIG
=.gitmodules git-config submodule.
"$name".url
)
151 die
"No url found for submodule path '$path' in .gitmodules"
153 git-config submodule.
"$name".url
"$url" ||
154 die
"Failed to register url for submodule path '$path'"
156 say
"Submodule '$name' ($url) registered for path '$path'"
161 # Update each submodule path to correct revision, using clone and checkout as needed
163 # $@ = requested paths (default to all)
167 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
168 while read mode sha1 stage path
170 name
=$
(module_name
"$path") ||
exit
171 url
=$
(git-config submodule.
"$name".url
)
174 # Only mention uninitialized submodules when its
175 # path have been specified
177 say
"Submodule path '$path' not initialized"
181 if ! test -d "$path"/.git
183 module_clone
"$path" "$url" ||
exit
186 subsha1
=$
(unset GIT_DIR
&& cd "$path" &&
187 git-rev-parse
--verify HEAD
) ||
188 die
"Unable to find current revision in submodule path '$path'"
191 if test "$subsha1" != "$sha1"
193 (unset GIT_DIR
&& cd "$path" && git-fetch
&&
194 git-checkout
-q "$sha1") ||
195 die
"Unable to checkout '$sha1' in submodule path '$path'"
197 say
"Submodule path '$path': checked out '$sha1'"
206 git-describe
"$2" 2>/dev
/null ||
207 git-describe
--tags "$2" 2>/dev
/null ||
208 git-describe
--contains --tags "$2"
211 test -z "$revname" || revname
=" ($revname)"
215 # List all submodules, prefixed with:
216 # - submodule not initialized
217 # + different revision checked out
219 # If --cached was specified the revision in the index will be printed
220 # instead of the currently checked out revision.
222 # $@ = requested paths (default to all)
226 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
227 while read mode sha1 stage path
229 name
=$
(module_name
"$path") ||
exit
230 url
=$
(git-config submodule.
"$name".url
)
231 if test -z "url" ||
! test -d "$path"/.git
236 revname
=$
(unset GIT_DIR
&& cd "$path" && git-describe
--tags $sha1)
237 set_name_rev
"$path" $
"sha1"
238 if git diff-files
--quiet -- "$path"
240 say
" $sha1 $path$revname"
244 sha1
=$
(unset GIT_DIR
&& cd "$path" && git-rev-parse
--verify HEAD
)
245 set_name_rev
"$path" $
"sha1"
247 say
"+$sha1 $path$revname"
252 while case "$#" in 0) break ;; esac
294 case "$add,$branch" in
304 case "$add,$init,$update,$status,$cached" in