@@ -51,41 +51,41 @@ class Submodule(util.IndexObject, Iterable, Traversable):
51
51
# this is a bogus type for base class compatability
52
52
type = 'submodule'
53
53
54
- __slots__ = ('_parent_commit' , '_url' , '_branch ' , '_name' , '__weakref__' )
55
- _cache_attrs = ('path' , '_url' , '_branch ' )
54
+ __slots__ = ('_parent_commit' , '_url' , '_branch_path ' , '_name' , '__weakref__' )
55
+ _cache_attrs = ('path' , '_url' , '_branch_path ' )
56
56
57
- def __init__ (self , repo , binsha , mode = None , path = None , name = None , parent_commit = None , url = None , branch = None ):
57
+ def __init__ (self , repo , binsha , mode = None , path = None , name = None , parent_commit = None , url = None , branch_path = None ):
58
58
"""Initialize this instance with its attributes. We only document the ones
59
59
that differ from ``IndexObject``
60
60
61
61
:param repo: Our parent repository
62
62
:param binsha: binary sha referring to a commit in the remote repository, see url parameter
63
63
:param parent_commit: see set_parent_commit()
64
64
:param url: The url to the remote repository which is the submodule
65
- :param branch: Head instance to checkout when cloning the remote repository"""
65
+ :param branch_path: full (relative) path to ref to checkout when cloning the remote repository"""
66
66
super (Submodule , self ).__init__ (repo , binsha , mode , path )
67
67
self .size = 0
68
68
if parent_commit is not None :
69
69
self ._parent_commit = parent_commit
70
70
if url is not None :
71
71
self ._url = url
72
- if branch is not None :
73
- assert isinstance (branch , git . Head )
74
- self ._branch = branch
72
+ if branch_path is not None :
73
+ assert isinstance (branch_path , basestring )
74
+ self ._branch_path = branch_path
75
75
if name is not None :
76
76
self ._name = name
77
77
78
78
def _set_cache_ (self , attr ):
79
79
if attr == '_parent_commit' :
80
80
# set a default value, which is the root tree of the current head
81
81
self ._parent_commit = self .repo .commit ()
82
- elif attr in ('path' , '_url' , '_branch ' ):
82
+ elif attr in ('path' , '_url' , '_branch_path ' ):
83
83
reader = self .config_reader ()
84
84
# default submodule values
85
85
self .path = reader .get_value ('path' )
86
86
self ._url = reader .get_value ('url' )
87
87
# git-python extension values - optional
88
- self ._branch = mkhead ( self . repo , reader .get_value (self .k_head_option , self .k_head_default ))
88
+ self ._branch_path = reader .get_value (self .k_head_option , git . Head . to_full_path ( self .k_head_default ))
89
89
elif attr == '_name' :
90
90
raise AttributeError ("Cannot retrieve the name of a submodule if it was not set initially" )
91
91
else :
@@ -119,7 +119,7 @@ def __str__(self):
119
119
return self ._name
120
120
121
121
def __repr__ (self ):
122
- return "git.%s(name=%s, path=%s, url=%s, branch =%s)" % (type (self ).__name__ , self ._name , self .path , self .url , self .branch )
122
+ return "git.%s(name=%s, path=%s, url=%s, branch_path =%s)" % (type (self ).__name__ , self ._name , self .path , self .url , self .branch_path )
123
123
124
124
@classmethod
125
125
def _config_parser (cls , repo , parent_commit , read_only ):
@@ -226,7 +226,7 @@ def add(cls, repo, name, path, url=None, branch=None, no_checkout=False):
226
226
# END handle exceptions
227
227
# END handle existing
228
228
229
- br = mkhead ( repo , branch or cls .k_head_default )
229
+ br = git . Head . to_full_path ( str ( branch ) or cls .k_head_default )
230
230
has_module = sm .module_exists ()
231
231
branch_is_default = branch is None
232
232
if has_module and url is not None :
@@ -250,7 +250,7 @@ def add(cls, repo, name, path, url=None, branch=None, no_checkout=False):
250
250
# clone new repo
251
251
kwargs = {'n' : no_checkout }
252
252
if not branch_is_default :
253
- kwargs ['b' ] = str ( br )
253
+ kwargs ['b' ] = br
254
254
# END setup checkout-branch
255
255
mrepo = git .Repo .clone_from (url , path , ** kwargs )
256
256
# END verify url
@@ -264,8 +264,8 @@ def add(cls, repo, name, path, url=None, branch=None, no_checkout=False):
264
264
sm ._url = url
265
265
if not branch_is_default :
266
266
# store full path
267
- writer .set_value (cls .k_head_option , br . path )
268
- sm ._branch = br . path
267
+ writer .set_value (cls .k_head_option , br )
268
+ sm ._branch_path = br
269
269
# END handle path
270
270
del (writer )
271
271
@@ -327,13 +327,8 @@ def update(self, recursive=False, init=True, to_latest_revision=False):
327
327
# see whether we have a valid branch to checkout
328
328
try :
329
329
# find a remote which has our branch - we try to be flexible
330
- remote_branch = find_first_remote_branch (mrepo .remotes , self .branch )
331
- local_branch = self .branch
332
- if not local_branch .is_valid ():
333
- # Setup a tracking configuration - branch doesn't need to
334
- # exist to do that
335
- local_branch .set_tracking_branch (remote_branch )
336
- #END handle local branch
330
+ remote_branch = find_first_remote_branch (mrepo .remotes , self .branch_name )
331
+ local_branch = mkhead (mrepo , self .branch_path )
337
332
338
333
# have a valid branch, but no checkout - make sure we can figure
339
334
# that out by marking the commit with a null_sha
@@ -349,8 +344,9 @@ def update(self, recursive=False, init=True, to_latest_revision=False):
349
344
350
345
# make sure HEAD is not detached
351
346
mrepo .head .ref = local_branch
347
+ mrepo .head .ref .set_tracking_branch (remote_branch )
352
348
except IndexError :
353
- print >> sys .stderr , "Warning: Failed to checkout tracking branch %s" % self .branch
349
+ print >> sys .stderr , "Warning: Failed to checkout tracking branch %s" % self .branch_path
354
350
#END handle tracking branch
355
351
356
352
# NOTE: Have to write the repo config file as well, otherwise
@@ -516,8 +512,8 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
516
512
:param module: If True, the module we point to will be deleted
517
513
as well. If the module is currently on a commit which is not part
518
514
of any branch in the remote, if the currently checked out branch
519
- is ahead of its tracking branch, if you have modifications in the
520
515
working tree, or untracked files,
516
+ is ahead of its tracking branch, if you have modifications in the
521
517
In case the removal of the repository fails for these reasons, the
522
518
submodule status will not have been altered.
523
519
If this submodule has child-modules on its own, these will be deleted
@@ -611,6 +607,9 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
611
607
self .repo .config_writer ().remove_section (sm_section (self .name ))
612
608
self .config_writer ().remove_section ()
613
609
# END delete configuration
610
+
611
+ # void our data not to delay invalid access
612
+ self ._clear_cache ()
614
613
615
614
return self
616
615
@@ -732,8 +731,22 @@ def exists(self):
732
731
733
732
@property
734
733
def branch (self ):
735
- """:return: The branch instance that we are to checkout"""
736
- return self ._branch
734
+ """:return: The branch instance that we are to checkout
735
+ :raise InvalidGitRepositoryError: if our module is not yet checked out"""
736
+ return mkhead (self .module (), self ._branch_path )
737
+
738
+ @property
739
+ def branch_path (self ):
740
+ """:return: full (relative) path as string to the branch we would checkout
741
+ from the remote and track"""
742
+ return self ._branch_path
743
+
744
+ @property
745
+ def branch_name (self ):
746
+ """:return: the name of the branch, which is the shortest possible branch name"""
747
+ # use an instance method, for this we create a temporary Head instance
748
+ # which uses a repository that is available at least ( it makes no difference )
749
+ return git .Head (self .repo , self ._branch_path ).name
737
750
738
751
@property
739
752
def url (self ):
@@ -814,7 +827,7 @@ def iter_items(cls, repo, parent_commit='HEAD'):
814
827
# fill in remaining info - saves time as it doesn't have to be parsed again
815
828
sm ._name = n
816
829
sm ._parent_commit = pc
817
- sm ._branch = mkhead ( repo , b )
830
+ sm ._branch_path = git . Head . to_full_path ( b )
818
831
sm ._url = u
819
832
820
833
yield sm
0 commit comments