From: W. Trevor King Date: Sun, 2 Dec 2012 02:28:31 +0000 (-0500) Subject: submodule update: add submodule..remote config option X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=refs%2Fheads%2Fwtk%2Fsubmodule.name.branch-v6;p=git.git submodule update: add submodule..remote config option Don't force the user to clone from the tracked repository (branch..remote) or `origin`. By setting submodule..remote in .gitmodules or the usual git config files, you can easily point a submodule at a different remote when using `submodule update --remote`. The configured remote name is also used in `submodule sync` to determine which remote..url is updated with the submodule's origin URL. Signed-off-by: W. Trevor King --- diff --git a/Documentation/config.txt b/Documentation/config.txt index 6f4663c38..c54b9b4f8 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -1999,10 +1999,11 @@ submodule..update:: by 'git submodule init'; edit them to override the URL and other values found in the `.gitmodules` file. See +submodule..remote:: submodule..branch:: - The remote branch name for a submodule, used by `git submodule - update --remote`. Set this option to override the value found in - the `.gitmodules` file. See linkgit:git-submodule[1] and + The remote repository and branch names for a submodule, used by `git + submodule update --remote`. Set these options to override the value + found in the `.gitmodules` file. See linkgit:git-submodule[1] and linkgit:gitmodules[5] for details. submodule..fetchRecurseSubmodules:: diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt index 988bba934..1d8d5f176 100644 --- a/Documentation/git-submodule.txt +++ b/Documentation/git-submodule.txt @@ -242,11 +242,11 @@ OPTIONS This option is only valid for the update command. Instead of using the superproject's recorded SHA-1 to update the submodule, use the status of the submodule's remote tracking branch. The remote used - is branch's remote (`branch..remote`), defaulting to `origin`. - The remote branch used defaults to `master`, but the branch name may - be overridden by setting the `submodule..branch` option in - either `.gitmodules` or `.git/config` (with `.git/config` taking - precedence). + is branch's remote (`branch..remote`, defaulting to `origin`), + and the remote branch used defaults to `master`, but either may be + overridden by setting the `submodule..remote` or + `submodule..branch` option in `.gitmodules` or `.git/config` + (with `.git/config` taking precedence). + This works for any of the supported update procedures (`--checkout`, `--rebase`, etc.). The only change is the source of the target SHA-1. diff --git a/git-submodule.sh b/git-submodule.sh index 27b02fe4c..3e39e29a1 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -179,6 +179,21 @@ get_submodule_config() printf '%s' "${value:-$default}" } +# +# Print the name of a submodule's configured remote +# +# $1 = submodule name +# +get_submodule_remote() +{ + name="$1" + remote=$(get_submodule_config "$name" remote) + if test -z "$remote" + then + remote=$(get_default_remote) + fi + printf '%s' "${remote}" +} # # Map submodule path to submodule name @@ -605,6 +620,7 @@ cmd_update() fi name=$(module_name "$sm_path") || exit url=$(git config submodule."$name".url) + remote_name=$(get_submodule_remote "$name") branch=$(get_submodule_config "$name" branch master) if ! test -z "$update" then @@ -645,10 +661,9 @@ Maybe you want to use 'update --init'?")" if test -z "$nofetch" then # Fetch remote before determining tracking $sha1 - (clear_local_git_env; cd "$sm_path" && git-fetch) || - die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")" + (clear_local_git_env; cd "$sm_path" && git-fetch "$remote_name") || + die "$(eval_gettext "Unable to fetch '\$remote_name' in submodule path '\$sm_path'")" fi - remote_name=$(get_default_remote) sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify "${remote_name}/${branch}") || die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")" @@ -669,8 +684,8 @@ Maybe you want to use 'update --init'?")" # is not reachable from a ref. (clear_local_git_env; cd "$sm_path" && ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) && - test -z "$rev") || git-fetch)) || - die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")" + test -z "$rev") || git-fetch "$remote_name")) || + die "$(eval_gettext "Unable to fetch '\$remote_name' in submodule path '\$sm_path'")" fi # Is this something we just cloned? @@ -1110,7 +1125,7 @@ cmd_sync() ( clear_local_git_env cd "$sm_path" - remote=$(get_default_remote) + remote=$(get_submodule_remote "$name") git config remote."$remote".url "$sub_origin_url" ) fi diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh index a56783497..86c85f8d6 100755 --- a/t/t7406-submodule-update.sh +++ b/t/t7406-submodule-update.sh @@ -149,6 +149,24 @@ test_expect_success 'submodule update --remote should fetch upstream changes' ' ) ' +test_expect_success 'local config should override .gitmodules remote' ' + (cd submodule && + echo line5-master >> file && + git add file && + test_tick && + git commit -m "upstream line5-master" + ) && + (cd super/submodule && + git remote rename origin test-remote + ) && + (cd super && + git config submodule.submodule.remote test-remote && + git submodule update --remote --force submodule && + cd submodule && + test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)" + ) +' + test_expect_success 'local config should override .gitmodules branch' ' (cd submodule && git checkout -b test-branch &&