Skip to content

Commit 3f2d76b

Browse files
committed
Added test for branch changes - it appears to work well, at least as far as the restricted tests are concerned
1 parent cf5eadd commit 3f2d76b

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

lib/git/objects/submodule.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def wrapper(self, *args, **kwargs):
4141
wrapper.__name__ = func.__name__
4242
return wrapper
4343

44-
def find_remote_branch(remotes, branch):
44+
def find_first_remote_branch(remotes, branch):
4545
"""Find the remote branch matching the name of the given branch or raise InvalidGitRepositoryError"""
4646
for remote in remotes:
4747
try:
@@ -394,7 +394,7 @@ def update(self, recursive=False, init=True, to_latest_revision=False):
394394
# see whether we have a valid branch to checkout
395395
try:
396396
# find a remote which has our branch - we try to be flexible
397-
remote_branch = find_remote_branch(mrepo.remotes, self.branch)
397+
remote_branch = find_first_remote_branch(mrepo.remotes, self.branch)
398398
local_branch = self.branch
399399
if not local_branch.is_valid():
400400
# Setup a tracking configuration - branch doesn't need to
@@ -1078,14 +1078,23 @@ def update(self, previous_commit=None, recursive=True, force_remove=False, init=
10781078
# new remote branch
10791079
smm = sm.module()
10801080
smmr = smm.remotes
1081-
tbr = git.Head.create(smm, sm.branch.name)
1082-
tbr.set_tracking_branch(find_remote_branch(smmr, sm.branch))
1081+
try:
1082+
tbr = git.Head.create(smm, sm.branch.name)
1083+
except git.GitCommandError, e:
1084+
if e.status != 128:
1085+
raise
1086+
#END handle something unexpected
1087+
1088+
# ... or reuse the existing one
1089+
tbr = git.Head(smm, git.Head.to_full_path(sm.branch.name))
1090+
#END assure tracking branch exists
10831091

1092+
tbr.set_tracking_branch(find_first_remote_branch(smmr, sm.branch))
10841093
# figure out whether the previous tracking branch contains
10851094
# new commits compared to the other one, if not we can
10861095
# delete it.
10871096
try:
1088-
tbr = find_remote_branch(smmr, psm.branch)
1097+
tbr = find_first_remote_branch(smmr, psm.branch)
10891098
if len(smm.git.cherry(tbr, psm.branch)) == 0:
10901099
psm.branch.delete(smm, psm.branch)
10911100
#END delete original tracking branch if there are no changes

test/git/test_submodule.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -437,16 +437,34 @@ def test_root_module(self, rwrepo):
437437
# head changed, as the remote url and its commit changed
438438
assert prev_commit != nsm.module().head.commit
439439

440-
assert False
440+
# add the submodule's changed commit to the index, which is what the
441+
# user would do
442+
# beforehand, update our instance's binsha with the new one
443+
nsm.binsha = nsm.module().head.commit.binsha
444+
rwrepo.index.add([nsm])
441445

442446
# change branch
443447
#=================
444-
nsm.set_parent_commit(csmpathchange)
445-
# the branch used here is an old failure branch which should ideally stay ... lets see how long that works ;)
446-
nbn = 'pack_offset_cache'
447-
assert nsm.branch.name != nbn
448-
nsm.config_writer().set_value(Submodule.k_head_option, nbn)
449-
csmbranchchange = rwrepo.index.commit("changed branch")
448+
# we only have one branch, so we switch to a virtual one, and back
449+
# to the current one to trigger the difference
450+
cur_branch = nsm.branch
451+
nsmm = nsm.module()
452+
prev_commit = nsmm.head.commit
453+
for branch in ("some_virtual_branch", cur_branch.name):
454+
nsm.config_writer().set_value(Submodule.k_head_option, branch)
455+
csmbranchchange = rwrepo.index.commit("changed branch to %s" % branch)
456+
nsm.set_parent_commit(csmbranchchange)
457+
# END for each branch to change
458+
459+
# Lets remove our tracking branch to simulate some changes
460+
nsmmh = nsmm.head
461+
assert nsmmh.ref.tracking_branch() is None # never set it up until now
462+
assert not nsmmh.is_detached
463+
464+
rm.update(recursive=False)
465+
466+
assert nsmmh.ref.tracking_branch() is not None
467+
assert not nsmmh.is_detached
450468

451469

452470
# recursive update

0 commit comments

Comments
 (0)