diff options
-rw-r--r-- | lib/git/objects/submodule.py | 19 | ||||
-rw-r--r-- | test/git/test_submodule.py | 32 |
2 files changed, 39 insertions, 12 deletions
diff --git a/lib/git/objects/submodule.py b/lib/git/objects/submodule.py index 948a267f..aa11909f 100644 --- a/lib/git/objects/submodule.py +++ b/lib/git/objects/submodule.py @@ -41,7 +41,7 @@ def unbare_repo(func): wrapper.__name__ = func.__name__ return wrapper -def find_remote_branch(remotes, branch): +def find_first_remote_branch(remotes, branch): """Find the remote branch matching the name of the given branch or raise InvalidGitRepositoryError""" for remote in remotes: try: @@ -394,7 +394,7 @@ class Submodule(base.IndexObject, Iterable, Traversable): # see whether we have a valid branch to checkout try: # find a remote which has our branch - we try to be flexible - remote_branch = find_remote_branch(mrepo.remotes, self.branch) + remote_branch = find_first_remote_branch(mrepo.remotes, self.branch) local_branch = self.branch if not local_branch.is_valid(): # Setup a tracking configuration - branch doesn't need to @@ -1078,14 +1078,23 @@ class RootModule(Submodule): # new remote branch smm = sm.module() smmr = smm.remotes - tbr = git.Head.create(smm, sm.branch.name) - tbr.set_tracking_branch(find_remote_branch(smmr, sm.branch)) + try: + tbr = git.Head.create(smm, sm.branch.name) + except git.GitCommandError, e: + if e.status != 128: + raise + #END handle something unexpected + + # ... or reuse the existing one + tbr = git.Head(smm, git.Head.to_full_path(sm.branch.name)) + #END assure tracking branch exists + tbr.set_tracking_branch(find_first_remote_branch(smmr, sm.branch)) # figure out whether the previous tracking branch contains # new commits compared to the other one, if not we can # delete it. try: - tbr = find_remote_branch(smmr, psm.branch) + tbr = find_first_remote_branch(smmr, psm.branch) if len(smm.git.cherry(tbr, psm.branch)) == 0: psm.branch.delete(smm, psm.branch) #END delete original tracking branch if there are no changes diff --git a/test/git/test_submodule.py b/test/git/test_submodule.py index 5e209f1b..dbc2ef08 100644 --- a/test/git/test_submodule.py +++ b/test/git/test_submodule.py @@ -437,16 +437,34 @@ class TestSubmodule(TestBase): # head changed, as the remote url and its commit changed assert prev_commit != nsm.module().head.commit - assert False + # add the submodule's changed commit to the index, which is what the + # user would do + # beforehand, update our instance's binsha with the new one + nsm.binsha = nsm.module().head.commit.binsha + rwrepo.index.add([nsm]) # change branch #================= - nsm.set_parent_commit(csmpathchange) - # the branch used here is an old failure branch which should ideally stay ... lets see how long that works ;) - nbn = 'pack_offset_cache' - assert nsm.branch.name != nbn - nsm.config_writer().set_value(Submodule.k_head_option, nbn) - csmbranchchange = rwrepo.index.commit("changed branch") + # we only have one branch, so we switch to a virtual one, and back + # to the current one to trigger the difference + cur_branch = nsm.branch + nsmm = nsm.module() + prev_commit = nsmm.head.commit + for branch in ("some_virtual_branch", cur_branch.name): + nsm.config_writer().set_value(Submodule.k_head_option, branch) + csmbranchchange = rwrepo.index.commit("changed branch to %s" % branch) + nsm.set_parent_commit(csmbranchchange) + # END for each branch to change + + # Lets remove our tracking branch to simulate some changes + nsmmh = nsmm.head + assert nsmmh.ref.tracking_branch() is None # never set it up until now + assert not nsmmh.is_detached + + rm.update(recursive=False) + + assert nsmmh.ref.tracking_branch() is not None + assert not nsmmh.is_detached # recursive update |