]> git.ipfire.org Git - thirdparty/git.git/blame - git-submodule.sh
git-submodule - Add 'foreach' subcommand
[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>]] \
19a31f9c 9[--] [<path>...]|[foreach <command>]"
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
c2f93917
ML
175
176 case "$repo" in
177 ./*|../*)
178 url=$(resolve_relative_url "$repo") || exit
179 ;;
180 *)
181 url="$repo"
182 ;;
183 esac
184 git config submodule."$path".url "$url"
d4264ca3 185 else
d4264ca3
ML
186
187 module_clone "$path" "$realrepo" || exit
188 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
189 die "Unable to checkout submodule '$path'"
190 fi
191
ecda0723
SV
192 git add "$path" ||
193 die "Failed to add submodule '$path'"
194
a5099bb4
IY
195 git config -f .gitmodules submodule."$path".path "$path" &&
196 git config -f .gitmodules submodule."$path".url "$repo" &&
ecda0723
SV
197 git add .gitmodules ||
198 die "Failed to register submodule '$path'"
199}
200
19a31f9c
ML
201#
202# Execute an arbitrary command sequence in each checked out
203# submodule
204#
205# $@ = command to execute
206#
207cmd_foreach()
208{
209 git ls-files --stage | grep '^160000 ' |
210 while read mode sha1 stage path
211 do
212 if test -e "$path"/.git
213 then
214 say "Entering '$path'"
215 (cd "$path" && eval "$@") ||
216 die "Stopping at '$path'; script returned non-zero status."
217 fi
218 done
219}
220
70c7ac22 221#
211b7f19 222# Register submodules in .git/config
70c7ac22
LH
223#
224# $@ = requested paths (default to all)
225#
23a485e3 226cmd_init()
70c7ac22 227{
5c08dbbd
JH
228 # parse $args after "submodule ... init".
229 while test $# -ne 0
230 do
231 case "$1" in
232 -q|--quiet)
233 quiet=1
234 ;;
235 --)
236 shift
237 break
238 ;;
239 -*)
240 usage
241 ;;
242 *)
243 break
244 ;;
245 esac
246 shift
247 done
248
aadbe44f 249 git ls-files --stage -- "$@" | grep '^160000 ' |
70c7ac22
LH
250 while read mode sha1 stage path
251 do
211b7f19 252 # Skip already registered paths
941987a5 253 name=$(module_name "$path") || exit
5be60078 254 url=$(git config submodule."$name".url)
211b7f19 255 test -z "$url" || continue
70c7ac22 256
a5099bb4 257 url=$(git config -f .gitmodules submodule."$name".url)
70c7ac22 258 test -z "$url" &&
941987a5 259 die "No url found for submodule path '$path' in .gitmodules"
70c7ac22 260
f31a522a
ML
261 # Possibly a url relative to parent
262 case "$url" in
263 ./*|../*)
8e7e6f39 264 url=$(resolve_relative_url "$url") || exit
f31a522a
ML
265 ;;
266 esac
267
5be60078 268 git config submodule."$name".url "$url" ||
941987a5 269 die "Failed to register url for submodule path '$path'"
70c7ac22 270
941987a5 271 say "Submodule '$name' ($url) registered for path '$path'"
70c7ac22
LH
272 done
273}
274
275#
211b7f19 276# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
277#
278# $@ = requested paths (default to all)
279#
23a485e3 280cmd_update()
70c7ac22 281{
5c08dbbd
JH
282 # parse $args after "submodule ... update".
283 while test $# -ne 0
284 do
285 case "$1" in
286 -q|--quiet)
d1f63a37 287 shift
5c08dbbd
JH
288 quiet=1
289 ;;
be4d2c83
JS
290 -i|--init)
291 shift
292 cmd_init "$@" || return
293 ;;
5c08dbbd
JH
294 --)
295 shift
296 break
297 ;;
298 -*)
299 usage
300 ;;
301 *)
302 break
303 ;;
304 esac
5c08dbbd
JH
305 done
306
aadbe44f 307 git ls-files --stage -- "$@" | grep '^160000 ' |
70c7ac22
LH
308 while read mode sha1 stage path
309 do
941987a5 310 name=$(module_name "$path") || exit
5be60078 311 url=$(git config submodule."$name".url)
211b7f19 312 if test -z "$url"
70c7ac22
LH
313 then
314 # Only mention uninitialized submodules when its
315 # path have been specified
316 test "$#" != "0" &&
941987a5 317 say "Submodule path '$path' not initialized"
be4d2c83 318 say "Maybe you want to use 'update --init'?"
211b7f19
LH
319 continue
320 fi
321
ba88a1fe 322 if ! test -d "$path"/.git -o -f "$path"/.git
211b7f19
LH
323 then
324 module_clone "$path" "$url" || exit
bf2d8246
LH
325 subsha1=
326 else
51884080 327 subsha1=$(unset GIT_DIR; cd "$path" &&
5be60078 328 git rev-parse --verify HEAD) ||
941987a5 329 die "Unable to find current revision in submodule path '$path'"
70c7ac22 330 fi
211b7f19 331
70c7ac22
LH
332 if test "$subsha1" != "$sha1"
333 then
51884080 334 (unset GIT_DIR; cd "$path" && git-fetch &&
70c7ac22 335 git-checkout -q "$sha1") ||
941987a5 336 die "Unable to checkout '$sha1' in submodule path '$path'"
70c7ac22 337
941987a5 338 say "Submodule path '$path': checked out '$sha1'"
70c7ac22
LH
339 fi
340 done
341}
342
bffe71f4
EM
343set_name_rev () {
344 revname=$( (
51884080 345 unset GIT_DIR
bffe71f4 346 cd "$1" && {
5be60078
JH
347 git describe "$2" 2>/dev/null ||
348 git describe --tags "$2" 2>/dev/null ||
f669ac0b
ML
349 git describe --contains "$2" 2>/dev/null ||
350 git describe --all --always "$2"
bffe71f4
EM
351 }
352 ) )
353 test -z "$revname" || revname=" ($revname)"
354}
28f9af5d
PY
355#
356# Show commit summary for submodules in index or working tree
357#
358# If '--cached' is given, show summary between index and given commit,
359# or between working tree and given commit
360#
361# $@ = [commit (default 'HEAD'),] requested paths (default all)
362#
363cmd_summary() {
f2dc06a3 364 summary_limit=-1
d0f64dd4 365 for_status=
f2dc06a3 366
28f9af5d
PY
367 # parse $args after "submodule ... summary".
368 while test $# -ne 0
369 do
370 case "$1" in
371 --cached)
372 cached="$1"
373 ;;
d0f64dd4
PY
374 --for-status)
375 for_status="$1"
376 ;;
f2dc06a3
PY
377 -n|--summary-limit)
378 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
379 then
380 :
381 else
382 usage
383 fi
384 shift
385 ;;
28f9af5d
PY
386 --)
387 shift
388 break
389 ;;
390 -*)
391 usage
392 ;;
393 *)
394 break
395 ;;
396 esac
397 shift
398 done
bffe71f4 399
f2dc06a3
PY
400 test $summary_limit = 0 && return
401
28f9af5d
PY
402 if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
403 then
404 head=$rev
405 shift
406 else
407 head=HEAD
408 fi
409
410 cd_to_toplevel
411 # Get modified modules cared by user
412 modules=$(git diff-index $cached --raw $head -- "$@" |
413 grep -e '^:160000' -e '^:[0-7]* 160000' |
414 while read mod_src mod_dst sha1_src sha1_dst status name
415 do
416 # Always show modules deleted or type-changed (blob<->module)
417 test $status = D -o $status = T && echo "$name" && continue
418 # Also show added or modified modules which are checked out
419 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
420 echo "$name"
421 done
422 )
1cb639e6 423
d0f64dd4
PY
424 test -z "$modules" && return
425
1cb639e6
PY
426 git diff-index $cached --raw $head -- $modules |
427 grep -e '^:160000' -e '^:[0-7]* 160000' |
428 cut -c2- |
429 while read mod_src mod_dst sha1_src sha1_dst status name
430 do
431 if test -z "$cached" &&
432 test $sha1_dst = 0000000000000000000000000000000000000000
433 then
434 case "$mod_dst" in
435 160000)
436 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
437 ;;
438 100644 | 100755 | 120000)
439 sha1_dst=$(git hash-object $name)
440 ;;
441 000000)
442 ;; # removed
443 *)
444 # unexpected type
445 echo >&2 "unexpected mode $mod_dst"
446 continue ;;
447 esac
448 fi
449 missing_src=
450 missing_dst=
451
452 test $mod_src = 160000 &&
453 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 &&
454 missing_src=t
455
456 test $mod_dst = 160000 &&
457 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 &&
458 missing_dst=t
bffe71f4 459
1cb639e6
PY
460 total_commits=
461 case "$missing_src,$missing_dst" in
462 t,)
463 errmsg=" Warn: $name doesn't contain commit $sha1_src"
464 ;;
465 ,t)
466 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
467 ;;
468 t,t)
469 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
470 ;;
471 *)
472 errmsg=
473 total_commits=$(
474 if test $mod_src = 160000 -a $mod_dst = 160000
475 then
476 range="$sha1_src...$sha1_dst"
477 elif test $mod_src = 160000
478 then
479 range=$sha1_src
480 else
481 range=$sha1_dst
482 fi
483 GIT_DIR="$name/.git" \
484 git log --pretty=oneline --first-parent $range | wc -l
485 )
eed35595 486 total_commits=" ($(($total_commits + 0)))"
1cb639e6
PY
487 ;;
488 esac
489
490 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
491 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
492 if test $status = T
493 then
494 if test $mod_dst = 160000
495 then
496 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
497 else
498 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
499 fi
500 else
501 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
502 fi
503 if test -n "$errmsg"
504 then
505 # Don't give error msg for modification whose dst is not submodule
506 # i.e. deleted or changed to blob
507 test $mod_dst = 160000 && echo "$errmsg"
508 else
509 if test $mod_src = 160000 -a $mod_dst = 160000
510 then
f2dc06a3
PY
511 limit=
512 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e6 513 GIT_DIR="$name/.git" \
f2dc06a3 514 git log $limit --pretty='format: %m %s' \
1cb639e6
PY
515 --first-parent $sha1_src...$sha1_dst
516 elif test $mod_dst = 160000
517 then
518 GIT_DIR="$name/.git" \
519 git log --pretty='format: > %s' -1 $sha1_dst
520 else
521 GIT_DIR="$name/.git" \
522 git log --pretty='format: < %s' -1 $sha1_src
523 fi
524 echo
525 fi
526 echo
d0f64dd4
PY
527 done |
528 if test -n "$for_status"; then
529 echo "# Modified submodules:"
530 echo "#"
531 sed -e 's|^|# |' -e 's|^# $|#|'
532 else
533 cat
534 fi
28f9af5d 535}
70c7ac22 536#
941987a5 537# List all submodules, prefixed with:
70c7ac22
LH
538# - submodule not initialized
539# + different revision checked out
540#
541# If --cached was specified the revision in the index will be printed
542# instead of the currently checked out revision.
543#
544# $@ = requested paths (default to all)
545#
23a485e3 546cmd_status()
70c7ac22 547{
5c08dbbd
JH
548 # parse $args after "submodule ... status".
549 while test $# -ne 0
550 do
551 case "$1" in
552 -q|--quiet)
553 quiet=1
554 ;;
555 --cached)
556 cached=1
557 ;;
558 --)
559 shift
560 break
561 ;;
562 -*)
563 usage
564 ;;
565 *)
566 break
567 ;;
568 esac
569 shift
570 done
571
aadbe44f 572 git ls-files --stage -- "$@" | grep '^160000 ' |
70c7ac22
LH
573 while read mode sha1 stage path
574 do
941987a5 575 name=$(module_name "$path") || exit
5be60078 576 url=$(git config submodule."$name".url)
ba88a1fe 577 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
70c7ac22
LH
578 then
579 say "-$sha1 $path"
580 continue;
581 fi
41c7c1bd 582 set_name_rev "$path" "$sha1"
70c7ac22
LH
583 if git diff-files --quiet -- "$path"
584 then
bffe71f4 585 say " $sha1 $path$revname"
70c7ac22
LH
586 else
587 if test -z "$cached"
588 then
51884080 589 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
41c7c1bd 590 set_name_rev "$path" "$sha1"
70c7ac22 591 fi
bffe71f4 592 say "+$sha1 $path$revname"
70c7ac22
LH
593 fi
594 done
595}
596
5c08dbbd
JH
597# This loop parses the command line arguments to find the
598# subcommand name to dispatch. Parsing of the subcommand specific
599# options are primarily done by the subcommand implementations.
600# Subcommand specific options such as --branch and --cached are
601# parsed here as well, for backward compatibility.
602
603while test $# != 0 && test -z "$command"
70c7ac22
LH
604do
605 case "$1" in
19a31f9c 606 add | foreach | init | update | status | summary)
5c08dbbd 607 command=$1
70c7ac22
LH
608 ;;
609 -q|--quiet)
610 quiet=1
611 ;;
ecda0723
SV
612 -b|--branch)
613 case "$2" in
614 '')
615 usage
616 ;;
617 esac
618 branch="$2"; shift
619 ;;
70c7ac22 620 --cached)
28f9af5d 621 cached="$1"
70c7ac22
LH
622 ;;
623 --)
624 break
625 ;;
626 -*)
627 usage
628 ;;
629 *)
630 break
631 ;;
632 esac
633 shift
634done
635
5c08dbbd
JH
636# No command word defaults to "status"
637test -n "$command" || command=status
638
639# "-b branch" is accepted only by "add"
640if test -n "$branch" && test "$command" != add
641then
ecda0723 642 usage
5c08dbbd
JH
643fi
644
28f9af5d
PY
645# "--cached" is accepted only by "status" and "summary"
646if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 647then
70c7ac22 648 usage
5c08dbbd
JH
649fi
650
651"cmd_$command" "$@"