git-prompt: use here-doc instead of here-string
[git/gitster.git] / git-submodule.sh
blob7f9582d92343450e0c71ba72e5faee845cc1165b
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 -v|--verbose)
248 quiet=0
250 --progress)
251 progress=1
253 -i|--init)
254 init=1
256 --require-init)
257 require_init=1
259 --remote)
260 remote=1
262 -N|--no-fetch)
263 nofetch=1
265 -f|--force)
266 force=$1
268 -r|--rebase)
269 rebase=1
271 --reference)
272 case "$2" in '') usage ;; esac
273 reference="--reference=$2"
274 shift
276 --reference=*)
277 reference="$1"
279 --dissociate)
280 dissociate=1
282 -m|--merge)
283 merge=1
285 --recursive)
286 recursive=1
288 --checkout)
289 checkout=1
291 --recommend-shallow)
292 recommend_shallow="--recommend-shallow"
294 --no-recommend-shallow)
295 recommend_shallow="--no-recommend-shallow"
297 --depth)
298 case "$2" in '') usage ;; esac
299 depth="--depth=$2"
300 shift
302 --depth=*)
303 depth=$1
305 -j|--jobs)
306 case "$2" in '') usage ;; esac
307 jobs="--jobs=$2"
308 shift
310 --jobs=*)
311 jobs=$1
313 --single-branch)
314 single_branch="--single-branch"
316 --no-single-branch)
317 single_branch="--no-single-branch"
319 --filter)
320 case "$2" in '') usage ;; esac
321 filter="--filter=$2"
322 shift
324 --filter=*)
325 filter="$1"
328 shift
329 break
332 usage
335 break
337 esac
338 shift
339 done
341 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper update \
342 ${quiet:+--quiet} \
343 ${force:+--force} \
344 ${progress:+"--progress"} \
345 ${remote:+--remote} \
346 ${recursive:+--recursive} \
347 ${init:+--init} \
348 ${nofetch:+--no-fetch} \
349 ${rebase:+--rebase} \
350 ${merge:+--merge} \
351 ${checkout:+--checkout} \
352 ${reference:+"$reference"} \
353 ${dissociate:+"--dissociate"} \
354 ${depth:+"$depth"} \
355 ${require_init:+--require-init} \
356 ${dissociate:+"--dissociate"} \
357 $single_branch \
358 $recommend_shallow \
359 $jobs \
360 $filter \
361 -- \
362 "$@"
366 # Configures a submodule's default branch
368 # $@ = requested path
370 cmd_set_branch() {
371 default=
372 branch=
374 while test $# -ne 0
376 case "$1" in
377 -q|--quiet)
378 # we don't do anything with this but we need to accept it
380 -d|--default)
381 default=1
383 -b|--branch)
384 case "$2" in '') usage ;; esac
385 branch=$2
386 shift
389 shift
390 break
393 usage
396 break
398 esac
399 shift
400 done
402 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch ${quiet:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
406 # Configures a submodule's remote url
408 # $@ = requested path, requested url
410 cmd_set_url() {
411 while test $# -ne 0
413 case "$1" in
414 -q|--quiet)
415 quiet=1
418 shift
419 break
422 usage
425 break
427 esac
428 shift
429 done
431 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url ${quiet:+--quiet} -- "$@"
435 # Show commit summary for submodules in index or working tree
437 # If '--cached' is given, show summary between index and given commit,
438 # or between working tree and given commit
440 # $@ = [commit (default 'HEAD'),] requested paths (default all)
442 cmd_summary() {
443 summary_limit=-1
444 for_status=
445 diff_cmd=diff-index
447 # parse $args after "submodule ... summary".
448 while test $# -ne 0
450 case "$1" in
451 --cached)
452 cached=1
454 --files)
455 files="$1"
457 --for-status)
458 for_status="$1"
460 -n|--summary-limit)
461 summary_limit="$2"
462 isnumber "$summary_limit" || usage
463 shift
465 --summary-limit=*)
466 summary_limit="${1#--summary-limit=}"
467 isnumber "$summary_limit" || usage
470 shift
471 break
474 usage
477 break
479 esac
480 shift
481 done
483 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary ${files:+--files} ${cached:+--cached} ${for_status:+--for-status} ${summary_limit:+-n $summary_limit} -- "$@"
486 # List all submodules, prefixed with:
487 # - submodule not initialized
488 # + different revision checked out
490 # If --cached was specified the revision in the index will be printed
491 # instead of the currently checked out revision.
493 # $@ = requested paths (default to all)
495 cmd_status()
497 # parse $args after "submodule ... status".
498 while test $# -ne 0
500 case "$1" in
501 -q|--quiet)
502 quiet=1
504 --cached)
505 cached=1
507 --recursive)
508 recursive=1
511 shift
512 break
515 usage
518 break
520 esac
521 shift
522 done
524 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status ${quiet:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
527 # Sync remote urls for submodules
528 # This makes the value for remote.$remote.url match the value
529 # specified in .gitmodules.
531 cmd_sync()
533 while test $# -ne 0
535 case "$1" in
536 -q|--quiet)
537 quiet=1
538 shift
540 --recursive)
541 recursive=1
542 shift
545 shift
546 break
549 usage
552 break
554 esac
555 done
557 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync ${quiet:+--quiet} ${recursive:+--recursive} -- "$@"
560 cmd_absorbgitdirs()
562 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper absorbgitdirs "$@"
565 # This loop parses the command line arguments to find the
566 # subcommand name to dispatch. Parsing of the subcommand specific
567 # options are primarily done by the subcommand implementations.
568 # Subcommand specific options such as --branch and --cached are
569 # parsed here as well, for backward compatibility.
571 while test $# != 0 && test -z "$command"
573 case "$1" in
574 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
575 command=$1
577 -q|--quiet)
578 quiet=1
580 --cached)
581 cached=1
584 break
587 usage
590 break
592 esac
593 shift
594 done
596 # No command word defaults to "status"
597 if test -z "$command"
598 then
599 if test $# = 0
600 then
601 command=status
602 else
603 usage
607 # "--cached" is accepted only by "status" and "summary"
608 if test -n "$cached" && test "$command" != status && test "$command" != summary
609 then
610 usage
613 "cmd_$(echo $command | sed -e s/-/_/g)" "$@"