t/helper: fix leaking buffer in "dump-untracked-cache"
[git/gitster.git] / git-submodule.sh
blob03c5a220a26ded60e1bdf67e1fa6393c0f50568b
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 --ref-format)
98 case "$2" in '') usage ;; esac
99 ref_format="--ref-format=$2"
100 shift
102 --ref-format=*)
103 ref_format="$1"
105 --dissociate)
106 dissociate=1
108 --name)
109 case "$2" in '') usage ;; esac
110 custom_name=$2
111 shift
113 --depth)
114 case "$2" in '') usage ;; esac
115 depth="--depth=$2"
116 shift
118 --depth=*)
119 depth=$1
122 shift
123 break
126 usage
129 break
131 esac
132 shift
133 done
135 if test -z "$1"
136 then
137 usage
140 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper add \
141 ${quiet:+--quiet} \
142 ${force:+--force} \
143 ${progress:+"--progress"} \
144 ${branch:+--branch "$branch"} \
145 ${reference_path:+--reference "$reference_path"} \
146 ${ref_format:+"$ref_format"} \
147 ${dissociate:+--dissociate} \
148 ${custom_name:+--name "$custom_name"} \
149 ${depth:+"$depth"} \
150 -- \
151 "$@"
155 # Execute an arbitrary command sequence in each checked out
156 # submodule
158 # $@ = command to execute
160 cmd_foreach()
162 # parse $args after "submodule ... foreach".
163 while test $# -ne 0
165 case "$1" in
166 -q|--quiet)
167 quiet=1
169 --recursive)
170 recursive=1
173 usage
176 break
178 esac
179 shift
180 done
182 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach \
183 ${quiet:+--quiet} \
184 ${recursive:+--recursive} \
185 -- \
186 "$@"
190 # Register submodules in .git/config
192 # $@ = requested paths (default to all)
194 cmd_init()
196 # parse $args after "submodule ... init".
197 while test $# -ne 0
199 case "$1" in
200 -q|--quiet)
201 quiet=1
204 shift
205 break
208 usage
211 break
213 esac
214 shift
215 done
217 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init \
218 ${quiet:+--quiet} \
219 -- \
220 "$@"
224 # Unregister submodules from .git/config and remove their work tree
226 cmd_deinit()
228 # parse $args after "submodule ... deinit".
229 deinit_all=
230 while test $# -ne 0
232 case "$1" in
233 -f|--force)
234 force=$1
236 -q|--quiet)
237 quiet=1
239 --all)
240 deinit_all=t
243 shift
244 break
247 usage
250 break
252 esac
253 shift
254 done
256 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \
257 ${quiet:+--quiet} \
258 ${force:+--force} \
259 ${deinit_all:+--all} \
260 -- \
261 "$@"
265 # Update each submodule path to correct revision, using clone and checkout as needed
267 # $@ = requested paths (default to all)
269 cmd_update()
271 # parse $args after "submodule ... update".
272 while test $# -ne 0
274 case "$1" in
275 -q|--quiet)
276 quiet=1
278 -v|--verbose)
279 quiet=0
281 --progress)
282 progress=1
284 -i|--init)
285 init=1
287 --require-init)
288 require_init=1
290 --remote)
291 remote=1
293 -N|--no-fetch)
294 nofetch=1
296 -f|--force)
297 force=$1
299 -r|--rebase)
300 rebase=1
302 --ref-format)
303 case "$2" in '') usage ;; esac
304 ref_format="--ref-format=$2"
305 shift
307 --ref-format=*)
308 ref_format="$1"
310 --reference)
311 case "$2" in '') usage ;; esac
312 reference="--reference=$2"
313 shift
315 --reference=*)
316 reference="$1"
318 --dissociate)
319 dissociate=1
321 -m|--merge)
322 merge=1
324 --recursive)
325 recursive=1
327 --checkout)
328 checkout=1
330 --recommend-shallow)
331 recommend_shallow="--recommend-shallow"
333 --no-recommend-shallow)
334 recommend_shallow="--no-recommend-shallow"
336 --depth)
337 case "$2" in '') usage ;; esac
338 depth="--depth=$2"
339 shift
341 --depth=*)
342 depth=$1
344 -j|--jobs)
345 case "$2" in '') usage ;; esac
346 jobs="--jobs=$2"
347 shift
349 --jobs=*)
350 jobs=$1
352 --single-branch)
353 single_branch="--single-branch"
355 --no-single-branch)
356 single_branch="--no-single-branch"
358 --filter)
359 case "$2" in '') usage ;; esac
360 filter="--filter=$2"
361 shift
363 --filter=*)
364 filter="$1"
367 shift
368 break
371 usage
374 break
376 esac
377 shift
378 done
380 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper update \
381 ${quiet:+--quiet} \
382 ${force:+--force} \
383 ${progress:+"--progress"} \
384 ${remote:+--remote} \
385 ${recursive:+--recursive} \
386 ${init:+--init} \
387 ${nofetch:+--no-fetch} \
388 ${rebase:+--rebase} \
389 ${merge:+--merge} \
390 ${checkout:+--checkout} \
391 ${ref_format:+"$ref_format"} \
392 ${reference:+"$reference"} \
393 ${dissociate:+"--dissociate"} \
394 ${depth:+"$depth"} \
395 ${require_init:+--require-init} \
396 ${dissociate:+"--dissociate"} \
397 $single_branch \
398 $recommend_shallow \
399 $jobs \
400 $filter \
401 -- \
402 "$@"
406 # Configures a submodule's default branch
408 # $@ = requested path
410 cmd_set_branch() {
411 default=
412 branch=
414 while test $# -ne 0
416 case "$1" in
417 -q|--quiet)
418 # we don't do anything with this but we need to accept it
420 -d|--default)
421 default=1
423 -b|--branch)
424 case "$2" in '') usage ;; esac
425 branch=$2
426 shift
429 shift
430 break
433 usage
436 break
438 esac
439 shift
440 done
442 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch \
443 ${quiet:+--quiet} \
444 ${branch:+--branch "$branch"} \
445 ${default:+--default} \
446 -- \
447 "$@"
451 # Configures a submodule's remote url
453 # $@ = requested path, requested url
455 cmd_set_url() {
456 while test $# -ne 0
458 case "$1" in
459 -q|--quiet)
460 quiet=1
463 shift
464 break
467 usage
470 break
472 esac
473 shift
474 done
476 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url \
477 ${quiet:+--quiet} \
478 -- \
479 "$@"
483 # Show commit summary for submodules in index or working tree
485 # If '--cached' is given, show summary between index and given commit,
486 # or between working tree and given commit
488 # $@ = [commit (default 'HEAD'),] requested paths (default all)
490 cmd_summary() {
491 summary_limit=-1
492 for_status=
493 diff_cmd=diff-index
495 # parse $args after "submodule ... summary".
496 while test $# -ne 0
498 case "$1" in
499 --cached)
500 cached=1
502 --files)
503 files="$1"
505 --for-status)
506 for_status="$1"
508 -n|--summary-limit)
509 summary_limit="$2"
510 isnumber "$summary_limit" || usage
511 shift
513 --summary-limit=*)
514 summary_limit="${1#--summary-limit=}"
515 isnumber "$summary_limit" || usage
518 shift
519 break
522 usage
525 break
527 esac
528 shift
529 done
531 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary \
532 ${files:+--files} \
533 ${cached:+--cached} \
534 ${for_status:+--for-status} \
535 ${summary_limit:+-n $summary_limit} \
536 -- \
537 "$@"
540 # List all submodules, prefixed with:
541 # - submodule not initialized
542 # + different revision checked out
544 # If --cached was specified the revision in the index will be printed
545 # instead of the currently checked out revision.
547 # $@ = requested paths (default to all)
549 cmd_status()
551 # parse $args after "submodule ... status".
552 while test $# -ne 0
554 case "$1" in
555 -q|--quiet)
556 quiet=1
558 --cached)
559 cached=1
561 --recursive)
562 recursive=1
565 shift
566 break
569 usage
572 break
574 esac
575 shift
576 done
578 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status \
579 ${quiet:+--quiet} \
580 ${cached:+--cached} \
581 ${recursive:+--recursive} \
582 -- \
583 "$@"
587 # Sync remote urls for submodules
588 # This makes the value for remote.$remote.url match the value
589 # specified in .gitmodules.
591 cmd_sync()
593 while test $# -ne 0
595 case "$1" in
596 -q|--quiet)
597 quiet=1
598 shift
600 --recursive)
601 recursive=1
602 shift
605 shift
606 break
609 usage
612 break
614 esac
615 done
617 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync \
618 ${quiet:+--quiet} \
619 ${recursive:+--recursive} \
620 -- \
621 "$@"
624 cmd_absorbgitdirs()
626 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper absorbgitdirs "$@"
629 # This loop parses the command line arguments to find the
630 # subcommand name to dispatch. Parsing of the subcommand specific
631 # options are primarily done by the subcommand implementations.
632 # Subcommand specific options such as --branch and --cached are
633 # parsed here as well, for backward compatibility.
635 while test $# != 0 && test -z "$command"
637 case "$1" in
638 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
639 command=$1
641 -q|--quiet)
642 quiet=1
644 --cached)
645 cached=1
648 break
651 usage
654 break
656 esac
657 shift
658 done
660 # No command word defaults to "status"
661 if test -z "$command"
662 then
663 if test $# = 0
664 then
665 command=status
666 else
667 usage
671 # "--cached" is accepted only by "status" and "summary"
672 if test -n "$cached" && test "$command" != status && test "$command" != summary
673 then
674 usage
677 "cmd_$(echo $command | sed -e s/-/_/g)" "$@"