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