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