@@ -8,7 +8,7 @@ dashless=$(basename "$0" | sed -e 's/-/ /')
88USAGE=" [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
99 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
1010 or: $dashless [--quiet] init [--] [<path>...]
11- or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
11+ or: $dashless [--quiet] update [--init] [--remote] [- N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
1212 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
1313 or: $dashless [--quiet] foreach [--recursive] <command>
1414 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
@@ -26,6 +26,7 @@ cached=
2626recursive=
2727init=
2828files=
29+ remote=
2930nofetch=
3031update=
3132prefix=
@@ -152,6 +153,32 @@ die_if_unmatched ()
152153 fi
153154}
154155
156+ #
157+ # Print a submodule configuration setting
158+ #
159+ # $1 = submodule name
160+ # $2 = option name
161+ # $3 = default value
162+ #
163+ # Checks in the usual git-config places first (for overrides),
164+ # otherwise it falls back on .gitmodules. This allows you to
165+ # distribute project-wide defaults in .gitmodules, while still
166+ # customizing individual repositories if necessary. If the option is
167+ # not in .gitmodules either, print a default value.
168+ #
169+ get_submodule_config () {
170+ name=" $1 "
171+ option=" $2 "
172+ default=" $3 "
173+ value=$( git config submodule." $name " ." $option " )
174+ if test -z " $value "
175+ then
176+ value=$( git config -f .gitmodules submodule." $name " ." $option " )
177+ fi
178+ printf ' %s' " ${value:- $default } "
179+ }
180+
181+
155182#
156183# Map submodule path to submodule name
157184#
@@ -390,6 +417,10 @@ Use -f if you really want to add it." >&2
390417
391418 git config -f .gitmodules submodule." $sm_name " .path " $sm_path " &&
392419 git config -f .gitmodules submodule." $sm_name " .url " $repo " &&
420+ if test -n " $branch "
421+ then
422+ git config -f .gitmodules submodule." $sm_name " .branch " $branch "
423+ fi &&
393424 git add --force .gitmodules ||
394425 die " $( eval_gettext " Failed to register submodule '\$ sm_path'" ) "
395426}
@@ -533,6 +564,9 @@ cmd_update()
533564 -i|--init)
534565 init=1
535566 ;;
567+ --remote)
568+ remote=1
569+ ;;
536570 -N|--no-fetch)
537571 nofetch=1
538572 ;;
@@ -593,6 +627,7 @@ cmd_update()
593627 fi
594628 name=$( module_name " $sm_path " ) || exit
595629 url=$( git config submodule." $name " .url)
630+ branch=$( get_submodule_config " $name " branch master)
596631 if ! test -z " $update "
597632 then
598633 update_module=$update
@@ -627,6 +662,20 @@ Maybe you want to use 'update --init'?")"
627662 die " $( eval_gettext " Unable to find current revision in submodule path '\$ sm_path'" ) "
628663 fi
629664
665+ if test -n " $remote "
666+ then
667+ if test -z " $nofetch "
668+ then
669+ # Fetch remote before determining tracking $sha1
670+ (clear_local_git_env; cd " $sm_path " && git-fetch) ||
671+ die " $( eval_gettext " Unable to fetch in submodule path '\$ sm_path'" ) "
672+ fi
673+ remote_name=$( clear_local_git_env; cd " $sm_path " && get_default_remote)
674+ sha1=$( clear_local_git_env; cd " $sm_path " &&
675+ git rev-parse --verify " ${remote_name} /${branch} " ) ||
676+ die " $( eval_gettext " Unable to find current ${remote_name} /${branch} revision in submodule path '\$ sm_path'" ) "
677+ fi
678+
630679 if test " $subsha1 " ! = " $sha1 " -o -n " $force "
631680 then
632681 subforce=$force
0 commit comments