@@ -24,6 +24,10 @@ def sm_name(section):
24
24
""":return: name of the submodule as parsed from the section name"""
25
25
section = section .strip ()
26
26
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 ))
27
31
#} END utilities
28
32
29
33
@@ -96,13 +100,14 @@ def __init__(self, repo, binsha, mode=None, path=None, name = None, parent_commi
96
100
:param binsha: binary sha referring to a commit in the remote repository, see url parameter
97
101
:param parent_commit: see set_parent_commit()
98
102
: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"""
100
104
super (Submodule , self ).__init__ (repo , binsha , mode , path )
101
105
if parent_commit is not None :
102
106
self ._parent_commit = parent_commit
103
107
if url is not None :
104
108
self ._url = url
105
109
if branch is not None :
110
+ assert isinstance (branch , git .Head )
106
111
self ._branch = branch
107
112
if name is not None :
108
113
self ._name = name
@@ -119,7 +124,7 @@ def _set_cache_(self, attr):
119
124
self .path = reader .get_value ('path' )
120
125
self ._url = reader .get_value ('url' )
121
126
# 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 ) )
123
128
elif attr == '_name' :
124
129
raise AttributeError ("Cannot retrieve the name of a submodule if it was not set initially" )
125
130
else :
@@ -203,7 +208,7 @@ def _config_parser_constrained(self, read_only):
203
208
#{ Edit Interface
204
209
205
210
@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 ):
207
212
"""Add a new submodule to the given repository. This will alter the index
208
213
as well as the .gitmodules file, but will not create a new commit.
209
214
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
220
225
:param branch: branch at which the submodule should (later) be checked out.
221
226
The given branch must exist in the remote repository, and will be checked
222
227
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
225
232
:param no_checkout: if True, and if the repository has to be cloned manually,
226
233
no checkout will be performed
227
234
: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
243
250
return repo .head .commit .tree [path ]
244
251
# END handle existing
245
252
246
- branch = git . Head (repo , git . Head . to_full_path ( branch ) )
253
+ br = mkhead (repo , branch or cls . k_head_default )
247
254
has_module = sm .module_exists ()
248
- branch_is_default = branch . name == cls . k_head_default
255
+ branch_is_default = branch is None
249
256
if has_module and url is not None :
250
257
if url not in [r .url for r in sm .module ().remotes ]:
251
258
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
266
273
else :
267
274
# clone new repo
268
275
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 )
271
278
# END setup checkout-branch
272
279
mrepo = git .Repo .clone_from (url , path , ** kwargs )
273
280
# END verify url
@@ -280,8 +287,8 @@ def add(cls, repo, name, path, url=None, branch=k_head_default, no_checkout=Fals
280
287
sm ._url = url
281
288
if not branch_is_default :
282
289
# 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
285
292
# END handle path
286
293
del (writer )
287
294
@@ -348,8 +355,8 @@ def update(self, recursive=False, init=True, to_latest_revision=False):
348
355
349
356
# see whether we have a valid branch to checkout
350
357
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
353
360
if not local_branch .is_valid ():
354
361
# Setup a tracking configuration - branch doesn't need to
355
362
# exist to do that
@@ -578,15 +585,14 @@ def module(self):
578
585
:raise InvalidGitRepositoryError: if a repository was not available. This could
579
586
also mean that it was not yet initialized"""
580
587
# late import to workaround circular dependencies
581
- from git .repo import Repo
582
588
583
589
if self .repo .bare :
584
590
raise InvalidGitRepositoryError ("Cannot retrieve module repository in bare parent repositories" )
585
591
# END handle bare mode
586
592
587
593
module_path = self .module_path ()
588
594
try :
589
- repo = Repo (module_path )
595
+ repo = git . Repo (module_path )
590
596
if repo != self .repo :
591
597
return repo
592
598
# END handle repo uninitialized
@@ -640,7 +646,7 @@ def exists(self):
640
646
641
647
@property
642
648
def branch (self ):
643
- """:return: The branch name that we are to checkout"""
649
+ """:return: The branch instance that we are to checkout"""
644
650
return self ._branch
645
651
646
652
@property
@@ -715,7 +721,7 @@ def iter_items(cls, repo, parent_commit='HEAD'):
715
721
# fill in remaining info - saves time as it doesn't have to be parsed again
716
722
sm ._name = n
717
723
sm ._parent_commit = pc
718
- sm ._branch = b
724
+ sm ._branch = mkhead ( repo , b )
719
725
sm ._url = u
720
726
721
727
yield sm
@@ -742,7 +748,7 @@ def __init__(self, repo):
742
748
name = self .k_root_name ,
743
749
parent_commit = repo .head .commit ,
744
750
url = '' ,
745
- branch = self .k_head_default
751
+ branch = mkhead ( repo , self .k_head_default )
746
752
)
747
753
748
754
0 commit comments