| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -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 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,5 @@ | |||
| 1 | 1 | from base import Submodule | |
| 2 | 2 | from util import ( | |
| 3 | - mkhead, | ||
| 4 | 3 | find_first_remote_branch | |
| 5 | 4 | ) | |
| 6 | 5 | from git.exc import InvalidGitRepositoryError | |
@@ -29,7 +28,7 @@ def __init__(self, repo): | |||
| 29 | 28 | name = self.k_root_name, | |
| 30 | 29 | parent_commit = repo.head.commit, | |
| 31 | 30 | url = '', | |
| 32 | - branch = mkhead(repo, self.k_head_default) | ||
| 31 | + branch_path = git.Head.to_full_path(self.k_head_default) | ||
| 33 | 32 | ) | |
| 34 | 33 | ||
| 35 | 34 | ||
@@ -135,8 +134,8 @@ def update(self, previous_commit=None, recursive=True, force_remove=False, init= | |||
| 135 | 134 | ||
| 136 | 135 | # If we have a tracking branch, it should be available | |
| 137 | 136 | # in the new remote as well. | |
| 138 | - if len([r for r in smr.refs if r.remote_head == sm.branch.name]) == 0: | ||
| 139 | - raise ValueError("Submodule branch named %r was not available in new submodule remote at %r" % (sm.branch.name, sm.url)) | ||
| 137 | + if len([r for r in smr.refs if r.remote_head == sm.branch_name]) == 0: | ||
| 138 | + raise ValueError("Submodule branch named %r was not available in new submodule remote at %r" % (sm.branch_name, sm.url)) | ||
| 140 | 139 | # END head is not detached | |
| 141 | 140 | ||
| 142 | 141 | # now delete the changed one | |
@@ -181,7 +180,7 @@ def update(self, previous_commit=None, recursive=True, force_remove=False, init= | |||
| 181 | 180 | # tracking branch. | |
| 182 | 181 | smsha = sm.binsha | |
| 183 | 182 | found = False | |
| 184 | - rref = smr.refs[self.branch.name] | ||
| 183 | + rref = smr.refs[self.branch_name] | ||
| 185 | 184 | for c in rref.commit.traverse(): | |
| 186 | 185 | if c.binsha == smsha: | |
| 187 | 186 | found = True | |
@@ -203,28 +202,28 @@ def update(self, previous_commit=None, recursive=True, force_remove=False, init= | |||
| 203 | 202 | # END skip remote handling if new url already exists in module | |
| 204 | 203 | # END handle url | |
| 205 | 204 | ||
| 206 | - if sm.branch != psm.branch: | ||
| 205 | + if sm.branch_path != psm.branch_path: | ||
| 207 | 206 | # finally, create a new tracking branch which tracks the | |
| 208 | 207 | # new remote branch | |
| 209 | 208 | smm = sm.module() | |
| 210 | 209 | smmr = smm.remotes | |
| 211 | 210 | try: | |
| 212 | - tbr = git.Head.create(smm, sm.branch.name) | ||
| 211 | + tbr = git.Head.create(smm, sm.branch_name) | ||
| 213 | 212 | except git.GitCommandError, e: | |
| 214 | 213 | if e.status != 128: | |
| 215 | 214 | raise | |
| 216 | 215 | #END handle something unexpected | |
| 217 | 216 | ||
| 218 | 217 | # ... or reuse the existing one | |
| 219 | - tbr = git.Head(smm, git.Head.to_full_path(sm.branch.name)) | ||
| 218 | + tbr = git.Head(smm, sm.branch_path) | ||
| 220 | 219 | #END assure tracking branch exists | |
| 221 | 220 | ||
| 222 | - tbr.set_tracking_branch(find_first_remote_branch(smmr, sm.branch)) | ||
| 221 | + tbr.set_tracking_branch(find_first_remote_branch(smmr, sm.branch_name)) | ||
| 223 | 222 | # figure out whether the previous tracking branch contains | |
| 224 | 223 | # new commits compared to the other one, if not we can | |
| 225 | 224 | # delete it. | |
| 226 | 225 | try: | |
| 227 | - tbr = find_first_remote_branch(smmr, psm.branch) | ||
| 226 | + tbr = find_first_remote_branch(smmr, psm.branch_name) | ||
| 228 | 227 | if len(smm.git.cherry(tbr, psm.branch)) == 0: | |
| 229 | 228 | psm.branch.delete(smm, psm.branch) | |
| 230 | 229 | #END delete original tracking branch if there are no changes | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,16 +34,16 @@ def wrapper(self, *args, **kwargs): | |||
| 34 | 34 | wrapper.__name__ = func.__name__ | |
| 35 | 35 | return wrapper | |
| 36 | 36 | ||
| 37 | - def find_first_remote_branch(remotes, branch): | ||
| 37 | + def find_first_remote_branch(remotes, branch_name): | ||
| 38 | 38 | """Find the remote branch matching the name of the given branch or raise InvalidGitRepositoryError""" | |
| 39 | 39 | for remote in remotes: | |
| 40 | 40 | try: | |
| 41 | - return remote.refs[branch.name] | ||
| 41 | + return remote.refs[branch_name] | ||
| 42 | 42 | except IndexError: | |
| 43 | 43 | continue | |
| 44 | 44 | # END exception handling | |
| 45 | 45 | #END for remote | |
| 46 | - raise InvalidGitRepositoryError("Didn't find remote branch %r in any of the given remotes", branch) | ||
| 46 | + raise InvalidGitRepositoryError("Didn't find remote branch %r in any of the given remotes", branch_name) | ||
| 47 | 47 | ||
| 48 | 48 | #} END utilities | |
| 49 | 49 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,10 +35,19 @@ def _do_base_tests(self, rwrepo): | |||
| 35 | 35 | assert sm.path == 'lib/git/ext/gitdb' | |
| 36 | 36 | assert sm.path != sm.name # in our case, we have ids there, which don't equal the path | |
| 37 | 37 | assert sm.url == 'git://gitorious.org/git-python/gitdb.git' | |
| 38 | - assert sm.branch.name == 'master' # its unset in this case | ||
| 38 | + assert sm.branch_path == 'refs/heads/master' # the default ... | ||
| 39 | + assert sm.branch_name == 'master' | ||
| 39 | 40 | assert sm.parent_commit == rwrepo.head.commit | |
| 40 | 41 | # size is always 0 | |
| 41 | 42 | assert sm.size == 0 | |
| 43 | + # the module is not checked-out yet | ||
| 44 | + self.failUnlessRaises(InvalidGitRepositoryError, sm.module) | ||
| 45 | + | ||
| 46 | + # which is why we can't get the branch either - it points into the module() repository | ||
| 47 | + self.failUnlessRaises(InvalidGitRepositoryError, getattr, sm, 'branch') | ||
| 48 | + | ||
| 49 | + # branch_path works, as its just a string | ||
| 50 | + assert isinstance(sm.branch_path, basestring) | ||
| 42 | 51 | ||
| 43 | 52 | # some commits earlier we still have a submodule, but its at a different commit | |
| 44 | 53 | smold = Submodule.iter_items(rwrepo, self.k_subm_changed).next() | |
@@ -455,7 +464,7 @@ def test_root_module(self, rwrepo): | |||
| 455 | 464 | nsmm = nsm.module() | |
| 456 | 465 | prev_commit = nsmm.head.commit | |
| 457 | 466 | for branch in ("some_virtual_branch", cur_branch.name): | |
| 458 | - nsm.config_writer().set_value(Submodule.k_head_option, branch) | ||
| 467 | + nsm.config_writer().set_value(Submodule.k_head_option, git.Head.to_full_path(branch)) | ||
| 459 | 468 | csmbranchchange = rwrepo.index.commit("changed branch to %s" % branch) | |
| 460 | 469 | nsm.set_parent_commit(csmbranchchange) | |
| 461 | 470 | # END for each branch to change | |
| Back | FazBrowse Home | New Git URL |
0 commit comments