Skip to content

Commit 5fe2add

Browse files
committed
fix(submodule): check out reused branches at a different commit
When an inherited branch already pointed to the new gitlink, assigning `HEAD` made the final submodule update skip checkout. The index and working tree still contained the previous branch's files. Perform a `Head.checkout()` when the branch tip differs from the current commit and forward `force_reset`. Keep the lightweight reference switch when both branches point to the same commit. Set up tracking after a successful switch. The scripted fixture has distinct trees and verifies the index and working tree. It failed before the fix. The focused selection passes 25 tests and seven subtests under each backend. Agent-Model: unknown
1 parent 177a279 commit 5fe2add

3 files changed

Lines changed: 43 additions & 12 deletions

File tree

‎git/objects/submodule/root.py‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,12 @@ def update( # type: ignore[override]
416416
tbr = git.Head(smm, branch.path)
417417
# END ensure tracking branch exists
418418

419-
tbr.set_tracking_branch(find_first_remote_branch(smmr, branch.name))
420-
# NOTE: All head-resetting is done in the base
421-
# implementation of update but we will have to checkout the
422-
# new branch here. As it still points to the currently
423-
# checked out commit, we don't do any harm.
424-
# As we don't want to update working-tree or index, changing
425-
# the ref is all there is to do.
426-
smm.head.reference = tbr
419+
tracking_branch = find_first_remote_branch(smmr, branch.name)
420+
if tbr.commit == smm.head.commit:
421+
smm.head.reference = tbr
422+
else:
423+
tbr.checkout(force=force_reset)
424+
tbr.set_tracking_branch(tracking_branch)
427425
# END handle resolved tracking branch
428426
# END handle dry_run
429427

‎test/test_submodule.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,25 @@ def test_root_update_detects_change_to_inherited_branch(self):
21662166
assert module.head.is_detached
21672167
assert module.head.commit.binsha == submodule.binsha
21682168

2169+
@patch_git_config("protocol.file.allow", "always")
2170+
def test_root_update_checks_out_reused_inherited_branch(self):
2171+
# The submodule URL points to this private fixture's source.
2172+
rwdir = self._fixture("root-reuse-inherited-branch", execute=True)
2173+
with git.Repo(rwdir / "parent") as parent:
2174+
submodule = parent.submodules[0]
2175+
with submodule.module() as module:
2176+
path = Path(submodule.abspath, "file")
2177+
assert module.active_branch.name == "master"
2178+
assert path.read_text() == "master\n"
2179+
assert module.heads.dev.commit.binsha == submodule.binsha
2180+
2181+
RootModule(parent).update(previous_commit="before-branch-change")
2182+
2183+
assert module.active_branch == module.heads.dev
2184+
assert module.head.commit.binsha == submodule.binsha
2185+
assert module.git.show(":file") == "dev"
2186+
assert path.read_text() == "dev\n"
2187+
21692188
@patch_git_config("protocol.file.allow", "always")
21702189
def test_root_update_retries_url_change_after_detached_superproject(self):
21712190
# The submodule URLs point to this private fixture's source and replacement.

‎tests/fixtures/submodule-cases.sh‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,32 @@ case "$scenario" in
4646
deinit-nonempty) printf '%s' 'user content' > parent/module/file ;;
4747
esac
4848
;;
49-
branch-dot|branch-dot-alias|root-url-branch-dot)
49+
branch-dot|branch-dot-alias|root-url-branch-dot|root-reuse-inherited-branch)
5050
git init -q -b master source
5151
git -C source commit --allow-empty -qm initial
5252
git -C source branch dev
53-
git -C source commit --allow-empty -qm 'Advance master'
53+
printf 'master\n' > source/file
54+
git -C source add file
55+
git -C source commit -qm 'Advance master'
5456
git -C source checkout -q dev
55-
git -C source commit --allow-empty -qm 'Advance dev'
57+
printf 'dev\n' > source/file
58+
git -C source add file
59+
git -C source commit -qm 'Advance dev'
5660
git -C source checkout -q master
5761
source="$root/source"
5862
add_module module module
63+
if test "$scenario" = root-reuse-inherited-branch; then
64+
git -C parent config -f .gitmodules submodule.module.branch master
65+
git -C parent commit -qam 'Track master'
66+
git -C parent checkout -qb dev
67+
git -C parent/module branch dev origin/dev
68+
git -C parent tag before-branch-change
69+
git -C parent update-index --cacheinfo "160000,$(git -C source rev-parse dev),module"
70+
fi
5971
git -C parent config -f .gitmodules submodule.module.branch .
60-
git -C parent commit -qam 'Track the superproject branch'
72+
git -C parent add .gitmodules
73+
git -C parent commit -qm 'Track the superproject branch'
74+
if test "$scenario" = root-reuse-inherited-branch; then exit 0; fi
6175
git -C parent branch dev
6276
git clone -q -b dev parent clone
6377
if test "$scenario" = branch-dot-alias; then

0 commit comments

Comments
 (0)