]> git.ipfire.org Git - thirdparty/git.git/blame - git-submodule.sh
submodule--helper: eliminate internal "--update" option
[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=
8f12108c
ÆAB
43rebase=
44merge=
45checkout=
73b0898d 46custom_name=
275cd184 47depth=
72c5f883 48progress=
a0ef2934 49dissociate=
132f600b 50single_branch=
65d100c4
LX
51jobs=
52recommend_shallow=
f05da2b4 53filter=
70c7ac22 54
862ae6cd
RS
55isnumber()
56{
57 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
58}
59
ecda0723
SV
60#
61# Add a new submodule to the working tree, .gitmodules and the index
62#
ec05df35 63# $@ = repo path
ecda0723
SV
64#
65# optional branch is stored in global branch variable
66#
23a485e3 67cmd_add()
ecda0723 68{
5c08dbbd 69 # parse $args after "submodule ... add".
091a6eb0 70 reference_path=
5c08dbbd
JH
71 while test $# -ne 0
72 do
73 case "$1" in
74 -b | --branch)
75 case "$2" in '') usage ;; esac
76 branch=$2
77 shift
78 ;;
d27b876b
JL
79 -f | --force)
80 force=$1
81 ;;
5c08dbbd 82 -q|--quiet)
2e6a30ef 83 GIT_QUIET=1
5c08dbbd 84 ;;
6d33e1c2
CF
85 --progress)
86 progress=1
87 ;;
d92a3959
MT
88 --reference)
89 case "$2" in '') usage ;; esac
091a6eb0 90 reference_path=$2
d92a3959
MT
91 shift
92 ;;
93 --reference=*)
091a6eb0 94 reference_path="${1#--reference=}"
d92a3959 95 ;;
a0ef2934
CF
96 --dissociate)
97 dissociate=1
98 ;;
73b0898d
JL
99 --name)
100 case "$2" in '') usage ;; esac
101 custom_name=$2
102 shift
103 ;;
275cd184
FG
104 --depth)
105 case "$2" in '') usage ;; esac
106 depth="--depth=$2"
107 shift
108 ;;
109 --depth=*)
110 depth=$1
111 ;;
5c08dbbd
JH
112 --)
113 shift
114 break
115 ;;
116 -*)
117 usage
118 ;;
119 *)
120 break
121 ;;
122 esac
123 shift
124 done
125
a6226fd7 126 if test -z "$1"
76e9bdc4 127 then
ecda0723
SV
128 usage
129 fi
130
960fad98 131 git ${wt_prefix:+-C "$wt_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
132}
133
19a31f9c
ML
134#
135# Execute an arbitrary command sequence in each checked out
136# submodule
137#
138# $@ = command to execute
139#
140cmd_foreach()
141{
1d5bec8b
JH
142 # parse $args after "submodule ... foreach".
143 while test $# -ne 0
144 do
145 case "$1" in
146 -q|--quiet)
147 GIT_QUIET=1
148 ;;
15fc56a8
JH
149 --recursive)
150 recursive=1
151 ;;
1d5bec8b
JH
152 -*)
153 usage
154 ;;
155 *)
156 break
157 ;;
158 esac
159 shift
160 done
161
1cf823d8 162 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
19a31f9c
ML
163}
164
70c7ac22 165#
211b7f19 166# Register submodules in .git/config
70c7ac22
LH
167#
168# $@ = requested paths (default to all)
169#
23a485e3 170cmd_init()
70c7ac22 171{
5c08dbbd
JH
172 # parse $args after "submodule ... init".
173 while test $# -ne 0
174 do
175 case "$1" in
176 -q|--quiet)
2e6a30ef 177 GIT_QUIET=1
5c08dbbd
JH
178 ;;
179 --)
180 shift
181 break
182 ;;
183 -*)
184 usage
185 ;;
186 *)
187 break
188 ;;
189 esac
190 shift
191 done
192
960fad98 193 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init ${GIT_QUIET:+--quiet} -- "$@"
70c7ac22
LH
194}
195
cf419828
JL
196#
197# Unregister submodules from .git/config and remove their work tree
198#
cf419828
JL
199cmd_deinit()
200{
201 # parse $args after "submodule ... deinit".
f6a52799 202 deinit_all=
cf419828
JL
203 while test $# -ne 0
204 do
205 case "$1" in
206 -f|--force)
207 force=$1
208 ;;
209 -q|--quiet)
210 GIT_QUIET=1
211 ;;
f6a52799
SB
212 --all)
213 deinit_all=t
214 ;;
cf419828
JL
215 --)
216 shift
217 break
218 ;;
219 -*)
220 usage
221 ;;
222 *)
223 break
224 ;;
225 esac
226 shift
227 done
228
1cf823d8 229 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${force:+--force} ${deinit_all:+--all} -- "$@"
cf419828
JL
230}
231
70c7ac22 232#
211b7f19 233# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
234#
235# $@ = requested paths (default to all)
236#
23a485e3 237cmd_update()
70c7ac22 238{
5c08dbbd
JH
239 # parse $args after "submodule ... update".
240 while test $# -ne 0
241 do
242 case "$1" in
243 -q|--quiet)
2e6a30ef 244 GIT_QUIET=1
5c08dbbd 245 ;;
72c5f883 246 --progress)
c7199e3a 247 progress=1
72c5f883 248 ;;
be4d2c83 249 -i|--init)
d92a3959 250 init=1
be4d2c83 251 ;;
0060fd15 252 --require-init)
0060fd15
JS
253 require_init=1
254 ;;
06b1abb5
TK
255 --remote)
256 remote=1
257 ;;
31ca3ac3 258 -N|--no-fetch)
31ca3ac3
FF
259 nofetch=1
260 ;;
9db31bdf
NMC
261 -f|--force)
262 force=$1
263 ;;
ca2cedba 264 -r|--rebase)
8f12108c 265 rebase=1
ca2cedba 266 ;;
d92a3959
MT
267 --reference)
268 case "$2" in '') usage ;; esac
269 reference="--reference=$2"
98dbe63d 270 shift
d92a3959
MT
271 ;;
272 --reference=*)
273 reference="$1"
d92a3959 274 ;;
a0ef2934
CF
275 --dissociate)
276 dissociate=1
277 ;;
42b49178 278 -m|--merge)
8f12108c 279 merge=1
42b49178 280 ;;
b13fd5c1 281 --recursive)
b13fd5c1
JH
282 recursive=1
283 ;;
efc5fb6a 284 --checkout)
8f12108c 285 checkout=1
efc5fb6a 286 ;;
abed000a
SB
287 --recommend-shallow)
288 recommend_shallow="--recommend-shallow"
289 ;;
290 --no-recommend-shallow)
291 recommend_shallow="--no-recommend-shallow"
292 ;;
275cd184
FG
293 --depth)
294 case "$2" in '') usage ;; esac
295 depth="--depth=$2"
296 shift
297 ;;
298 --depth=*)
299 depth=$1
300 ;;
2335b870
SB
301 -j|--jobs)
302 case "$2" in '') usage ;; esac
303 jobs="--jobs=$2"
304 shift
305 ;;
306 --jobs=*)
307 jobs=$1
308 ;;
132f600b
ES
309 --single-branch)
310 single_branch="--single-branch"
311 ;;
312 --no-single-branch)
313 single_branch="--no-single-branch"
314 ;;
f05da2b4
JS
315 --filter)
316 case "$2" in '') usage ;; esac
317 filter="--filter=$2"
318 shift
319 ;;
320 --filter=*)
321 filter="$1"
322 ;;
5c08dbbd
JH
323 --)
324 shift
325 break
326 ;;
327 -*)
328 usage
329 ;;
330 *)
331 break
332 ;;
333 esac
98dbe63d 334 shift
5c08dbbd
JH
335 done
336
b3c5f5cb 337 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper update \
29a5e9e1 338 ${GIT_QUIET:+--quiet} \
b3c5f5cb 339 ${force:+--force} \
c7199e3a 340 ${progress:+"--progress"} \
b3c5f5cb
AR
341 ${remote:+--remote} \
342 ${recursive:+--recursive} \
29a5e9e1 343 ${init:+--init} \
b3c5f5cb 344 ${nofetch:+--no-fetch} \
48308681 345 ${wt_prefix:+--prefix "$wt_prefix"} \
8f12108c
ÆAB
346 ${rebase:+--rebase} \
347 ${merge:+--merge} \
348 ${checkout:+--checkout} \
5f50f33e 349 ${reference:+"$reference"} \
a0ef2934 350 ${dissociate:+"--dissociate"} \
c9911c93 351 ${depth:+"$depth"} \
0060fd15 352 ${require_init:+--require-init} \
b3c5f5cb 353 ${dissociate:+"--dissociate"} \
132f600b 354 $single_branch \
c7199e3a
CF
355 $recommend_shallow \
356 $jobs \
f05da2b4 357 $filter \
a282f5a9 358 -- \
b3c5f5cb 359 "$@"
70c7ac22
LH
360}
361
b57e8119
DL
362#
363# Configures a submodule's default branch
364#
365# $@ = requested path
366#
367cmd_set_branch() {
2964d6e5 368 default=
b57e8119
DL
369 branch=
370
371 while test $# -ne 0
372 do
373 case "$1" in
374 -q|--quiet)
375 # we don't do anything with this but we need to accept it
376 ;;
377 -d|--default)
2964d6e5 378 default=1
b57e8119
DL
379 ;;
380 -b|--branch)
381 case "$2" in '') usage ;; esac
382 branch=$2
383 shift
384 ;;
385 --)
386 shift
387 break
388 ;;
389 -*)
390 usage
391 ;;
392 *)
393 break
394 ;;
395 esac
396 shift
397 done
398
1cf823d8 399 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch ${GIT_QUIET:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
b57e8119
DL
400}
401
26b06100
DL
402#
403# Configures a submodule's remote url
404#
405# $@ = requested path, requested url
406#
407cmd_set_url() {
408 while test $# -ne 0
409 do
410 case "$1" in
411 -q|--quiet)
412 GIT_QUIET=1
413 ;;
414 --)
415 shift
416 break
417 ;;
418 -*)
419 usage
420 ;;
421 *)
422 break
423 ;;
424 esac
425 shift
426 done
427
1cf823d8 428 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url ${GIT_QUIET:+--quiet} -- "$@"
26b06100
DL
429}
430
28f9af5d
PY
431#
432# Show commit summary for submodules in index or working tree
433#
434# If '--cached' is given, show summary between index and given commit,
435# or between working tree and given commit
436#
437# $@ = [commit (default 'HEAD'),] requested paths (default all)
438#
439cmd_summary() {
f2dc06a3 440 summary_limit=-1
d0f64dd4 441 for_status=
1c244f6e 442 diff_cmd=diff-index
f2dc06a3 443
28f9af5d
PY
444 # parse $args after "submodule ... summary".
445 while test $# -ne 0
446 do
447 case "$1" in
448 --cached)
757d0927 449 cached=1
28f9af5d 450 ;;
1c244f6e
JL
451 --files)
452 files="$1"
453 ;;
d0f64dd4
PY
454 --for-status)
455 for_status="$1"
456 ;;
f2dc06a3 457 -n|--summary-limit)
862ae6cd
RS
458 summary_limit="$2"
459 isnumber "$summary_limit" || usage
f2dc06a3
PY
460 shift
461 ;;
862ae6cd
RS
462 --summary-limit=*)
463 summary_limit="${1#--summary-limit=}"
464 isnumber "$summary_limit" || usage
465 ;;
28f9af5d
PY
466 --)
467 shift
468 break
469 ;;
470 -*)
471 usage
472 ;;
473 *)
474 break
475 ;;
476 esac
477 shift
478 done
bffe71f4 479
1cf823d8 480 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary ${files:+--files} ${cached:+--cached} ${for_status:+--for-status} ${summary_limit:+-n $summary_limit} -- "$@"
28f9af5d 481}
70c7ac22 482#
941987a5 483# List all submodules, prefixed with:
70c7ac22
LH
484# - submodule not initialized
485# + different revision checked out
486#
487# If --cached was specified the revision in the index will be printed
488# instead of the currently checked out revision.
489#
490# $@ = requested paths (default to all)
491#
23a485e3 492cmd_status()
70c7ac22 493{
5c08dbbd
JH
494 # parse $args after "submodule ... status".
495 while test $# -ne 0
496 do
497 case "$1" in
498 -q|--quiet)
2e6a30ef 499 GIT_QUIET=1
5c08dbbd
JH
500 ;;
501 --cached)
502 cached=1
503 ;;
64b19ffe
JH
504 --recursive)
505 recursive=1
506 ;;
5c08dbbd
JH
507 --)
508 shift
509 break
510 ;;
511 -*)
512 usage
513 ;;
514 *)
515 break
516 ;;
517 esac
518 shift
519 done
520
1cf823d8 521 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
70c7ac22 522}
2327f61e
DA
523#
524# Sync remote urls for submodules
525# This makes the value for remote.$remote.url match the value
526# specified in .gitmodules.
527#
528cmd_sync()
529{
530 while test $# -ne 0
531 do
532 case "$1" in
533 -q|--quiet)
2e6a30ef 534 GIT_QUIET=1
2327f61e
DA
535 shift
536 ;;
82f49f29
PH
537 --recursive)
538 recursive=1
539 shift
540 ;;
2327f61e
DA
541 --)
542 shift
543 break
544 ;;
545 -*)
546 usage
547 ;;
548 *)
549 break
550 ;;
551 esac
552 done
e7849a96 553
1cf823d8 554 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
2327f61e 555}
70c7ac22 556
f6f85861
SB
557cmd_absorbgitdirs()
558{
6e556c41 559 git submodule--helper absorbgitdirs --prefix "$wt_prefix" "$@"
f6f85861
SB
560}
561
5c08dbbd
JH
562# This loop parses the command line arguments to find the
563# subcommand name to dispatch. Parsing of the subcommand specific
564# options are primarily done by the subcommand implementations.
565# Subcommand specific options such as --branch and --cached are
566# parsed here as well, for backward compatibility.
567
568while test $# != 0 && test -z "$command"
70c7ac22
LH
569do
570 case "$1" in
26b06100 571 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
5c08dbbd 572 command=$1
70c7ac22
LH
573 ;;
574 -q|--quiet)
2e6a30ef 575 GIT_QUIET=1
70c7ac22
LH
576 ;;
577 --cached)
757d0927 578 cached=1
70c7ac22
LH
579 ;;
580 --)
581 break
582 ;;
583 -*)
584 usage
585 ;;
586 *)
587 break
588 ;;
589 esac
590 shift
591done
592
5c08dbbd 593# No command word defaults to "status"
af9c9f97
RR
594if test -z "$command"
595then
596 if test $# = 0
597 then
598 command=status
599 else
600 usage
601 fi
602fi
5c08dbbd 603
28f9af5d 604# "--cached" is accepted only by "status" and "summary"
496eeeb1 605if test -n "$cached" && test "$command" != status && test "$command" != summary
5c08dbbd 606then
70c7ac22 607 usage
5c08dbbd
JH
608fi
609
b57e8119 610"cmd_$(echo $command | sed -e s/-/_/g)" "$@"