3 # git-submodules.sh: add, init, update or list git submodules
4 # or recurse any git command over the submodules recursively.
6 # Copyright (c) 2007 Lars Hjemli
8 USAGE
='[[--quiet] [--cached] [add <repo> [-b branch]|status|init|update] [--] [<path>...]|[recurse [-v] command arguments ...]]'
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" &&
44 # Map submodule path to submodule name
50 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
51 re
=$
(printf '%s' "$1" |
sed -e 's/[].[^$\\*]/\\&/g')
52 name
=$
( GIT_CONFIG
=.gitmodules \
53 git config
--get-regexp '^submodule\..*\.path$' |
54 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
56 die
"No submodule mapping found in .gitmodules for path '$path'"
63 # Prior to calling, modules_update checks that a possibly existing
64 # path is not a git repository.
65 # Likewise, module_add checks that path does not exist at all,
66 # since it is the location of a new submodule.
73 # If there already is a directory at the submodule path,
74 # expect it to be empty (since that is the default checkout
75 # action) and try to remove it.
76 # Note: if $path is a symlink to a directory the test will
77 # succeed but the rmdir will fail. We might want to fix this.
80 rmdir "$path" 2>/dev
/null ||
81 die
"Directory '$path' exist, but is neither empty nor a git repository"
85 die
"A file already exist at path '$path'"
87 git-clone
-n "$url" "$path" ||
88 die
"Clone of '$url' into submodule path '$path' failed"
92 # Add a new submodule to the working tree, .gitmodules and the index
96 # optional branch is stored in global branch variable
103 if test -z "$repo"; then
107 # Turn the source into an absolute path if
109 if base
=$
(get_repo_base
"$repo"); then
113 # Guess path from repo if not specified or strip trailing slashes
114 if test -z "$path"; then
115 path
=$
(echo "$repo" |
sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
117 path
=$
(echo "$path" |
sed -e 's|/*$||')
121 die
"'$path' already exists"
123 git ls-files
--error-unmatch "$path" > /dev
/null
2>&1 &&
124 die
"'$path' already exists in the index"
126 module_clone
"$path" "$repo" ||
exit
127 (unset GIT_DIR
&& cd "$path" && git checkout
-q ${branch:+-b "$branch" "origin/$branch"}) ||
128 die
"Unable to checkout submodule '$path'"
130 die
"Failed to add submodule '$path'"
132 GIT_CONFIG
=.gitmodules git config submodule.
"$path".path
"$path" &&
133 GIT_CONFIG
=.gitmodules git config submodule.
"$path".url
"$repo" &&
134 git add .gitmodules ||
135 die
"Failed to register submodule '$path'"
139 # Register submodules in .git/config
141 # $@ = requested paths (default to all)
145 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
146 while read mode sha1 stage path
148 # Skip already registered paths
149 name
=$
(module_name
"$path") ||
exit
150 url
=$
(git config submodule.
"$name".url
)
151 test -z "$url" ||
continue
153 url
=$
(GIT_CONFIG
=.gitmodules git config submodule.
"$name".url
)
155 die
"No url found for submodule path '$path' in .gitmodules"
157 git config submodule.
"$name".url
"$url" ||
158 die
"Failed to register url for submodule path '$path'"
160 say
"Submodule '$name' ($url) registered for path '$path'"
165 # Update each submodule path to correct revision, using clone and checkout as needed
167 # $@ = requested paths (default to all)
171 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
172 while read mode sha1 stage path
174 name
=$
(module_name
"$path") ||
exit
175 url
=$
(git config submodule.
"$name".url
)
178 # Only mention uninitialized submodules when its
179 # path have been specified
181 say
"Submodule path '$path' not initialized"
185 if ! test -d "$path"/.git
187 module_clone
"$path" "$url" ||
exit
190 subsha1
=$
(unset GIT_DIR
&& cd "$path" &&
191 git rev-parse
--verify HEAD
) ||
192 die
"Unable to find current revision in submodule path '$path'"
195 if test "$subsha1" != "$sha1"
197 (unset GIT_DIR
&& cd "$path" && git-fetch
&&
198 git-checkout
-q "$sha1") ||
199 die
"Unable to checkout '$sha1' in submodule path '$path'"
201 say
"Submodule path '$path': checked out '$sha1'"
210 git describe
"$2" 2>/dev
/null ||
211 git describe
--tags "$2" 2>/dev
/null ||
212 git describe
--contains --tags "$2"
215 test -z "$revname" || revname
=" ($revname)"
219 # List all submodules, prefixed with:
220 # - submodule not initialized
221 # + different revision checked out
223 # If --cached was specified the revision in the index will be printed
224 # instead of the currently checked out revision.
226 # $@ = requested paths (default to all)
230 git ls-files
--stage -- "$@" |
grep -e '^160000 ' |
231 while read mode sha1 stage path
233 name
=$
(module_name
"$path") ||
exit
234 url
=$
(git config submodule.
"$name".url
)
235 if test -z "url" ||
! test -d "$path"/.git
240 set_name_rev
"$path" "$sha1"
241 if git diff-files
--quiet -- "$path"
243 say
" $sha1 $path$revname"
247 sha1
=$
(unset GIT_DIR
&& cd "$path" && git rev-parse
--verify HEAD
)
248 set_name_rev
"$path" "$sha1"
250 say
"+$sha1 $path$revname"
255 3 Simply checks whether the submodule is initialized
256 # or not. If not initialized it does so.
257 initializeSubModule
() {
258 if [ ! -d "$1"/.git
]; then
259 if [ $recurse_verbose -eq 1 ]; then
260 echo Initializing and updating
"$1"
262 git-submodule init
"$1"; git-submodule update
"$1"
266 # This actually traverses the module; checks
267 # whether the module is initialized or not.
268 # if not initialized, then done so and then the
269 # intended command is evaluated. Then it
270 # recursively goes into it modules.
273 dir_path
="$current_dir:$dir_path"
274 initializeSubModule
"$1"
276 if [ $recurse_verbose -eq 1 ]; then
277 echo Working
in mod
$1 @
`pwd` with
$2
280 if [ -f .gitmodules
]; then
281 for mod_path
in `grep "path =" .gitmodules | awk '{print $3}'`; do
282 traverseModule
"$mod_path" "$2"
285 old_dir
=$
(echo $dir_path | cut
-d':' -f1-1)
286 length_old_dir
=`expr "$old_dir" : '.*'`
288 index
=$
(echo "$length_old_dir+2" |
bc)
289 dir_path
=`echo $dir_path $index | awk '{print substr($1, $2)}'`
292 # Propagates or recurses over all the submodules at any
293 # depth with any git command, e.g. git-clone, git-status,
294 # git-commit etc., with the arguments supplied exactly as
295 # it would have been supplied to the command otherwise.
296 # This actually starts the recursive propagation
299 echo Project Home
: $project_home
300 if [ -d $project_home/.git
/ ]; then
305 if [ `expr index "$arg" ' '` -gt 0 ]; then
308 command_arguments
="$command_arguments $arg"
310 if [ $recurse_verbose -eq 1 ]; then
311 echo GIT Command git-
$git_command with arguments\
($#\
) "$command_arguments"
313 main_command
="git-$git_command $command_arguments"
315 if [ -f .gitmodules
]; then
316 for mod_path
in `grep "path =" .gitmodules | awk '{print $3}'`; do
317 traverseModule
$mod_path "$main_command"
321 echo $project_home not a git repo thus exiting
380 case "$add,$branch" in
390 case "$add,$init,$update,$recurse,$status,$cached" in