Move git-dir for submodules
[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
1d5bec8b 7dashless=$(basename "$0" | sed -e 's/-/ /')
d27b876b 8USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>]
64b19ffe 9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
1d5bec8b 10 or: $dashless [--quiet] init [--] [<path>...]
9db31bdf 11 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
adc54235 12 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
15fc56a8 13 or: $dashless [--quiet] foreach [--recursive] <command>
1d5bec8b 14 or: $dashless [--quiet] sync [--] [<path>...]"
8f321a39 15OPTIONS_SPEC=
70c7ac22 16. git-sh-setup
d0ad8251 17. git-sh-i18n
98fcf840 18. git-parse-remote
70c7ac22
LH
19require_work_tree
20
5c08dbbd 21command=
ecda0723 22branch=
d27b876b 23force=
d92a3959 24reference=
70c7ac22 25cached=
48bb3033
GP
26recursive=
27init=
1c244f6e 28files=
31ca3ac3 29nofetch=
32948425 30update=
15fc56a8 31prefix=
70c7ac22 32
f31a522a
ML
33# Resolve relative url by appending to parent's url
34resolve_relative_url ()
35{
98fcf840 36 remote=$(get_default_remote)
8e7e6f39 37 remoteurl=$(git config "remote.$remote.url") ||
4d689320 38 remoteurl=$(pwd) # the repository is its own authoritative upstream
f31a522a 39 url="$1"
99b120af 40 remoteurl=${remoteurl%/}
ea640cc6 41 sep=/
f31a522a
ML
42 while test -n "$url"
43 do
44 case "$url" in
45 ../*)
46 url="${url#../}"
ea640cc6
TR
47 case "$remoteurl" in
48 */*)
49 remoteurl="${remoteurl%/*}"
50 ;;
51 *:*)
52 remoteurl="${remoteurl%:*}"
53 sep=:
54 ;;
55 *)
497ee872 56 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
ea640cc6
TR
57 ;;
58 esac
f31a522a
ML
59 ;;
60 ./*)
61 url="${url#./}"
62 ;;
63 *)
64 break;;
65 esac
66 done
ea640cc6 67 echo "$remoteurl$sep${url%/}"
f31a522a
ML
68}
69
a7b3269c
DA
70#
71# Get submodule info for registered submodules
72# $@ = path to limit submodule list
73#
74module_list()
75{
313ee0d6
NMC
76 git ls-files --error-unmatch --stage -- "$@" |
77 perl -e '
78 my %unmerged = ();
79 my ($null_sha1) = ("0" x 40);
80 while (<STDIN>) {
81 chomp;
82 my ($mode, $sha1, $stage, $path) =
83 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
84 next unless $mode eq "160000";
85 if ($stage ne "0") {
86 if (!$unmerged{$path}++) {
87 print "$mode $null_sha1 U\t$path\n";
88 }
89 next;
90 }
91 print "$_\n";
92 }
93 '
a7b3269c
DA
94}
95
941987a5
LH
96#
97# Map submodule path to submodule name
98#
99# $1 = path
100#
101module_name()
102{
537601ac 103 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
fe22e542 104 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
a5099bb4 105 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
537601ac 106 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
941987a5 107 test -z "$name" &&
497ee872 108 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$path'")"
941987a5
LH
109 echo "$name"
110}
33aa6fff
LH
111
112#
113# Clone a submodule
114#
23a485e3 115# Prior to calling, cmd_update checks that a possibly existing
ecda0723 116# path is not a git repository.
23a485e3 117# Likewise, cmd_add checks that path does not exist at all,
ecda0723
SV
118# since it is the location of a new submodule.
119#
33aa6fff
LH
120module_clone()
121{
122 path=$1
123 url=$2
d92a3959 124 reference="$3"
501770e1
FG
125 gitdir=
126 gitdir_base=
127 name=$(module_name "$path")
128 base_path=$(dirname "$path")
33aa6fff 129
501770e1
FG
130 gitdir=$(git rev-parse --git-dir)
131 gitdir_base="$gitdir/modules/$base_path"
132 gitdir="$gitdir/modules/$path"
133
134 case $gitdir in
135 /*)
136 a="$(cd_to_toplevel && pwd)/"
137 b=$gitdir
138 while [ "$b" ] && [ "${a%%/*}" = "${b%%/*}" ]
139 do
140 a=${a#*/} b=${b#*/};
141 done
142
143 rel="$a$name"
144 rel=`echo $rel | sed -e 's|[^/]*|..|g'`
145 rel_gitdir="$rel/$b"
146 ;;
147 *)
148 rel=`echo $name | sed -e 's|[^/]*|..|g'`
149 rel_gitdir="$rel/$gitdir"
150 ;;
151 esac
152
153 if test -d "$gitdir"
d92a3959 154 then
501770e1
FG
155 mkdir -p "$path"
156 echo "gitdir: $rel_gitdir" >"$path/.git"
157 rm -f "$gitdir/index"
d92a3959 158 else
501770e1
FG
159 mkdir -p "$gitdir_base"
160 if test -n "$reference"
161 then
162 git-clone "$reference" -n "$url" "$path" --separate-git-dir "$gitdir"
163 else
164 git-clone -n "$url" "$path" --separate-git-dir "$gitdir"
165 fi ||
166 die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")"
167 fi
33aa6fff
LH
168}
169
ecda0723
SV
170#
171# Add a new submodule to the working tree, .gitmodules and the index
172#
ec05df35 173# $@ = repo path
ecda0723
SV
174#
175# optional branch is stored in global branch variable
176#
23a485e3 177cmd_add()
ecda0723 178{
5c08dbbd
JH
179 # parse $args after "submodule ... add".
180 while test $# -ne 0
181 do
182 case "$1" in
183 -b | --branch)
184 case "$2" in '') usage ;; esac
185 branch=$2
186 shift
187 ;;
d27b876b
JL
188 -f | --force)
189 force=$1
190 ;;
5c08dbbd 191 -q|--quiet)
2e6a30ef 192 GIT_QUIET=1
5c08dbbd 193 ;;
d92a3959
MT
194 --reference)
195 case "$2" in '') usage ;; esac
196 reference="--reference=$2"
197 shift
198 ;;
199 --reference=*)
200 reference="$1"
201 shift
202 ;;
5c08dbbd
JH
203 --)
204 shift
205 break
206 ;;
207 -*)
208 usage
209 ;;
210 *)
211 break
212 ;;
213 esac
214 shift
215 done
216
ecda0723
SV
217 repo=$1
218 path=$2
219
1414e578
JL
220 if test -z "$path"; then
221 path=$(echo "$repo" |
222 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
223 fi
224
ec05df35 225 if test -z "$repo" -o -z "$path"; then
ecda0723
SV
226 usage
227 fi
228
ec05df35
ML
229 # assure repo is absolute or relative to parent
230 case "$repo" in
231 ./*|../*)
232 # dereference source url relative to parent's url
233 realrepo=$(resolve_relative_url "$repo") || exit
234 ;;
235 *:*|/*)
236 # absolute url
237 realrepo=$repo
238 ;;
239 *)
497ee872 240 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
ec05df35
ML
241 ;;
242 esac
243
db75ada5
MG
244 # normalize path:
245 # multiple //; leading ./; /./; /../; trailing /
246 path=$(printf '%s/\n' "$path" |
247 sed -e '
248 s|//*|/|g
249 s|^\(\./\)*||
250 s|/\./|/|g
251 :start
252 s|\([^/]*\)/\.\./||
253 tstart
254 s|/*$||
255 ')
5be60078 256 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
497ee872 257 die "$(eval_gettext "'\$path' already exists in the index")"
ecda0723 258
d27b876b
JL
259 if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
260 then
3a4c3ed7
ÆAB
261 (
262 eval_gettext "The following path is ignored by one of your .gitignore files:
263\$path
264Use -f if you really want to add it." &&
265 echo
266 ) >&2
d27b876b
JL
267 exit 1
268 fi
269
d4264ca3
ML
270 # perhaps the path exists and is already a git repo, else clone it
271 if test -e "$path"
272 then
e9656473 273 if test -d "$path"/.git -o -f "$path"/.git
d4264ca3 274 then
dc2204fe 275 eval_gettext "Adding existing repo at '\$path' to the index"; echo
d4264ca3 276 else
497ee872 277 die "$(eval_gettext "'\$path' already exists and is not a valid git repo")"
d4264ca3 278 fi
c2f93917 279
d4264ca3 280 else
d4264ca3 281
d92a3959 282 module_clone "$path" "$realrepo" "$reference" || exit
ea10b60c 283 (
74ae1419 284 clear_local_git_env
ea10b60c
BJ
285 cd "$path" &&
286 # ash fails to wordsplit ${branch:+-b "$branch"...}
287 case "$branch" in
288 '') git checkout -f -q ;;
502dc5b6 289 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
ea10b60c 290 esac
497ee872 291 ) || die "$(eval_gettext "Unable to checkout submodule '\$path'")"
d4264ca3 292 fi
c56dce3b 293 git config submodule."$path".url "$realrepo"
d4264ca3 294
d27b876b 295 git add $force "$path" ||
497ee872 296 die "$(eval_gettext "Failed to add submodule '\$path'")"
ecda0723 297
a5099bb4
IY
298 git config -f .gitmodules submodule."$path".path "$path" &&
299 git config -f .gitmodules submodule."$path".url "$repo" &&
31991b02 300 git add --force .gitmodules ||
497ee872 301 die "$(eval_gettext "Failed to register submodule '\$path'")"
ecda0723
SV
302}
303
19a31f9c
ML
304#
305# Execute an arbitrary command sequence in each checked out
306# submodule
307#
308# $@ = command to execute
309#
310cmd_foreach()
311{
1d5bec8b
JH
312 # parse $args after "submodule ... foreach".
313 while test $# -ne 0
314 do
315 case "$1" in
316 -q|--quiet)
317 GIT_QUIET=1
318 ;;
15fc56a8
JH
319 --recursive)
320 recursive=1
321 ;;
1d5bec8b
JH
322 -*)
323 usage
324 ;;
325 *)
326 break
327 ;;
328 esac
329 shift
330 done
331
f030c96d
ÆAB
332 toplevel=$(pwd)
333
4dca1aa6
BC
334 # dup stdin so that it can be restored when running the external
335 # command in the subshell (and a recursive call to this function)
336 exec 3<&0
337
a7b3269c 338 module_list |
19a31f9c
ML
339 while read mode sha1 stage path
340 do
341 if test -e "$path"/.git
342 then
490b6d57 343 say "$(eval_gettext "Entering '\$prefix\$path'")"
1e7f2aad 344 name=$(module_name "$path")
15fc56a8
JH
345 (
346 prefix="$prefix$path/"
74ae1419 347 clear_local_git_env
15fc56a8
JH
348 cd "$path" &&
349 eval "$@" &&
350 if test -n "$recursive"
351 then
352 cmd_foreach "--recursive" "$@"
353 fi
4dca1aa6 354 ) <&3 3<&- ||
497ee872 355 die "$(eval_gettext "Stopping at '\$path'; script returned non-zero status.")"
19a31f9c
ML
356 fi
357 done
358}
359
70c7ac22 360#
211b7f19 361# Register submodules in .git/config
70c7ac22
LH
362#
363# $@ = requested paths (default to all)
364#
23a485e3 365cmd_init()
70c7ac22 366{
5c08dbbd
JH
367 # parse $args after "submodule ... init".
368 while test $# -ne 0
369 do
370 case "$1" in
371 -q|--quiet)
2e6a30ef 372 GIT_QUIET=1
5c08dbbd
JH
373 ;;
374 --)
375 shift
376 break
377 ;;
378 -*)
379 usage
380 ;;
381 *)
382 break
383 ;;
384 esac
385 shift
386 done
387
a7b3269c 388 module_list "$@" |
70c7ac22
LH
389 while read mode sha1 stage path
390 do
211b7f19 391 # Skip already registered paths
941987a5 392 name=$(module_name "$path") || exit
2cd9de3e
JL
393 if test -z "$(git config "submodule.$name.url")"
394 then
395 url=$(git config -f .gitmodules submodule."$name".url)
396 test -z "$url" &&
0591c0a5 397 die "$(eval_gettext "No url found for submodule path '\$path' in .gitmodules")"
2cd9de3e
JL
398
399 # Possibly a url relative to parent
400 case "$url" in
401 ./*|../*)
402 url=$(resolve_relative_url "$url") || exit
403 ;;
404 esac
405 git config submodule."$name".url "$url" ||
0591c0a5 406 die "$(eval_gettext "Failed to register url for submodule path '\$path'")"
2cd9de3e 407 fi
70c7ac22 408
2cd9de3e 409 # Copy "update" setting when it is not set yet
32948425
JH
410 upd="$(git config -f .gitmodules submodule."$name".update)"
411 test -z "$upd" ||
2cd9de3e 412 test -n "$(git config submodule."$name".update)" ||
32948425 413 git config submodule."$name".update "$upd" ||
497ee872 414 die "$(eval_gettext "Failed to register update mode for submodule path '\$path'")"
ca2cedba 415
497ee872 416 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$path'")"
70c7ac22
LH
417 done
418}
419
420#
211b7f19 421# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
422#
423# $@ = requested paths (default to all)
424#
23a485e3 425cmd_update()
70c7ac22 426{
5c08dbbd 427 # parse $args after "submodule ... update".
98dbe63d 428 orig_flags=
5c08dbbd
JH
429 while test $# -ne 0
430 do
431 case "$1" in
432 -q|--quiet)
2e6a30ef 433 GIT_QUIET=1
5c08dbbd 434 ;;
be4d2c83 435 -i|--init)
d92a3959 436 init=1
be4d2c83 437 ;;
31ca3ac3 438 -N|--no-fetch)
31ca3ac3
FF
439 nofetch=1
440 ;;
9db31bdf
NMC
441 -f|--force)
442 force=$1
443 ;;
ca2cedba 444 -r|--rebase)
32948425 445 update="rebase"
ca2cedba 446 ;;
d92a3959
MT
447 --reference)
448 case "$2" in '') usage ;; esac
449 reference="--reference=$2"
98dbe63d
KB
450 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
451 shift
d92a3959
MT
452 ;;
453 --reference=*)
454 reference="$1"
d92a3959 455 ;;
42b49178 456 -m|--merge)
42b49178
JH
457 update="merge"
458 ;;
b13fd5c1 459 --recursive)
b13fd5c1
JH
460 recursive=1
461 ;;
5c08dbbd
JH
462 --)
463 shift
464 break
465 ;;
466 -*)
467 usage
468 ;;
469 *)
470 break
471 ;;
472 esac
98dbe63d
KB
473 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
474 shift
5c08dbbd
JH
475 done
476
d92a3959
MT
477 if test -n "$init"
478 then
479 cmd_init "--" "$@" || return
480 fi
481
1b4735d9 482 cloned_modules=
15ffb7cd
FG
483 module_list "$@" | {
484 err=
70c7ac22
LH
485 while read mode sha1 stage path
486 do
313ee0d6
NMC
487 if test "$stage" = U
488 then
489 echo >&2 "Skipping unmerged submodule $path"
490 continue
491 fi
941987a5 492 name=$(module_name "$path") || exit
5be60078 493 url=$(git config submodule."$name".url)
32948425 494 update_module=$(git config submodule."$name".update)
211b7f19 495 if test -z "$url"
70c7ac22
LH
496 then
497 # Only mention uninitialized submodules when its
498 # path have been specified
499 test "$#" != "0" &&
1c2ef66f
ÆAB
500 say "$(eval_gettext "Submodule path '\$path' not initialized
501Maybe you want to use 'update --init'?")"
211b7f19
LH
502 continue
503 fi
504
ba88a1fe 505 if ! test -d "$path"/.git -o -f "$path"/.git
211b7f19 506 then
d92a3959 507 module_clone "$path" "$url" "$reference"|| exit
1b4735d9 508 cloned_modules="$cloned_modules;$name"
bf2d8246
LH
509 subsha1=
510 else
74ae1419 511 subsha1=$(clear_local_git_env; cd "$path" &&
5be60078 512 git rev-parse --verify HEAD) ||
497ee872 513 die "$(eval_gettext "Unable to find current revision in submodule path '\$path'")"
70c7ac22 514 fi
211b7f19 515
32948425 516 if ! test -z "$update"
ca2cedba 517 then
32948425 518 update_module=$update
ca2cedba
PH
519 fi
520
70c7ac22
LH
521 if test "$subsha1" != "$sha1"
522 then
9db31bdf
NMC
523 subforce=$force
524 # If we don't already have a -f flag and the submodule has never been checked out
525 if test -z "$subsha1" -a -z "$force"
b9b378a0 526 then
9db31bdf 527 subforce="-f"
b9b378a0 528 fi
31ca3ac3
FF
529
530 if test -z "$nofetch"
531 then
e5f522d6
JL
532 # Run fetch only if $sha1 isn't present or it
533 # is not reachable from a ref.
74ae1419 534 (clear_local_git_env; cd "$path" &&
f5799e05 535 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
e5f522d6 536 test -z "$rev") || git-fetch)) ||
497ee872 537 die "$(eval_gettext "Unable to fetch in submodule path '\$path'")"
31ca3ac3
FF
538 fi
539
1b4735d9
SO
540 # Is this something we just cloned?
541 case ";$cloned_modules;" in
542 *";$name;"*)
543 # then there is no local change to integrate
544 update_module= ;;
545 esac
546
877449c1 547 must_die_on_failure=
32948425
JH
548 case "$update_module" in
549 rebase)
550 command="git rebase"
ee653c89
ÆAB
551 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$path'")"
552 say_msg="$(eval_gettext "Submodule path '\$path': rebased into '\$sha1'")"
877449c1 553 must_die_on_failure=yes
32948425 554 ;;
42b49178
JH
555 merge)
556 command="git merge"
ee653c89
ÆAB
557 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$path'")"
558 say_msg="$(eval_gettext "Submodule path '\$path': merged in '\$sha1'")"
877449c1 559 must_die_on_failure=yes
42b49178 560 ;;
32948425 561 *)
9db31bdf 562 command="git checkout $subforce -q"
ee653c89
ÆAB
563 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$path'")"
564 say_msg="$(eval_gettext "Submodule path '\$path': checked out '\$sha1'")"
32948425
JH
565 ;;
566 esac
70c7ac22 567
15ffb7cd
FG
568 if (clear_local_git_env; cd "$path" && $command "$sha1")
569 then
ff968f03 570 say "$say_msg"
877449c1
JH
571 elif test -n "$must_die_on_failure"
572 then
ff968f03 573 die_with_status 2 "$die_msg"
15ffb7cd 574 else
ff968f03 575 err="${err};$die_msg"
877449c1 576 continue
15ffb7cd 577 fi
70c7ac22 578 fi
b13fd5c1
JH
579
580 if test -n "$recursive"
581 then
15ffb7cd
FG
582 (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags")
583 res=$?
584 if test $res -gt 0
585 then
ff968f03 586 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$path'")"
15ffb7cd
FG
587 if test $res -eq 1
588 then
ff968f03 589 err="${err};$die_msg"
15ffb7cd
FG
590 continue
591 else
ff968f03 592 die_with_status $res "$die_msg"
15ffb7cd
FG
593 fi
594 fi
b13fd5c1 595 fi
70c7ac22 596 done
15ffb7cd
FG
597
598 if test -n "$err"
599 then
600 OIFS=$IFS
601 IFS=';'
602 for e in $err
603 do
604 if test -n "$e"
605 then
606 echo >&2 "$e"
607 fi
608 done
609 IFS=$OIFS
610 exit 1
611 fi
612 }
70c7ac22
LH
613}
614
bffe71f4
EM
615set_name_rev () {
616 revname=$( (
74ae1419 617 clear_local_git_env
bffe71f4 618 cd "$1" && {
5be60078
JH
619 git describe "$2" 2>/dev/null ||
620 git describe --tags "$2" 2>/dev/null ||
f669ac0b
ML
621 git describe --contains "$2" 2>/dev/null ||
622 git describe --all --always "$2"
bffe71f4
EM
623 }
624 ) )
625 test -z "$revname" || revname=" ($revname)"
626}
28f9af5d
PY
627#
628# Show commit summary for submodules in index or working tree
629#
630# If '--cached' is given, show summary between index and given commit,
631# or between working tree and given commit
632#
633# $@ = [commit (default 'HEAD'),] requested paths (default all)
634#
635cmd_summary() {
f2dc06a3 636 summary_limit=-1
d0f64dd4 637 for_status=
1c244f6e 638 diff_cmd=diff-index
f2dc06a3 639
28f9af5d
PY
640 # parse $args after "submodule ... summary".
641 while test $# -ne 0
642 do
643 case "$1" in
644 --cached)
645 cached="$1"
646 ;;
1c244f6e
JL
647 --files)
648 files="$1"
649 ;;
d0f64dd4
PY
650 --for-status)
651 for_status="$1"
652 ;;
f2dc06a3
PY
653 -n|--summary-limit)
654 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
655 then
656 :
657 else
658 usage
659 fi
660 shift
661 ;;
28f9af5d
PY
662 --)
663 shift
664 break
665 ;;
666 -*)
667 usage
668 ;;
669 *)
670 break
671 ;;
672 esac
673 shift
674 done
bffe71f4 675
f2dc06a3
PY
676 test $summary_limit = 0 && return
677
3deea89c 678 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
28f9af5d
PY
679 then
680 head=$rev
caa9c3ca 681 test $# = 0 || shift
3deea89c
JH
682 elif test -z "$1" -o "$1" = "HEAD"
683 then
14e940d7
JH
684 # before the first commit: compare with an empty tree
685 head=$(git hash-object -w -t tree --stdin </dev/null)
2ea6c2c9 686 test -z "$1" || shift
28f9af5d 687 else
3deea89c 688 head="HEAD"
28f9af5d
PY
689 fi
690
1c244f6e
JL
691 if [ -n "$files" ]
692 then
693 test -n "$cached" &&
b9b9c22f 694 die "$(gettext -- "--cached cannot be used with --files")"
1c244f6e
JL
695 diff_cmd=diff-files
696 head=
697 fi
698
28f9af5d
PY
699 cd_to_toplevel
700 # Get modified modules cared by user
18076502 701 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
e1622bfc 702 sane_egrep '^:([0-7]* )?160000' |
28f9af5d
PY
703 while read mod_src mod_dst sha1_src sha1_dst status name
704 do
705 # Always show modules deleted or type-changed (blob<->module)
706 test $status = D -o $status = T && echo "$name" && continue
707 # Also show added or modified modules which are checked out
708 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
709 echo "$name"
710 done
711 )
1cb639e6 712
d0f64dd4
PY
713 test -z "$modules" && return
714
18076502 715 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
e1622bfc 716 sane_egrep '^:([0-7]* )?160000' |
1cb639e6
PY
717 cut -c2- |
718 while read mod_src mod_dst sha1_src sha1_dst status name
719 do
720 if test -z "$cached" &&
721 test $sha1_dst = 0000000000000000000000000000000000000000
722 then
723 case "$mod_dst" in
724 160000)
725 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
726 ;;
727 100644 | 100755 | 120000)
728 sha1_dst=$(git hash-object $name)
729 ;;
730 000000)
731 ;; # removed
732 *)
733 # unexpected type
dc2204fe
ÆAB
734 (
735 eval_gettext "unexpected mode \$mod_dst" &&
736 echo
737 ) >&2
1cb639e6
PY
738 continue ;;
739 esac
740 fi
741 missing_src=
742 missing_dst=
743
744 test $mod_src = 160000 &&
30353a40 745 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1cb639e6
PY
746 missing_src=t
747
748 test $mod_dst = 160000 &&
30353a40 749 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1cb639e6 750 missing_dst=t
bffe71f4 751
1cb639e6
PY
752 total_commits=
753 case "$missing_src,$missing_dst" in
754 t,)
f62f8212 755 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
1cb639e6
PY
756 ;;
757 ,t)
f62f8212 758 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
1cb639e6
PY
759 ;;
760 t,t)
f62f8212 761 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
1cb639e6
PY
762 ;;
763 *)
764 errmsg=
765 total_commits=$(
766 if test $mod_src = 160000 -a $mod_dst = 160000
767 then
768 range="$sha1_src...$sha1_dst"
769 elif test $mod_src = 160000
770 then
771 range=$sha1_src
772 else
773 range=$sha1_dst
774 fi
775 GIT_DIR="$name/.git" \
b0e621ad 776 git rev-list --first-parent $range -- | wc -l
1cb639e6 777 )
eed35595 778 total_commits=" ($(($total_commits + 0)))"
1cb639e6
PY
779 ;;
780 esac
781
782 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
783 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
784 if test $status = T
785 then
b3e73449
ÆAB
786 blob="$(gettext "blob")"
787 submodule="$(gettext "submodule")"
1cb639e6
PY
788 if test $mod_dst = 160000
789 then
b3e73449 790 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1cb639e6 791 else
b3e73449 792 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1cb639e6
PY
793 fi
794 else
795 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
796 fi
797 if test -n "$errmsg"
798 then
799 # Don't give error msg for modification whose dst is not submodule
800 # i.e. deleted or changed to blob
801 test $mod_dst = 160000 && echo "$errmsg"
802 else
803 if test $mod_src = 160000 -a $mod_dst = 160000
804 then
f2dc06a3
PY
805 limit=
806 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e6 807 GIT_DIR="$name/.git" \
f2dc06a3 808 git log $limit --pretty='format: %m %s' \
1cb639e6
PY
809 --first-parent $sha1_src...$sha1_dst
810 elif test $mod_dst = 160000
811 then
812 GIT_DIR="$name/.git" \
813 git log --pretty='format: > %s' -1 $sha1_dst
814 else
815 GIT_DIR="$name/.git" \
816 git log --pretty='format: < %s' -1 $sha1_src
817 fi
818 echo
819 fi
820 echo
d0f64dd4
PY
821 done |
822 if test -n "$for_status"; then
f17a5d34 823 if [ -n "$files" ]; then
165119e9 824 gettext "# Submodules changed but not updated:"; echo
f17a5d34 825 else
165119e9 826 gettext "# Submodule changes to be committed:"; echo
f17a5d34 827 fi
d0f64dd4
PY
828 echo "#"
829 sed -e 's|^|# |' -e 's|^# $|#|'
830 else
831 cat
832 fi
28f9af5d 833}
70c7ac22 834#
941987a5 835# List all submodules, prefixed with:
70c7ac22
LH
836# - submodule not initialized
837# + different revision checked out
838#
839# If --cached was specified the revision in the index will be printed
840# instead of the currently checked out revision.
841#
842# $@ = requested paths (default to all)
843#
23a485e3 844cmd_status()
70c7ac22 845{
5c08dbbd 846 # parse $args after "submodule ... status".
98dbe63d 847 orig_flags=
5c08dbbd
JH
848 while test $# -ne 0
849 do
850 case "$1" in
851 -q|--quiet)
2e6a30ef 852 GIT_QUIET=1
5c08dbbd
JH
853 ;;
854 --cached)
855 cached=1
856 ;;
64b19ffe
JH
857 --recursive)
858 recursive=1
859 ;;
5c08dbbd
JH
860 --)
861 shift
862 break
863 ;;
864 -*)
865 usage
866 ;;
867 *)
868 break
869 ;;
870 esac
98dbe63d 871 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
5c08dbbd
JH
872 shift
873 done
874
a7b3269c 875 module_list "$@" |
70c7ac22
LH
876 while read mode sha1 stage path
877 do
941987a5 878 name=$(module_name "$path") || exit
5be60078 879 url=$(git config submodule."$name".url)
64b19ffe 880 displaypath="$prefix$path"
313ee0d6
NMC
881 if test "$stage" = U
882 then
883 say "U$sha1 $displaypath"
884 continue
885 fi
ba88a1fe 886 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
70c7ac22 887 then
64b19ffe 888 say "-$sha1 $displaypath"
70c7ac22
LH
889 continue;
890 fi
41c7c1bd 891 set_name_rev "$path" "$sha1"
18076502 892 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
70c7ac22 893 then
64b19ffe 894 say " $sha1 $displaypath$revname"
70c7ac22
LH
895 else
896 if test -z "$cached"
897 then
74ae1419 898 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
41c7c1bd 899 set_name_rev "$path" "$sha1"
70c7ac22 900 fi
64b19ffe
JH
901 say "+$sha1 $displaypath$revname"
902 fi
903
904 if test -n "$recursive"
905 then
906 (
907 prefix="$displaypath/"
74ae1419 908 clear_local_git_env
64b19ffe 909 cd "$path" &&
a7eff1a8 910 eval cmd_status "$orig_args"
64b19ffe 911 ) ||
497ee872 912 die "$(eval_gettext "Failed to recurse into submodule path '\$path'")"
70c7ac22
LH
913 fi
914 done
915}
2327f61e
DA
916#
917# Sync remote urls for submodules
918# This makes the value for remote.$remote.url match the value
919# specified in .gitmodules.
920#
921cmd_sync()
922{
923 while test $# -ne 0
924 do
925 case "$1" in
926 -q|--quiet)
2e6a30ef 927 GIT_QUIET=1
2327f61e
DA
928 shift
929 ;;
930 --)
931 shift
932 break
933 ;;
934 -*)
935 usage
936 ;;
937 *)
938 break
939 ;;
940 esac
941 done
942 cd_to_toplevel
943 module_list "$@" |
944 while read mode sha1 stage path
945 do
946 name=$(module_name "$path")
947 url=$(git config -f .gitmodules --get submodule."$name".url)
baede9f8
JH
948
949 # Possibly a url relative to parent
950 case "$url" in
951 ./*|../*)
952 url=$(resolve_relative_url "$url") || exit
953 ;;
954 esac
955
ccee6086 956 if git config "submodule.$name.url" >/dev/null 2>/dev/null
2327f61e 957 then
0591c0a5 958 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
ccee6086
JH
959 git config submodule."$name".url "$url"
960
961 if test -e "$path"/.git
962 then
963 (
964 clear_local_git_env
965 cd "$path"
966 remote=$(get_default_remote)
967 git config remote."$remote".url "$url"
968 )
969 fi
2327f61e
DA
970 fi
971 done
972}
70c7ac22 973
5c08dbbd
JH
974# This loop parses the command line arguments to find the
975# subcommand name to dispatch. Parsing of the subcommand specific
976# options are primarily done by the subcommand implementations.
977# Subcommand specific options such as --branch and --cached are
978# parsed here as well, for backward compatibility.
979
980while test $# != 0 && test -z "$command"
70c7ac22
LH
981do
982 case "$1" in
2327f61e 983 add | foreach | init | update | status | summary | sync)
5c08dbbd 984 command=$1
70c7ac22
LH
985 ;;
986 -q|--quiet)
2e6a30ef 987 GIT_QUIET=1
70c7ac22 988 ;;
ecda0723
SV
989 -b|--branch)
990 case "$2" in
991 '')
992 usage
993 ;;
994 esac
995 branch="$2"; shift
996 ;;
70c7ac22 997 --cached)
28f9af5d 998 cached="$1"
70c7ac22
LH
999 ;;
1000 --)
1001 break
1002 ;;
1003 -*)
1004 usage
1005 ;;
1006 *)
1007 break
1008 ;;
1009 esac
1010 shift
1011done
1012
5c08dbbd
JH
1013# No command word defaults to "status"
1014test -n "$command" || command=status
1015
1016# "-b branch" is accepted only by "add"
1017if test -n "$branch" && test "$command" != add
1018then
ecda0723 1019 usage
5c08dbbd
JH
1020fi
1021
28f9af5d
PY
1022# "--cached" is accepted only by "status" and "summary"
1023if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 1024then
70c7ac22 1025 usage
5c08dbbd
JH
1026fi
1027
1028"cmd_$command" "$@"