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