]> git.ipfire.org Git - thirdparty/git.git/blame - git-submodule.sh
mingw: sh arguments need quoting in more circumstances
[thirdparty/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/-/ /')
681b036f 8USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
64b19ffe 9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
1d5bec8b 10 or: $dashless [--quiet] init [--] [<path>...]
f6a52799 11 or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
abed000a 12 or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--] [<path>...]
adc54235 13 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
15fc56a8 14 or: $dashless [--quiet] foreach [--recursive] <command>
c32eaa8a
SB
15 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
16 or: $dashless [--quiet] absorbgitdirs [--] [<path>...]"
8f321a39 17OPTIONS_SPEC=
091a6eb0 18SUBDIRECTORY_OK=Yes
70c7ac22 19. git-sh-setup
98fcf840 20. git-parse-remote
70c7ac22 21require_work_tree
091a6eb0
JK
22wt_prefix=$(git rev-parse --show-prefix)
23cd_to_toplevel
70c7ac22 24
f1762d77
BW
25# Tell the rest of git that any URLs we get don't come
26# directly from the user, so it can apply policy as appropriate.
27GIT_PROTOCOL_FROM_USER=0
28export GIT_PROTOCOL_FROM_USER
33cfccbb 29
5c08dbbd 30command=
ecda0723 31branch=
d27b876b 32force=
d92a3959 33reference=
70c7ac22 34cached=
48bb3033
GP
35recursive=
36init=
0060fd15 37require_init=
1c244f6e 38files=
06b1abb5 39remote=
31ca3ac3 40nofetch=
32948425 41update=
15fc56a8 42prefix=
73b0898d 43custom_name=
275cd184 44depth=
72c5f883 45progress=
a0ef2934 46dissociate=
70c7ac22 47
be9d0a3a
HV
48die_if_unmatched ()
49{
50 if test "$1" = "#unmatched"
51 then
c4c02bf1 52 exit ${2:-1}
be9d0a3a
HV
53 fi
54}
55
88ce00c3
TK
56#
57# Print a submodule configuration setting
58#
59# $1 = submodule name
60# $2 = option name
61# $3 = default value
62#
63# Checks in the usual git-config places first (for overrides),
64# otherwise it falls back on .gitmodules. This allows you to
65# distribute project-wide defaults in .gitmodules, while still
66# customizing individual repositories if necessary. If the option is
67# not in .gitmodules either, print a default value.
68#
69get_submodule_config () {
70 name="$1"
71 option="$2"
72 default="$3"
73 value=$(git config submodule."$name"."$option")
74 if test -z "$value"
75 then
b2faad44 76 value=$(git submodule--helper config submodule."$name"."$option")
88ce00c3
TK
77 fi
78 printf '%s' "${value:-$default}"
79}
80
862ae6cd
RS
81isnumber()
82{
83 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
84}
85
dda63468 86# Given a full hex object ID, is this the zero OID?
87is_zero_oid () {
88 echo "$1" | sane_egrep '^0+$' >/dev/null 2>&1
89}
90
14111fc4
JK
91# Sanitize the local git environment for use within a submodule. We
92# can't simply use clear_local_git_env since we want to preserve some
93# of the settings from GIT_CONFIG_PARAMETERS.
94sanitize_submodule_env()
95{
89044baa 96 save_config=$GIT_CONFIG_PARAMETERS
14111fc4 97 clear_local_git_env
89044baa 98 GIT_CONFIG_PARAMETERS=$save_config
860cba61 99 export GIT_CONFIG_PARAMETERS
14111fc4
JK
100}
101
ecda0723
SV
102#
103# Add a new submodule to the working tree, .gitmodules and the index
104#
ec05df35 105# $@ = repo path
ecda0723
SV
106#
107# optional branch is stored in global branch variable
108#
23a485e3 109cmd_add()
ecda0723 110{
5c08dbbd 111 # parse $args after "submodule ... add".
091a6eb0 112 reference_path=
5c08dbbd
JH
113 while test $# -ne 0
114 do
115 case "$1" in
116 -b | --branch)
117 case "$2" in '') usage ;; esac
118 branch=$2
119 shift
120 ;;
d27b876b
JL
121 -f | --force)
122 force=$1
123 ;;
5c08dbbd 124 -q|--quiet)
2e6a30ef 125 GIT_QUIET=1
5c08dbbd 126 ;;
6d33e1c2
CF
127 --progress)
128 progress=1
129 ;;
d92a3959
MT
130 --reference)
131 case "$2" in '') usage ;; esac
091a6eb0 132 reference_path=$2
d92a3959
MT
133 shift
134 ;;
135 --reference=*)
091a6eb0 136 reference_path="${1#--reference=}"
d92a3959 137 ;;
a0ef2934
CF
138 --dissociate)
139 dissociate=1
140 ;;
73b0898d
JL
141 --name)
142 case "$2" in '') usage ;; esac
143 custom_name=$2
144 shift
145 ;;
275cd184
FG
146 --depth)
147 case "$2" in '') usage ;; esac
148 depth="--depth=$2"
149 shift
150 ;;
151 --depth=*)
152 depth=$1
153 ;;
5c08dbbd
JH
154 --)
155 shift
156 break
157 ;;
158 -*)
159 usage
160 ;;
161 *)
162 break
163 ;;
164 esac
165 shift
166 done
167
76e9bdc4
AO
168 if ! git submodule--helper config --check-writeable >/dev/null 2>&1
169 then
170 die "$(eval_gettext "please make sure that the .gitmodules file is in the working tree")"
171 fi
172
091a6eb0
JK
173 if test -n "$reference_path"
174 then
175 is_absolute_path "$reference_path" ||
176 reference_path="$wt_prefix$reference_path"
177
178 reference="--reference=$reference_path"
179 fi
180
ecda0723 181 repo=$1
64394e3a 182 sm_path=$2
ecda0723 183
64394e3a 184 if test -z "$sm_path"; then
6a066230 185 sm_path=$(printf '%s\n' "$repo" |
1414e578
JL
186 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
187 fi
188
496eeeb1 189 if test -z "$repo" || test -z "$sm_path"; then
ecda0723
SV
190 usage
191 fi
192
091a6eb0
JK
193 is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
194
ec05df35
ML
195 # assure repo is absolute or relative to parent
196 case "$repo" in
197 ./*|../*)
091a6eb0
JK
198 test -z "$wt_prefix" ||
199 die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
200
ec05df35 201 # dereference source url relative to parent's url
63e95beb 202 realrepo=$(git submodule--helper resolve-relative-url "$repo") || exit
ec05df35
ML
203 ;;
204 *:*|/*)
205 # absolute url
206 realrepo=$repo
207 ;;
208 *)
497ee872 209 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
ec05df35
ML
210 ;;
211 esac
212
db75ada5
MG
213 # normalize path:
214 # multiple //; leading ./; /./; /../; trailing /
64394e3a 215 sm_path=$(printf '%s/\n' "$sm_path" |
db75ada5
MG
216 sed -e '
217 s|//*|/|g
218 s|^\(\./\)*||
8196e728 219 s|/\(\./\)*|/|g
db75ada5
MG
220 :start
221 s|\([^/]*\)/\.\./||
222 tstart
223 s|/*$||
224 ')
619acfc7
SB
225 if test -z "$force"
226 then
227 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
228 die "$(eval_gettext "'\$sm_path' already exists in the index")"
229 else
230 git ls-files -s "$sm_path" | sane_grep -v "^160000" > /dev/null 2>&1 &&
231 die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
232 fi
ecda0723 233
53213994
JK
234 if test -z "$force" &&
235 ! git add --dry-run --ignore-missing --no-warn-embedded-repo "$sm_path" > /dev/null 2>&1
d27b876b 236 then
6ff875c5 237 eval_gettextln "The following path is ignored by one of your .gitignore files:
64394e3a 238\$sm_path
6ff875c5 239Use -f if you really want to add it." >&2
d27b876b
JL
240 exit 1
241 fi
242
73b0898d
JL
243 if test -n "$custom_name"
244 then
245 sm_name="$custom_name"
246 else
247 sm_name="$sm_path"
248 fi
249
0383bbb9
JK
250 if ! git submodule--helper check-name "$sm_name"
251 then
252 die "$(eval_gettext "'$sm_name' is not a valid submodule name")"
253 fi
254
d4264ca3 255 # perhaps the path exists and is already a git repo, else clone it
64394e3a 256 if test -e "$sm_path"
d4264ca3 257 then
496eeeb1 258 if test -d "$sm_path"/.git || test -f "$sm_path"/.git
d4264ca3 259 then
64394e3a 260 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
d4264ca3 261 else
64394e3a 262 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
d4264ca3 263 fi
c2f93917 264
d4264ca3 265 else
4b7c286e
JL
266 if test -d ".git/modules/$sm_name"
267 then
268 if test -z "$force"
269 then
0d71dbfd 270 eval_gettextln >&2 "A git directory for '\$sm_name' is found locally with remote(s):"
4b7c286e 271 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
0d71dbfd
VA
272 die "$(eval_gettextln "\
273If you want to reuse this local git directory instead of cloning again from
274 \$realrepo
275use the '--force' option. If the local git directory is not the correct repo
276or you are unsure what this means choose another name with the '--name' option.")"
4b7c286e 277 else
0d71dbfd 278 eval_gettextln "Reactivating local git directory for submodule '\$sm_name'."
4b7c286e
JL
279 fi
280 fi
a0ef2934 281 git submodule--helper clone ${GIT_QUIET:+--quiet} ${progress:+"--progress"} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${dissociate:+"--dissociate"} ${depth:+"$depth"} || exit
d851ffb9 282 (
14111fc4 283 sanitize_submodule_env
d851ffb9
JH
284 cd "$sm_path" &&
285 # ash fails to wordsplit ${branch:+-b "$branch"...}
286 case "$branch" in
287 '') git checkout -f -q ;;
288 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
289 esac
290 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
d4264ca3 291 fi
73b0898d 292 git config submodule."$sm_name".url "$realrepo"
d4264ca3 293
53213994 294 git add --no-warn-embedded-repo $force "$sm_path" ||
64394e3a 295 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
ecda0723 296
b2faad44
AO
297 git submodule--helper config submodule."$sm_name".path "$sm_path" &&
298 git submodule--helper config submodule."$sm_name".url "$repo" &&
b9289227
TK
299 if test -n "$branch"
300 then
b2faad44 301 git submodule--helper config submodule."$sm_name".branch "$branch"
b9289227 302 fi &&
31991b02 303 git add --force .gitmodules ||
64394e3a 304 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
1b614c07
BW
305
306 # NEEDSWORK: In a multi-working-tree world, this needs to be
307 # set in the per-worktree config.
308 if git config --get submodule.active >/dev/null
309 then
310 # If the submodule being adding isn't already covered by the
311 # current configured pathspec, set the submodule's active flag
312 if ! git submodule--helper is-active "$sm_path"
313 then
314 git config submodule."$sm_name".active "true"
315 fi
316 else
317 git config submodule."$sm_name".active "true"
318 fi
ecda0723
SV
319}
320
19a31f9c
ML
321#
322# Execute an arbitrary command sequence in each checked out
323# submodule
324#
325# $@ = command to execute
326#
327cmd_foreach()
328{
1d5bec8b
JH
329 # parse $args after "submodule ... foreach".
330 while test $# -ne 0
331 do
332 case "$1" in
333 -q|--quiet)
334 GIT_QUIET=1
335 ;;
15fc56a8
JH
336 --recursive)
337 recursive=1
338 ;;
1d5bec8b
JH
339 -*)
340 usage
341 ;;
342 *)
343 break
344 ;;
345 esac
346 shift
347 done
348
fc1b9243 349 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} "$@"
19a31f9c
ML
350}
351
70c7ac22 352#
211b7f19 353# Register submodules in .git/config
70c7ac22
LH
354#
355# $@ = requested paths (default to all)
356#
23a485e3 357cmd_init()
70c7ac22 358{
5c08dbbd
JH
359 # parse $args after "submodule ... init".
360 while test $# -ne 0
361 do
362 case "$1" in
363 -q|--quiet)
2e6a30ef 364 GIT_QUIET=1
5c08dbbd
JH
365 ;;
366 --)
367 shift
368 break
369 ;;
370 -*)
371 usage
372 ;;
373 *)
374 break
375 ;;
376 esac
377 shift
378 done
379
6e7c14e6 380 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} "$@"
70c7ac22
LH
381}
382
cf419828
JL
383#
384# Unregister submodules from .git/config and remove their work tree
385#
cf419828
JL
386cmd_deinit()
387{
388 # parse $args after "submodule ... deinit".
f6a52799 389 deinit_all=
cf419828
JL
390 while test $# -ne 0
391 do
392 case "$1" in
393 -f|--force)
394 force=$1
395 ;;
396 -q|--quiet)
397 GIT_QUIET=1
398 ;;
f6a52799
SB
399 --all)
400 deinit_all=t
401 ;;
cf419828
JL
402 --)
403 shift
404 break
405 ;;
406 -*)
407 usage
408 ;;
409 *)
410 break
411 ;;
412 esac
413 shift
414 done
415
2e612731 416 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} ${force:+--force} ${deinit_all:+--all} "$@"
cf419828
JL
417}
418
fb43e31f 419is_tip_reachable () (
01e1d544 420 sanitize_submodule_env &&
fb43e31f
SB
421 cd "$1" &&
422 rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
423 test -z "$rev"
424)
425
426fetch_in_submodule () (
01e1d544 427 sanitize_submodule_env &&
fb43e31f
SB
428 cd "$1" &&
429 case "$2" in
430 '')
431 git fetch ;;
432 *)
6cbf454a
SB
433 shift
434 git fetch $(get_default_remote) "$@" ;;
fb43e31f
SB
435 esac
436)
437
70c7ac22 438#
211b7f19 439# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
440#
441# $@ = requested paths (default to all)
442#
23a485e3 443cmd_update()
70c7ac22 444{
5c08dbbd
JH
445 # parse $args after "submodule ... update".
446 while test $# -ne 0
447 do
448 case "$1" in
449 -q|--quiet)
2e6a30ef 450 GIT_QUIET=1
5c08dbbd 451 ;;
e84c3cf3
SB
452 -v)
453 GIT_QUIET=0
454 ;;
72c5f883 455 --progress)
c7199e3a 456 progress=1
72c5f883 457 ;;
be4d2c83 458 -i|--init)
d92a3959 459 init=1
be4d2c83 460 ;;
0060fd15
JS
461 --require-init)
462 init=1
463 require_init=1
464 ;;
06b1abb5
TK
465 --remote)
466 remote=1
467 ;;
31ca3ac3 468 -N|--no-fetch)
31ca3ac3
FF
469 nofetch=1
470 ;;
9db31bdf
NMC
471 -f|--force)
472 force=$1
473 ;;
ca2cedba 474 -r|--rebase)
32948425 475 update="rebase"
ca2cedba 476 ;;
d92a3959
MT
477 --reference)
478 case "$2" in '') usage ;; esac
479 reference="--reference=$2"
98dbe63d 480 shift
d92a3959
MT
481 ;;
482 --reference=*)
483 reference="$1"
d92a3959 484 ;;
a0ef2934
CF
485 --dissociate)
486 dissociate=1
487 ;;
42b49178 488 -m|--merge)
42b49178
JH
489 update="merge"
490 ;;
b13fd5c1 491 --recursive)
b13fd5c1
JH
492 recursive=1
493 ;;
efc5fb6a
JH
494 --checkout)
495 update="checkout"
496 ;;
abed000a
SB
497 --recommend-shallow)
498 recommend_shallow="--recommend-shallow"
499 ;;
500 --no-recommend-shallow)
501 recommend_shallow="--no-recommend-shallow"
502 ;;
275cd184
FG
503 --depth)
504 case "$2" in '') usage ;; esac
505 depth="--depth=$2"
506 shift
507 ;;
508 --depth=*)
509 depth=$1
510 ;;
2335b870
SB
511 -j|--jobs)
512 case "$2" in '') usage ;; esac
513 jobs="--jobs=$2"
514 shift
515 ;;
516 --jobs=*)
517 jobs=$1
518 ;;
5c08dbbd
JH
519 --)
520 shift
521 break
522 ;;
523 -*)
524 usage
525 ;;
526 *)
527 break
528 ;;
529 esac
98dbe63d 530 shift
5c08dbbd
JH
531 done
532
d92a3959
MT
533 if test -n "$init"
534 then
535 cmd_init "--" "$@" || return
536 fi
537
48308681
SB
538 {
539 git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
c7199e3a 540 ${progress:+"--progress"} \
48308681
SB
541 ${wt_prefix:+--prefix "$wt_prefix"} \
542 ${prefix:+--recursive-prefix "$prefix"} \
543 ${update:+--update "$update"} \
5f50f33e 544 ${reference:+"$reference"} \
a0ef2934 545 ${dissociate:+"--dissociate"} \
48308681 546 ${depth:+--depth "$depth"} \
0060fd15 547 ${require_init:+--require-init} \
c7199e3a
CF
548 $recommend_shallow \
549 $jobs \
c4c02bf1 550 "$@" || echo "#unmatched" $?
48308681 551 } | {
15ffb7cd 552 err=
9eca701f 553 while read -r quickabort sha1 just_cloned sm_path
70c7ac22 554 do
9eca701f 555 die_if_unmatched "$quickabort" "$sha1"
48308681 556
5d124f41 557 git submodule--helper ensure-core-worktree "$sm_path" || exit 1
74d4731d 558
ee69b2a9 559 update_module=$(git submodule--helper update-module-mode $just_cloned "$sm_path" $update)
efc5fb6a 560
44431df0 561 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
091a6eb0 562
48308681 563 if test $just_cloned -eq 1
d851ffb9 564 then
bf2d8246
LH
565 subsha1=
566 else
14111fc4 567 subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
5be60078 568 git rev-parse --verify HEAD) ||
091a6eb0 569 die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
70c7ac22 570 fi
211b7f19 571
06b1abb5
TK
572 if test -n "$remote"
573 then
92bbe7cc 574 branch=$(git submodule--helper remote-branch "$sm_path")
06b1abb5
TK
575 if test -z "$nofetch"
576 then
577 # Fetch remote before determining tracking $sha1
6cbf454a 578 fetch_in_submodule "$sm_path" $depth ||
06b1abb5
TK
579 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
580 fi
14111fc4
JK
581 remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote)
582 sha1=$(sanitize_submodule_env; cd "$sm_path" &&
06b1abb5 583 git rev-parse --verify "${remote_name}/${branch}") ||
c87302bf 584 die "$(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")"
06b1abb5
TK
585 fi
586
496eeeb1 587 if test "$subsha1" != "$sha1" || test -n "$force"
70c7ac22 588 then
9db31bdf
NMC
589 subforce=$force
590 # If we don't already have a -f flag and the submodule has never been checked out
496eeeb1 591 if test -z "$subsha1" && test -z "$force"
b9b378a0 592 then
9db31bdf 593 subforce="-f"
b9b378a0 594 fi
31ca3ac3
FF
595
596 if test -z "$nofetch"
597 then
e5f522d6
JL
598 # Run fetch only if $sha1 isn't present or it
599 # is not reachable from a ref.
fb43e31f 600 is_tip_reachable "$sm_path" "$sha1" ||
6cbf454a 601 fetch_in_submodule "$sm_path" $depth ||
e30d8336 602 say "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
fb43e31f
SB
603
604 # Now we tried the usual fetch, but $sha1 may
605 # not be reachable from any of the refs
606 is_tip_reachable "$sm_path" "$sha1" ||
6cbf454a 607 fetch_in_submodule "$sm_path" $depth "$sha1" ||
c87302bf 608 die "$(eval_gettext "Fetched in submodule path '\$displaypath', but it did not contain \$sha1. Direct fetching of that commit failed.")"
31ca3ac3
FF
609 fi
610
877449c1 611 must_die_on_failure=
32948425 612 case "$update_module" in
a2aed08b
TK
613 checkout)
614 command="git checkout $subforce -q"
615 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
616 say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
617 ;;
32948425
JH
618 rebase)
619 command="git rebase"
091a6eb0
JK
620 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
621 say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
877449c1 622 must_die_on_failure=yes
32948425 623 ;;
42b49178
JH
624 merge)
625 command="git merge"
091a6eb0
JK
626 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
627 say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
877449c1 628 must_die_on_failure=yes
42b49178 629 ;;
6cb5728c
CP
630 !*)
631 command="${update_module#!}"
b08238ac
SB
632 die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$displaypath'")"
633 say_msg="$(eval_gettext "Submodule path '\$displaypath': '\$command \$sha1'")"
6cb5728c
CP
634 must_die_on_failure=yes
635 ;;
32948425 636 *)
ff03d930 637 die "$(eval_gettext "Invalid update mode '$update_module' for submodule path '$path'")"
32948425 638 esac
70c7ac22 639
14111fc4 640 if (sanitize_submodule_env; cd "$sm_path" && $command "$sha1")
15ffb7cd 641 then
ff968f03 642 say "$say_msg"
877449c1
JH
643 elif test -n "$must_die_on_failure"
644 then
ff968f03 645 die_with_status 2 "$die_msg"
15ffb7cd 646 else
ff968f03 647 err="${err};$die_msg"
877449c1 648 continue
15ffb7cd 649 fi
70c7ac22 650 fi
b13fd5c1
JH
651
652 if test -n "$recursive"
653 then
75bf5e60 654 (
44431df0 655 prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
3604242f 656 wt_prefix=
14111fc4 657 sanitize_submodule_env
75bf5e60 658 cd "$sm_path" &&
36141282 659 eval cmd_update
75bf5e60 660 )
15ffb7cd
FG
661 res=$?
662 if test $res -gt 0
663 then
091a6eb0 664 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
bb9d91b4 665 if test $res -ne 2
15ffb7cd 666 then
ff968f03 667 err="${err};$die_msg"
15ffb7cd
FG
668 continue
669 else
ff968f03 670 die_with_status $res "$die_msg"
15ffb7cd
FG
671 fi
672 fi
b13fd5c1 673 fi
70c7ac22 674 done
15ffb7cd
FG
675
676 if test -n "$err"
677 then
678 OIFS=$IFS
679 IFS=';'
680 for e in $err
681 do
682 if test -n "$e"
683 then
684 echo >&2 "$e"
685 fi
686 done
687 IFS=$OIFS
688 exit 1
689 fi
690 }
70c7ac22
LH
691}
692
28f9af5d
PY
693#
694# Show commit summary for submodules in index or working tree
695#
696# If '--cached' is given, show summary between index and given commit,
697# or between working tree and given commit
698#
699# $@ = [commit (default 'HEAD'),] requested paths (default all)
700#
701cmd_summary() {
f2dc06a3 702 summary_limit=-1
d0f64dd4 703 for_status=
1c244f6e 704 diff_cmd=diff-index
f2dc06a3 705
28f9af5d
PY
706 # parse $args after "submodule ... summary".
707 while test $# -ne 0
708 do
709 case "$1" in
710 --cached)
711 cached="$1"
712 ;;
1c244f6e
JL
713 --files)
714 files="$1"
715 ;;
d0f64dd4
PY
716 --for-status)
717 for_status="$1"
718 ;;
f2dc06a3 719 -n|--summary-limit)
862ae6cd
RS
720 summary_limit="$2"
721 isnumber "$summary_limit" || usage
f2dc06a3
PY
722 shift
723 ;;
862ae6cd
RS
724 --summary-limit=*)
725 summary_limit="${1#--summary-limit=}"
726 isnumber "$summary_limit" || usage
727 ;;
28f9af5d
PY
728 --)
729 shift
730 break
731 ;;
732 -*)
733 usage
734 ;;
735 *)
736 break
737 ;;
738 esac
739 shift
740 done
bffe71f4 741
f2dc06a3
PY
742 test $summary_limit = 0 && return
743
3deea89c 744 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
28f9af5d
PY
745 then
746 head=$rev
caa9c3ca 747 test $# = 0 || shift
496eeeb1 748 elif test -z "$1" || test "$1" = "HEAD"
3deea89c 749 then
14e940d7
JH
750 # before the first commit: compare with an empty tree
751 head=$(git hash-object -w -t tree --stdin </dev/null)
2ea6c2c9 752 test -z "$1" || shift
28f9af5d 753 else
3deea89c 754 head="HEAD"
28f9af5d
PY
755 fi
756
1c244f6e
JL
757 if [ -n "$files" ]
758 then
759 test -n "$cached" &&
465d6a00 760 die "$(gettext "The --cached option cannot be used with the --files option")"
1c244f6e
JL
761 diff_cmd=diff-files
762 head=
763 fi
764
28f9af5d 765 cd_to_toplevel
091a6eb0 766 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
28f9af5d 767 # Get modified modules cared by user
18076502 768 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
e1622bfc 769 sane_egrep '^:([0-7]* )?160000' |
cf9e55f4 770 while read -r mod_src mod_dst sha1_src sha1_dst status sm_path
28f9af5d
PY
771 do
772 # Always show modules deleted or type-changed (blob<->module)
496eeeb1
EP
773 if test "$status" = D || test "$status" = T
774 then
6a066230 775 printf '%s\n' "$sm_path"
496eeeb1
EP
776 continue
777 fi
927b26f8 778 # Respect the ignore setting for --for-status.
779 if test -n "$for_status"
780 then
0ea306ef 781 name=$(git submodule--helper name "$sm_path")
927b26f8 782 ignore_config=$(get_submodule_config "$name" ignore none)
496eeeb1 783 test $status != A && test $ignore_config = all && continue
927b26f8 784 fi
28f9af5d 785 # Also show added or modified modules which are checked out
974ce807 786 GIT_DIR="$sm_path/.git" git rev-parse --git-dir >/dev/null 2>&1 &&
6a066230 787 printf '%s\n' "$sm_path"
28f9af5d
PY
788 done
789 )
1cb639e6 790
d0f64dd4
PY
791 test -z "$modules" && return
792
18076502 793 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
e1622bfc 794 sane_egrep '^:([0-7]* )?160000' |
1cb639e6 795 cut -c2- |
cf9e55f4 796 while read -r mod_src mod_dst sha1_src sha1_dst status name
1cb639e6
PY
797 do
798 if test -z "$cached" &&
dda63468 799 is_zero_oid $sha1_dst
1cb639e6
PY
800 then
801 case "$mod_dst" in
802 160000)
803 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
804 ;;
805 100644 | 100755 | 120000)
806 sha1_dst=$(git hash-object $name)
807 ;;
808 000000)
809 ;; # removed
810 *)
811 # unexpected type
6ff875c5 812 eval_gettextln "unexpected mode \$mod_dst" >&2
1cb639e6
PY
813 continue ;;
814 esac
815 fi
816 missing_src=
817 missing_dst=
818
819 test $mod_src = 160000 &&
974ce807 820 ! GIT_DIR="$name/.git" git rev-parse -q --verify $sha1_src^0 >/dev/null &&
1cb639e6
PY
821 missing_src=t
822
823 test $mod_dst = 160000 &&
974ce807 824 ! GIT_DIR="$name/.git" git rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1cb639e6 825 missing_dst=t
bffe71f4 826
44431df0 827 display_name=$(git submodule--helper relative-path "$name" "$wt_prefix")
091a6eb0 828
1cb639e6
PY
829 total_commits=
830 case "$missing_src,$missing_dst" in
831 t,)
091a6eb0 832 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_src")"
1cb639e6
PY
833 ;;
834 ,t)
091a6eb0 835 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_dst")"
1cb639e6
PY
836 ;;
837 t,t)
091a6eb0 838 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
1cb639e6
PY
839 ;;
840 *)
841 errmsg=
842 total_commits=$(
496eeeb1 843 if test $mod_src = 160000 && test $mod_dst = 160000
1cb639e6
PY
844 then
845 range="$sha1_src...$sha1_dst"
846 elif test $mod_src = 160000
847 then
848 range=$sha1_src
849 else
850 range=$sha1_dst
851 fi
852 GIT_DIR="$name/.git" \
b0e621ad 853 git rev-list --first-parent $range -- | wc -l
1cb639e6 854 )
eed35595 855 total_commits=" ($(($total_commits + 0)))"
1cb639e6
PY
856 ;;
857 esac
858
0586a438
SH
859 sha1_abbr_src=$(GIT_DIR="$name/.git" git rev-parse --short $sha1_src 2>/dev/null ||
860 echo $sha1_src | cut -c1-7)
861 sha1_abbr_dst=$(GIT_DIR="$name/.git" git rev-parse --short $sha1_dst 2>/dev/null ||
862 echo $sha1_dst | cut -c1-7)
863
1cb639e6
PY
864 if test $status = T
865 then
b3e73449
ÆAB
866 blob="$(gettext "blob")"
867 submodule="$(gettext "submodule")"
1cb639e6
PY
868 if test $mod_dst = 160000
869 then
091a6eb0 870 echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1cb639e6 871 else
091a6eb0 872 echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1cb639e6
PY
873 fi
874 else
091a6eb0 875 echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1cb639e6
PY
876 fi
877 if test -n "$errmsg"
878 then
879 # Don't give error msg for modification whose dst is not submodule
880 # i.e. deleted or changed to blob
881 test $mod_dst = 160000 && echo "$errmsg"
882 else
496eeeb1 883 if test $mod_src = 160000 && test $mod_dst = 160000
1cb639e6 884 then
f2dc06a3
PY
885 limit=
886 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e6 887 GIT_DIR="$name/.git" \
f2dc06a3 888 git log $limit --pretty='format: %m %s' \
1cb639e6
PY
889 --first-parent $sha1_src...$sha1_dst
890 elif test $mod_dst = 160000
891 then
892 GIT_DIR="$name/.git" \
893 git log --pretty='format: > %s' -1 $sha1_dst
894 else
895 GIT_DIR="$name/.git" \
896 git log --pretty='format: < %s' -1 $sha1_src
897 fi
898 echo
899 fi
900 echo
3ba7407b 901 done
28f9af5d 902}
70c7ac22 903#
941987a5 904# List all submodules, prefixed with:
70c7ac22
LH
905# - submodule not initialized
906# + different revision checked out
907#
908# If --cached was specified the revision in the index will be printed
909# instead of the currently checked out revision.
910#
911# $@ = requested paths (default to all)
912#
23a485e3 913cmd_status()
70c7ac22 914{
5c08dbbd
JH
915 # parse $args after "submodule ... status".
916 while test $# -ne 0
917 do
918 case "$1" in
919 -q|--quiet)
2e6a30ef 920 GIT_QUIET=1
5c08dbbd
JH
921 ;;
922 --cached)
923 cached=1
924 ;;
64b19ffe
JH
925 --recursive)
926 recursive=1
927 ;;
5c08dbbd
JH
928 --)
929 shift
930 break
931 ;;
932 -*)
933 usage
934 ;;
935 *)
936 break
937 ;;
938 esac
939 shift
940 done
941
a9f8a375 942 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} "$@"
70c7ac22 943}
2327f61e
DA
944#
945# Sync remote urls for submodules
946# This makes the value for remote.$remote.url match the value
947# specified in .gitmodules.
948#
949cmd_sync()
950{
951 while test $# -ne 0
952 do
953 case "$1" in
954 -q|--quiet)
2e6a30ef 955 GIT_QUIET=1
2327f61e
DA
956 shift
957 ;;
82f49f29
PH
958 --recursive)
959 recursive=1
960 shift
961 ;;
2327f61e
DA
962 --)
963 shift
964 break
965 ;;
966 -*)
967 usage
968 ;;
969 *)
970 break
971 ;;
972 esac
973 done
e7849a96 974
13424764 975 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} "$@"
2327f61e 976}
70c7ac22 977
f6f85861
SB
978cmd_absorbgitdirs()
979{
980 git submodule--helper absorb-git-dirs --prefix "$wt_prefix" "$@"
981}
982
5c08dbbd
JH
983# This loop parses the command line arguments to find the
984# subcommand name to dispatch. Parsing of the subcommand specific
985# options are primarily done by the subcommand implementations.
986# Subcommand specific options such as --branch and --cached are
987# parsed here as well, for backward compatibility.
988
989while test $# != 0 && test -z "$command"
70c7ac22
LH
990do
991 case "$1" in
f6f85861 992 add | foreach | init | deinit | update | status | summary | sync | absorbgitdirs)
5c08dbbd 993 command=$1
70c7ac22
LH
994 ;;
995 -q|--quiet)
2e6a30ef 996 GIT_QUIET=1
70c7ac22 997 ;;
ecda0723
SV
998 -b|--branch)
999 case "$2" in
1000 '')
1001 usage
1002 ;;
1003 esac
1004 branch="$2"; shift
1005 ;;
70c7ac22 1006 --cached)
28f9af5d 1007 cached="$1"
70c7ac22
LH
1008 ;;
1009 --)
1010 break
1011 ;;
1012 -*)
1013 usage
1014 ;;
1015 *)
1016 break
1017 ;;
1018 esac
1019 shift
1020done
1021
5c08dbbd 1022# No command word defaults to "status"
af9c9f97
RR
1023if test -z "$command"
1024then
1025 if test $# = 0
1026 then
1027 command=status
1028 else
1029 usage
1030 fi
1031fi
5c08dbbd
JH
1032
1033# "-b branch" is accepted only by "add"
1034if test -n "$branch" && test "$command" != add
1035then
ecda0723 1036 usage
5c08dbbd
JH
1037fi
1038
28f9af5d 1039# "--cached" is accepted only by "status" and "summary"
496eeeb1 1040if test -n "$cached" && test "$command" != status && test "$command" != summary
5c08dbbd 1041then
70c7ac22 1042 usage
5c08dbbd
JH
1043fi
1044
1045"cmd_$command" "$@"