1#!/bin/sh
2#
3# git-submodules.sh: add, init, update or list git submodules
4#
5# Copyright (c) 2007 Lars Hjemli
6
7USAGE="[--quiet] [--cached] \
8[add <repo> [-b branch] <path>]|[status|init|update [-i|--init]|summary [-n|--summary-limit <n>] [<commit>]] \
9[--] [<path>...]"
10OPTIONS_SPEC=
11. git-sh-setup
12require_work_tree
13
14command=
15branch=
16quiet=
17cached=
18
19#
20# print stuff on stdout unless -q was specified
21#
22say()
23{
24 if test -z "$quiet"
25 then
26 echo "$@"
27 fi
28}
29
30# Resolve relative url by appending to parent's url
31resolve_relative_url ()
32{
33 branch="$(git symbolic-ref HEAD 2>/dev/null)"
34 remote="$(git config branch.${branch#refs/heads/}.remote)"
35 remote="${remote:-origin}"
36 remoteurl=$(git config "remote.$remote.url") ||
37 die "remote ($remote) does not have a url defined in .git/config"
38 url="$1"
39 while test -n "$url"
40 do
41 case "$url" in
42 ../*)
43 url="${url#../}"
44 remoteurl="${remoteurl%/*}"
45 ;;
46 ./*)
47 url="${url#./}"
48 ;;
49 *)
50 break;;
51 esac
52 done
53 echo "$remoteurl/$url"
54}
55
56#
57# Map submodule path to submodule name
58#
59# $1 = path
60#
61module_name()
62{
63 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
64 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
65 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
66 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
67 test -z "$name" &&
68 die "No submodule mapping found in .gitmodules for path '$path'"
69 echo "$name"
70}
71
72#
73# Clone a submodule
74#
75# Prior to calling, cmd_update checks that a possibly existing
76# path is not a git repository.
77# Likewise, cmd_add checks that path does not exist at all,
78# since it is the location of a new submodule.
79#
80module_clone()
81{
82 path=$1
83 url=$2
84
85 # If there already is a directory at the submodule path,
86 # expect it to be empty (since that is the default checkout
87 # action) and try to remove it.
88 # Note: if $path is a symlink to a directory the test will
89 # succeed but the rmdir will fail. We might want to fix this.
90 if test -d "$path"
91 then
92 rmdir "$path" 2>/dev/null ||
93 die "Directory '$path' exist, but is neither empty nor a git repository"
94 fi
95
96 test -e "$path" &&
97 die "A file already exist at path '$path'"
98
99 git-clone -n "$url" "$path" ||
100 die "Clone of '$url' into submodule path '$path' failed"
101}
102
103#
104# Add a new submodule to the working tree, .gitmodules and the index
105#
106# $@ = repo path
107#
108# optional branch is stored in global branch variable
109#
110cmd_add()
111{
112 # parse $args after "submodule ... add".
113 while test $# -ne 0
114 do
115 case "$1" in
116 -b | --branch)
117 case "$2" in '') usage ;; esac
118 branch=$2
119 shift
120 ;;
121 -q|--quiet)
122 quiet=1
123 ;;
124 --)
125 shift
126 break
127 ;;
128 -*)
129 usage
130 ;;
131 *)
132 break
133 ;;
134 esac
135 shift
136 done
137
138 repo=$1
139 path=$2
140
141 if test -z "$repo" -o -z "$path"; then
142 usage
143 fi
144
145 # assure repo is absolute or relative to parent
146 case "$repo" in
147 ./*|../*)
148 # dereference source url relative to parent's url
149 realrepo=$(resolve_relative_url "$repo") || exit
150 ;;
151 *:*|/*)
152 # absolute url
153 realrepo=$repo
154 ;;
155 *)
156 die "repo URL: '$repo' must be absolute or begin with ./|../"
157 ;;
158 esac
159
160 # strip trailing slashes from path
161 path=$(echo "$path" | sed -e 's|/*$||')
162
163 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
164 die "'$path' already exists in the index"
165
166 # perhaps the path exists and is already a git repo, else clone it
167 if test -e "$path"
168 then
169 if test -d "$path"/.git -o -f "$path"/.git
170 then
171 echo "Adding existing repo at '$path' to the index"
172 else
173 die "'$path' already exists and is not a valid git repo"
174 fi
175 else
176
177 module_clone "$path" "$realrepo" || exit
178 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
179 die "Unable to checkout submodule '$path'"
180 fi
181
182 git add "$path" ||
183 die "Failed to add submodule '$path'"
184
185 git config -f .gitmodules submodule."$path".path "$path" &&
186 git config -f .gitmodules submodule."$path".url "$repo" &&
187 git add .gitmodules ||
188 die "Failed to register submodule '$path'"
189}
190
191#
192# Register submodules in .git/config
193#
194# $@ = requested paths (default to all)
195#
196cmd_init()
197{
198 # parse $args after "submodule ... init".
199 while test $# -ne 0
200 do
201 case "$1" in
202 -q|--quiet)
203 quiet=1
204 ;;
205 --)
206 shift
207 break
208 ;;
209 -*)
210 usage
211 ;;
212 *)
213 break
214 ;;
215 esac
216 shift
217 done
218
219 git ls-files --stage -- "$@" | grep '^160000 ' |
220 while read mode sha1 stage path
221 do
222 # Skip already registered paths
223 name=$(module_name "$path") || exit
224 url=$(git config submodule."$name".url)
225 test -z "$url" || continue
226
227 url=$(git config -f .gitmodules submodule."$name".url)
228 test -z "$url" &&
229 die "No url found for submodule path '$path' in .gitmodules"
230
231 # Possibly a url relative to parent
232 case "$url" in
233 ./*|../*)
234 url=$(resolve_relative_url "$url") || exit
235 ;;
236 esac
237
238 git config submodule."$name".url "$url" ||
239 die "Failed to register url for submodule path '$path'"
240
241 say "Submodule '$name' ($url) registered for path '$path'"
242 done
243}
244
245#
246# Update each submodule path to correct revision, using clone and checkout as needed
247#
248# $@ = requested paths (default to all)
249#
250cmd_update()
251{
252 # parse $args after "submodule ... update".
253 while test $# -ne 0
254 do
255 case "$1" in
256 -q|--quiet)
257 quiet=1
258 ;;
259 -i|--init)
260 shift
261 cmd_init "$@" || return
262 ;;
263 --)
264 shift
265 break
266 ;;
267 -*)
268 usage
269 ;;
270 *)
271 break
272 ;;
273 esac
274 shift
275 done
276
277 git ls-files --stage -- "$@" | grep '^160000 ' |
278 while read mode sha1 stage path
279 do
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 say "Maybe you want to use 'update --init'?"
289 continue
290 fi
291
292 if ! test -d "$path"/.git -o -f "$path"/.git
293 then
294 module_clone "$path" "$url" || exit
295 subsha1=
296 else
297 subsha1=$(unset GIT_DIR; cd "$path" &&
298 git rev-parse --verify HEAD) ||
299 die "Unable to find current revision in submodule path '$path'"
300 fi
301
302 if test "$subsha1" != "$sha1"
303 then
304 (unset GIT_DIR; cd "$path" && git-fetch &&
305 git-checkout -q "$sha1") ||
306 die "Unable to checkout '$sha1' in submodule path '$path'"
307
308 say "Submodule path '$path': checked out '$sha1'"
309 fi
310 done
311}
312
313set_name_rev () {
314 revname=$( (
315 unset GIT_DIR
316 cd "$1" && {
317 git describe "$2" 2>/dev/null ||
318 git describe --tags "$2" 2>/dev/null ||
319 git describe --contains "$2" 2>/dev/null ||
320 git describe --all --always "$2"
321 }
322 ) )
323 test -z "$revname" || revname=" ($revname)"
324}
325#
326# Show commit summary for submodules in index or working tree
327#
328# If '--cached' is given, show summary between index and given commit,
329# or between working tree and given commit
330#
331# $@ = [commit (default 'HEAD'),] requested paths (default all)
332#
333cmd_summary() {
334 summary_limit=-1
335 for_status=
336
337 # parse $args after "submodule ... summary".
338 while test $# -ne 0
339 do
340 case "$1" in
341 --cached)
342 cached="$1"
343 ;;
344 --for-status)
345 for_status="$1"
346 ;;
347 -n|--summary-limit)
348 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
349 then
350 :
351 else
352 usage
353 fi
354 shift
355 ;;
356 --)
357 shift
358 break
359 ;;
360 -*)
361 usage
362 ;;
363 *)
364 break
365 ;;
366 esac
367 shift
368 done
369
370 test $summary_limit = 0 && return
371
372 if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
373 then
374 head=$rev
375 shift
376 else
377 head=HEAD
378 fi
379
380 cd_to_toplevel
381 # Get modified modules cared by user
382 modules=$(git diff-index $cached --raw $head -- "$@" |
383 grep -e '^:160000' -e '^:[0-7]* 160000' |
384 while read mod_src mod_dst sha1_src sha1_dst status name
385 do
386 # Always show modules deleted or type-changed (blob<->module)
387 test $status = D -o $status = T && echo "$name" && continue
388 # Also show added or modified modules which are checked out
389 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
390 echo "$name"
391 done
392 )
393
394 test -z "$modules" && return
395
396 git diff-index $cached --raw $head -- $modules |
397 grep -e '^:160000' -e '^:[0-7]* 160000' |
398 cut -c2- |
399 while read mod_src mod_dst sha1_src sha1_dst status name
400 do
401 if test -z "$cached" &&
402 test $sha1_dst = 0000000000000000000000000000000000000000
403 then
404 case "$mod_dst" in
405 160000)
406 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
407 ;;
408 100644 | 100755 | 120000)
409 sha1_dst=$(git hash-object $name)
410 ;;
411 000000)
412 ;; # removed
413 *)
414 # unexpected type
415 echo >&2 "unexpected mode $mod_dst"
416 continue ;;
417 esac
418 fi
419 missing_src=
420 missing_dst=
421
422 test $mod_src = 160000 &&
423 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 &&
424 missing_src=t
425
426 test $mod_dst = 160000 &&
427 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 &&
428 missing_dst=t
429
430 total_commits=
431 case "$missing_src,$missing_dst" in
432 t,)
433 errmsg=" Warn: $name doesn't contain commit $sha1_src"
434 ;;
435 ,t)
436 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
437 ;;
438 t,t)
439 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
440 ;;
441 *)
442 errmsg=
443 total_commits=$(
444 if test $mod_src = 160000 -a $mod_dst = 160000
445 then
446 range="$sha1_src...$sha1_dst"
447 elif test $mod_src = 160000
448 then
449 range=$sha1_src
450 else
451 range=$sha1_dst
452 fi
453 GIT_DIR="$name/.git" \
454 git log --pretty=oneline --first-parent $range | wc -l
455 )
456 total_commits=" ($(($total_commits + 0)))"
457 ;;
458 esac
459
460 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
461 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
462 if test $status = T
463 then
464 if test $mod_dst = 160000
465 then
466 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
467 else
468 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
469 fi
470 else
471 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
472 fi
473 if test -n "$errmsg"
474 then
475 # Don't give error msg for modification whose dst is not submodule
476 # i.e. deleted or changed to blob
477 test $mod_dst = 160000 && echo "$errmsg"
478 else
479 if test $mod_src = 160000 -a $mod_dst = 160000
480 then
481 limit=
482 test $summary_limit -gt 0 && limit="-$summary_limit"
483 GIT_DIR="$name/.git" \
484 git log $limit --pretty='format: %m %s' \
485 --first-parent $sha1_src...$sha1_dst
486 elif test $mod_dst = 160000
487 then
488 GIT_DIR="$name/.git" \
489 git log --pretty='format: > %s' -1 $sha1_dst
490 else
491 GIT_DIR="$name/.git" \
492 git log --pretty='format: < %s' -1 $sha1_src
493 fi
494 echo
495 fi
496 echo
497 done |
498 if test -n "$for_status"; then
499 echo "# Modified submodules:"
500 echo "#"
501 sed -e 's|^|# |' -e 's|^# $|#|'
502 else
503 cat
504 fi
505}
506#
507# List all submodules, prefixed with:
508# - submodule not initialized
509# + different revision checked out
510#
511# If --cached was specified the revision in the index will be printed
512# instead of the currently checked out revision.
513#
514# $@ = requested paths (default to all)
515#
516cmd_status()
517{
518 # parse $args after "submodule ... status".
519 while test $# -ne 0
520 do
521 case "$1" in
522 -q|--quiet)
523 quiet=1
524 ;;
525 --cached)
526 cached=1
527 ;;
528 --)
529 shift
530 break
531 ;;
532 -*)
533 usage
534 ;;
535 *)
536 break
537 ;;
538 esac
539 shift
540 done
541
542 git ls-files --stage -- "$@" | grep '^160000 ' |
543 while read mode sha1 stage path
544 do
545 name=$(module_name "$path") || exit
546 url=$(git config submodule."$name".url)
547 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
548 then
549 say "-$sha1 $path"
550 continue;
551 fi
552 set_name_rev "$path" "$sha1"
553 if git diff-files --quiet -- "$path"
554 then
555 say " $sha1 $path$revname"
556 else
557 if test -z "$cached"
558 then
559 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
560 set_name_rev "$path" "$sha1"
561 fi
562 say "+$sha1 $path$revname"
563 fi
564 done
565}
566
567# This loop parses the command line arguments to find the
568# subcommand name to dispatch. Parsing of the subcommand specific
569# options are primarily done by the subcommand implementations.
570# Subcommand specific options such as --branch and --cached are
571# parsed here as well, for backward compatibility.
572
573while test $# != 0 && test -z "$command"
574do
575 case "$1" in
576 add | init | update | status | summary)
577 command=$1
578 ;;
579 -q|--quiet)
580 quiet=1
581 ;;
582 -b|--branch)
583 case "$2" in
584 '')
585 usage
586 ;;
587 esac
588 branch="$2"; shift
589 ;;
590 --cached)
591 cached="$1"
592 ;;
593 --)
594 break
595 ;;
596 -*)
597 usage
598 ;;
599 *)
600 break
601 ;;
602 esac
603 shift
604done
605
606# No command word defaults to "status"
607test -n "$command" || command=status
608
609# "-b branch" is accepted only by "add"
610if test -n "$branch" && test "$command" != add
611then
612 usage
613fi
614
615# "--cached" is accepted only by "status" and "summary"
616if test -n "$cached" && test "$command" != status -a "$command" != summary
617then
618 usage
619fi
620
621"cmd_$command" "$@"