The fifth batch
[alt-git.git] / git-submodule.sh
blob2999b31fad3e9747faa14edc82dae64b64ff7297
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 no_fetch=
44 rebase=
45 merge=
46 checkout=
47 name=
48 depth=
49 progress=
50 dissociate=
51 single_branch=
52 jobs=
53 recommend_shallow=
54 filter=
55 all=
56 default=
57 summary_limit=
58 for_status=
61 # Add a new submodule to the working tree, .gitmodules and the index
63 # $@ = repo path
65 # optional branch is stored in global branch variable
67 cmd_add()
69 # parse $args after "submodule ... add".
70 while test $# -ne 0
72 case "$1" in
73 -b | --branch)
74 case "$2" in '') usage ;; esac
75 branch="--branch=$2"
76 shift
78 -b* | --branch=*)
79 branch="$1"
81 -f | --force)
82 force=$1
84 -q|--quiet)
85 quiet=$1
87 --progress)
88 progress=$1
90 --reference)
91 case "$2" in '') usage ;; esac
92 reference="--reference=$2"
93 shift
95 --reference=*)
96 reference="$1"
98 --ref-format)
99 case "$2" in '') usage ;; esac
100 ref_format="--ref-format=$2"
101 shift
103 --ref-format=*)
104 ref_format="$1"
106 --dissociate)
107 dissociate=$1
109 --name)
110 case "$2" in '') usage ;; esac
111 name="--name=$2"
112 shift
114 --name=*)
115 name="$1"
117 --depth)
118 case "$2" in '') usage ;; esac
119 depth="--depth=$2"
120 shift
122 --depth=*)
123 depth="$1"
126 shift
127 break
130 usage
133 break
135 esac
136 shift
137 done
139 if test -z "$1"
140 then
141 usage
144 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper add \
145 $quiet \
146 $force \
147 $progress \
148 ${branch:+"$branch"} \
149 ${reference:+"$reference"} \
150 ${ref_format:+"$ref_format"} \
151 $dissociate \
152 ${name:+"$name"} \
153 ${depth:+"$depth"} \
154 -- \
155 "$@"
159 # Execute an arbitrary command sequence in each checked out
160 # submodule
162 # $@ = command to execute
164 cmd_foreach()
166 # parse $args after "submodule ... foreach".
167 while test $# -ne 0
169 case "$1" in
170 -q|--quiet)
171 quiet=$1
173 --recursive)
174 recursive=$1
177 usage
180 break
182 esac
183 shift
184 done
186 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach \
187 $quiet \
188 $recursive \
189 -- \
190 "$@"
194 # Register submodules in .git/config
196 # $@ = requested paths (default to all)
198 cmd_init()
200 # parse $args after "submodule ... init".
201 while test $# -ne 0
203 case "$1" in
204 -q|--quiet)
205 quiet=$1
208 shift
209 break
212 usage
215 break
217 esac
218 shift
219 done
221 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init \
222 $quiet \
223 -- \
224 "$@"
228 # Unregister submodules from .git/config and remove their work tree
230 cmd_deinit()
232 # parse $args after "submodule ... deinit".
233 while test $# -ne 0
235 case "$1" in
236 -f|--force)
237 force=$1
239 -q|--quiet)
240 quiet=$1
242 --all)
243 all=$1
246 shift
247 break
250 usage
253 break
255 esac
256 shift
257 done
259 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \
260 $quiet \
261 $force \
262 $all \
263 -- \
264 "$@"
268 # Update each submodule path to correct revision, using clone and checkout as needed
270 # $@ = requested paths (default to all)
272 cmd_update()
274 # parse $args after "submodule ... update".
275 while test $# -ne 0
277 case "$1" in
278 -q|--quiet)
279 quiet=$1
281 -v|--verbose)
282 quiet=
284 --progress)
285 progress=$1
287 -i|--init)
288 init=$1
290 --require-init)
291 require_init=$1
293 --remote)
294 remote=$1
296 -N|--no-fetch)
297 no_fetch=$1
299 -f|--force)
300 force=$1
302 -r|--rebase)
303 rebase=$1
305 --ref-format)
306 case "$2" in '') usage ;; esac
307 ref_format="--ref-format=$2"
308 shift
310 --ref-format=*)
311 ref_format="$1"
313 --reference)
314 case "$2" in '') usage ;; esac
315 reference="--reference=$2"
316 shift
318 --reference=*)
319 reference="$1"
321 --dissociate)
322 dissociate=$1
324 -m|--merge)
325 merge=$1
327 --recursive)
328 recursive=$1
330 --checkout)
331 checkout=$1
333 --recommend-shallow|--no-recommend-shallow)
334 recommend_shallow=$1
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 -j*|--jobs=*)
350 jobs="$1"
352 --single-branch|--no-single-branch)
353 single_branch=$1
355 --filter)
356 case "$2" in '') usage ;; esac
357 filter="--filter=$2"
358 shift
360 --filter=*)
361 filter="$1"
364 shift
365 break
368 usage
371 break
373 esac
374 shift
375 done
377 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper update \
378 $quiet \
379 $force \
380 $progress \
381 $remote \
382 $recursive \
383 $init \
384 $no_fetch \
385 $rebase \
386 $merge \
387 $checkout \
388 ${ref_format:+"$ref_format"} \
389 ${reference:+"$reference"} \
390 $dissociate \
391 ${depth:+"$depth"} \
392 $require_init \
393 $single_branch \
394 $recommend_shallow \
395 $jobs \
396 $filter \
397 -- \
398 "$@"
402 # Configures a submodule's default branch
404 # $@ = requested path
406 cmd_set_branch() {
407 # parse $args after "submodule ... set-branch".
408 while test $# -ne 0
410 case "$1" in
411 -q|--quiet)
412 # we don't do anything with this but we need to accept it
414 -d|--default)
415 default=$1
417 -b|--branch)
418 case "$2" in '') usage ;; esac
419 branch="--branch=$2"
420 shift
422 -b*|--branch=*)
423 branch="$1"
426 shift
427 break
430 usage
433 break
435 esac
436 shift
437 done
439 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch \
440 $quiet \
441 ${branch:+"$branch"} \
442 $default \
443 -- \
444 "$@"
448 # Configures a submodule's remote url
450 # $@ = requested path, requested url
452 cmd_set_url() {
453 # parse $args after "submodule ... set-url".
454 while test $# -ne 0
456 case "$1" in
457 -q|--quiet)
458 quiet=$1
461 shift
462 break
465 usage
468 break
470 esac
471 shift
472 done
474 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url \
475 $quiet \
476 -- \
477 "$@"
481 # Show commit summary for submodules in index or working tree
483 # If '--cached' is given, show summary between index and given commit,
484 # or between working tree and given commit
486 # $@ = [commit (default 'HEAD'),] requested paths (default all)
488 cmd_summary() {
489 # parse $args after "submodule ... summary".
490 while test $# -ne 0
492 case "$1" in
493 --cached)
494 cached=$1
496 --files)
497 files=$1
499 --for-status)
500 for_status=$1
502 -n|--summary-limit)
503 case "$2" in '') usage ;; esac
504 summary_limit="--summary-limit=$2"
505 shift
507 -n*|--summary-limit=*)
508 summary_limit="$1"
511 shift
512 break
515 usage
518 break
520 esac
521 shift
522 done
524 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary \
525 $files \
526 $cached \
527 $for_status \
528 ${summary_limit:+"$summary_limit"} \
529 -- \
530 "$@"
533 # List all submodules, prefixed with:
534 # - submodule not initialized
535 # + different revision checked out
537 # If --cached was specified the revision in the index will be printed
538 # instead of the currently checked out revision.
540 # $@ = requested paths (default to all)
542 cmd_status()
544 # parse $args after "submodule ... status".
545 while test $# -ne 0
547 case "$1" in
548 -q|--quiet)
549 quiet=$1
551 --cached)
552 cached=$1
554 --recursive)
555 recursive=$1
558 shift
559 break
562 usage
565 break
567 esac
568 shift
569 done
571 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status \
572 $quiet \
573 $cached \
574 $recursive \
575 -- \
576 "$@"
580 # Sync remote urls for submodules
581 # This makes the value for remote.$remote.url match the value
582 # specified in .gitmodules.
584 cmd_sync()
586 # parse $args after "submodule ... sync".
587 while test $# -ne 0
589 case "$1" in
590 -q|--quiet)
591 quiet=$1
592 shift
594 --recursive)
595 recursive=$1
596 shift
599 shift
600 break
603 usage
606 break
608 esac
609 done
611 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync \
612 $quiet \
613 $recursive \
614 -- \
615 "$@"
618 cmd_absorbgitdirs()
620 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper absorbgitdirs "$@"
623 # This loop parses the command line arguments to find the
624 # subcommand name to dispatch. Parsing of the subcommand specific
625 # options are primarily done by the subcommand implementations.
626 # Subcommand specific options such as --branch and --cached are
627 # parsed here as well, for backward compatibility.
629 while test $# != 0 && test -z "$command"
631 case "$1" in
632 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
633 command=$1
635 -q|--quiet)
636 quiet=$1
638 --cached)
639 cached=$1
642 break
645 usage
648 break
650 esac
651 shift
652 done
654 # No command word defaults to "status"
655 if test -z "$command"
656 then
657 if test $# = 0
658 then
659 command=status
660 else
661 usage
665 # "--cached" is accepted only by "status" and "summary"
666 if test -n "$cached" && test "$command" != status && test "$command" != summary
667 then
668 usage
671 "cmd_$(echo $command | sed -e s/-/_/g)" "$@"