Skip to content

Commit 21b4db5

Browse files
committed
Improved efficiency of the submodule.update process, improved test
1 parent 9f73e8b commit 21b4db5

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

lib/git/objects/submodule.py

+27-19
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ def __hash__(self):
144144
"""Hash this instance using its logical id, not the sha"""
145145
return hash(self._name)
146146

147+
def __str__(self):
148+
return self._name
149+
147150
@classmethod
148151
def _config_parser(cls, repo, parent_commit, read_only):
149152
""":return: Config Parser constrained to our submodule in read or write mode
@@ -250,20 +253,24 @@ def update(self, recursive=False, init=True):
250253
remote_branch = mrepo.remotes.origin.refs[self.branch]
251254
local_branch = git.Head(mrepo, git.Head.to_full_path(self.branch))
252255
if not local_branch.is_valid():
253-
mrepo.git.checkout(remote_branch, b=self.branch)
254-
else:
255-
# have a valid branch, but no checkout - make sure we can figure
256-
# that out by marking the commit with a null_sha
257-
# have to write it directly as .commit = NULLSHA tries to resolve the sha
258-
ref = mrepo.head.ref
259-
refpath = join_path_native(mrepo.git_dir, ref.to_full_path(ref.path))
260-
refdir = os.path.dirname(refpath)
261-
if not os.path.isdir(refdir):
262-
os.makedirs(refdir)
263-
#END handle directory
264-
open(refpath, 'w').write(self.NULL_HEX_SHA)
256+
# Setup a tracking configuration - branch doesn't need to
257+
# exist to do that
258+
local_branch.set_tracking_branch(remote_branch)
259+
#END handle local branch
260+
261+
# have a valid branch, but no checkout - make sure we can figure
262+
# that out by marking the commit with a null_sha
263+
# have to write it directly as .commit = NULLSHA tries to resolve the sha
264+
# This will bring the branch into existance
265+
refpath = join_path_native(mrepo.git_dir, local_branch.path)
266+
refdir = os.path.dirname(refpath)
267+
if not os.path.isdir(refdir):
268+
os.makedirs(refdir)
269+
#END handle directory
270+
open(refpath, 'w').write(self.NULL_HEX_SHA)
265271
# END initial checkout + branch creation
266-
# make sure we are not detached
272+
273+
# make sure HEAD is not detached
267274
mrepo.head.ref = local_branch
268275
except IndexError:
269276
print >> sys.stderr, "Warning: Failed to checkout tracking branch %s" % self.branch
@@ -280,13 +287,14 @@ def update(self, recursive=False, init=True):
280287
# branch - this should be prevented when setting the branch option
281288
mrepo.head.reset(self.hexsha, index=True, working_tree=True)
282289
# END handle checkout
283-
284-
if recursive:
285-
for submodule in self.iter_items(self.module()):
286-
submodule.update(recursive, init)
287-
# END handle recursive update
288-
# END for each submodule
289290
# END update to new commit only if needed
291+
292+
# HANDLE RECURSION
293+
if recursive:
294+
for submodule in self.iter_items(self.module()):
295+
submodule.update(recursive, init)
296+
# END handle recursive update
297+
# END for each submodule
290298

291299
return self
292300

test/git/test_submodule.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ def _do_base_tests(self, rwrepo):
8282

8383
# TEST TODO: if a path in the gitmodules file, but not in the index, it raises
8484

85+
# TEST UPDATE
86+
##############
8587
# module retrieval is not always possible
8688
if rwrepo.bare:
8789
self.failUnlessRaises(InvalidGitRepositoryError, sm.module)
@@ -106,6 +108,9 @@ def _do_base_tests(self, rwrepo):
106108
assert isinstance(sm.module(), git.Repo)
107109
assert sm.module().working_tree_dir == sm.module_path()
108110

111+
# we should have setup a tracking branch, which is also active
112+
assert sm.module().head.ref.tracking_branch() is not None
113+
109114
# delete the whole directory and re-initialize
110115
shutil.rmtree(sm.module_path())
111116
sm.update(recursive=False)
@@ -119,10 +124,12 @@ def _do_base_tests(self, rwrepo):
119124
csm.config_writer().set_value('url', new_csm_path)
120125
assert csm.url == new_csm_path
121126

122-
123127
# update recuesively again
124128
sm.update(recursive=True)
125129

130+
# tracking branch once again
131+
csm.module().head.ref.tracking_branch() is not None
132+
126133
# this flushed in a sub-submodule
127134
assert len(list(rwrepo.iter_submodules())) == 2
128135
# END handle bare mode

0 commit comments

Comments
 (0)