Merge branch 'main' of github.com:git/git
[alt-git.git] / git-submodule.sh
blob5e5d21c010f7d4337dbf95cd50eeaf8c97791ba8
1 #!/bin/sh
3 # git-submodule.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
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
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
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=
56 isnumber()
58 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
62 # Add a new submodule to the working tree, .gitmodules and the index
64 # $@ = repo path
66 # optional branch is stored in global branch variable
68 cmd_add()
70 # parse $args after "submodule ... add".
71 reference_path=
72 while test $# -ne 0
74 case "$1" in
75 -b | --branch)
76 case "$2" in '') usage ;; esac
77 branch=$2
78 shift
80 -f | --force)
81 force=$1
83 -q|--quiet)
84 quiet=1
86 --progress)
87 progress=1
89 --reference)
90 case "$2" in '') usage ;; esac
91 reference_path=$2
92 shift
94 --reference=*)
95 reference_path="${1#--reference=}"
97 --dissociate)
98 dissociate=1
100 --name)
101 case "$2" in '') usage ;; esac
102 custom_name=$2
103 shift
105 --depth)
106 case "$2" in '') usage ;; esac
107 depth="--depth=$2"
108 shift
110 --depth=*)
111 depth=$1
114 shift
115 break
118 usage
121 break
123 esac
124 shift
125 done
127 if test -z "$1"
128 then
129 usage
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"} -- "$@"
136 # Execute an arbitrary command sequence in each checked out
137 # submodule
139 # $@ = command to execute
141 cmd_foreach()
143 # parse $args after "submodule ... foreach".
144 while test $# -ne 0
146 case "$1" in
147 -q|--quiet)
148 quiet=1
150 --recursive)
151 recursive=1
154 usage
157 break
159 esac
160 shift
161 done
163 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach ${quiet:+--quiet} ${recursive:+--recursive} -- "$@"
167 # Register submodules in .git/config
169 # $@ = requested paths (default to all)
171 cmd_init()
173 # parse $args after "submodule ... init".
174 while test $# -ne 0
176 case "$1" in
177 -q|--quiet)
178 quiet=1
181 shift
182 break
185 usage
188 break
190 esac
191 shift
192 done
194 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init ${quiet:+--quiet} -- "$@"
198 # Unregister submodules from .git/config and remove their work tree
200 cmd_deinit()
202 # parse $args after "submodule ... deinit".
203 deinit_all=
204 while test $# -ne 0
206 case "$1" in
207 -f|--force)
208 force=$1
210 -q|--quiet)
211 quiet=1
213 --all)
214 deinit_all=t
217 shift
218 break
221 usage
224 break
226 esac
227 shift
228 done
230 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${quiet:+--quiet} ${force:+--force} ${deinit_all:+--all} -- "$@"
234 # Update each submodule path to correct revision, using clone and checkout as needed
236 # $@ = requested paths (default to all)
238 cmd_update()
240 # parse $args after "submodule ... update".
241 while test $# -ne 0
243 case "$1" in
244 -q|--quiet)
245 quiet=1
247 --progress)
248 progress=1
250 -i|--init)
251 init=1
253 --require-init)
254 require_init=1
256 --remote)
257 remote=1
259 -N|--no-fetch)
260 nofetch=1
262 -f|--force)
263 force=$1
265 -r|--rebase)
266 rebase=1
268 --reference)
269 case "$2" in '') usage ;; esac
270 reference="--reference=$2"
271 shift
273 --reference=*)
274 reference="$1"
276 --dissociate)
277 dissociate=1
279 -m|--merge)
280 merge=1
282 --recursive)
283 recursive=1
285 --checkout)
286 checkout=1
288 --recommend-shallow)
289 recommend_shallow="--recommend-shallow"
291 --no-recommend-shallow)
292 recommend_shallow="--no-recommend-shallow"
294 --depth)
295 case "$2" in '') usage ;; esac
296 depth="--depth=$2"
297 shift
299 --depth=*)
300 depth=$1
302 -j|--jobs)
303 case "$2" in '') usage ;; esac
304 jobs="--jobs=$2"
305 shift
307 --jobs=*)
308 jobs=$1
310 --single-branch)
311 single_branch="--single-branch"
313 --no-single-branch)
314 single_branch="--no-single-branch"
316 --filter)
317 case "$2" in '') usage ;; esac
318 filter="--filter=$2"
319 shift
321 --filter=*)
322 filter="$1"
325 shift
326 break
329 usage
332 break
334 esac
335 shift
336 done
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 ${wt_prefix:+--prefix "$wt_prefix"} \
347 ${rebase:+--rebase} \
348 ${merge:+--merge} \
349 ${checkout:+--checkout} \
350 ${reference:+"$reference"} \
351 ${dissociate:+"--dissociate"} \
352 ${depth:+"$depth"} \
353 ${require_init:+--require-init} \
354 ${dissociate:+"--dissociate"} \
355 $single_branch \
356 $recommend_shallow \
357 $jobs \
358 $filter \
359 -- \
360 "$@"
364 # Configures a submodule's default branch
366 # $@ = requested path
368 cmd_set_branch() {
369 default=
370 branch=
372 while test $# -ne 0
374 case "$1" in
375 -q|--quiet)
376 # we don't do anything with this but we need to accept it
378 -d|--default)
379 default=1
381 -b|--branch)
382 case "$2" in '') usage ;; esac
383 branch=$2
384 shift
387 shift
388 break
391 usage
394 break
396 esac
397 shift
398 done
400 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch ${quiet:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
404 # Configures a submodule's remote url
406 # $@ = requested path, requested url
408 cmd_set_url() {
409 while test $# -ne 0
411 case "$1" in
412 -q|--quiet)
413 quiet=1
416 shift
417 break
420 usage
423 break
425 esac
426 shift
427 done
429 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url ${quiet:+--quiet} -- "$@"
433 # Show commit summary for submodules in index or working tree
435 # If '--cached' is given, show summary between index and given commit,
436 # or between working tree and given commit
438 # $@ = [commit (default 'HEAD'),] requested paths (default all)
440 cmd_summary() {
441 summary_limit=-1
442 for_status=
443 diff_cmd=diff-index
445 # parse $args after "submodule ... summary".
446 while test $# -ne 0
448 case "$1" in
449 --cached)
450 cached=1
452 --files)
453 files="$1"
455 --for-status)
456 for_status="$1"
458 -n|--summary-limit)
459 summary_limit="$2"
460 isnumber "$summary_limit" || usage
461 shift
463 --summary-limit=*)
464 summary_limit="${1#--summary-limit=}"
465 isnumber "$summary_limit" || usage
468 shift
469 break
472 usage
475 break
477 esac
478 shift
479 done
481 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary ${files:+--files} ${cached:+--cached} ${for_status:+--for-status} ${summary_limit:+-n $summary_limit} -- "$@"
484 # List all submodules, prefixed with:
485 # - submodule not initialized
486 # + different revision checked out
488 # If --cached was specified the revision in the index will be printed
489 # instead of the currently checked out revision.
491 # $@ = requested paths (default to all)
493 cmd_status()
495 # parse $args after "submodule ... status".
496 while test $# -ne 0
498 case "$1" in
499 -q|--quiet)
500 quiet=1
502 --cached)
503 cached=1
505 --recursive)
506 recursive=1
509 shift
510 break
513 usage
516 break
518 esac
519 shift
520 done
522 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status ${quiet:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
525 # Sync remote urls for submodules
526 # This makes the value for remote.$remote.url match the value
527 # specified in .gitmodules.
529 cmd_sync()
531 while test $# -ne 0
533 case "$1" in
534 -q|--quiet)
535 quiet=1
536 shift
538 --recursive)
539 recursive=1
540 shift
543 shift
544 break
547 usage
550 break
552 esac
553 done
555 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync ${quiet:+--quiet} ${recursive:+--recursive} -- "$@"
558 cmd_absorbgitdirs()
560 git submodule--helper absorbgitdirs --prefix "$wt_prefix" "$@"
563 # This loop parses the command line arguments to find the
564 # subcommand name to dispatch. Parsing of the subcommand specific
565 # options are primarily done by the subcommand implementations.
566 # Subcommand specific options such as --branch and --cached are
567 # parsed here as well, for backward compatibility.
569 while test $# != 0 && test -z "$command"
571 case "$1" in
572 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
573 command=$1
575 -q|--quiet)
576 quiet=1
578 --cached)
579 cached=1
582 break
585 usage
588 break
590 esac
591 shift
592 done
594 # No command word defaults to "status"
595 if test -z "$command"
596 then
597 if test $# = 0
598 then
599 command=status
600 else
601 usage
605 # "--cached" is accepted only by "status" and "summary"
606 if test -n "$cached" && test "$command" != status && test "$command" != summary
607 then
608 usage
611 "cmd_$(echo $command | sed -e s/-/_/g)" "$@"