]> git.ipfire.org Git - thirdparty/git.git/blame - git-submodule.sh
builtin/submodule--helper.c: rename option struct to "opt"
[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/-/ /')
68cabbfd
DL
8USAGE="[--quiet] [--cached]
9 or: $dashless [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
64b19ffe 10 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
1d5bec8b 11 or: $dashless [--quiet] init [--] [<path>...]
f6a52799 12 or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
f05da2b4 13 or: $dashless [--quiet] update [--init [--filter=<filter-spec>]] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--[no-]single-branch] [--] [<path>...]
b57e8119 14 or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>
26b06100 15 or: $dashless [--quiet] set-url [--] <path> <newurl>
adc54235 16 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
15fc56a8 17 or: $dashless [--quiet] foreach [--recursive] <command>
c32eaa8a
SB
18 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
19 or: $dashless [--quiet] absorbgitdirs [--] [<path>...]"
8f321a39 20OPTIONS_SPEC=
091a6eb0 21SUBDIRECTORY_OK=Yes
70c7ac22
LH
22. git-sh-setup
23require_work_tree
091a6eb0
JK
24wt_prefix=$(git rev-parse --show-prefix)
25cd_to_toplevel
70c7ac22 26
f1762d77
BW
27# Tell the rest of git that any URLs we get don't come
28# directly from the user, so it can apply policy as appropriate.
29GIT_PROTOCOL_FROM_USER=0
30export GIT_PROTOCOL_FROM_USER
33cfccbb 31
5c08dbbd 32command=
ecda0723 33branch=
d27b876b 34force=
d92a3959 35reference=
70c7ac22 36cached=
48bb3033
GP
37recursive=
38init=
0060fd15 39require_init=
1c244f6e 40files=
06b1abb5 41remote=
31ca3ac3 42nofetch=
32948425 43update=
15fc56a8 44prefix=
73b0898d 45custom_name=
275cd184 46depth=
72c5f883 47progress=
a0ef2934 48dissociate=
132f600b 49single_branch=
65d100c4
LX
50jobs=
51recommend_shallow=
f05da2b4 52filter=
70c7ac22 53
be9d0a3a
HV
54die_if_unmatched ()
55{
56 if test "$1" = "#unmatched"
57 then
c4c02bf1 58 exit ${2:-1}
be9d0a3a
HV
59 fi
60}
61
862ae6cd
RS
62isnumber()
63{
64 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
65}
66
14111fc4
JK
67# Sanitize the local git environment for use within a submodule. We
68# can't simply use clear_local_git_env since we want to preserve some
69# of the settings from GIT_CONFIG_PARAMETERS.
70sanitize_submodule_env()
71{
89044baa 72 save_config=$GIT_CONFIG_PARAMETERS
14111fc4 73 clear_local_git_env
89044baa 74 GIT_CONFIG_PARAMETERS=$save_config
860cba61 75 export GIT_CONFIG_PARAMETERS
14111fc4
JK
76}
77
ecda0723
SV
78#
79# Add a new submodule to the working tree, .gitmodules and the index
80#
ec05df35 81# $@ = repo path
ecda0723
SV
82#
83# optional branch is stored in global branch variable
84#
23a485e3 85cmd_add()
ecda0723 86{
5c08dbbd 87 # parse $args after "submodule ... add".
091a6eb0 88 reference_path=
5c08dbbd
JH
89 while test $# -ne 0
90 do
91 case "$1" in
92 -b | --branch)
93 case "$2" in '') usage ;; esac
94 branch=$2
95 shift
96 ;;
d27b876b
JL
97 -f | --force)
98 force=$1
99 ;;
5c08dbbd 100 -q|--quiet)
2e6a30ef 101 GIT_QUIET=1
5c08dbbd 102 ;;
6d33e1c2
CF
103 --progress)
104 progress=1
105 ;;
d92a3959
MT
106 --reference)
107 case "$2" in '') usage ;; esac
091a6eb0 108 reference_path=$2
d92a3959
MT
109 shift
110 ;;
111 --reference=*)
091a6eb0 112 reference_path="${1#--reference=}"
d92a3959 113 ;;
a0ef2934
CF
114 --dissociate)
115 dissociate=1
116 ;;
73b0898d
JL
117 --name)
118 case "$2" in '') usage ;; esac
119 custom_name=$2
120 shift
121 ;;
275cd184
FG
122 --depth)
123 case "$2" in '') usage ;; esac
124 depth="--depth=$2"
125 shift
126 ;;
127 --depth=*)
128 depth=$1
129 ;;
5c08dbbd
JH
130 --)
131 shift
132 break
133 ;;
134 -*)
135 usage
136 ;;
137 *)
138 break
139 ;;
140 esac
141 shift
142 done
143
a6226fd7 144 if test -z "$1"
76e9bdc4 145 then
ecda0723
SV
146 usage
147 fi
148
a6226fd7 149 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper add ${GIT_QUIET:+--quiet} ${force:+--force} ${progress:+"--progress"} ${branch:+--branch "$branch"} ${reference_path:+--reference "$reference_path"} ${dissociate:+--dissociate} ${custom_name:+--name "$custom_name"} ${depth:+"$depth"} -- "$@"
ecda0723
SV
150}
151
19a31f9c
ML
152#
153# Execute an arbitrary command sequence in each checked out
154# submodule
155#
156# $@ = command to execute
157#
158cmd_foreach()
159{
1d5bec8b
JH
160 # parse $args after "submodule ... foreach".
161 while test $# -ne 0
162 do
163 case "$1" in
164 -q|--quiet)
165 GIT_QUIET=1
166 ;;
15fc56a8
JH
167 --recursive)
168 recursive=1
169 ;;
1d5bec8b
JH
170 -*)
171 usage
172 ;;
173 *)
174 break
175 ;;
176 esac
177 shift
178 done
179
1cf823d8 180 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
19a31f9c
ML
181}
182
70c7ac22 183#
211b7f19 184# Register submodules in .git/config
70c7ac22
LH
185#
186# $@ = requested paths (default to all)
187#
23a485e3 188cmd_init()
70c7ac22 189{
5c08dbbd
JH
190 # parse $args after "submodule ... init".
191 while test $# -ne 0
192 do
193 case "$1" in
194 -q|--quiet)
2e6a30ef 195 GIT_QUIET=1
5c08dbbd
JH
196 ;;
197 --)
198 shift
199 break
200 ;;
201 -*)
202 usage
203 ;;
204 *)
205 break
206 ;;
207 esac
208 shift
209 done
210
a282f5a9 211 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} -- "$@"
70c7ac22
LH
212}
213
cf419828
JL
214#
215# Unregister submodules from .git/config and remove their work tree
216#
cf419828
JL
217cmd_deinit()
218{
219 # parse $args after "submodule ... deinit".
f6a52799 220 deinit_all=
cf419828
JL
221 while test $# -ne 0
222 do
223 case "$1" in
224 -f|--force)
225 force=$1
226 ;;
227 -q|--quiet)
228 GIT_QUIET=1
229 ;;
f6a52799
SB
230 --all)
231 deinit_all=t
232 ;;
cf419828
JL
233 --)
234 shift
235 break
236 ;;
237 -*)
238 usage
239 ;;
240 *)
241 break
242 ;;
243 esac
244 shift
245 done
246
1cf823d8 247 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${force:+--force} ${deinit_all:+--all} -- "$@"
cf419828
JL
248}
249
70c7ac22 250#
211b7f19 251# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
252#
253# $@ = requested paths (default to all)
254#
23a485e3 255cmd_update()
70c7ac22 256{
5c08dbbd
JH
257 # parse $args after "submodule ... update".
258 while test $# -ne 0
259 do
260 case "$1" in
261 -q|--quiet)
2e6a30ef 262 GIT_QUIET=1
5c08dbbd 263 ;;
e84c3cf3 264 -v)
3ad0401e 265 unset GIT_QUIET
e84c3cf3 266 ;;
72c5f883 267 --progress)
c7199e3a 268 progress=1
72c5f883 269 ;;
be4d2c83 270 -i|--init)
d92a3959 271 init=1
be4d2c83 272 ;;
0060fd15
JS
273 --require-init)
274 init=1
275 require_init=1
276 ;;
06b1abb5
TK
277 --remote)
278 remote=1
279 ;;
31ca3ac3 280 -N|--no-fetch)
31ca3ac3
FF
281 nofetch=1
282 ;;
9db31bdf
NMC
283 -f|--force)
284 force=$1
285 ;;
ca2cedba 286 -r|--rebase)
32948425 287 update="rebase"
ca2cedba 288 ;;
d92a3959
MT
289 --reference)
290 case "$2" in '') usage ;; esac
291 reference="--reference=$2"
98dbe63d 292 shift
d92a3959
MT
293 ;;
294 --reference=*)
295 reference="$1"
d92a3959 296 ;;
a0ef2934
CF
297 --dissociate)
298 dissociate=1
299 ;;
42b49178 300 -m|--merge)
42b49178
JH
301 update="merge"
302 ;;
b13fd5c1 303 --recursive)
b13fd5c1
JH
304 recursive=1
305 ;;
efc5fb6a
JH
306 --checkout)
307 update="checkout"
308 ;;
abed000a
SB
309 --recommend-shallow)
310 recommend_shallow="--recommend-shallow"
311 ;;
312 --no-recommend-shallow)
313 recommend_shallow="--no-recommend-shallow"
314 ;;
275cd184
FG
315 --depth)
316 case "$2" in '') usage ;; esac
317 depth="--depth=$2"
318 shift
319 ;;
320 --depth=*)
321 depth=$1
322 ;;
2335b870
SB
323 -j|--jobs)
324 case "$2" in '') usage ;; esac
325 jobs="--jobs=$2"
326 shift
327 ;;
328 --jobs=*)
329 jobs=$1
330 ;;
132f600b
ES
331 --single-branch)
332 single_branch="--single-branch"
333 ;;
334 --no-single-branch)
335 single_branch="--no-single-branch"
336 ;;
f05da2b4
JS
337 --filter)
338 case "$2" in '') usage ;; esac
339 filter="--filter=$2"
340 shift
341 ;;
342 --filter=*)
343 filter="$1"
344 ;;
5c08dbbd
JH
345 --)
346 shift
347 break
348 ;;
349 -*)
350 usage
351 ;;
352 *)
353 break
354 ;;
355 esac
98dbe63d 356 shift
5c08dbbd
JH
357 done
358
48308681 359 {
29a5e9e1
GC
360 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper update-clone \
361 ${GIT_QUIET:+--quiet} \
c7199e3a 362 ${progress:+"--progress"} \
29a5e9e1 363 ${init:+--init} \
48308681
SB
364 ${wt_prefix:+--prefix "$wt_prefix"} \
365 ${prefix:+--recursive-prefix "$prefix"} \
366 ${update:+--update "$update"} \
5f50f33e 367 ${reference:+"$reference"} \
a0ef2934 368 ${dissociate:+"--dissociate"} \
48308681 369 ${depth:+--depth "$depth"} \
0060fd15 370 ${require_init:+--require-init} \
132f600b 371 $single_branch \
c7199e3a
CF
372 $recommend_shallow \
373 $jobs \
f05da2b4 374 $filter \
a282f5a9 375 -- \
c4c02bf1 376 "$@" || echo "#unmatched" $?
48308681 377 } | {
15ffb7cd 378 err=
9eca701f 379 while read -r quickabort sha1 just_cloned sm_path
70c7ac22 380 do
9eca701f 381 die_if_unmatched "$quickabort" "$sha1"
48308681 382
44431df0 383 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
091a6eb0 384
e4419665 385 if test $just_cloned -eq 0
d851ffb9 386 then
c51f8f94 387 just_cloned=
70c7ac22 388 fi
211b7f19 389
c51f8f94
AR
390 out=$(git submodule--helper run-update-procedure \
391 ${wt_prefix:+--prefix "$wt_prefix"} \
392 ${GIT_QUIET:+--quiet} \
393 ${force:+--force} \
394 ${just_cloned:+--just-cloned} \
395 ${nofetch:+--no-fetch} \
396 ${depth:+"$depth"} \
397 ${update:+--update "$update"} \
398 ${prefix:+--recursive-prefix "$prefix"} \
399 ${sha1:+--oid "$sha1"} \
1012a5cb 400 ${remote:+--remote} \
c51f8f94
AR
401 "--" \
402 "$sm_path")
403
404 # exit codes for run-update-procedure:
405 # 0: update was successful, say command output
406 # 1: update procedure failed, but should not die
55b3f12c 407 # 128: subcommand died during execution
c51f8f94
AR
408 # 3: no update procedure was run
409 res="$?"
410 case $res in
411 0)
412 say "$out"
413 ;;
414 1)
55b3f12c 415 err="${err};$out"
c51f8f94
AR
416 continue
417 ;;
55b3f12c
GC
418 128)
419 printf >&2 "$out"
420 exit $res
c51f8f94
AR
421 ;;
422 esac
b13fd5c1
JH
423
424 if test -n "$recursive"
425 then
75bf5e60 426 (
44431df0 427 prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
3604242f 428 wt_prefix=
14111fc4 429 sanitize_submodule_env
75bf5e60 430 cd "$sm_path" &&
36141282 431 eval cmd_update
75bf5e60 432 )
15ffb7cd
FG
433 res=$?
434 if test $res -gt 0
435 then
0008d122 436 die_msg="fatal: $(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
bb9d91b4 437 if test $res -ne 2
15ffb7cd 438 then
ff968f03 439 err="${err};$die_msg"
15ffb7cd
FG
440 continue
441 else
ff968f03 442 die_with_status $res "$die_msg"
15ffb7cd
FG
443 fi
444 fi
b13fd5c1 445 fi
70c7ac22 446 done
15ffb7cd
FG
447
448 if test -n "$err"
449 then
450 OIFS=$IFS
451 IFS=';'
452 for e in $err
453 do
454 if test -n "$e"
455 then
456 echo >&2 "$e"
457 fi
458 done
459 IFS=$OIFS
460 exit 1
461 fi
462 }
70c7ac22
LH
463}
464
b57e8119
DL
465#
466# Configures a submodule's default branch
467#
468# $@ = requested path
469#
470cmd_set_branch() {
2964d6e5 471 default=
b57e8119
DL
472 branch=
473
474 while test $# -ne 0
475 do
476 case "$1" in
477 -q|--quiet)
478 # we don't do anything with this but we need to accept it
479 ;;
480 -d|--default)
2964d6e5 481 default=1
b57e8119
DL
482 ;;
483 -b|--branch)
484 case "$2" in '') usage ;; esac
485 branch=$2
486 shift
487 ;;
488 --)
489 shift
490 break
491 ;;
492 -*)
493 usage
494 ;;
495 *)
496 break
497 ;;
498 esac
499 shift
500 done
501
1cf823d8 502 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch ${GIT_QUIET:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
b57e8119
DL
503}
504
26b06100
DL
505#
506# Configures a submodule's remote url
507#
508# $@ = requested path, requested url
509#
510cmd_set_url() {
511 while test $# -ne 0
512 do
513 case "$1" in
514 -q|--quiet)
515 GIT_QUIET=1
516 ;;
517 --)
518 shift
519 break
520 ;;
521 -*)
522 usage
523 ;;
524 *)
525 break
526 ;;
527 esac
528 shift
529 done
530
1cf823d8 531 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url ${GIT_QUIET:+--quiet} -- "$@"
26b06100
DL
532}
533
28f9af5d
PY
534#
535# Show commit summary for submodules in index or working tree
536#
537# If '--cached' is given, show summary between index and given commit,
538# or between working tree and given commit
539#
540# $@ = [commit (default 'HEAD'),] requested paths (default all)
541#
542cmd_summary() {
f2dc06a3 543 summary_limit=-1
d0f64dd4 544 for_status=
1c244f6e 545 diff_cmd=diff-index
f2dc06a3 546
28f9af5d
PY
547 # parse $args after "submodule ... summary".
548 while test $# -ne 0
549 do
550 case "$1" in
551 --cached)
552 cached="$1"
553 ;;
1c244f6e
JL
554 --files)
555 files="$1"
556 ;;
d0f64dd4
PY
557 --for-status)
558 for_status="$1"
559 ;;
f2dc06a3 560 -n|--summary-limit)
862ae6cd
RS
561 summary_limit="$2"
562 isnumber "$summary_limit" || usage
f2dc06a3
PY
563 shift
564 ;;
862ae6cd
RS
565 --summary-limit=*)
566 summary_limit="${1#--summary-limit=}"
567 isnumber "$summary_limit" || usage
568 ;;
28f9af5d
PY
569 --)
570 shift
571 break
572 ;;
573 -*)
574 usage
575 ;;
576 *)
577 break
578 ;;
579 esac
580 shift
581 done
bffe71f4 582
1cf823d8 583 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary ${files:+--files} ${cached:+--cached} ${for_status:+--for-status} ${summary_limit:+-n $summary_limit} -- "$@"
28f9af5d 584}
70c7ac22 585#
941987a5 586# List all submodules, prefixed with:
70c7ac22
LH
587# - submodule not initialized
588# + different revision checked out
589#
590# If --cached was specified the revision in the index will be printed
591# instead of the currently checked out revision.
592#
593# $@ = requested paths (default to all)
594#
23a485e3 595cmd_status()
70c7ac22 596{
5c08dbbd
JH
597 # parse $args after "submodule ... status".
598 while test $# -ne 0
599 do
600 case "$1" in
601 -q|--quiet)
2e6a30ef 602 GIT_QUIET=1
5c08dbbd
JH
603 ;;
604 --cached)
605 cached=1
606 ;;
64b19ffe
JH
607 --recursive)
608 recursive=1
609 ;;
5c08dbbd
JH
610 --)
611 shift
612 break
613 ;;
614 -*)
615 usage
616 ;;
617 *)
618 break
619 ;;
620 esac
621 shift
622 done
623
1cf823d8 624 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
70c7ac22 625}
2327f61e
DA
626#
627# Sync remote urls for submodules
628# This makes the value for remote.$remote.url match the value
629# specified in .gitmodules.
630#
631cmd_sync()
632{
633 while test $# -ne 0
634 do
635 case "$1" in
636 -q|--quiet)
2e6a30ef 637 GIT_QUIET=1
2327f61e
DA
638 shift
639 ;;
82f49f29
PH
640 --recursive)
641 recursive=1
642 shift
643 ;;
2327f61e
DA
644 --)
645 shift
646 break
647 ;;
648 -*)
649 usage
650 ;;
651 *)
652 break
653 ;;
654 esac
655 done
e7849a96 656
1cf823d8 657 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
2327f61e 658}
70c7ac22 659
f6f85861
SB
660cmd_absorbgitdirs()
661{
662 git submodule--helper absorb-git-dirs --prefix "$wt_prefix" "$@"
663}
664
5c08dbbd
JH
665# This loop parses the command line arguments to find the
666# subcommand name to dispatch. Parsing of the subcommand specific
667# options are primarily done by the subcommand implementations.
668# Subcommand specific options such as --branch and --cached are
669# parsed here as well, for backward compatibility.
670
671while test $# != 0 && test -z "$command"
70c7ac22
LH
672do
673 case "$1" in
26b06100 674 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
5c08dbbd 675 command=$1
70c7ac22
LH
676 ;;
677 -q|--quiet)
2e6a30ef 678 GIT_QUIET=1
70c7ac22 679 ;;
ecda0723
SV
680 -b|--branch)
681 case "$2" in
682 '')
683 usage
684 ;;
685 esac
686 branch="$2"; shift
687 ;;
70c7ac22 688 --cached)
28f9af5d 689 cached="$1"
70c7ac22
LH
690 ;;
691 --)
692 break
693 ;;
694 -*)
695 usage
696 ;;
697 *)
698 break
699 ;;
700 esac
701 shift
702done
703
5c08dbbd 704# No command word defaults to "status"
af9c9f97
RR
705if test -z "$command"
706then
707 if test $# = 0
708 then
709 command=status
710 else
711 usage
712 fi
713fi
5c08dbbd 714
b57e8119
DL
715# "-b branch" is accepted only by "add" and "set-branch"
716if test -n "$branch" && (test "$command" != add || test "$command" != set-branch)
5c08dbbd 717then
ecda0723 718 usage
5c08dbbd
JH
719fi
720
28f9af5d 721# "--cached" is accepted only by "status" and "summary"
496eeeb1 722if test -n "$cached" && test "$command" != status && test "$command" != summary
5c08dbbd 723then
70c7ac22 724 usage
5c08dbbd
JH
725fi
726
b57e8119 727"cmd_$(echo $command | sed -e s/-/_/g)" "$@"