Skip to content

Commit 902f2f4

Browse files
committed
Merge branch 'wk/submodule-update-remote'
The beginning of 'integrate with the tip of the remote branch, not the commit recorded in the superproject gitlink' support. * wk/submodule-update-remote: submodule add: If --branch is given, record it in .gitmodules submodule update: add --remote for submodule's upstream changes submodule: add get_submodule_config helper funtion
2 parents 971e829 + b928922 commit 902f2f4

6 files changed

Lines changed: 117 additions & 2 deletions

File tree

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,12 @@ submodule.<name>.update::
20072007
URL and other values found in the `.gitmodules` file. See
20082008
linkgit:git-submodule[1] and linkgit:gitmodules[5] for details.
20092009

2010+
submodule.<name>.branch::
2011+
The remote branch name for a submodule, used by `git submodule
2012+
update --remote`. Set this option to override the value found in
2013+
the `.gitmodules` file. See linkgit:git-submodule[1] and
2014+
linkgit:gitmodules[5] for details.
2015+
20102016
submodule.<name>.fetchRecurseSubmodules::
20112017
This option can be used to control recursive fetching of this
20122018
submodule. It can be overridden by using the --[no-]recurse-submodules

Documentation/git-submodule.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SYNOPSIS
1313
[--reference <repository>] [--] <repository> [<path>]
1414
'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...]
1515
'git submodule' [--quiet] init [--] [<path>...]
16-
'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--rebase]
16+
'git submodule' [--quiet] update [--init] [--remote] [-N|--no-fetch] [--rebase]
1717
[--reference <repository>] [--merge] [--recursive] [--] [<path>...]
1818
'git submodule' [--quiet] summary [--cached|--files] [(-n|--summary-limit) <n>]
1919
[commit] [--] [<path>...]
@@ -208,6 +208,8 @@ OPTIONS
208208
-b::
209209
--branch::
210210
Branch of repository to add as submodule.
211+
The name of the branch is recorded as `submodule.<path>.branch` in
212+
`.gitmodules` for `update --remote`.
211213

212214
-f::
213215
--force::
@@ -236,6 +238,27 @@ OPTIONS
236238
(the default). This limit only applies to modified submodules. The
237239
size is always limited to 1 for added/deleted/typechanged submodules.
238240

241+
--remote::
242+
This option is only valid for the update command. Instead of using
243+
the superproject's recorded SHA-1 to update the submodule, use the
244+
status of the submodule's remote tracking branch. The remote used
245+
is branch's remote (`branch.<name>.remote`), defaulting to `origin`.
246+
The remote branch used defaults to `master`, but the branch name may
247+
be overridden by setting the `submodule.<name>.branch` option in
248+
either `.gitmodules` or `.git/config` (with `.git/config` taking
249+
precedence).
250+
+
251+
This works for any of the supported update procedures (`--checkout`,
252+
`--rebase`, etc.). The only change is the source of the target SHA-1.
253+
For example, `submodule update --remote --merge` will merge upstream
254+
submodule changes into the submodules, while `submodule update
255+
--merge` will merge superproject gitlink changes into the submodules.
256+
+
257+
In order to ensure a current tracking branch state, `update --remote`
258+
fetches the submodule's remote repository before calculating the
259+
SHA-1. If you don't want to fetch, you should use `submodule update
260+
--remote --no-fetch`.
261+
239262
-N::
240263
--no-fetch::
241264
This option is only valid for the update command.

Documentation/gitmodules.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ submodule.<name>.update::
4949
This config option is overridden if 'git submodule update' is given
5050
the '--merge', '--rebase' or '--checkout' options.
5151

52+
submodule.<name>.branch::
53+
A remote branch name for tracking updates in the upstream submodule.
54+
If the option is not specified, it defaults to 'master'. See the
55+
`--remote` documentation in linkgit:git-submodule[1] for details.
56+
5257
submodule.<name>.fetchRecurseSubmodules::
5358
This option can be used to control recursive fetching of this
5459
submodule. If this option is also present in the submodules entry in

git-submodule.sh

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dashless=$(basename "$0" | sed -e 's/-/ /')
88
USAGE="[--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=
2626
recursive=
2727
init=
2828
files=
29+
remote=
2930
nofetch=
3031
update=
3132
prefix=
@@ -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

t/t7400-submodule-basic.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ test_expect_success 'submodule add --branch' '
133133
(
134134
cd addtest &&
135135
git submodule add -b initial "$submodurl" submod-branch &&
136+
test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
136137
git submodule init
137138
) &&
138139

t/t7406-submodule-update.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,37 @@ test_expect_success 'submodule update --force forcibly checks out submodules' '
135135
)
136136
'
137137

138+
test_expect_success 'submodule update --remote should fetch upstream changes' '
139+
(cd submodule &&
140+
echo line4 >> file &&
141+
git add file &&
142+
test_tick &&
143+
git commit -m "upstream line4"
144+
) &&
145+
(cd super &&
146+
git submodule update --remote --force submodule &&
147+
cd submodule &&
148+
test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
149+
)
150+
'
151+
152+
test_expect_success 'local config should override .gitmodules branch' '
153+
(cd submodule &&
154+
git checkout -b test-branch &&
155+
echo line5 >> file &&
156+
git add file &&
157+
test_tick &&
158+
git commit -m "upstream line5" &&
159+
git checkout master
160+
) &&
161+
(cd super &&
162+
git config submodule.submodule.branch test-branch &&
163+
git submodule update --remote --force submodule &&
164+
cd submodule &&
165+
test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
166+
)
167+
'
168+
138169
test_expect_success 'submodule update --rebase staying on master' '
139170
(cd super/submodule &&
140171
git checkout master

0 commit comments

Comments
 (0)