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>...]"
24 wt_prefix
=$
(git rev-parse
--show-prefix)
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
58 n
=$
(($1 + 0)) 2>/dev
/null
&& test "$n" = "$1"
62 # Add a new submodule to the working tree, .gitmodules and the index
66 # optional branch is stored in global branch variable
70 # parse $args after "submodule ... add".
76 case "$2" in '') usage
;; esac
90 case "$2" in '') usage
;; esac
95 reference_path
="${1#--reference=}"
98 case "$2" in '') usage
;; esac
99 ref_format
="--ref-format=$2"
109 case "$2" in '') usage
;; esac
114 case "$2" in '') usage
;; esac
140 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper add \
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"} \
155 # Execute an arbitrary command sequence in each checked out
158 # $@ = command to execute
162 # parse $args after "submodule ... foreach".
182 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach \
184 ${recursive:+--recursive} \
190 # Register submodules in .git/config
192 # $@ = requested paths (default to all)
196 # parse $args after "submodule ... init".
217 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper init \
224 # Unregister submodules from .git/config and remove their work tree
228 # parse $args after "submodule ... deinit".
256 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \
259 ${deinit_all:+--all} \
265 # Update each submodule path to correct revision, using clone and checkout as needed
267 # $@ = requested paths (default to all)
271 # parse $args after "submodule ... update".
303 case "$2" in '') usage
;; esac
304 ref_format
="--ref-format=$2"
311 case "$2" in '') usage
;; esac
312 reference
="--reference=$2"
331 recommend_shallow
="--recommend-shallow"
333 --no-recommend-shallow)
334 recommend_shallow
="--no-recommend-shallow"
337 case "$2" in '') usage
;; esac
345 case "$2" in '') usage
;; esac
353 single_branch
="--single-branch"
356 single_branch
="--no-single-branch"
359 case "$2" in '') usage
;; esac
380 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper update \
383 ${progress:+"--progress"} \
384 ${remote:+--remote} \
385 ${recursive:+--recursive} \
387 ${nofetch:+--no-fetch} \
388 ${rebase:+--rebase} \
390 ${checkout:+--checkout} \
391 ${ref_format:+"$ref_format"} \
392 ${reference:+"$reference"} \
393 ${dissociate:+"--dissociate"} \
395 ${require_init:+--require-init} \
396 ${dissociate:+"--dissociate"} \
406 # Configures a submodule's default branch
408 # $@ = requested path
418 # we don't do anything with this but we need to accept it
424 case "$2" in '') usage
;; esac
442 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch \
444 ${branch:+--branch "$branch"} \
445 ${default:+--default} \
451 # Configures a submodule's remote url
453 # $@ = requested path, requested url
476 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url \
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)
495 # parse $args after "submodule ... summary".
510 isnumber
"$summary_limit" || usage
514 summary_limit
="${1#--summary-limit=}"
515 isnumber
"$summary_limit" || usage
531 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper summary \
533 ${cached:+--cached} \
534 ${for_status:+--for-status} \
535 ${summary_limit:+-n $summary_limit} \
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)
551 # parse $args after "submodule ... status".
578 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper status \
580 ${cached:+--cached} \
581 ${recursive:+--recursive} \
587 # Sync remote urls for submodules
588 # This makes the value for remote.$remote.url match the value
589 # specified in .gitmodules.
617 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper sync \
619 ${recursive:+--recursive} \
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"
638 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs
)
660 # No command word defaults to "status"
661 if test -z "$command"
671 # "--cached" is accepted only by "status" and "summary"
672 if test -n "$cached" && test "$command" != status
&& test "$command" != summary
677 "cmd_$(echo $command | sed -e s/-/_/g)" "$@"