]> git.ipfire.org Git - thirdparty/git.git/blob - git-submodule.sh
Merge branch 'tb/ci-concurrency' into maint-2.39
[thirdparty/git.git] / git-submodule.sh
1 #!/bin/sh
2 #
3 # git-submodule.sh: add, init, update or list git submodules
4 #
5 # Copyright (c) 2007 Lars Hjemli
6
7 dashless=$(basename "$0" | sed -e 's/-/ /')
8 USAGE="[--quiet] [--cached]
9 or: $dashless [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
10 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
11 or: $dashless [--quiet] init [--] [<path>...]
12 or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
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>...]
14 or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>
15 or: $dashless [--quiet] set-url [--] <path> <newurl>
16 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
17 or: $dashless [--quiet] foreach [--recursive] <command>
18 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
19 or: $dashless [--quiet] absorbgitdirs [--] [<path>...]"
20 OPTIONS_SPEC=
21 SUBDIRECTORY_OK=Yes
22 . git-sh-setup
23 require_work_tree
24 wt_prefix=$(git rev-parse --show-prefix)
25 cd_to_toplevel
26
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.
29 GIT_PROTOCOL_FROM_USER=0
30 export GIT_PROTOCOL_FROM_USER
31
32 command=
33 quiet=
34 branch=
35 force=
36 reference=
37 cached=
38 recursive=
39 init=
40 require_init=
41 files=
42 remote=
43 nofetch=
44 rebase=
45 merge=
46 checkout=
47 custom_name=
48 depth=
49 progress=
50 dissociate=
51 single_branch=
52 jobs=
53 recommend_shallow=
54 filter=
55
56 isnumber()
57 {
58 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
59 }
60
61 #
62 # Add a new submodule to the working tree, .gitmodules and the index
63 #
64 # $@ = repo path
65 #
66 # optional branch is stored in global branch variable
67 #
68 cmd_add()
69 {
70 # parse $args after "submodule ... add".
71 reference_path=
72 while test $# -ne 0
73 do
74 case "$1" in
75 -b | --branch)
76 case "$2" in '') usage ;; esac
77 branch=$2
78 shift
79 ;;
80 -f | --force)
81 force=$1
82 ;;
83 -q|--quiet)
84 quiet=1
85 ;;
86 --progress)
87 progress=1
88 ;;
89 --reference)
90 case "$2" in '') usage ;; esac
91 reference_path=$2
92 shift
93 ;;
94 --reference=*)
95 reference_path="${1#--reference=}"
96 ;;
97 --dissociate)
98 dissociate=1
99 ;;
100 --name)
101 case "$2" in '') usage ;; esac
102 custom_name=$2
103 shift
104 ;;
105 --depth)
106 case "$2" in '') usage ;; esac
107 depth="--depth=$2"
108 shift
109 ;;
110 --depth=*)
111 depth=$1
112 ;;
113 --)
114 shift
115 break
116 ;;
117 -*)
118 usage
119 ;;
120 *)
121 break
122 ;;
123 esac
124 shift
125 done
126
127 if test -z "$1"
128 then
129 usage
130 fi
131
132 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper add ${quiet:+--quiet} ${force:+--force} ${progress:+"--progress"} ${branch:+--branch "$branch"} ${reference_path:+--reference "$reference_path"} ${dissociate:+--dissociate} ${custom_name:+--name "$custom_name"} ${depth:+"$depth"} -- "$@"
133 }
134
135 #
136 # Execute an arbitrary command sequence in each checked out
137 # submodule
138 #
139 # $@ = command to execute
140 #
141 cmd_foreach()
142 {
143 # parse $args after "submodule ... foreach".
144 while test $# -ne 0
145 do
146 case "$1" in
147 -q|--quiet)
148 quiet=1
149 ;;
150 --recursive)
151 recursive=1
152 ;;
153 -*)
154 usage
155 ;;
156 *)
157 break
158 ;;
159 esac
160 shift
161 done
162
163 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach ${quiet:+--quiet} ${recursive:+--recursive} -- "$@"
164 }
165
166 #
167 # Register submodules in .git/config
168 #
169 # $@ = requested paths (default to all)
170 #
171 cmd_init()
172 {
173 # parse $args after "submodule ... init".
174 while test $# -ne 0
175 do
176 case "$1" in
177 -q|--quiet)
178 quiet=1
179 ;;
180 --)
181 shift
182 break
183 ;;
184 -*)
185 usage
186 ;;
187 *)
188 break
189 ;;
190 esac
191 shift
192 done
193
194 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init ${quiet:+--quiet} -- "$@"
195 }
196
197 #
198 # Unregister submodules from .git/config and remove their work tree
199 #
200 cmd_deinit()
201 {
202 # parse $args after "submodule ... deinit".
203 deinit_all=
204 while test $# -ne 0
205 do
206 case "$1" in
207 -f|--force)
208 force=$1
209 ;;
210 -q|--quiet)
211 quiet=1
212 ;;
213 --all)
214 deinit_all=t
215 ;;
216 --)
217 shift
218 break
219 ;;
220 -*)
221 usage
222 ;;
223 *)
224 break
225 ;;
226 esac
227 shift
228 done
229
230 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${quiet:+--quiet} ${force:+--force} ${deinit_all:+--all} -- "$@"
231 }
232
233 #
234 # Update each submodule path to correct revision, using clone and checkout as needed
235 #
236 # $@ = requested paths (default to all)
237 #
238 cmd_update()
239 {
240 # parse $args after "submodule ... update".
241 while test $# -ne 0
242 do
243 case "$1" in
244 -q|--quiet)
245 quiet=1
246 ;;
247 --progress)
248 progress=1
249 ;;
250 -i|--init)
251 init=1
252 ;;
253 --require-init)
254 require_init=1
255 ;;
256 --remote)
257 remote=1
258 ;;
259 -N|--no-fetch)
260 nofetch=1
261 ;;
262 -f|--force)
263 force=$1
264 ;;
265 -r|--rebase)
266 rebase=1
267 ;;
268 --reference)
269 case "$2" in '') usage ;; esac
270 reference="--reference=$2"
271 shift
272 ;;
273 --reference=*)
274 reference="$1"
275 ;;
276 --dissociate)
277 dissociate=1
278 ;;
279 -m|--merge)
280 merge=1
281 ;;
282 --recursive)
283 recursive=1
284 ;;
285 --checkout)
286 checkout=1
287 ;;
288 --recommend-shallow)
289 recommend_shallow="--recommend-shallow"
290 ;;
291 --no-recommend-shallow)
292 recommend_shallow="--no-recommend-shallow"
293 ;;
294 --depth)
295 case "$2" in '') usage ;; esac
296 depth="--depth=$2"
297 shift
298 ;;
299 --depth=*)
300 depth=$1
301 ;;
302 -j|--jobs)
303 case "$2" in '') usage ;; esac
304 jobs="--jobs=$2"
305 shift
306 ;;
307 --jobs=*)
308 jobs=$1
309 ;;
310 --single-branch)
311 single_branch="--single-branch"
312 ;;
313 --no-single-branch)
314 single_branch="--no-single-branch"
315 ;;
316 --filter)
317 case "$2" in '') usage ;; esac
318 filter="--filter=$2"
319 shift
320 ;;
321 --filter=*)
322 filter="$1"
323 ;;
324 --)
325 shift
326 break
327 ;;
328 -*)
329 usage
330 ;;
331 *)
332 break
333 ;;
334 esac
335 shift
336 done
337
338 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper update \
339 ${quiet:+--quiet} \
340 ${force:+--force} \
341 ${progress:+"--progress"} \
342 ${remote:+--remote} \
343 ${recursive:+--recursive} \
344 ${init:+--init} \
345 ${nofetch:+--no-fetch} \
346 ${rebase:+--rebase} \
347 ${merge:+--merge} \
348 ${checkout:+--checkout} \
349 ${reference:+"$reference"} \
350 ${dissociate:+"--dissociate"} \
351 ${depth:+"$depth"} \
352 ${require_init:+--require-init} \
353 ${dissociate:+"--dissociate"} \
354 $single_branch \
355 $recommend_shallow \
356 $jobs \
357 $filter \
358 -- \
359 "$@"
360 }
361
362 #
363 # Configures a submodule's default branch
364 #
365 # $@ = requested path
366 #
367 cmd_set_branch() {
368 default=
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)
378 default=1
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
399 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch ${quiet:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
400 }
401
402 #
403 # Configures a submodule's remote url
404 #
405 # $@ = requested path, requested url
406 #
407 cmd_set_url() {
408 while test $# -ne 0
409 do
410 case "$1" in
411 -q|--quiet)
412 quiet=1
413 ;;
414 --)
415 shift
416 break
417 ;;
418 -*)
419 usage
420 ;;
421 *)
422 break
423 ;;
424 esac
425 shift
426 done
427
428 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url ${quiet:+--quiet} -- "$@"
429 }
430
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 #
439 cmd_summary() {
440 summary_limit=-1
441 for_status=
442 diff_cmd=diff-index
443
444 # parse $args after "submodule ... summary".
445 while test $# -ne 0
446 do
447 case "$1" in
448 --cached)
449 cached=1
450 ;;
451 --files)
452 files="$1"
453 ;;
454 --for-status)
455 for_status="$1"
456 ;;
457 -n|--summary-limit)
458 summary_limit="$2"
459 isnumber "$summary_limit" || usage
460 shift
461 ;;
462 --summary-limit=*)
463 summary_limit="${1#--summary-limit=}"
464 isnumber "$summary_limit" || usage
465 ;;
466 --)
467 shift
468 break
469 ;;
470 -*)
471 usage
472 ;;
473 *)
474 break
475 ;;
476 esac
477 shift
478 done
479
480 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary ${files:+--files} ${cached:+--cached} ${for_status:+--for-status} ${summary_limit:+-n $summary_limit} -- "$@"
481 }
482 #
483 # List all submodules, prefixed with:
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 #
492 cmd_status()
493 {
494 # parse $args after "submodule ... status".
495 while test $# -ne 0
496 do
497 case "$1" in
498 -q|--quiet)
499 quiet=1
500 ;;
501 --cached)
502 cached=1
503 ;;
504 --recursive)
505 recursive=1
506 ;;
507 --)
508 shift
509 break
510 ;;
511 -*)
512 usage
513 ;;
514 *)
515 break
516 ;;
517 esac
518 shift
519 done
520
521 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status ${quiet:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
522 }
523 #
524 # Sync remote urls for submodules
525 # This makes the value for remote.$remote.url match the value
526 # specified in .gitmodules.
527 #
528 cmd_sync()
529 {
530 while test $# -ne 0
531 do
532 case "$1" in
533 -q|--quiet)
534 quiet=1
535 shift
536 ;;
537 --recursive)
538 recursive=1
539 shift
540 ;;
541 --)
542 shift
543 break
544 ;;
545 -*)
546 usage
547 ;;
548 *)
549 break
550 ;;
551 esac
552 done
553
554 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync ${quiet:+--quiet} ${recursive:+--recursive} -- "$@"
555 }
556
557 cmd_absorbgitdirs()
558 {
559 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper absorbgitdirs "$@"
560 }
561
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
568 while test $# != 0 && test -z "$command"
569 do
570 case "$1" in
571 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
572 command=$1
573 ;;
574 -q|--quiet)
575 quiet=1
576 ;;
577 --cached)
578 cached=1
579 ;;
580 --)
581 break
582 ;;
583 -*)
584 usage
585 ;;
586 *)
587 break
588 ;;
589 esac
590 shift
591 done
592
593 # No command word defaults to "status"
594 if test -z "$command"
595 then
596 if test $# = 0
597 then
598 command=status
599 else
600 usage
601 fi
602 fi
603
604 # "--cached" is accepted only by "status" and "summary"
605 if test -n "$cached" && test "$command" != status && test "$command" != summary
606 then
607 usage
608 fi
609
610 "cmd_$(echo $command | sed -e s/-/_/g)" "$@"