]> git.ipfire.org Git - thirdparty/git.git/blame - git-submodule.sh
git-submodule - make "submodule add" more strict, and document it
[thirdparty/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] \
ec05df35 8[add <repo> [-b branch] <path>]|[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
f31a522a
ML
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}"
8e7e6f39
ML
36 remoteurl=$(git config "remote.$remote.url") ||
37 die "remote ($remote) does not have a url defined in .git/config"
f31a522a
ML
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
941987a5
LH
56#
57# Map submodule path to submodule name
58#
59# $1 = path
60#
61module_name()
62{
537601ac 63 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
fe22e542 64 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
a5099bb4 65 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
537601ac 66 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
941987a5
LH
67 test -z "$name" &&
68 die "No submodule mapping found in .gitmodules for path '$path'"
69 echo "$name"
70}
33aa6fff
LH
71
72#
73# Clone a submodule
74#
23a485e3 75# Prior to calling, cmd_update checks that a possibly existing
ecda0723 76# path is not a git repository.
23a485e3 77# Likewise, cmd_add checks that path does not exist at all,
ecda0723
SV
78# since it is the location of a new submodule.
79#
33aa6fff
LH
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" ||
941987a5 100 die "Clone of '$url' into submodule path '$path' failed"
33aa6fff
LH
101}
102
ecda0723
SV
103#
104# Add a new submodule to the working tree, .gitmodules and the index
105#
ec05df35 106# $@ = repo path
ecda0723
SV
107#
108# optional branch is stored in global branch variable
109#
23a485e3 110cmd_add()
ecda0723 111{
5c08dbbd
JH
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
ecda0723
SV
138 repo=$1
139 path=$2
140
ec05df35 141 if test -z "$repo" -o -z "$path"; then
ecda0723
SV
142 usage
143 fi
144
ec05df35
ML
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|/*$||')
ecda0723 162
5be60078 163 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
ecda0723
SV
164 die "'$path' already exists in the index"
165
d4264ca3
ML
166 # perhaps the path exists and is already a git repo, else clone it
167 if test -e "$path"
168 then
e9656473 169 if test -d "$path"/.git -o -f "$path"/.git
d4264ca3
ML
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
d4264ca3
ML
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
ecda0723
SV
182 git add "$path" ||
183 die "Failed to add submodule '$path'"
184
a5099bb4
IY
185 git config -f .gitmodules submodule."$path".path "$path" &&
186 git config -f .gitmodules submodule."$path".url "$repo" &&
ecda0723
SV
187 git add .gitmodules ||
188 die "Failed to register submodule '$path'"
189}
190
70c7ac22 191#
211b7f19 192# Register submodules in .git/config
70c7ac22
LH
193#
194# $@ = requested paths (default to all)
195#
23a485e3 196cmd_init()
70c7ac22 197{
5c08dbbd
JH
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
aadbe44f 219 git ls-files --stage -- "$@" | grep '^160000 ' |
70c7ac22
LH
220 while read mode sha1 stage path
221 do
211b7f19 222 # Skip already registered paths
941987a5 223 name=$(module_name "$path") || exit
5be60078 224 url=$(git config submodule."$name".url)
211b7f19 225 test -z "$url" || continue
70c7ac22 226
a5099bb4 227 url=$(git config -f .gitmodules submodule."$name".url)
70c7ac22 228 test -z "$url" &&
941987a5 229 die "No url found for submodule path '$path' in .gitmodules"
70c7ac22 230
f31a522a
ML
231 # Possibly a url relative to parent
232 case "$url" in
233 ./*|../*)
8e7e6f39 234 url=$(resolve_relative_url "$url") || exit
f31a522a
ML
235 ;;
236 esac
237
5be60078 238 git config submodule."$name".url "$url" ||
941987a5 239 die "Failed to register url for submodule path '$path'"
70c7ac22 240
941987a5 241 say "Submodule '$name' ($url) registered for path '$path'"
70c7ac22
LH
242 done
243}
244
245#
211b7f19 246# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
247#
248# $@ = requested paths (default to all)
249#
23a485e3 250cmd_update()
70c7ac22 251{
5c08dbbd
JH
252 # parse $args after "submodule ... update".
253 while test $# -ne 0
254 do
255 case "$1" in
256 -q|--quiet)
257 quiet=1
258 ;;
be4d2c83
JS
259 -i|--init)
260 shift
261 cmd_init "$@" || return
262 ;;
5c08dbbd
JH
263 --)
264 shift
265 break
266 ;;
267 -*)
268 usage
269 ;;
270 *)
271 break
272 ;;
273 esac
274 shift
275 done
276
aadbe44f 277 git ls-files --stage -- "$@" | grep '^160000 ' |
70c7ac22
LH
278 while read mode sha1 stage path
279 do
941987a5 280 name=$(module_name "$path") || exit
5be60078 281 url=$(git config submodule."$name".url)
211b7f19 282 if test -z "$url"
70c7ac22
LH
283 then
284 # Only mention uninitialized submodules when its
285 # path have been specified
286 test "$#" != "0" &&
941987a5 287 say "Submodule path '$path' not initialized"
be4d2c83 288 say "Maybe you want to use 'update --init'?"
211b7f19
LH
289 continue
290 fi
291
ba88a1fe 292 if ! test -d "$path"/.git -o -f "$path"/.git
211b7f19
LH
293 then
294 module_clone "$path" "$url" || exit
bf2d8246
LH
295 subsha1=
296 else
51884080 297 subsha1=$(unset GIT_DIR; cd "$path" &&
5be60078 298 git rev-parse --verify HEAD) ||
941987a5 299 die "Unable to find current revision in submodule path '$path'"
70c7ac22 300 fi
211b7f19 301
70c7ac22
LH
302 if test "$subsha1" != "$sha1"
303 then
51884080 304 (unset GIT_DIR; cd "$path" && git-fetch &&
70c7ac22 305 git-checkout -q "$sha1") ||
941987a5 306 die "Unable to checkout '$sha1' in submodule path '$path'"
70c7ac22 307
941987a5 308 say "Submodule path '$path': checked out '$sha1'"
70c7ac22
LH
309 fi
310 done
311}
312
bffe71f4
EM
313set_name_rev () {
314 revname=$( (
51884080 315 unset GIT_DIR
bffe71f4 316 cd "$1" && {
5be60078
JH
317 git describe "$2" 2>/dev/null ||
318 git describe --tags "$2" 2>/dev/null ||
f669ac0b
ML
319 git describe --contains "$2" 2>/dev/null ||
320 git describe --all --always "$2"
bffe71f4
EM
321 }
322 ) )
323 test -z "$revname" || revname=" ($revname)"
324}
28f9af5d
PY
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() {
f2dc06a3 334 summary_limit=-1
d0f64dd4 335 for_status=
f2dc06a3 336
28f9af5d
PY
337 # parse $args after "submodule ... summary".
338 while test $# -ne 0
339 do
340 case "$1" in
341 --cached)
342 cached="$1"
343 ;;
d0f64dd4
PY
344 --for-status)
345 for_status="$1"
346 ;;
f2dc06a3
PY
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 ;;
28f9af5d
PY
356 --)
357 shift
358 break
359 ;;
360 -*)
361 usage
362 ;;
363 *)
364 break
365 ;;
366 esac
367 shift
368 done
bffe71f4 369
f2dc06a3
PY
370 test $summary_limit = 0 && return
371
28f9af5d
PY
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 )
1cb639e6 393
d0f64dd4
PY
394 test -z "$modules" && return
395
1cb639e6
PY
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
bffe71f4 429
1cb639e6
PY
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 )
eed35595 456 total_commits=" ($(($total_commits + 0)))"
1cb639e6
PY
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
f2dc06a3
PY
481 limit=
482 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e6 483 GIT_DIR="$name/.git" \
f2dc06a3 484 git log $limit --pretty='format: %m %s' \
1cb639e6
PY
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
d0f64dd4
PY
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
28f9af5d 505}
70c7ac22 506#
941987a5 507# List all submodules, prefixed with:
70c7ac22
LH
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#
23a485e3 516cmd_status()
70c7ac22 517{
5c08dbbd
JH
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
aadbe44f 542 git ls-files --stage -- "$@" | grep '^160000 ' |
70c7ac22
LH
543 while read mode sha1 stage path
544 do
941987a5 545 name=$(module_name "$path") || exit
5be60078 546 url=$(git config submodule."$name".url)
ba88a1fe 547 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
70c7ac22
LH
548 then
549 say "-$sha1 $path"
550 continue;
551 fi
41c7c1bd 552 set_name_rev "$path" "$sha1"
70c7ac22
LH
553 if git diff-files --quiet -- "$path"
554 then
bffe71f4 555 say " $sha1 $path$revname"
70c7ac22
LH
556 else
557 if test -z "$cached"
558 then
51884080 559 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
41c7c1bd 560 set_name_rev "$path" "$sha1"
70c7ac22 561 fi
bffe71f4 562 say "+$sha1 $path$revname"
70c7ac22
LH
563 fi
564 done
565}
566
5c08dbbd
JH
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"
70c7ac22
LH
574do
575 case "$1" in
28f9af5d 576 add | init | update | status | summary)
5c08dbbd 577 command=$1
70c7ac22
LH
578 ;;
579 -q|--quiet)
580 quiet=1
581 ;;
ecda0723
SV
582 -b|--branch)
583 case "$2" in
584 '')
585 usage
586 ;;
587 esac
588 branch="$2"; shift
589 ;;
70c7ac22 590 --cached)
28f9af5d 591 cached="$1"
70c7ac22
LH
592 ;;
593 --)
594 break
595 ;;
596 -*)
597 usage
598 ;;
599 *)
600 break
601 ;;
602 esac
603 shift
604done
605
5c08dbbd
JH
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
ecda0723 612 usage
5c08dbbd
JH
613fi
614
28f9af5d
PY
615# "--cached" is accepted only by "status" and "summary"
616if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 617then
70c7ac22 618 usage
5c08dbbd
JH
619fi
620
621"cmd_$command" "$@"