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