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] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] deinit [-f|--force] [--] <path>...
12 or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
13 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
14 or: $dashless [--quiet] foreach [--recursive] <command>
15 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
36 # The function takes at most 2 arguments. The first argument is the
37 # URL that navigates to the submodule origin repo. When relative, this URL
38 # is relative to the superproject origin URL repo. The second up_path
39 # argument, if specified, is the relative path that navigates
40 # from the submodule working tree to the superproject working tree.
42 # The output of the function is the origin URL of the submodule.
44 # The output will either be an absolute URL or filesystem path (if the
45 # superproject origin URL is an absolute URL or filesystem path,
46 # respectively) or a relative file system path (if the superproject
47 # origin URL is a relative file system path).
49 # When the output is a relative file system path, the path is either
50 # relative to the submodule working tree, if up_path is specified, or to
51 # the superproject working tree otherwise.
52 resolve_relative_url ()
54 remote=$(get_default_remote)
55 remoteurl=$(git config "remote.$remote.url") ||
56 remoteurl=$(pwd) # the repository is its own authoritative upstream
58 remoteurl=${remoteurl%/}
71 remoteurl="./$remoteurl"
82 remoteurl="${remoteurl%/*}"
85 remoteurl="${remoteurl%:*}"
89 if test -z "$is_relative" || test "." = "$remoteurl"
91 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
105 remoteurl="$remoteurl$sep${url%/}"
106 echo "${is_relative:+${up_path}}${remoteurl#./}"
110 # Get submodule info for registered submodules
111 # $@ = path to limit submodule list
116 git ls-files -z --error-unmatch --stage -- "$@" ||
117 echo "unmatched pathspec exists"
121 my ($null_sha1) = ("0" x 40);
126 if (/^unmatched pathspec/) {
131 my ($mode, $sha1, $stage, $path) =
132 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
133 next unless $mode eq "160000";
135 if (!$unmerged{$path}++) {
136 push @out, "$mode $null_sha1 U\t$path\n";
143 print "#unmatched\n";
152 if test "$1" = "#unmatched"
159 # Print a submodule configuration setting
161 # $1 = submodule name
165 # Checks in the usual git-config places first (for overrides),
166 # otherwise it falls back on .gitmodules. This allows you to
167 # distribute project-wide defaults in .gitmodules, while still
168 # customizing individual repositories if necessary. If the option is
169 # not in .gitmodules either, print a default value.
171 get_submodule_config () {
175 value=$(git config submodule."$name"."$option")
178 value=$(git config -f .gitmodules submodule."$name"."$option")
180 printf '%s' "${value:-$default}"
185 # Map submodule path to submodule name
191 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
193 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
194 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
195 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
197 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
204 # Prior to calling, cmd_update checks that a possibly existing
205 # path is not a git repository.
206 # Likewise, cmd_add checks that path does not exist at all,
207 # since it is the location of a new submodule.
216 if test -n "$GIT_QUIET"
223 base_name=$(dirname "$name")
225 gitdir=$(git rev-parse --git-dir)
226 gitdir_base="$gitdir/modules/$base_name"
227 gitdir="$gitdir/modules/$name"
232 rm -f "$gitdir/index"
234 mkdir -p "$gitdir_base"
237 git clone $quiet -n ${reference:+"$reference"} \
238 --separate-git-dir "$gitdir" "$url" "$sm_path"
240 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
243 # We already are at the root of the work tree but cd_to_toplevel will
244 # resolve any symlinks that might be present in $PWD
245 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
246 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
247 # normalize Windows-style absolute paths to POSIX-style absolute paths
248 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
249 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
250 # Remove all common leading directories after a sanity check
251 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
252 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
254 while test "${a%%/*}" = "${b%%/*}"
259 # Now chop off the trailing '/'s that were added in the beginning
263 # Turn each leading "*/" component into "../"
264 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
265 echo "gitdir: $rel/$a" >"$sm_path/.git"
267 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
268 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
273 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
277 # Add a new submodule to the working tree, .gitmodules and the index
281 # optional branch is stored in global branch variable
285 # parse $args after "submodule ... add".
290 case "$2" in '') usage ;; esac
301 case "$2" in '') usage ;; esac
302 reference="--reference=$2"
309 case "$2" in '') usage ;; esac
330 if test -z "$sm_path"; then
331 sm_path=$(echo "$repo" |
332 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
335 if test -z "$repo" -o -z "$sm_path"; then
339 # assure repo is absolute or relative to parent
342 # dereference source url relative to parent's url
343 realrepo=$(resolve_relative_url "$repo") || exit
350 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
355 # multiple //; leading ./; /./; /../; trailing /
356 sm_path=$(printf '%s/\n' "$sm_path" |
366 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
367 die "$(eval_gettext "'\$sm_path' already exists in the index")"
369 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
371 eval_gettextln "The following path is ignored by one of your .gitignore files:
373 Use -f if you really want to add it." >&2
377 if test -n "$custom_name"
379 sm_name="$custom_name"
384 # perhaps the path exists and is already a git repo, else clone it
385 if test -e "$sm_path"
387 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
389 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
391 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
395 if test -d ".git/modules/$sm_name"
399 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
400 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
401 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
402 echo >&2 " $realrepo"
403 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
404 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
406 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
409 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
413 # ash fails to wordsplit ${branch:+-b "$branch"...}
415 '') git checkout -f -q ;;
416 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
418 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
420 git config submodule."$sm_name".url "$realrepo"
422 git add $force "$sm_path" ||
423 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
425 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
426 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
429 git config -f .gitmodules submodule."$sm_name".branch "$branch"
431 git add --force .gitmodules ||
432 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
436 # Execute an arbitrary command sequence in each checked out
439 # $@ = command to execute
443 # parse $args after "submodule ... foreach".
465 # dup stdin so that it can be restored when running the external
466 # command in the subshell (and a recursive call to this function)
470 while read mode sha1 stage sm_path
472 die_if_unmatched "$mode"
473 if test -e "$sm_path"/.git
475 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
476 name=$(module_name "$sm_path")
478 prefix="$prefix$sm_path/"
480 # we make $path available to scripts ...
484 if test -n "$recursive"
486 cmd_foreach "--recursive" "$@"
489 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
495 # Register submodules in .git/config
497 # $@ = requested paths (default to all)
501 # parse $args after "submodule ... init".
523 while read mode sha1 stage sm_path
525 die_if_unmatched "$mode"
526 name=$(module_name "$sm_path") || exit
528 # Copy url setting when it is not set yet
529 if test -z "$(git config "submodule.$name.url")"
531 url=$(git config -f .gitmodules submodule."$name".url)
533 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
535 # Possibly a url relative to parent
538 url=$(resolve_relative_url "$url") || exit
541 git config submodule."$name".url "$url" ||
542 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
544 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
547 # Copy "update" setting when it is not set yet
548 upd="$(git config -f .gitmodules submodule."$name".update)"
550 test -n "$(git config submodule."$name".update)" ||
551 git config submodule."$name".update "$upd" ||
552 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
557 # Unregister submodules from .git/config and remove their work tree
559 # $@ = requested paths (use '.' to deinit all submodules)
563 # parse $args after "submodule ... deinit".
589 die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
593 while read mode sha1 stage sm_path
595 die_if_unmatched "$mode"
596 name=$(module_name "$sm_path") || exit
598 # Remove the submodule work tree (unless the user already did it)
599 if test -d "$sm_path"
601 # Protect submodules containing a .git directory
602 if test -d "$sm_path/.git"
604 echo >&2 "$(eval_gettext "Submodule work tree '\$sm_path' contains a .git directory")"
605 die "$(eval_gettext "(use 'rm -rf' if you really want to remove it including all of its history)")"
610 git rm -qn "$sm_path" ||
611 die "$(eval_gettext "Submodule work tree '\$sm_path' contains local modifications; use '-f' to discard them")"
614 say "$(eval_gettext "Cleared directory '\$sm_path'")" ||
615 say "$(eval_gettext "Could not remove submodule work tree '\$sm_path'")"
618 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$sm_path'")"
620 # Remove the .git/config entries (unless the user already did it)
621 if test -n "$(git config --get-regexp submodule."$name\.")"
623 # Remove the whole section so we have a clean state when
624 # the user later decides to init this submodule again
625 url=$(git config submodule."$name".url)
626 git config --remove-section submodule."$name" 2>/dev/null &&
627 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$sm_path'")"
633 # Update each submodule path to correct revision, using clone and checkout as needed
635 # $@ = requested paths (default to all)
639 # parse $args after "submodule ... update".
663 case "$2" in '') usage ;; esac
664 reference="--reference=$2"
665 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
691 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
697 cmd_init "--" "$@" || return
703 while read mode sha1 stage sm_path
705 die_if_unmatched "$mode"
708 echo >&2 "Skipping unmerged submodule $prefix$sm_path"
711 name=$(module_name "$sm_path") || exit
712 url=$(git config submodule."$name".url)
713 branch=$(get_submodule_config "$name" branch master)
714 if ! test -z "$update"
716 update_module=$update
718 update_module=$(git config submodule."$name".update)
721 if test "$update_module" = "none"
723 echo "Skipping submodule '$prefix$sm_path'"
729 # Only mention uninitialized submodules when its
730 # path have been specified
732 say "$(eval_gettext "Submodule path '\$prefix\$sm_path' not initialized
733 Maybe you want to use 'update --init'?")"
737 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
739 module_clone "$sm_path" "$name" "$url" "$reference" || exit
740 cloned_modules="$cloned_modules;$name"
743 subsha1=$(clear_local_git_env; cd "$sm_path" &&
744 git rev-parse --verify HEAD) ||
745 die "$(eval_gettext "Unable to find current revision in submodule path '\$prefix\$sm_path'")"
750 if test -z "$nofetch"
752 # Fetch remote before determining tracking $sha1
753 (clear_local_git_env; cd "$sm_path" && git-fetch) ||
754 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
756 remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
757 sha1=$(clear_local_git_env; cd "$sm_path" &&
758 git rev-parse --verify "${remote_name}/${branch}") ||
759 die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
762 if test "$subsha1" != "$sha1" -o -n "$force"
765 # If we don't already have a -f flag and the submodule has never been checked out
766 if test -z "$subsha1" -a -z "$force"
771 if test -z "$nofetch"
773 # Run fetch only if $sha1 isn't present or it
774 # is not reachable from a ref.
775 (clear_local_git_env; cd "$sm_path" &&
776 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
777 test -z "$rev") || git-fetch)) ||
778 die "$(eval_gettext "Unable to fetch in submodule path '\$prefix\$sm_path'")"
781 # Is this something we just cloned?
782 case ";$cloned_modules;" in
784 # then there is no local change to integrate
789 case "$update_module" in
792 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$prefix\$sm_path'")"
793 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': rebased into '\$sha1'")"
794 must_die_on_failure=yes
798 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$prefix\$sm_path'")"
799 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': merged in '\$sha1'")"
800 must_die_on_failure=yes
803 command="git checkout $subforce -q"
804 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$prefix\$sm_path'")"
805 say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': checked out '\$sha1'")"
809 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
812 elif test -n "$must_die_on_failure"
814 die_with_status 2 "$die_msg"
816 err="${err};$die_msg"
821 if test -n "$recursive"
824 prefix="$prefix$sm_path/"
827 eval cmd_update "$orig_flags"
832 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$prefix\$sm_path'")"
835 err="${err};$die_msg"
838 die_with_status $res "$die_msg"
865 git describe "$2" 2>/dev/null ||
866 git describe --tags "$2" 2>/dev/null ||
867 git describe --contains "$2" 2>/dev/null ||
868 git describe --all --always "$2"
871 test -z "$revname" || revname=" ($revname)"
874 # Show commit summary for submodules in index or working tree
876 # If '--cached' is given, show summary between index and given commit,
877 # or between working tree and given commit
879 # $@ = [commit (default 'HEAD'),] requested paths (default all)
886 # parse $args after "submodule ... summary".
901 isnumber "$summary_limit" || usage
905 summary_limit="${1#--summary-limit=}"
906 isnumber "$summary_limit" || usage
922 test $summary_limit = 0 && return
924 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
928 elif test -z "$1" -o "$1" = "HEAD"
930 # before the first commit: compare with an empty tree
931 head=$(git hash-object -w -t tree --stdin </dev/null)
932 test -z "$1" || shift
940 die "$(gettext "The --cached option cannot be used with the --files option")"
946 # Get modified modules cared by user
947 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
948 sane_egrep '^:([0-7]* )?160000' |
949 while read mod_src mod_dst sha1_src sha1_dst status name
951 # Always show modules deleted or type-changed (blob<->module)
952 test $status = D -o $status = T && echo "$name" && continue
953 # Also show added or modified modules which are checked out
954 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
959 test -z "$modules" && return
961 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
962 sane_egrep '^:([0-7]* )?160000' |
964 while read mod_src mod_dst sha1_src sha1_dst status name
966 if test -z "$cached" &&
967 test $sha1_dst = 0000000000000000000000000000000000000000
971 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
973 100644 | 100755 | 120000)
974 sha1_dst=$(git hash-object $name)
980 eval_gettextln "unexpected mode \$mod_dst" >&2
987 test $mod_src = 160000 &&
988 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
991 test $mod_dst = 160000 &&
992 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
996 case "$missing_src,$missing_dst" in
998 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
1001 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
1004 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
1009 if test $mod_src = 160000 -a $mod_dst = 160000
1011 range="$sha1_src...$sha1_dst"
1012 elif test $mod_src = 160000
1018 GIT_DIR="$name/.git" \
1019 git rev-list --first-parent $range -- | wc -l
1021 total_commits=" ($(($total_commits + 0)))"
1025 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
1026 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
1029 blob="$(gettext "blob")"
1030 submodule="$(gettext "submodule")"
1031 if test $mod_dst = 160000
1033 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1035 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1038 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1040 if test -n "$errmsg"
1042 # Don't give error msg for modification whose dst is not submodule
1043 # i.e. deleted or changed to blob
1044 test $mod_dst = 160000 && echo "$errmsg"
1046 if test $mod_src = 160000 -a $mod_dst = 160000
1049 test $summary_limit -gt 0 && limit="-$summary_limit"
1050 GIT_DIR="$name/.git" \
1051 git log $limit --pretty='format: %m %s' \
1052 --first-parent $sha1_src...$sha1_dst
1053 elif test $mod_dst = 160000
1055 GIT_DIR="$name/.git" \
1056 git log --pretty='format: > %s' -1 $sha1_dst
1058 GIT_DIR="$name/.git" \
1059 git log --pretty='format: < %s' -1 $sha1_src
1065 if test -n "$for_status"; then
1066 if [ -n "$files" ]; then
1067 gettextln "Submodules changed but not updated:" | git stripspace -c
1069 gettextln "Submodule changes to be committed:" | git stripspace -c
1071 printf "\n" | git stripspace -c
1078 # List all submodules, prefixed with:
1079 # - submodule not initialized
1080 # + different revision checked out
1082 # If --cached was specified the revision in the index will be printed
1083 # instead of the currently checked out revision.
1085 # $@ = requested paths (default to all)
1089 # parse $args after "submodule ... status".
1117 while read mode sha1 stage sm_path
1119 die_if_unmatched "$mode"
1120 name=$(module_name "$sm_path") || exit
1121 url=$(git config submodule."$name".url)
1122 displaypath="$prefix$sm_path"
1123 if test "$stage" = U
1125 say "U$sha1 $displaypath"
1128 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
1130 say "-$sha1 $displaypath"
1133 set_name_rev "$sm_path" "$sha1"
1134 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1136 say " $sha1 $displaypath$revname"
1138 if test -z "$cached"
1140 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1141 set_name_rev "$sm_path" "$sha1"
1143 say "+$sha1 $displaypath$revname"
1146 if test -n "$recursive"
1149 prefix="$displaypath/"
1154 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1159 # Sync remote urls for submodules
1160 # This makes the value for remote.$remote.url match the value
1161 # specified in .gitmodules.
1190 while read mode sha1 stage sm_path
1192 die_if_unmatched "$mode"
1193 name=$(module_name "$sm_path")
1194 url=$(git config -f .gitmodules --get submodule."$name".url)
1196 # Possibly a url relative to parent
1199 # rewrite foo/bar as ../.. to find path from
1200 # submodule work tree to superproject work tree
1201 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1202 # guarantee a trailing /
1203 up_path=${up_path%/}/ &&
1204 # path from submodule work tree to submodule origin repo
1205 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1206 # path from superproject work tree to submodule origin repo
1207 super_config_url=$(resolve_relative_url "$url") || exit
1210 sub_origin_url="$url"
1211 super_config_url="$url"
1215 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1217 say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")"
1218 git config submodule."$name".url "$super_config_url"
1220 if test -e "$sm_path"/.git
1225 remote=$(get_default_remote)
1226 git config remote."$remote".url "$sub_origin_url"
1228 if test -n "$recursive"
1230 prefix="$prefix$sm_path/"
1239 # This loop parses the command line arguments to find the
1240 # subcommand name to dispatch. Parsing of the subcommand specific
1241 # options are primarily done by the subcommand implementations.
1242 # Subcommand specific options such as --branch and --cached are
1243 # parsed here as well, for backward compatibility.
1245 while test $# != 0 && test -z "$command"
1248 add | foreach | init | deinit | update | status | summary | sync)
1278 # No command word defaults to "status"
1279 if test -z "$command"
1289 # "-b branch" is accepted only by "add"
1290 if test -n "$branch" && test "$command" != add
1295 # "--cached" is accepted only by "status" and "summary"
1296 if test -n "$cached" && test "$command" != status -a "$command" != summary