##// END OF EJS Templates
backends: use reference explicitly to properly translate GIT references to commits such as numeric branches
milka -
r4653:5035738c default
parent child Browse files
Show More
@@ -655,7 +655,7 b' def get_clone_url(request, uri_tmpl, rep'
655
655
656
656
657 def get_commit_safe(repo, commit_id=None, commit_idx=None, pre_load=None,
657 def get_commit_safe(repo, commit_id=None, commit_idx=None, pre_load=None,
658 maybe_unreachable=False):
658 maybe_unreachable=False, reference_obj=None):
659 """
659 """
660 Safe version of get_commit if this commit doesn't exists for a
660 Safe version of get_commit if this commit doesn't exists for a
661 repository it returns a Dummy one instead
661 repository it returns a Dummy one instead
@@ -665,6 +665,7 b' def get_commit_safe(repo, commit_id=None'
665 :param commit_idx: numeric commit index
665 :param commit_idx: numeric commit index
666 :param pre_load: optional list of commit attributes to load
666 :param pre_load: optional list of commit attributes to load
667 :param maybe_unreachable: translate unreachable commits on git repos
667 :param maybe_unreachable: translate unreachable commits on git repos
668 :param reference_obj: explicitly search via a reference obj in git. E.g "branch:123" would mean branch "123"
668 """
669 """
669 # TODO(skreft): remove these circular imports
670 # TODO(skreft): remove these circular imports
670 from rhodecode.lib.vcs.backends.base import BaseRepository, EmptyCommit
671 from rhodecode.lib.vcs.backends.base import BaseRepository, EmptyCommit
@@ -676,7 +677,7 b' def get_commit_safe(repo, commit_id=None'
676 try:
677 try:
677 commit = repo.get_commit(
678 commit = repo.get_commit(
678 commit_id=commit_id, commit_idx=commit_idx, pre_load=pre_load,
679 commit_id=commit_id, commit_idx=commit_idx, pre_load=pre_load,
679 maybe_unreachable=maybe_unreachable)
680 maybe_unreachable=maybe_unreachable, reference_obj=reference_obj)
680 except (RepositoryError, LookupError):
681 except (RepositoryError, LookupError):
681 commit = EmptyCommit()
682 commit = EmptyCommit()
682 return commit
683 return commit
@@ -72,6 +72,10 b' class Reference(_Reference):'
72 if self.type == 'book':
72 if self.type == 'book':
73 return self.name
73 return self.name
74
74
75 @property
76 def to_unicode(self):
77 return reference_to_unicode(self)
78
75
79
76 def unicode_to_reference(raw):
80 def unicode_to_reference(raw):
77 """
81 """
@@ -483,7 +487,7 b' class BaseRepository(object):'
483 self._is_empty = False
487 self._is_empty = False
484
488
485 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None,
489 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None,
486 translate_tag=None, maybe_unreachable=False):
490 translate_tag=None, maybe_unreachable=False, reference_obj=None):
487 """
491 """
488 Returns instance of `BaseCommit` class. If `commit_id` and `commit_idx`
492 Returns instance of `BaseCommit` class. If `commit_id` and `commit_idx`
489 are both None, most recent commit is returned.
493 are both None, most recent commit is returned.
@@ -228,7 +228,8 b' class GitRepository(BaseRepository):'
228 return []
228 return []
229 return output.splitlines()
229 return output.splitlines()
230
230
231 def _lookup_commit(self, commit_id_or_idx, translate_tag=True, maybe_unreachable=False):
231 def _lookup_commit(self, commit_id_or_idx, translate_tag=True, maybe_unreachable=False, reference_obj=None):
232
232 def is_null(value):
233 def is_null(value):
233 return len(value) == commit_id_or_idx.count('0')
234 return len(value) == commit_id_or_idx.count('0')
234
235
@@ -239,15 +240,20 b' class GitRepository(BaseRepository):'
239 *map(safe_str, [commit_id_or_idx, self.name]))
240 *map(safe_str, [commit_id_or_idx, self.name]))
240
241
241 is_bstr = isinstance(commit_id_or_idx, (str, unicode))
242 is_bstr = isinstance(commit_id_or_idx, (str, unicode))
242 if ((is_bstr and commit_id_or_idx.isdigit() and len(commit_id_or_idx) < 12)
243 is_branch = reference_obj and reference_obj.branch
243 or isinstance(commit_id_or_idx, int) or is_null(commit_id_or_idx)):
244 is_numeric_idx = \
245 (is_bstr and commit_id_or_idx.isdigit() and len(commit_id_or_idx) < 12) \
246 or isinstance(commit_id_or_idx, int)
247
248 if not is_branch and (is_numeric_idx or is_null(commit_id_or_idx)):
244 try:
249 try:
245 commit_id_or_idx = self.commit_ids[int(commit_id_or_idx)]
250 commit_id_or_idx = self.commit_ids[int(commit_id_or_idx)]
246 except Exception:
251 except Exception:
247 raise CommitDoesNotExistError(commit_missing_err)
252 raise CommitDoesNotExistError(commit_missing_err)
248
253
249 elif is_bstr:
254 elif is_bstr:
250 # Need to call remote to translate id for tagging scenario
255 # Need to call remote to translate id for tagging scenarios,
256 # or branch that are numeric
251 try:
257 try:
252 remote_data = self._remote.get_object(commit_id_or_idx,
258 remote_data = self._remote.get_object(commit_id_or_idx,
253 maybe_unreachable=maybe_unreachable)
259 maybe_unreachable=maybe_unreachable)
@@ -413,11 +419,12 b' class GitRepository(BaseRepository):'
413 return
419 return
414
420
415 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None,
421 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None,
416 translate_tag=True, maybe_unreachable=False):
422 translate_tag=True, maybe_unreachable=False, reference_obj=None):
417 """
423 """
418 Returns `GitCommit` object representing commit from git repository
424 Returns `GitCommit` object representing commit from git repository
419 at the given `commit_id` or head (most recent commit) if None given.
425 at the given `commit_id` or head (most recent commit) if None given.
420 """
426 """
427
421 if self.is_empty():
428 if self.is_empty():
422 raise EmptyRepositoryError("There are no commits yet")
429 raise EmptyRepositoryError("There are no commits yet")
423
430
@@ -443,7 +450,9 b' class GitRepository(BaseRepository):'
443 commit_id = "tip"
450 commit_id = "tip"
444
451
445 if translate_tag:
452 if translate_tag:
446 commit_id = self._lookup_commit(commit_id, maybe_unreachable=maybe_unreachable)
453 commit_id = self._lookup_commit(
454 commit_id, maybe_unreachable=maybe_unreachable,
455 reference_obj=reference_obj)
447
456
448 try:
457 try:
449 idx = self._commit_ids[commit_id]
458 idx = self._commit_ids[commit_id]
@@ -437,7 +437,7 b' class MercurialRepository(BaseRepository'
437 return os.path.join(self.path, '.hg', '.hgrc')
437 return os.path.join(self.path, '.hg', '.hgrc')
438
438
439 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None,
439 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None,
440 translate_tag=None, maybe_unreachable=False):
440 translate_tag=None, maybe_unreachable=False, reference_obj=None):
441 """
441 """
442 Returns ``MercurialCommit`` object representing repository's
442 Returns ``MercurialCommit`` object representing repository's
443 commit at the given `commit_id` or `commit_idx`.
443 commit at the given `commit_id` or `commit_idx`.
@@ -277,7 +277,7 b' class SubversionRepository(base.BaseRepo'
277 return os.path.join(self.path, 'hooks')
277 return os.path.join(self.path, 'hooks')
278
278
279 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None,
279 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None,
280 translate_tag=None, maybe_unreachable=False):
280 translate_tag=None, maybe_unreachable=False, reference_obj=None):
281 if self.is_empty():
281 if self.is_empty():
282 raise EmptyRepositoryError("There are no commits yet")
282 raise EmptyRepositoryError("There are no commits yet")
283 if commit_id is not None:
283 if commit_id is not None:
@@ -2398,10 +2398,10 b' class Repository(Base, BaseModel):'
2398 # SCM PROPERTIES
2398 # SCM PROPERTIES
2399 #==========================================================================
2399 #==========================================================================
2400
2400
2401 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None, maybe_unreachable=False):
2401 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None, maybe_unreachable=False, reference_obj=None):
2402 return get_commit_safe(
2402 return get_commit_safe(
2403 self.scm_instance(), commit_id, commit_idx, pre_load=pre_load,
2403 self.scm_instance(), commit_id, commit_idx, pre_load=pre_load,
2404 maybe_unreachable=maybe_unreachable)
2404 maybe_unreachable=maybe_unreachable, reference_obj=reference_obj)
2405
2405
2406 def get_changeset(self, rev=None, pre_load=None):
2406 def get_changeset(self, rev=None, pre_load=None):
2407 warnings.warn("Use get_commit", DeprecationWarning)
2407 warnings.warn("Use get_commit", DeprecationWarning)
@@ -908,7 +908,8 b' class PullRequestModel(BaseModel):'
908
908
909 try:
909 try:
910 if source_ref_type in self.REF_TYPES:
910 if source_ref_type in self.REF_TYPES:
911 source_commit = source_repo.get_commit(source_ref_name)
911 source_commit = source_repo.get_commit(
912 source_ref_name, reference_obj=pull_request.source_ref_parts)
912 else:
913 else:
913 source_commit = source_repo.get_commit(source_ref_id)
914 source_commit = source_repo.get_commit(source_ref_id)
914 except CommitDoesNotExistError:
915 except CommitDoesNotExistError:
@@ -922,7 +923,8 b' class PullRequestModel(BaseModel):'
922
923
923 try:
924 try:
924 if target_ref_type in self.REF_TYPES:
925 if target_ref_type in self.REF_TYPES:
925 target_commit = target_repo.get_commit(target_ref_name)
926 target_commit = target_repo.get_commit(
927 target_ref_name, reference_obj=pull_request.target_ref_parts)
926 else:
928 else:
927 target_commit = target_repo.get_commit(target_ref_id)
929 target_commit = target_repo.get_commit(target_ref_id)
928 except CommitDoesNotExistError:
930 except CommitDoesNotExistError:
General Comments 0
You need to be logged in to leave comments. Login now