git-submodule summary: code framework
[git/jnareb-git.git] / git-submodule.sh
blobdbb866644a24d587b67b85a3efa8c23e7bef0178
1 #!/bin/sh
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|summary [<commit>]] [--] [<path>...]'
8 OPTIONS_SPEC=
9 . git-sh-setup
10 require_work_tree
12 command=
13 branch=
14 quiet=
15 cached=
18 # print stuff on stdout unless -q was specified
20 say()
22 if test -z "$quiet"
23 then
24 echo "$@"
28 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
29 get_repo_base() {
31 cd "`/bin/pwd`" &&
32 cd "$1" || cd "$1.git" &&
34 cd .git
35 pwd
37 ) 2>/dev/null
40 # Resolve relative url by appending to parent's url
41 resolve_relative_url ()
43 branch="$(git symbolic-ref HEAD 2>/dev/null)"
44 remote="$(git config branch.${branch#refs/heads/}.remote)"
45 remote="${remote:-origin}"
46 remoteurl="$(git config remote.$remote.url)" ||
47 die "remote ($remote) does not have a url in .git/config"
48 url="$1"
49 while test -n "$url"
51 case "$url" in
52 ../*)
53 url="${url#../}"
54 remoteurl="${remoteurl%/*}"
56 ./*)
57 url="${url#./}"
60 break;;
61 esac
62 done
63 echo "$remoteurl/$url"
67 # Map submodule path to submodule name
69 # $1 = path
71 module_name()
73 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
74 re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
75 name=$( GIT_CONFIG=.gitmodules \
76 git config --get-regexp '^submodule\..*\.path$' |
77 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
78 test -z "$name" &&
79 die "No submodule mapping found in .gitmodules for path '$path'"
80 echo "$name"
84 # Clone a submodule
86 # Prior to calling, cmd_update checks that a possibly existing
87 # path is not a git repository.
88 # Likewise, cmd_add checks that path does not exist at all,
89 # since it is the location of a new submodule.
91 module_clone()
93 path=$1
94 url=$2
96 # If there already is a directory at the submodule path,
97 # expect it to be empty (since that is the default checkout
98 # action) and try to remove it.
99 # Note: if $path is a symlink to a directory the test will
100 # succeed but the rmdir will fail. We might want to fix this.
101 if test -d "$path"
102 then
103 rmdir "$path" 2>/dev/null ||
104 die "Directory '$path' exist, but is neither empty nor a git repository"
107 test -e "$path" &&
108 die "A file already exist at path '$path'"
110 git-clone -n "$url" "$path" ||
111 die "Clone of '$url' into submodule path '$path' failed"
115 # Add a new submodule to the working tree, .gitmodules and the index
117 # $@ = repo [path]
119 # optional branch is stored in global branch variable
121 cmd_add()
123 # parse $args after "submodule ... add".
124 while test $# -ne 0
126 case "$1" in
127 -b | --branch)
128 case "$2" in '') usage ;; esac
129 branch=$2
130 shift
132 -q|--quiet)
133 quiet=1
136 shift
137 break
140 usage
143 break
145 esac
146 shift
147 done
149 repo=$1
150 path=$2
152 if test -z "$repo"; then
153 usage
156 case "$repo" in
157 ./*|../*)
158 # dereference source url relative to parent's url
159 realrepo="$(resolve_relative_url $repo)" ;;
161 # Turn the source into an absolute path if
162 # it is local
163 if base=$(get_repo_base "$repo"); then
164 repo="$base"
166 realrepo=$repo
168 esac
170 # Guess path from repo if not specified or strip trailing slashes
171 if test -z "$path"; then
172 path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
173 else
174 path=$(echo "$path" | sed -e 's|/*$||')
177 test -e "$path" &&
178 die "'$path' already exists"
180 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
181 die "'$path' already exists in the index"
183 module_clone "$path" "$realrepo" || exit
184 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
185 die "Unable to checkout submodule '$path'"
186 git add "$path" ||
187 die "Failed to add submodule '$path'"
189 GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
190 GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
191 git add .gitmodules ||
192 die "Failed to register submodule '$path'"
196 # Register submodules in .git/config
198 # $@ = requested paths (default to all)
200 cmd_init()
202 # parse $args after "submodule ... init".
203 while test $# -ne 0
205 case "$1" in
206 -q|--quiet)
207 quiet=1
210 shift
211 break
214 usage
217 break
219 esac
220 shift
221 done
223 git ls-files --stage -- "$@" | grep -e '^160000 ' |
224 while read mode sha1 stage path
226 # Skip already registered paths
227 name=$(module_name "$path") || exit
228 url=$(git config submodule."$name".url)
229 test -z "$url" || continue
231 url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
232 test -z "$url" &&
233 die "No url found for submodule path '$path' in .gitmodules"
235 # Possibly a url relative to parent
236 case "$url" in
237 ./*|../*)
238 url="$(resolve_relative_url "$url")"
240 esac
242 git config submodule."$name".url "$url" ||
243 die "Failed to register url for submodule path '$path'"
245 say "Submodule '$name' ($url) registered for path '$path'"
246 done
250 # Update each submodule path to correct revision, using clone and checkout as needed
252 # $@ = requested paths (default to all)
254 cmd_update()
256 # parse $args after "submodule ... update".
257 while test $# -ne 0
259 case "$1" in
260 -q|--quiet)
261 quiet=1
264 shift
265 break
268 usage
271 break
273 esac
274 shift
275 done
277 git ls-files --stage -- "$@" | grep -e '^160000 ' |
278 while read mode sha1 stage path
280 name=$(module_name "$path") || exit
281 url=$(git config submodule."$name".url)
282 if test -z "$url"
283 then
284 # Only mention uninitialized submodules when its
285 # path have been specified
286 test "$#" != "0" &&
287 say "Submodule path '$path' not initialized"
288 continue
291 if ! test -d "$path"/.git
292 then
293 module_clone "$path" "$url" || exit
294 subsha1=
295 else
296 subsha1=$(unset GIT_DIR; cd "$path" &&
297 git rev-parse --verify HEAD) ||
298 die "Unable to find current revision in submodule path '$path'"
301 if test "$subsha1" != "$sha1"
302 then
303 (unset GIT_DIR; cd "$path" && git-fetch &&
304 git-checkout -q "$sha1") ||
305 die "Unable to checkout '$sha1' in submodule path '$path'"
307 say "Submodule path '$path': checked out '$sha1'"
309 done
312 set_name_rev () {
313 revname=$( (
314 unset GIT_DIR
315 cd "$1" && {
316 git describe "$2" 2>/dev/null ||
317 git describe --tags "$2" 2>/dev/null ||
318 git describe --contains --tags "$2"
321 test -z "$revname" || revname=" ($revname)"
324 # Show commit summary for submodules in index or working tree
326 # If '--cached' is given, show summary between index and given commit,
327 # or between working tree and given commit
329 # $@ = [commit (default 'HEAD'),] requested paths (default all)
331 cmd_summary() {
332 # parse $args after "submodule ... summary".
333 while test $# -ne 0
335 case "$1" in
336 --cached)
337 cached="$1"
340 shift
341 break
344 usage
347 break
349 esac
350 shift
351 done
353 if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
354 then
355 head=$rev
356 shift
357 else
358 head=HEAD
361 cd_to_toplevel
362 # Get modified modules cared by user
363 modules=$(git diff-index $cached --raw $head -- "$@" |
364 grep -e '^:160000' -e '^:[0-7]* 160000' |
365 while read mod_src mod_dst sha1_src sha1_dst status name
367 # Always show modules deleted or type-changed (blob<->module)
368 test $status = D -o $status = T && echo "$name" && continue
369 # Also show added or modified modules which are checked out
370 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
371 echo "$name"
372 done
376 # List all submodules, prefixed with:
377 # - submodule not initialized
378 # + different revision checked out
380 # If --cached was specified the revision in the index will be printed
381 # instead of the currently checked out revision.
383 # $@ = requested paths (default to all)
385 cmd_status()
387 # parse $args after "submodule ... status".
388 while test $# -ne 0
390 case "$1" in
391 -q|--quiet)
392 quiet=1
394 --cached)
395 cached=1
398 shift
399 break
402 usage
405 break
407 esac
408 shift
409 done
411 git ls-files --stage -- "$@" | grep -e '^160000 ' |
412 while read mode sha1 stage path
414 name=$(module_name "$path") || exit
415 url=$(git config submodule."$name".url)
416 if test -z "$url" || ! test -d "$path"/.git
417 then
418 say "-$sha1 $path"
419 continue;
421 set_name_rev "$path" "$sha1"
422 if git diff-files --quiet -- "$path"
423 then
424 say " $sha1 $path$revname"
425 else
426 if test -z "$cached"
427 then
428 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
429 set_name_rev "$path" "$sha1"
431 say "+$sha1 $path$revname"
433 done
436 # This loop parses the command line arguments to find the
437 # subcommand name to dispatch. Parsing of the subcommand specific
438 # options are primarily done by the subcommand implementations.
439 # Subcommand specific options such as --branch and --cached are
440 # parsed here as well, for backward compatibility.
442 while test $# != 0 && test -z "$command"
444 case "$1" in
445 add | init | update | status | summary)
446 command=$1
448 -q|--quiet)
449 quiet=1
451 -b|--branch)
452 case "$2" in
454 usage
456 esac
457 branch="$2"; shift
459 --cached)
460 cached="$1"
463 break
466 usage
469 break
471 esac
472 shift
473 done
475 # No command word defaults to "status"
476 test -n "$command" || command=status
478 # "-b branch" is accepted only by "add"
479 if test -n "$branch" && test "$command" != add
480 then
481 usage
484 # "--cached" is accepted only by "status" and "summary"
485 if test -n "$cached" && test "$command" != status -a "$command" != summary
486 then
487 usage
490 "cmd_$command" "$@"