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