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] [--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
55 if test "$1" = "#unmatched"
63 n
=$
(($1 + 0)) 2>/dev
/null
&& test "$n" = "$1"
66 # Given a full hex object ID, is this the zero OID?
68 echo "$1" | sane_egrep
'^0+$' >/dev
/null
2>&1
71 # Sanitize the local git environment for use within a submodule. We
72 # can't simply use clear_local_git_env since we want to preserve some
73 # of the settings from GIT_CONFIG_PARAMETERS.
74 sanitize_submodule_env
()
76 save_config
=$GIT_CONFIG_PARAMETERS
78 GIT_CONFIG_PARAMETERS
=$save_config
79 export GIT_CONFIG_PARAMETERS
83 # Add a new submodule to the working tree, .gitmodules and the index
87 # optional branch is stored in global branch variable
91 # parse $args after "submodule ... add".
97 case "$2" in '') usage
;; esac
111 case "$2" in '') usage
;; esac
116 reference_path
="${1#--reference=}"
122 case "$2" in '') usage
;; esac
127 case "$2" in '') usage
;; esac
153 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"} -- "$@"
157 # Execute an arbitrary command sequence in each checked out
160 # $@ = command to execute
164 # parse $args after "submodule ... foreach".
184 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
188 # Register submodules in .git/config
190 # $@ = requested paths (default to all)
194 # parse $args after "submodule ... init".
215 git
${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} -- "$@"
219 # Unregister submodules from .git/config and remove their work tree
223 # parse $args after "submodule ... deinit".
251 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${force:+--force} ${deinit_all:+--all} -- "$@"
254 # usage: fetch_in_submodule <module_path> [<depth>] [<sha1>]
255 # Because arguments are positional, use an empty string to omit <depth>
256 # but include <sha1>.
257 fetch_in_submodule
() (
258 sanitize_submodule_env
&&
262 echo "$3" | git fetch
${GIT_QUIET:+--quiet} --stdin ${2:+"$2"}
264 git fetch
${GIT_QUIET:+--quiet} ${2:+"$2"}
269 # Update each submodule path to correct revision, using clone and checkout as needed
271 # $@ = requested paths (default to all)
275 # parse $args after "submodule ... update".
308 case "$2" in '') usage
;; esac
309 reference
="--reference=$2"
328 recommend_shallow
="--recommend-shallow"
330 --no-recommend-shallow)
331 recommend_shallow
="--no-recommend-shallow"
334 case "$2" in '') usage
;; esac
342 case "$2" in '') usage
;; esac
350 single_branch
="--single-branch"
353 single_branch
="--no-single-branch"
371 cmd_init
"--" "$@" ||
return
375 git submodule--helper update-clone
${GIT_QUIET:+--quiet} \
376 ${progress:+"--progress"} \
377 ${wt_prefix:+--prefix "$wt_prefix"} \
378 ${prefix:+--recursive-prefix "$prefix"} \
379 ${update:+--update "$update"} \
380 ${reference:+"$reference"} \
381 ${dissociate:+"--dissociate"} \
382 ${depth:+--depth "$depth"} \
383 ${require_init:+--require-init} \
388 "$@" ||
echo "#unmatched" $?
391 while read -r quickabort sha1 just_cloned sm_path
393 die_if_unmatched
"$quickabort" "$sha1"
395 git submodule--helper ensure-core-worktree
"$sm_path" ||
exit 1
397 displaypath
=$
(git submodule--helper relative-path
"$prefix$sm_path" "$wt_prefix")
399 if test $just_cloned -eq 1
404 subsha1
=$
(sanitize_submodule_env
; cd "$sm_path" &&
405 git rev-parse
--verify HEAD
) ||
406 die
"fatal: $(eval_gettext "Unable to
find current revision
in submodule path
'\$displaypath'")"
411 branch
=$
(git submodule--helper remote-branch
"$sm_path")
412 if test -z "$nofetch"
414 # Fetch remote before determining tracking $sha1
415 fetch_in_submodule
"$sm_path" $depth ||
416 die
"fatal: $(eval_gettext "Unable to fetch
in submodule path
'\$sm_path'")"
418 remote_name
=$
(sanitize_submodule_env
; cd "$sm_path" && git submodule--helper print-default-remote
)
419 sha1
=$
(sanitize_submodule_env
; cd "$sm_path" &&
420 git rev-parse
--verify "${remote_name}/${branch}") ||
421 die
"fatal: $(eval_gettext "Unable to
find current \
${remote_name}/\
${branch} revision
in submodule path
'\$sm_path'")"
424 out
=$
(git submodule--helper run-update-procedure \
425 ${wt_prefix:+--prefix "$wt_prefix"} \
426 ${GIT_QUIET:+--quiet} \
428 ${just_cloned:+--just-cloned} \
429 ${nofetch:+--no-fetch} \
431 ${update:+--update "$update"} \
432 ${prefix:+--recursive-prefix "$prefix"} \
433 ${sha1:+--oid "$sha1"} \
434 ${subsha1:+--suboid "$subsha1"} \
438 # exit codes for run-update-procedure:
439 # 0: update was successful, say command output
440 # 1: update procedure failed, but should not die
441 # 2 or 128: subcommand died during execution
442 # 3: no update procedure was run
449 err
="${err};fatal: $out"
453 die_with_status
$res "fatal: $out"
457 if test -n "$recursive"
460 prefix
=$
(git submodule--helper relative-path
"$prefix$sm_path/" "$wt_prefix")
462 sanitize_submodule_env
469 die_msg
="fatal: $(eval_gettext "Failed to recurse into submodule path
'\$displaypath'")"
472 err
="${err};$die_msg"
475 die_with_status
$res "$die_msg"
499 # Configures a submodule's default branch
501 # $@ = requested path
511 # we don't do anything with this but we need to accept it
517 case "$2" in '') usage
;; esac
535 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch ${GIT_QUIET:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
539 # Configures a submodule's remote url
541 # $@ = requested path, requested url
564 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url
${GIT_QUIET:+--quiet} -- "$@"
568 # Show commit summary for submodules in index or working tree
570 # If '--cached' is given, show summary between index and given commit,
571 # or between working tree and given commit
573 # $@ = [commit (default 'HEAD'),] requested paths (default all)
580 # parse $args after "submodule ... summary".
595 isnumber
"$summary_limit" || usage
599 summary_limit
="${1#--summary-limit=}"
600 isnumber
"$summary_limit" || usage
616 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper summary ${files:+--files} ${cached:+--cached} ${for_status:+--for-status} ${summary_limit:+-n $summary_limit} -- "$@"
619 # List all submodules, prefixed with:
620 # - submodule not initialized
621 # + different revision checked out
623 # If --cached was specified the revision in the index will be printed
624 # instead of the currently checked out revision.
626 # $@ = requested paths (default to all)
630 # parse $args after "submodule ... status".
657 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
660 # Sync remote urls for submodules
661 # This makes the value for remote.$remote.url match the value
662 # specified in .gitmodules.
690 git
${wt_prefix:+-C "$wt_prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
695 git submodule--helper absorb-git-dirs
--prefix "$wt_prefix" "$@"
698 # This loop parses the command line arguments to find the
699 # subcommand name to dispatch. Parsing of the subcommand specific
700 # options are primarily done by the subcommand implementations.
701 # Subcommand specific options such as --branch and --cached are
702 # parsed here as well, for backward compatibility.
704 while test $# != 0 && test -z "$command"
707 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs
)
737 # No command word defaults to "status"
738 if test -z "$command"
748 # "-b branch" is accepted only by "add" and "set-branch"
749 if test -n "$branch" && (test "$command" != add ||
test "$command" != set-branch
)
754 # "--cached" is accepted only by "status" and "summary"
755 if test -n "$cached" && test "$command" != status
&& test "$command" != summary
760 "cmd_$(echo $command | sed -e s/-/_/g)" "$@"