]> git.ipfire.org Git - thirdparty/git.git/blame - git-submodule.sh
Merge branch 'mw/send-email'
[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] \
835a3eea 8[add [-b branch] <repo> <path>]|[status|init|update [-i|--init] [-N|--no-fetch]|summary [-n|--summary-limit <n>] [<commit>]] \
2327f61e 9[--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
8f321a39 10OPTIONS_SPEC=
70c7ac22 11. git-sh-setup
98fcf840 12. git-parse-remote
70c7ac22
LH
13require_work_tree
14
5c08dbbd 15command=
ecda0723 16branch=
70c7ac22 17quiet=
d92a3959 18reference=
70c7ac22 19cached=
31ca3ac3 20nofetch=
70c7ac22
LH
21
22#
23# print stuff on stdout unless -q was specified
24#
25say()
26{
27 if test -z "$quiet"
28 then
29 echo "$@"
30 fi
31}
32
f31a522a
ML
33# Resolve relative url by appending to parent's url
34resolve_relative_url ()
35{
98fcf840 36 remote=$(get_default_remote)
8e7e6f39
ML
37 remoteurl=$(git config "remote.$remote.url") ||
38 die "remote ($remote) does not have a url defined in .git/config"
f31a522a 39 url="$1"
99b120af 40 remoteurl=${remoteurl%/}
f31a522a
ML
41 while test -n "$url"
42 do
43 case "$url" in
44 ../*)
45 url="${url#../}"
46 remoteurl="${remoteurl%/*}"
47 ;;
48 ./*)
49 url="${url#./}"
50 ;;
51 *)
52 break;;
53 esac
54 done
99b120af 55 echo "$remoteurl/${url%/}"
f31a522a
ML
56}
57
a7b3269c
DA
58#
59# Get submodule info for registered submodules
60# $@ = path to limit submodule list
61#
62module_list()
63{
496917b7 64 git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
a7b3269c
DA
65}
66
941987a5
LH
67#
68# Map submodule path to submodule name
69#
70# $1 = path
71#
72module_name()
73{
537601ac 74 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
fe22e542 75 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
a5099bb4 76 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
537601ac 77 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
941987a5
LH
78 test -z "$name" &&
79 die "No submodule mapping found in .gitmodules for path '$path'"
80 echo "$name"
81}
33aa6fff
LH
82
83#
84# Clone a submodule
85#
23a485e3 86# Prior to calling, cmd_update checks that a possibly existing
ecda0723 87# path is not a git repository.
23a485e3 88# Likewise, cmd_add checks that path does not exist at all,
ecda0723
SV
89# since it is the location of a new submodule.
90#
33aa6fff
LH
91module_clone()
92{
93 path=$1
94 url=$2
d92a3959 95 reference="$3"
33aa6fff
LH
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
d92a3959
MT
111 if test -n "$reference"
112 then
113 git-clone "$reference" -n "$url" "$path"
114 else
115 git-clone -n "$url" "$path"
116 fi ||
941987a5 117 die "Clone of '$url' into submodule path '$path' failed"
33aa6fff
LH
118}
119
ecda0723
SV
120#
121# Add a new submodule to the working tree, .gitmodules and the index
122#
ec05df35 123# $@ = repo path
ecda0723
SV
124#
125# optional branch is stored in global branch variable
126#
23a485e3 127cmd_add()
ecda0723 128{
5c08dbbd
JH
129 # parse $args after "submodule ... add".
130 while test $# -ne 0
131 do
132 case "$1" in
133 -b | --branch)
134 case "$2" in '') usage ;; esac
135 branch=$2
136 shift
137 ;;
138 -q|--quiet)
139 quiet=1
140 ;;
d92a3959
MT
141 --reference)
142 case "$2" in '') usage ;; esac
143 reference="--reference=$2"
144 shift
145 ;;
146 --reference=*)
147 reference="$1"
148 shift
149 ;;
5c08dbbd
JH
150 --)
151 shift
152 break
153 ;;
154 -*)
155 usage
156 ;;
157 *)
158 break
159 ;;
160 esac
161 shift
162 done
163
ecda0723
SV
164 repo=$1
165 path=$2
166
ec05df35 167 if test -z "$repo" -o -z "$path"; then
ecda0723
SV
168 usage
169 fi
170
ec05df35
ML
171 # assure repo is absolute or relative to parent
172 case "$repo" in
173 ./*|../*)
174 # dereference source url relative to parent's url
175 realrepo=$(resolve_relative_url "$repo") || exit
176 ;;
177 *:*|/*)
178 # absolute url
179 realrepo=$repo
180 ;;
181 *)
182 die "repo URL: '$repo' must be absolute or begin with ./|../"
183 ;;
184 esac
185
db75ada5
MG
186 # normalize path:
187 # multiple //; leading ./; /./; /../; trailing /
188 path=$(printf '%s/\n' "$path" |
189 sed -e '
190 s|//*|/|g
191 s|^\(\./\)*||
192 s|/\./|/|g
193 :start
194 s|\([^/]*\)/\.\./||
195 tstart
196 s|/*$||
197 ')
5be60078 198 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
ecda0723
SV
199 die "'$path' already exists in the index"
200
d4264ca3
ML
201 # perhaps the path exists and is already a git repo, else clone it
202 if test -e "$path"
203 then
e9656473 204 if test -d "$path"/.git -o -f "$path"/.git
d4264ca3
ML
205 then
206 echo "Adding existing repo at '$path' to the index"
207 else
208 die "'$path' already exists and is not a valid git repo"
209 fi
c2f93917
ML
210
211 case "$repo" in
212 ./*|../*)
213 url=$(resolve_relative_url "$repo") || exit
214 ;;
215 *)
216 url="$repo"
217 ;;
218 esac
219 git config submodule."$path".url "$url"
d4264ca3 220 else
d4264ca3 221
d92a3959 222 module_clone "$path" "$realrepo" "$reference" || exit
ea10b60c
BJ
223 (
224 unset GIT_DIR
225 cd "$path" &&
226 # ash fails to wordsplit ${branch:+-b "$branch"...}
227 case "$branch" in
228 '') git checkout -f -q ;;
229 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
230 esac
231 ) || die "Unable to checkout submodule '$path'"
d4264ca3
ML
232 fi
233
ecda0723
SV
234 git add "$path" ||
235 die "Failed to add submodule '$path'"
236
a5099bb4
IY
237 git config -f .gitmodules submodule."$path".path "$path" &&
238 git config -f .gitmodules submodule."$path".url "$repo" &&
ecda0723
SV
239 git add .gitmodules ||
240 die "Failed to register submodule '$path'"
241}
242
19a31f9c
ML
243#
244# Execute an arbitrary command sequence in each checked out
245# submodule
246#
247# $@ = command to execute
248#
249cmd_foreach()
250{
a7b3269c 251 module_list |
19a31f9c
ML
252 while read mode sha1 stage path
253 do
254 if test -e "$path"/.git
255 then
256 say "Entering '$path'"
257 (cd "$path" && eval "$@") ||
258 die "Stopping at '$path'; script returned non-zero status."
259 fi
260 done
261}
262
70c7ac22 263#
211b7f19 264# Register submodules in .git/config
70c7ac22
LH
265#
266# $@ = requested paths (default to all)
267#
23a485e3 268cmd_init()
70c7ac22 269{
5c08dbbd
JH
270 # parse $args after "submodule ... init".
271 while test $# -ne 0
272 do
273 case "$1" in
274 -q|--quiet)
275 quiet=1
276 ;;
277 --)
278 shift
279 break
280 ;;
281 -*)
282 usage
283 ;;
284 *)
285 break
286 ;;
287 esac
288 shift
289 done
290
a7b3269c 291 module_list "$@" |
70c7ac22
LH
292 while read mode sha1 stage path
293 do
211b7f19 294 # Skip already registered paths
941987a5 295 name=$(module_name "$path") || exit
5be60078 296 url=$(git config submodule."$name".url)
211b7f19 297 test -z "$url" || continue
70c7ac22 298
a5099bb4 299 url=$(git config -f .gitmodules submodule."$name".url)
70c7ac22 300 test -z "$url" &&
941987a5 301 die "No url found for submodule path '$path' in .gitmodules"
70c7ac22 302
f31a522a
ML
303 # Possibly a url relative to parent
304 case "$url" in
305 ./*|../*)
8e7e6f39 306 url=$(resolve_relative_url "$url") || exit
f31a522a
ML
307 ;;
308 esac
309
5be60078 310 git config submodule."$name".url "$url" ||
941987a5 311 die "Failed to register url for submodule path '$path'"
70c7ac22 312
941987a5 313 say "Submodule '$name' ($url) registered for path '$path'"
70c7ac22
LH
314 done
315}
316
317#
211b7f19 318# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
319#
320# $@ = requested paths (default to all)
321#
23a485e3 322cmd_update()
70c7ac22 323{
5c08dbbd
JH
324 # parse $args after "submodule ... update".
325 while test $# -ne 0
326 do
327 case "$1" in
328 -q|--quiet)
d1f63a37 329 shift
5c08dbbd
JH
330 quiet=1
331 ;;
be4d2c83 332 -i|--init)
d92a3959 333 init=1
be4d2c83 334 shift
be4d2c83 335 ;;
31ca3ac3
FF
336 -N|--no-fetch)
337 shift
338 nofetch=1
339 ;;
d92a3959
MT
340 --reference)
341 case "$2" in '') usage ;; esac
342 reference="--reference=$2"
343 shift 2
344 ;;
345 --reference=*)
346 reference="$1"
347 shift
348 ;;
5c08dbbd
JH
349 --)
350 shift
351 break
352 ;;
353 -*)
354 usage
355 ;;
356 *)
357 break
358 ;;
359 esac
5c08dbbd
JH
360 done
361
d92a3959
MT
362 if test -n "$init"
363 then
364 cmd_init "--" "$@" || return
365 fi
366
a7b3269c 367 module_list "$@" |
70c7ac22
LH
368 while read mode sha1 stage path
369 do
941987a5 370 name=$(module_name "$path") || exit
5be60078 371 url=$(git config submodule."$name".url)
211b7f19 372 if test -z "$url"
70c7ac22
LH
373 then
374 # Only mention uninitialized submodules when its
375 # path have been specified
376 test "$#" != "0" &&
989206f5 377 say "Submodule path '$path' not initialized" &&
be4d2c83 378 say "Maybe you want to use 'update --init'?"
211b7f19
LH
379 continue
380 fi
381
ba88a1fe 382 if ! test -d "$path"/.git -o -f "$path"/.git
211b7f19 383 then
d92a3959 384 module_clone "$path" "$url" "$reference"|| exit
bf2d8246
LH
385 subsha1=
386 else
51884080 387 subsha1=$(unset GIT_DIR; cd "$path" &&
5be60078 388 git rev-parse --verify HEAD) ||
941987a5 389 die "Unable to find current revision in submodule path '$path'"
70c7ac22 390 fi
211b7f19 391
70c7ac22
LH
392 if test "$subsha1" != "$sha1"
393 then
b9b378a0
PY
394 force=
395 if test -z "$subsha1"
396 then
397 force="-f"
398 fi
31ca3ac3
FF
399
400 if test -z "$nofetch"
401 then
402 (unset GIT_DIR; cd "$path" &&
403 git-fetch) ||
404 die "Unable to fetch in submodule path '$path'"
405 fi
406
407 (unset GIT_DIR; cd "$path" &&
408 git-checkout $force -q "$sha1") ||
941987a5 409 die "Unable to checkout '$sha1' in submodule path '$path'"
70c7ac22 410
941987a5 411 say "Submodule path '$path': checked out '$sha1'"
70c7ac22
LH
412 fi
413 done
414}
415
bffe71f4
EM
416set_name_rev () {
417 revname=$( (
51884080 418 unset GIT_DIR
bffe71f4 419 cd "$1" && {
5be60078
JH
420 git describe "$2" 2>/dev/null ||
421 git describe --tags "$2" 2>/dev/null ||
f669ac0b
ML
422 git describe --contains "$2" 2>/dev/null ||
423 git describe --all --always "$2"
bffe71f4
EM
424 }
425 ) )
426 test -z "$revname" || revname=" ($revname)"
427}
28f9af5d
PY
428#
429# Show commit summary for submodules in index or working tree
430#
431# If '--cached' is given, show summary between index and given commit,
432# or between working tree and given commit
433#
434# $@ = [commit (default 'HEAD'),] requested paths (default all)
435#
436cmd_summary() {
f2dc06a3 437 summary_limit=-1
d0f64dd4 438 for_status=
f2dc06a3 439
28f9af5d
PY
440 # parse $args after "submodule ... summary".
441 while test $# -ne 0
442 do
443 case "$1" in
444 --cached)
445 cached="$1"
446 ;;
d0f64dd4
PY
447 --for-status)
448 for_status="$1"
449 ;;
f2dc06a3
PY
450 -n|--summary-limit)
451 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
452 then
453 :
454 else
455 usage
456 fi
457 shift
458 ;;
28f9af5d
PY
459 --)
460 shift
461 break
462 ;;
463 -*)
464 usage
465 ;;
466 *)
467 break
468 ;;
469 esac
470 shift
471 done
bffe71f4 472
f2dc06a3
PY
473 test $summary_limit = 0 && return
474
30353a40 475 if rev=$(git rev-parse -q --verify "$1^0")
28f9af5d
PY
476 then
477 head=$rev
478 shift
479 else
480 head=HEAD
481 fi
482
483 cd_to_toplevel
484 # Get modified modules cared by user
485 modules=$(git diff-index $cached --raw $head -- "$@" |
759ad19e 486 egrep '^:([0-7]* )?160000' |
28f9af5d
PY
487 while read mod_src mod_dst sha1_src sha1_dst status name
488 do
489 # Always show modules deleted or type-changed (blob<->module)
490 test $status = D -o $status = T && echo "$name" && continue
491 # Also show added or modified modules which are checked out
492 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
493 echo "$name"
494 done
495 )
1cb639e6 496
d0f64dd4
PY
497 test -z "$modules" && return
498
1cb639e6 499 git diff-index $cached --raw $head -- $modules |
759ad19e 500 egrep '^:([0-7]* )?160000' |
1cb639e6
PY
501 cut -c2- |
502 while read mod_src mod_dst sha1_src sha1_dst status name
503 do
504 if test -z "$cached" &&
505 test $sha1_dst = 0000000000000000000000000000000000000000
506 then
507 case "$mod_dst" in
508 160000)
509 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
510 ;;
511 100644 | 100755 | 120000)
512 sha1_dst=$(git hash-object $name)
513 ;;
514 000000)
515 ;; # removed
516 *)
517 # unexpected type
518 echo >&2 "unexpected mode $mod_dst"
519 continue ;;
520 esac
521 fi
522 missing_src=
523 missing_dst=
524
525 test $mod_src = 160000 &&
30353a40 526 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1cb639e6
PY
527 missing_src=t
528
529 test $mod_dst = 160000 &&
30353a40 530 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1cb639e6 531 missing_dst=t
bffe71f4 532
1cb639e6
PY
533 total_commits=
534 case "$missing_src,$missing_dst" in
535 t,)
536 errmsg=" Warn: $name doesn't contain commit $sha1_src"
537 ;;
538 ,t)
539 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
540 ;;
541 t,t)
542 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
543 ;;
544 *)
545 errmsg=
546 total_commits=$(
547 if test $mod_src = 160000 -a $mod_dst = 160000
548 then
549 range="$sha1_src...$sha1_dst"
550 elif test $mod_src = 160000
551 then
552 range=$sha1_src
553 else
554 range=$sha1_dst
555 fi
556 GIT_DIR="$name/.git" \
557 git log --pretty=oneline --first-parent $range | wc -l
558 )
eed35595 559 total_commits=" ($(($total_commits + 0)))"
1cb639e6
PY
560 ;;
561 esac
562
563 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
564 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
565 if test $status = T
566 then
567 if test $mod_dst = 160000
568 then
569 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
570 else
571 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
572 fi
573 else
574 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
575 fi
576 if test -n "$errmsg"
577 then
578 # Don't give error msg for modification whose dst is not submodule
579 # i.e. deleted or changed to blob
580 test $mod_dst = 160000 && echo "$errmsg"
581 else
582 if test $mod_src = 160000 -a $mod_dst = 160000
583 then
f2dc06a3
PY
584 limit=
585 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e6 586 GIT_DIR="$name/.git" \
f2dc06a3 587 git log $limit --pretty='format: %m %s' \
1cb639e6
PY
588 --first-parent $sha1_src...$sha1_dst
589 elif test $mod_dst = 160000
590 then
591 GIT_DIR="$name/.git" \
592 git log --pretty='format: > %s' -1 $sha1_dst
593 else
594 GIT_DIR="$name/.git" \
595 git log --pretty='format: < %s' -1 $sha1_src
596 fi
597 echo
598 fi
599 echo
d0f64dd4
PY
600 done |
601 if test -n "$for_status"; then
602 echo "# Modified submodules:"
603 echo "#"
604 sed -e 's|^|# |' -e 's|^# $|#|'
605 else
606 cat
607 fi
28f9af5d 608}
70c7ac22 609#
941987a5 610# List all submodules, prefixed with:
70c7ac22
LH
611# - submodule not initialized
612# + different revision checked out
613#
614# If --cached was specified the revision in the index will be printed
615# instead of the currently checked out revision.
616#
617# $@ = requested paths (default to all)
618#
23a485e3 619cmd_status()
70c7ac22 620{
5c08dbbd
JH
621 # parse $args after "submodule ... status".
622 while test $# -ne 0
623 do
624 case "$1" in
625 -q|--quiet)
626 quiet=1
627 ;;
628 --cached)
629 cached=1
630 ;;
631 --)
632 shift
633 break
634 ;;
635 -*)
636 usage
637 ;;
638 *)
639 break
640 ;;
641 esac
642 shift
643 done
644
a7b3269c 645 module_list "$@" |
70c7ac22
LH
646 while read mode sha1 stage path
647 do
941987a5 648 name=$(module_name "$path") || exit
5be60078 649 url=$(git config submodule."$name".url)
ba88a1fe 650 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
70c7ac22
LH
651 then
652 say "-$sha1 $path"
653 continue;
654 fi
41c7c1bd 655 set_name_rev "$path" "$sha1"
70c7ac22
LH
656 if git diff-files --quiet -- "$path"
657 then
bffe71f4 658 say " $sha1 $path$revname"
70c7ac22
LH
659 else
660 if test -z "$cached"
661 then
51884080 662 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
41c7c1bd 663 set_name_rev "$path" "$sha1"
70c7ac22 664 fi
bffe71f4 665 say "+$sha1 $path$revname"
70c7ac22
LH
666 fi
667 done
668}
2327f61e
DA
669#
670# Sync remote urls for submodules
671# This makes the value for remote.$remote.url match the value
672# specified in .gitmodules.
673#
674cmd_sync()
675{
676 while test $# -ne 0
677 do
678 case "$1" in
679 -q|--quiet)
680 quiet=1
681 shift
682 ;;
683 --)
684 shift
685 break
686 ;;
687 -*)
688 usage
689 ;;
690 *)
691 break
692 ;;
693 esac
694 done
695 cd_to_toplevel
696 module_list "$@" |
697 while read mode sha1 stage path
698 do
699 name=$(module_name "$path")
700 url=$(git config -f .gitmodules --get submodule."$name".url)
baede9f8
JH
701
702 # Possibly a url relative to parent
703 case "$url" in
704 ./*|../*)
705 url=$(resolve_relative_url "$url") || exit
706 ;;
707 esac
708
2327f61e
DA
709 if test -e "$path"/.git
710 then
711 (
712 unset GIT_DIR
713 cd "$path"
714 remote=$(get_default_remote)
715 say "Synchronizing submodule url for '$name'"
716 git config remote."$remote".url "$url"
717 )
718 fi
719 done
720}
70c7ac22 721
5c08dbbd
JH
722# This loop parses the command line arguments to find the
723# subcommand name to dispatch. Parsing of the subcommand specific
724# options are primarily done by the subcommand implementations.
725# Subcommand specific options such as --branch and --cached are
726# parsed here as well, for backward compatibility.
727
728while test $# != 0 && test -z "$command"
70c7ac22
LH
729do
730 case "$1" in
2327f61e 731 add | foreach | init | update | status | summary | sync)
5c08dbbd 732 command=$1
70c7ac22
LH
733 ;;
734 -q|--quiet)
735 quiet=1
736 ;;
ecda0723
SV
737 -b|--branch)
738 case "$2" in
739 '')
740 usage
741 ;;
742 esac
743 branch="$2"; shift
744 ;;
70c7ac22 745 --cached)
28f9af5d 746 cached="$1"
70c7ac22
LH
747 ;;
748 --)
749 break
750 ;;
751 -*)
752 usage
753 ;;
754 *)
755 break
756 ;;
757 esac
758 shift
759done
760
5c08dbbd
JH
761# No command word defaults to "status"
762test -n "$command" || command=status
763
764# "-b branch" is accepted only by "add"
765if test -n "$branch" && test "$command" != add
766then
ecda0723 767 usage
5c08dbbd
JH
768fi
769
28f9af5d
PY
770# "--cached" is accepted only by "status" and "summary"
771if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 772then
70c7ac22 773 usage
5c08dbbd
JH
774fi
775
776"cmd_$command" "$@"