]> git.ipfire.org Git - thirdparty/git.git/blame - git-submodule.sh
revision.[ch]: document and move code declared around "init"
[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
407 # 2 or 128: subcommand died during execution
408 # 3: no update procedure was run
409 res="$?"
410 case $res in
411 0)
412 say "$out"
413 ;;
414 1)
415 err="${err};fatal: $out"
416 continue
417 ;;
418 2|128)
419 die_with_status $res "fatal: $out"
420 ;;
421 esac
b13fd5c1
JH
422
423 if test -n "$recursive"
424 then
75bf5e60 425 (
44431df0 426 prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
3604242f 427 wt_prefix=
14111fc4 428 sanitize_submodule_env
75bf5e60 429 cd "$sm_path" &&
36141282 430 eval cmd_update
75bf5e60 431 )
15ffb7cd
FG
432 res=$?
433 if test $res -gt 0
434 then
0008d122 435 die_msg="fatal: $(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
bb9d91b4 436 if test $res -ne 2
15ffb7cd 437 then
ff968f03 438 err="${err};$die_msg"
15ffb7cd
FG
439 continue
440 else
ff968f03 441 die_with_status $res "$die_msg"
15ffb7cd
FG
442 fi
443 fi
b13fd5c1 444 fi
70c7ac22 445 done
15ffb7cd
FG
446
447 if test -n "$err"
448 then
449 OIFS=$IFS
450 IFS=';'
451 for e in $err
452 do
453 if test -n "$e"
454 then
455 echo >&2 "$e"
456 fi
457 done
458 IFS=$OIFS
459 exit 1
460 fi
461 }
70c7ac22
LH
462}
463
b57e8119
DL
464#
465# Configures a submodule's default branch
466#
467# $@ = requested path
468#
469cmd_set_branch() {
2964d6e5 470 default=
b57e8119
DL
471 branch=
472
473 while test $# -ne 0
474 do
475 case "$1" in
476 -q|--quiet)
477 # we don't do anything with this but we need to accept it
478 ;;
479 -d|--default)
2964d6e5 480 default=1
b57e8119
DL
481 ;;
482 -b|--branch)
483 case "$2" in '') usage ;; esac
484 branch=$2
485 shift
486 ;;
487 --)
488 shift
489 break
490 ;;
491 -*)
492 usage
493 ;;
494 *)
495 break
496 ;;
497 esac
498 shift
499 done
500
1cf823d8 501 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch ${GIT_QUIET:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
b57e8119
DL
502}
503
26b06100
DL
504#
505# Configures a submodule's remote url
506#
507# $@ = requested path, requested url
508#
509cmd_set_url() {
510 while test $# -ne 0
511 do
512 case "$1" in
513 -q|--quiet)
514 GIT_QUIET=1
515 ;;
516 --)
517 shift
518 break
519 ;;
520 -*)
521 usage
522 ;;
523 *)
524 break
525 ;;
526 esac
527 shift
528 done
529
1cf823d8 530 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url ${GIT_QUIET:+--quiet} -- "$@"
26b06100
DL
531}
532
28f9af5d
PY
533#
534# Show commit summary for submodules in index or working tree
535#
536# If '--cached' is given, show summary between index and given commit,
537# or between working tree and given commit
538#
539# $@ = [commit (default 'HEAD'),] requested paths (default all)
540#
541cmd_summary() {
f2dc06a3 542 summary_limit=-1
d0f64dd4 543 for_status=
1c244f6e 544 diff_cmd=diff-index
f2dc06a3 545
28f9af5d
PY
546 # parse $args after "submodule ... summary".
547 while test $# -ne 0
548 do
549 case "$1" in
550 --cached)
551 cached="$1"
552 ;;
1c244f6e
JL
553 --files)
554 files="$1"
555 ;;
d0f64dd4
PY
556 --for-status)
557 for_status="$1"
558 ;;
f2dc06a3 559 -n|--summary-limit)
862ae6cd
RS
560 summary_limit="$2"
561 isnumber "$summary_limit" || usage
f2dc06a3
PY
562 shift
563 ;;
862ae6cd
RS
564 --summary-limit=*)
565 summary_limit="${1#--summary-limit=}"
566 isnumber "$summary_limit" || usage
567 ;;
28f9af5d
PY
568 --)
569 shift
570 break
571 ;;
572 -*)
573 usage
574 ;;
575 *)
576 break
577 ;;
578 esac
579 shift
580 done
bffe71f4 581
1cf823d8 582 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary ${files:+--files} ${cached:+--cached} ${for_status:+--for-status} ${summary_limit:+-n $summary_limit} -- "$@"
28f9af5d 583}
70c7ac22 584#
941987a5 585# List all submodules, prefixed with:
70c7ac22
LH
586# - submodule not initialized
587# + different revision checked out
588#
589# If --cached was specified the revision in the index will be printed
590# instead of the currently checked out revision.
591#
592# $@ = requested paths (default to all)
593#
23a485e3 594cmd_status()
70c7ac22 595{
5c08dbbd
JH
596 # parse $args after "submodule ... status".
597 while test $# -ne 0
598 do
599 case "$1" in
600 -q|--quiet)
2e6a30ef 601 GIT_QUIET=1
5c08dbbd
JH
602 ;;
603 --cached)
604 cached=1
605 ;;
64b19ffe
JH
606 --recursive)
607 recursive=1
608 ;;
5c08dbbd
JH
609 --)
610 shift
611 break
612 ;;
613 -*)
614 usage
615 ;;
616 *)
617 break
618 ;;
619 esac
620 shift
621 done
622
1cf823d8 623 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
70c7ac22 624}
2327f61e
DA
625#
626# Sync remote urls for submodules
627# This makes the value for remote.$remote.url match the value
628# specified in .gitmodules.
629#
630cmd_sync()
631{
632 while test $# -ne 0
633 do
634 case "$1" in
635 -q|--quiet)
2e6a30ef 636 GIT_QUIET=1
2327f61e
DA
637 shift
638 ;;
82f49f29
PH
639 --recursive)
640 recursive=1
641 shift
642 ;;
2327f61e
DA
643 --)
644 shift
645 break
646 ;;
647 -*)
648 usage
649 ;;
650 *)
651 break
652 ;;
653 esac
654 done
e7849a96 655
1cf823d8 656 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
2327f61e 657}
70c7ac22 658
f6f85861
SB
659cmd_absorbgitdirs()
660{
661 git submodule--helper absorb-git-dirs --prefix "$wt_prefix" "$@"
662}
663
5c08dbbd
JH
664# This loop parses the command line arguments to find the
665# subcommand name to dispatch. Parsing of the subcommand specific
666# options are primarily done by the subcommand implementations.
667# Subcommand specific options such as --branch and --cached are
668# parsed here as well, for backward compatibility.
669
670while test $# != 0 && test -z "$command"
70c7ac22
LH
671do
672 case "$1" in
26b06100 673 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
5c08dbbd 674 command=$1
70c7ac22
LH
675 ;;
676 -q|--quiet)
2e6a30ef 677 GIT_QUIET=1
70c7ac22 678 ;;
ecda0723
SV
679 -b|--branch)
680 case "$2" in
681 '')
682 usage
683 ;;
684 esac
685 branch="$2"; shift
686 ;;
70c7ac22 687 --cached)
28f9af5d 688 cached="$1"
70c7ac22
LH
689 ;;
690 --)
691 break
692 ;;
693 -*)
694 usage
695 ;;
696 *)
697 break
698 ;;
699 esac
700 shift
701done
702
5c08dbbd 703# No command word defaults to "status"
af9c9f97
RR
704if test -z "$command"
705then
706 if test $# = 0
707 then
708 command=status
709 else
710 usage
711 fi
712fi
5c08dbbd 713
b57e8119
DL
714# "-b branch" is accepted only by "add" and "set-branch"
715if test -n "$branch" && (test "$command" != add || test "$command" != set-branch)
5c08dbbd 716then
ecda0723 717 usage
5c08dbbd
JH
718fi
719
28f9af5d 720# "--cached" is accepted only by "status" and "summary"
496eeeb1 721if test -n "$cached" && test "$command" != status && test "$command" != summary
5c08dbbd 722then
70c7ac22 723 usage
5c08dbbd
JH
724fi
725
b57e8119 726"cmd_$(echo $command | sed -e s/-/_/g)" "$@"