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