Skip to content

Commit 7b3ef45

Browse files
committed
The submodule's branch is now a branch instance, not a plain string anymore
1 parent 33964af commit 7b3ef45

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

Diff for: lib/git/objects/submodule.py

+24-18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def sm_name(section):
2424
""":return: name of the submodule as parsed from the section name"""
2525
section = section.strip()
2626
return section[11:-1]
27+
28+
def mkhead(repo, path):
29+
""":return: New branch/head instance"""
30+
return git.Head(repo, git.Head.to_full_path(path))
2731
#} END utilities
2832

2933

@@ -96,13 +100,14 @@ def __init__(self, repo, binsha, mode=None, path=None, name = None, parent_commi
96100
:param binsha: binary sha referring to a commit in the remote repository, see url parameter
97101
:param parent_commit: see set_parent_commit()
98102
:param url: The url to the remote repository which is the submodule
99-
:param ref: Reference to checkout when cloning the remote repository"""
103+
:param branch: Head instance to checkout when cloning the remote repository"""
100104
super(Submodule, self).__init__(repo, binsha, mode, path)
101105
if parent_commit is not None:
102106
self._parent_commit = parent_commit
103107
if url is not None:
104108
self._url = url
105109
if branch is not None:
110+
assert isinstance(branch, git.Head)
106111
self._branch = branch
107112
if name is not None:
108113
self._name = name
@@ -119,7 +124,7 @@ def _set_cache_(self, attr):
119124
self.path = reader.get_value('path')
120125
self._url = reader.get_value('url')
121126
# git-python extension values - optional
122-
self._branch = reader.get_value(self.k_head_option, self.k_head_default)
127+
self._branch = mkhead(self.repo, reader.get_value(self.k_head_option, self.k_head_default))
123128
elif attr == '_name':
124129
raise AttributeError("Cannot retrieve the name of a submodule if it was not set initially")
125130
else:
@@ -203,7 +208,7 @@ def _config_parser_constrained(self, read_only):
203208
#{ Edit Interface
204209

205210
@classmethod
206-
def add(cls, repo, name, path, url=None, branch=k_head_default, no_checkout=False):
211+
def add(cls, repo, name, path, url=None, branch=None, no_checkout=False):
207212
"""Add a new submodule to the given repository. This will alter the index
208213
as well as the .gitmodules file, but will not create a new commit.
209214
If the submodule already exists, no matter if the configuration differs
@@ -220,8 +225,10 @@ def add(cls, repo, name, path, url=None, branch=k_head_default, no_checkout=Fals
220225
:param branch: branch at which the submodule should (later) be checked out.
221226
The given branch must exist in the remote repository, and will be checked
222227
out locally as a tracking branch.
223-
It will only be written into the configuration if it differs from the
224-
default.
228+
It will only be written into the configuration if it not None, which is
229+
when the checked out branch will be the one the remote HEAD pointed to.
230+
The result you get in these situation is somewhat fuzzy, and it is recommended
231+
to specify at least 'master' here
225232
:param no_checkout: if True, and if the repository has to be cloned manually,
226233
no checkout will be performed
227234
:return: The newly created submodule instance
@@ -243,9 +250,9 @@ def add(cls, repo, name, path, url=None, branch=k_head_default, no_checkout=Fals
243250
return repo.head.commit.tree[path]
244251
# END handle existing
245252

246-
branch = git.Head(repo, git.Head.to_full_path(branch))
253+
br = mkhead(repo, branch or cls.k_head_default)
247254
has_module = sm.module_exists()
248-
branch_is_default = branch.name == cls.k_head_default
255+
branch_is_default = branch is None
249256
if has_module and url is not None:
250257
if url not in [r.url for r in sm.module().remotes]:
251258
raise ValueError("Specified URL '%s' does not match any remote url of the repository at '%s'" % (url, sm.module_path()))
@@ -266,8 +273,8 @@ def add(cls, repo, name, path, url=None, branch=k_head_default, no_checkout=Fals
266273
else:
267274
# clone new repo
268275
kwargs = {'n' : no_checkout}
269-
if branch_is_default:
270-
kwargs['b'] = str(branch)
276+
if not branch_is_default:
277+
kwargs['b'] = str(br)
271278
# END setup checkout-branch
272279
mrepo = git.Repo.clone_from(url, path, **kwargs)
273280
# END verify url
@@ -280,8 +287,8 @@ def add(cls, repo, name, path, url=None, branch=k_head_default, no_checkout=Fals
280287
sm._url = url
281288
if not branch_is_default:
282289
# store full path
283-
writer.set_value(cls.k_head_option, branch.path)
284-
sm._branch = branch
290+
writer.set_value(cls.k_head_option, br.path)
291+
sm._branch = br.path
285292
# END handle path
286293
del(writer)
287294

@@ -348,8 +355,8 @@ def update(self, recursive=False, init=True, to_latest_revision=False):
348355

349356
# see whether we have a valid branch to checkout
350357
try:
351-
remote_branch = mrepo.remotes.origin.refs[self.branch]
352-
local_branch = git.Head(mrepo, git.Head.to_full_path(self.branch))
358+
remote_branch = mrepo.remotes.origin.refs[self.branch.name]
359+
local_branch = self.branch
353360
if not local_branch.is_valid():
354361
# Setup a tracking configuration - branch doesn't need to
355362
# exist to do that
@@ -578,15 +585,14 @@ def module(self):
578585
:raise InvalidGitRepositoryError: if a repository was not available. This could
579586
also mean that it was not yet initialized"""
580587
# late import to workaround circular dependencies
581-
from git.repo import Repo
582588

583589
if self.repo.bare:
584590
raise InvalidGitRepositoryError("Cannot retrieve module repository in bare parent repositories")
585591
# END handle bare mode
586592

587593
module_path = self.module_path()
588594
try:
589-
repo = Repo(module_path)
595+
repo = git.Repo(module_path)
590596
if repo != self.repo:
591597
return repo
592598
# END handle repo uninitialized
@@ -640,7 +646,7 @@ def exists(self):
640646

641647
@property
642648
def branch(self):
643-
""":return: The branch name that we are to checkout"""
649+
""":return: The branch instance that we are to checkout"""
644650
return self._branch
645651

646652
@property
@@ -715,7 +721,7 @@ def iter_items(cls, repo, parent_commit='HEAD'):
715721
# fill in remaining info - saves time as it doesn't have to be parsed again
716722
sm._name = n
717723
sm._parent_commit = pc
718-
sm._branch = b
724+
sm._branch = mkhead(repo, b)
719725
sm._url = u
720726

721727
yield sm
@@ -742,7 +748,7 @@ def __init__(self, repo):
742748
name = self.k_root_name,
743749
parent_commit = repo.head.commit,
744750
url = '',
745-
branch = self.k_head_default
751+
branch = mkhead(repo, self.k_head_default)
746752
)
747753

748754

Diff for: test/git/test_submodule.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _do_base_tests(self, rwrepo):
3434
assert sm.path == 'lib/git/ext/gitdb'
3535
assert sm.path == sm.name # for now, this is True
3636
assert sm.url == 'git://gitorious.org/git-python/gitdb.git'
37-
assert sm.branch == 'master' # its unset in this case
37+
assert sm.branch.name == 'master' # its unset in this case
3838
assert sm.parent_commit == rwrepo.head.commit
3939
# size is invalid
4040
self.failUnlessRaises(ValueError, getattr, sm, 'size')

0 commit comments

Comments
 (0)