- Added the recursively command propagation to the git submodule script
[git-modules-bs.git] / git-submodule
blobeb04f02ffddd197893148724f13cca026793adcd
1 #!/bin/sh
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 ...]]'
9 . git-sh-setup
10 require_work_tree
12 add=
13 branch=
14 init=
15 update=
16 status=
17 quiet=
18 cached=
21 # print stuff on stdout unless -q was specified
23 say()
25 if test -z "$quiet"
26 then
27 echo "$@"
31 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
32 get_repo_base() {
34 cd "`/bin/pwd`" &&
35 cd "$1" || cd "$1.git" &&
37 cd .git
38 pwd
40 ) 2>/dev/null
44 # Map submodule path to submodule name
46 # $1 = path
48 module_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' )
55 test -z "$name" &&
56 die "No submodule mapping found in .gitmodules for path '$path'"
57 echo "$name"
61 # Clone a submodule
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.
68 module_clone()
70 path=$1
71 url=$2
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.
78 if test -d "$path"
79 then
80 rmdir "$path" 2>/dev/null ||
81 die "Directory '$path' exist, but is neither empty nor a git repository"
84 test -e "$path" &&
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
94 # $@ = repo [path]
96 # optional branch is stored in global branch variable
98 module_add()
100 repo=$1
101 path=$2
103 if test -z "$repo"; then
104 usage
107 # Turn the source into an absolute path if
108 # it is local
109 if base=$(get_repo_base "$repo"); then
110 repo="$base"
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')
116 else
117 path=$(echo "$path" | sed -e 's|/*$||')
120 test -e "$path" &&
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'"
129 git add "$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)
143 modules_init()
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)
154 test -z "$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'"
161 done
165 # Update each submodule path to correct revision, using clone and checkout as needed
167 # $@ = requested paths (default to all)
169 modules_update()
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)
176 if test -z "$url"
177 then
178 # Only mention uninitialized submodules when its
179 # path have been specified
180 test "$#" != "0" &&
181 say "Submodule path '$path' not initialized"
182 continue
185 if ! test -d "$path"/.git
186 then
187 module_clone "$path" "$url" || exit
188 subsha1=
189 else
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"
196 then
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'"
203 done
206 set_name_rev () {
207 revname=$( (
208 unset GIT_DIR &&
209 cd "$1" && {
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)
228 modules_list()
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
236 then
237 say "-$sha1 $path"
238 continue;
240 set_name_rev "$path" "$sha1"
241 if git diff-files --quiet -- "$path"
242 then
243 say " $sha1 $path$revname"
244 else
245 if test -z "$cached"
246 then
247 sha1=$(unset GIT_DIR && cd "$path" && git rev-parse --verify HEAD)
248 set_name_rev "$path" "$sha1"
250 say "+$sha1 $path$revname"
252 done
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.
271 traverseModule() {
272 current_dir=`pwd`
273 dir_path="$current_dir:$dir_path"
274 initializeSubModule "$1"
275 cd "$1"
276 if [ $recurse_verbose -eq 1 ]; then
277 echo Working in mod $1 @ `pwd` with $2
279 eval "$2"
280 if [ -f .gitmodules ]; then
281 for mod_path in `grep "path =" .gitmodules | awk '{print $3}'`; do
282 traverseModule "$mod_path" "$2"
283 done
285 old_dir=$(echo $dir_path | cut -d':' -f1-1)
286 length_old_dir=`expr "$old_dir" : '.*'`
287 cd $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
297 propagate() {
298 project_home=`pwd`
299 echo Project Home: $project_home
300 if [ -d $project_home/.git/ ]; then
301 git_command=$1
302 shift
303 command_arguments=""
304 for arg in "$@"; do
305 if [ `expr index "$arg" ' '` -gt 0 ]; then
306 arg="\"$arg\""
308 command_arguments="$command_arguments $arg"
309 done
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"
314 eval $main_command
315 if [ -f .gitmodules ]; then
316 for mod_path in `grep "path =" .gitmodules | awk '{print $3}'`; do
317 traverseModule $mod_path "$main_command"
318 done
320 else
321 echo $project_home not a git repo thus exiting
322 exit
326 recurse_verbose=0
327 while test $# != 0
329 case "$1" in
330 add)
331 add=1
333 init)
334 init=1
336 update)
337 update=1
339 status)
340 status=1
342 -q|--quiet)
343 quiet=1
345 -b|--branch)
346 case "$2" in
348 usage
350 esac
351 branch="$2"; shift
353 --cached)
354 cached=1
357 break
360 usage
362 recurse)
363 recurse=1
364 case "$2" in
366 recurse_verbose=1
367 shift
369 esac
370 shift
371 break
374 break
376 esac
377 shift
378 done
380 case "$add,$branch" in
381 1,*)
386 usage
388 esac
390 case "$add,$init,$update,$recurse,$status,$cached" in
391 1,,,,,)
392 module_add "$@"
394 ,1,,,,)
395 modules_init "$@"
397 ,,1,,,)
398 modules_update "$@"
400 ,,,1,,)
401 propagate "$@"
403 ,,,,*,*)
404 modules_list "$@"
407 usage
409 esac