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