]> git.ipfire.org Git - thirdparty/git.git/blob - git-submodule.sh
Merge branch 'en/sparse-checkout-fixes'
[thirdparty/git.git] / git-submodule.sh
1 #!/bin/sh
2 #
3 # git-submodule.sh: add, init, update or list git submodules
4 #
5 # Copyright (c) 2007 Lars Hjemli
6
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
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.
29 GIT_PROTOCOL_FROM_USER=0
30 export GIT_PROTOCOL_FROM_USER
31
32 command=
33 branch=
34 force=
35 reference=
36 cached=
37 recursive=
38 init=
39 require_init=
40 files=
41 remote=
42 nofetch=
43 update=
44 prefix=
45 custom_name=
46 depth=
47 progress=
48 dissociate=
49 single_branch=
50 jobs=
51 recommend_shallow=
52 filter=
53
54 die_if_unmatched ()
55 {
56 if test "$1" = "#unmatched"
57 then
58 exit ${2:-1}
59 fi
60 }
61
62 isnumber()
63 {
64 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
65 }
66
67 # Sanitize the local git environment for use within a submodule. We
68 # can't simply use clear_local_git_env since we want to preserve some
69 # of the settings from GIT_CONFIG_PARAMETERS.
70 sanitize_submodule_env()
71 {
72 save_config=$GIT_CONFIG_PARAMETERS
73 clear_local_git_env
74 GIT_CONFIG_PARAMETERS=$save_config
75 export GIT_CONFIG_PARAMETERS
76 }
77
78 #
79 # Add a new submodule to the working tree, .gitmodules and the index
80 #
81 # $@ = repo path
82 #
83 # optional branch is stored in global branch variable
84 #
85 cmd_add()
86 {
87 # parse $args after "submodule ... add".
88 reference_path=
89 while test $# -ne 0
90 do
91 case "$1" in
92 -b | --branch)
93 case "$2" in '') usage ;; esac
94 branch=$2
95 shift
96 ;;
97 -f | --force)
98 force=$1
99 ;;
100 -q|--quiet)
101 GIT_QUIET=1
102 ;;
103 --progress)
104 progress=1
105 ;;
106 --reference)
107 case "$2" in '') usage ;; esac
108 reference_path=$2
109 shift
110 ;;
111 --reference=*)
112 reference_path="${1#--reference=}"
113 ;;
114 --dissociate)
115 dissociate=1
116 ;;
117 --name)
118 case "$2" in '') usage ;; esac
119 custom_name=$2
120 shift
121 ;;
122 --depth)
123 case "$2" in '') usage ;; esac
124 depth="--depth=$2"
125 shift
126 ;;
127 --depth=*)
128 depth=$1
129 ;;
130 --)
131 shift
132 break
133 ;;
134 -*)
135 usage
136 ;;
137 *)
138 break
139 ;;
140 esac
141 shift
142 done
143
144 if test -z "$1"
145 then
146 usage
147 fi
148
149 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper add ${GIT_QUIET:+--quiet} ${force:+--force} ${progress:+"--progress"} ${branch:+--branch "$branch"} ${reference_path:+--reference "$reference_path"} ${dissociate:+--dissociate} ${custom_name:+--name "$custom_name"} ${depth:+"$depth"} -- "$@"
150 }
151
152 #
153 # Execute an arbitrary command sequence in each checked out
154 # submodule
155 #
156 # $@ = command to execute
157 #
158 cmd_foreach()
159 {
160 # parse $args after "submodule ... foreach".
161 while test $# -ne 0
162 do
163 case "$1" in
164 -q|--quiet)
165 GIT_QUIET=1
166 ;;
167 --recursive)
168 recursive=1
169 ;;
170 -*)
171 usage
172 ;;
173 *)
174 break
175 ;;
176 esac
177 shift
178 done
179
180 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
181 }
182
183 #
184 # Register submodules in .git/config
185 #
186 # $@ = requested paths (default to all)
187 #
188 cmd_init()
189 {
190 # parse $args after "submodule ... init".
191 while test $# -ne 0
192 do
193 case "$1" in
194 -q|--quiet)
195 GIT_QUIET=1
196 ;;
197 --)
198 shift
199 break
200 ;;
201 -*)
202 usage
203 ;;
204 *)
205 break
206 ;;
207 esac
208 shift
209 done
210
211 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} -- "$@"
212 }
213
214 #
215 # Unregister submodules from .git/config and remove their work tree
216 #
217 cmd_deinit()
218 {
219 # parse $args after "submodule ... deinit".
220 deinit_all=
221 while test $# -ne 0
222 do
223 case "$1" in
224 -f|--force)
225 force=$1
226 ;;
227 -q|--quiet)
228 GIT_QUIET=1
229 ;;
230 --all)
231 deinit_all=t
232 ;;
233 --)
234 shift
235 break
236 ;;
237 -*)
238 usage
239 ;;
240 *)
241 break
242 ;;
243 esac
244 shift
245 done
246
247 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${force:+--force} ${deinit_all:+--all} -- "$@"
248 }
249
250 # usage: fetch_in_submodule <module_path> [<depth>] [<sha1>]
251 # Because arguments are positional, use an empty string to omit <depth>
252 # but include <sha1>.
253 fetch_in_submodule () (
254 sanitize_submodule_env &&
255 cd "$1" &&
256 if test $# -eq 3
257 then
258 echo "$3" | git fetch ${GIT_QUIET:+--quiet} --stdin ${2:+"$2"}
259 else
260 git fetch ${GIT_QUIET:+--quiet} ${2:+"$2"}
261 fi
262 )
263
264 #
265 # Update each submodule path to correct revision, using clone and checkout as needed
266 #
267 # $@ = requested paths (default to all)
268 #
269 cmd_update()
270 {
271 # parse $args after "submodule ... update".
272 while test $# -ne 0
273 do
274 case "$1" in
275 -q|--quiet)
276 GIT_QUIET=1
277 ;;
278 -v)
279 unset GIT_QUIET
280 ;;
281 --progress)
282 progress=1
283 ;;
284 -i|--init)
285 init=1
286 ;;
287 --require-init)
288 init=1
289 require_init=1
290 ;;
291 --remote)
292 remote=1
293 ;;
294 -N|--no-fetch)
295 nofetch=1
296 ;;
297 -f|--force)
298 force=$1
299 ;;
300 -r|--rebase)
301 update="rebase"
302 ;;
303 --reference)
304 case "$2" in '') usage ;; esac
305 reference="--reference=$2"
306 shift
307 ;;
308 --reference=*)
309 reference="$1"
310 ;;
311 --dissociate)
312 dissociate=1
313 ;;
314 -m|--merge)
315 update="merge"
316 ;;
317 --recursive)
318 recursive=1
319 ;;
320 --checkout)
321 update="checkout"
322 ;;
323 --recommend-shallow)
324 recommend_shallow="--recommend-shallow"
325 ;;
326 --no-recommend-shallow)
327 recommend_shallow="--no-recommend-shallow"
328 ;;
329 --depth)
330 case "$2" in '') usage ;; esac
331 depth="--depth=$2"
332 shift
333 ;;
334 --depth=*)
335 depth=$1
336 ;;
337 -j|--jobs)
338 case "$2" in '') usage ;; esac
339 jobs="--jobs=$2"
340 shift
341 ;;
342 --jobs=*)
343 jobs=$1
344 ;;
345 --single-branch)
346 single_branch="--single-branch"
347 ;;
348 --no-single-branch)
349 single_branch="--no-single-branch"
350 ;;
351 --filter)
352 case "$2" in '') usage ;; esac
353 filter="--filter=$2"
354 shift
355 ;;
356 --filter=*)
357 filter="$1"
358 ;;
359 --)
360 shift
361 break
362 ;;
363 -*)
364 usage
365 ;;
366 *)
367 break
368 ;;
369 esac
370 shift
371 done
372
373 if test -n "$filter" && test "$init" != "1"
374 then
375 usage
376 fi
377
378 if test -n "$init"
379 then
380 cmd_init "--" "$@" || return
381 fi
382
383 {
384 git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
385 ${progress:+"--progress"} \
386 ${wt_prefix:+--prefix "$wt_prefix"} \
387 ${prefix:+--recursive-prefix "$prefix"} \
388 ${update:+--update "$update"} \
389 ${reference:+"$reference"} \
390 ${dissociate:+"--dissociate"} \
391 ${depth:+--depth "$depth"} \
392 ${require_init:+--require-init} \
393 $single_branch \
394 $recommend_shallow \
395 $jobs \
396 $filter \
397 -- \
398 "$@" || echo "#unmatched" $?
399 } | {
400 err=
401 while read -r quickabort sha1 just_cloned sm_path
402 do
403 die_if_unmatched "$quickabort" "$sha1"
404
405 git submodule--helper ensure-core-worktree "$sm_path" || exit 1
406
407 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
408
409 if test $just_cloned -eq 1
410 then
411 subsha1=
412 else
413 just_cloned=
414 subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
415 git rev-parse --verify HEAD) ||
416 die "fatal: $(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
417 fi
418
419 if test -n "$remote"
420 then
421 branch=$(git submodule--helper remote-branch "$sm_path")
422 if test -z "$nofetch"
423 then
424 # Fetch remote before determining tracking $sha1
425 fetch_in_submodule "$sm_path" $depth ||
426 die "fatal: $(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
427 fi
428 remote_name=$(sanitize_submodule_env; cd "$sm_path" && git submodule--helper print-default-remote)
429 sha1=$(sanitize_submodule_env; cd "$sm_path" &&
430 git rev-parse --verify "${remote_name}/${branch}") ||
431 die "fatal: $(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")"
432 fi
433
434 out=$(git submodule--helper run-update-procedure \
435 ${wt_prefix:+--prefix "$wt_prefix"} \
436 ${GIT_QUIET:+--quiet} \
437 ${force:+--force} \
438 ${just_cloned:+--just-cloned} \
439 ${nofetch:+--no-fetch} \
440 ${depth:+"$depth"} \
441 ${update:+--update "$update"} \
442 ${prefix:+--recursive-prefix "$prefix"} \
443 ${sha1:+--oid "$sha1"} \
444 ${subsha1:+--suboid "$subsha1"} \
445 "--" \
446 "$sm_path")
447
448 # exit codes for run-update-procedure:
449 # 0: update was successful, say command output
450 # 1: update procedure failed, but should not die
451 # 2 or 128: subcommand died during execution
452 # 3: no update procedure was run
453 res="$?"
454 case $res in
455 0)
456 say "$out"
457 ;;
458 1)
459 err="${err};fatal: $out"
460 continue
461 ;;
462 2|128)
463 die_with_status $res "fatal: $out"
464 ;;
465 esac
466
467 if test -n "$recursive"
468 then
469 (
470 prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
471 wt_prefix=
472 sanitize_submodule_env
473 cd "$sm_path" &&
474 eval cmd_update
475 )
476 res=$?
477 if test $res -gt 0
478 then
479 die_msg="fatal: $(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
480 if test $res -ne 2
481 then
482 err="${err};$die_msg"
483 continue
484 else
485 die_with_status $res "$die_msg"
486 fi
487 fi
488 fi
489 done
490
491 if test -n "$err"
492 then
493 OIFS=$IFS
494 IFS=';'
495 for e in $err
496 do
497 if test -n "$e"
498 then
499 echo >&2 "$e"
500 fi
501 done
502 IFS=$OIFS
503 exit 1
504 fi
505 }
506 }
507
508 #
509 # Configures a submodule's default branch
510 #
511 # $@ = requested path
512 #
513 cmd_set_branch() {
514 default=
515 branch=
516
517 while test $# -ne 0
518 do
519 case "$1" in
520 -q|--quiet)
521 # we don't do anything with this but we need to accept it
522 ;;
523 -d|--default)
524 default=1
525 ;;
526 -b|--branch)
527 case "$2" in '') usage ;; esac
528 branch=$2
529 shift
530 ;;
531 --)
532 shift
533 break
534 ;;
535 -*)
536 usage
537 ;;
538 *)
539 break
540 ;;
541 esac
542 shift
543 done
544
545 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch ${GIT_QUIET:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
546 }
547
548 #
549 # Configures a submodule's remote url
550 #
551 # $@ = requested path, requested url
552 #
553 cmd_set_url() {
554 while test $# -ne 0
555 do
556 case "$1" in
557 -q|--quiet)
558 GIT_QUIET=1
559 ;;
560 --)
561 shift
562 break
563 ;;
564 -*)
565 usage
566 ;;
567 *)
568 break
569 ;;
570 esac
571 shift
572 done
573
574 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url ${GIT_QUIET:+--quiet} -- "$@"
575 }
576
577 #
578 # Show commit summary for submodules in index or working tree
579 #
580 # If '--cached' is given, show summary between index and given commit,
581 # or between working tree and given commit
582 #
583 # $@ = [commit (default 'HEAD'),] requested paths (default all)
584 #
585 cmd_summary() {
586 summary_limit=-1
587 for_status=
588 diff_cmd=diff-index
589
590 # parse $args after "submodule ... summary".
591 while test $# -ne 0
592 do
593 case "$1" in
594 --cached)
595 cached="$1"
596 ;;
597 --files)
598 files="$1"
599 ;;
600 --for-status)
601 for_status="$1"
602 ;;
603 -n|--summary-limit)
604 summary_limit="$2"
605 isnumber "$summary_limit" || usage
606 shift
607 ;;
608 --summary-limit=*)
609 summary_limit="${1#--summary-limit=}"
610 isnumber "$summary_limit" || usage
611 ;;
612 --)
613 shift
614 break
615 ;;
616 -*)
617 usage
618 ;;
619 *)
620 break
621 ;;
622 esac
623 shift
624 done
625
626 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary ${files:+--files} ${cached:+--cached} ${for_status:+--for-status} ${summary_limit:+-n $summary_limit} -- "$@"
627 }
628 #
629 # List all submodules, prefixed with:
630 # - submodule not initialized
631 # + different revision checked out
632 #
633 # If --cached was specified the revision in the index will be printed
634 # instead of the currently checked out revision.
635 #
636 # $@ = requested paths (default to all)
637 #
638 cmd_status()
639 {
640 # parse $args after "submodule ... status".
641 while test $# -ne 0
642 do
643 case "$1" in
644 -q|--quiet)
645 GIT_QUIET=1
646 ;;
647 --cached)
648 cached=1
649 ;;
650 --recursive)
651 recursive=1
652 ;;
653 --)
654 shift
655 break
656 ;;
657 -*)
658 usage
659 ;;
660 *)
661 break
662 ;;
663 esac
664 shift
665 done
666
667 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
668 }
669 #
670 # Sync remote urls for submodules
671 # This makes the value for remote.$remote.url match the value
672 # specified in .gitmodules.
673 #
674 cmd_sync()
675 {
676 while test $# -ne 0
677 do
678 case "$1" in
679 -q|--quiet)
680 GIT_QUIET=1
681 shift
682 ;;
683 --recursive)
684 recursive=1
685 shift
686 ;;
687 --)
688 shift
689 break
690 ;;
691 -*)
692 usage
693 ;;
694 *)
695 break
696 ;;
697 esac
698 done
699
700 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
701 }
702
703 cmd_absorbgitdirs()
704 {
705 git submodule--helper absorb-git-dirs --prefix "$wt_prefix" "$@"
706 }
707
708 # This loop parses the command line arguments to find the
709 # subcommand name to dispatch. Parsing of the subcommand specific
710 # options are primarily done by the subcommand implementations.
711 # Subcommand specific options such as --branch and --cached are
712 # parsed here as well, for backward compatibility.
713
714 while test $# != 0 && test -z "$command"
715 do
716 case "$1" in
717 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
718 command=$1
719 ;;
720 -q|--quiet)
721 GIT_QUIET=1
722 ;;
723 -b|--branch)
724 case "$2" in
725 '')
726 usage
727 ;;
728 esac
729 branch="$2"; shift
730 ;;
731 --cached)
732 cached="$1"
733 ;;
734 --)
735 break
736 ;;
737 -*)
738 usage
739 ;;
740 *)
741 break
742 ;;
743 esac
744 shift
745 done
746
747 # No command word defaults to "status"
748 if test -z "$command"
749 then
750 if test $# = 0
751 then
752 command=status
753 else
754 usage
755 fi
756 fi
757
758 # "-b branch" is accepted only by "add" and "set-branch"
759 if test -n "$branch" && (test "$command" != add || test "$command" != set-branch)
760 then
761 usage
762 fi
763
764 # "--cached" is accepted only by "status" and "summary"
765 if test -n "$cached" && test "$command" != status && test "$command" != summary
766 then
767 usage
768 fi
769
770 "cmd_$(echo $command | sed -e s/-/_/g)" "$@"