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