##// END OF EJS Templates
commits: re-implemented fetching a single commit for git case....
marcink -
r3740:dcd8fbea new-ui
parent child Browse files
Show More
@@ -105,10 +105,9 b' class RepoCommitsView(RepoAppView):'
105
105
106 c.commit_ranges = commits
106 c.commit_ranges = commits
107 if not c.commit_ranges:
107 if not c.commit_ranges:
108 raise RepositoryError(
108 raise RepositoryError('The commit range returned an empty result')
109 'The commit range returned an empty result')
109 except CommitDoesNotExistError as e:
110 except CommitDoesNotExistError:
110 msg = _('No such commit exists. Org exception: `{}`').format(e)
111 msg = _('No such commit exists for this repository')
112 h.flash(msg, category='error')
111 h.flash(msg, category='error')
113 raise HTTPNotFound()
112 raise HTTPNotFound()
114 except Exception:
113 except Exception:
@@ -94,6 +94,7 b' class GitInMemoryCommit(base.BaseInMemor'
94 commit_data, branch, commit_tree, updated, removed)
94 commit_data, branch, commit_tree, updated, removed)
95
95
96 # Update vcs repository object
96 # Update vcs repository object
97 if commit_id not in self.repository.commit_ids:
97 self.repository.commit_ids.append(commit_id)
98 self.repository.commit_ids.append(commit_id)
98 self.repository._rebuild_cache(self.repository.commit_ids)
99 self.repository._rebuild_cache(self.repository.commit_ids)
99
100
@@ -222,13 +222,10 b' class GitRepository(BaseRepository):'
222 return []
222 return []
223 return output.splitlines()
223 return output.splitlines()
224
224
225 def _get_commit_id(self, commit_id_or_idx):
225 def _lookup_commit(self, commit_id_or_idx, translate_tag=True):
226 def is_null(value):
226 def is_null(value):
227 return len(value) == commit_id_or_idx.count('0')
227 return len(value) == commit_id_or_idx.count('0')
228
228
229 if self.is_empty():
230 raise EmptyRepositoryError("There are no commits yet")
231
232 if commit_id_or_idx in (None, '', 'tip', 'HEAD', 'head', -1):
229 if commit_id_or_idx in (None, '', 'tip', 'HEAD', 'head', -1):
233 return self.commit_ids[-1]
230 return self.commit_ids[-1]
234
231
@@ -238,8 +235,7 b' class GitRepository(BaseRepository):'
238 try:
235 try:
239 commit_id_or_idx = self.commit_ids[int(commit_id_or_idx)]
236 commit_id_or_idx = self.commit_ids[int(commit_id_or_idx)]
240 except Exception:
237 except Exception:
241 msg = "Commit %s does not exist for %s" % (
238 msg = "Commit %s does not exist for %s" % (commit_id_or_idx, self.name)
242 commit_id_or_idx, self)
243 raise CommitDoesNotExistError(msg)
239 raise CommitDoesNotExistError(msg)
244
240
245 elif is_bstr:
241 elif is_bstr:
@@ -261,8 +257,7 b' class GitRepository(BaseRepository):'
261
257
262 if (not SHA_PATTERN.match(commit_id_or_idx) or
258 if (not SHA_PATTERN.match(commit_id_or_idx) or
263 commit_id_or_idx not in self.commit_ids):
259 commit_id_or_idx not in self.commit_ids):
264 msg = "Commit %s does not exist for %s" % (
260 msg = "Commit %s does not exist for %s" % (commit_id_or_idx, self.name)
265 commit_id_or_idx, self)
266 raise CommitDoesNotExistError(msg)
261 raise CommitDoesNotExistError(msg)
267
262
268 # Ensure we return full id
263 # Ensure we return full id
@@ -431,19 +426,42 b' class GitRepository(BaseRepository):'
431 Returns `GitCommit` object representing commit from git repository
426 Returns `GitCommit` object representing commit from git repository
432 at the given `commit_id` or head (most recent commit) if None given.
427 at the given `commit_id` or head (most recent commit) if None given.
433 """
428 """
429 if self.is_empty():
430 raise EmptyRepositoryError("There are no commits yet")
431
434 if commit_id is not None:
432 if commit_id is not None:
435 self._validate_commit_id(commit_id)
433 self._validate_commit_id(commit_id)
434 try:
435 # we have cached idx, use it without contacting the remote
436 idx = self._commit_ids[commit_id]
437 return GitCommit(self, commit_id, idx, pre_load=pre_load)
438 except KeyError:
439 pass
440
436 elif commit_idx is not None:
441 elif commit_idx is not None:
437 self._validate_commit_idx(commit_idx)
442 self._validate_commit_idx(commit_idx)
443 try:
444 _commit_id = self.commit_ids[commit_idx]
445 if commit_idx < 0:
446 commit_idx = self.commit_ids.index(_commit_id)
447 return GitCommit(self, _commit_id, commit_idx, pre_load=pre_load)
448 except IndexError:
438 commit_id = commit_idx
449 commit_id = commit_idx
439 commit_id = self._get_commit_id(commit_id)
450 else:
440 try:
451 commit_id = "tip"
452
453 commit_id = self._lookup_commit(commit_id)
454 remote_idx = None
441 if translate_tag:
455 if translate_tag:
442 # Need to call remote to translate id for tagging scenario
456 # Need to call remote to translate id for tagging scenario
443 commit_id = self._remote.get_object(commit_id)["commit_id"]
457 remote_data = self._remote.get_object(commit_id)
458 commit_id = remote_data["commit_id"]
459 remote_idx = remote_data["idx"]
460
461 try:
444 idx = self._commit_ids[commit_id]
462 idx = self._commit_ids[commit_id]
445 except KeyError:
463 except KeyError:
446 raise RepositoryError("Cannot get object with id %s" % commit_id)
464 idx = remote_idx or 0
447
465
448 return GitCommit(self, commit_id, idx, pre_load=pre_load)
466 return GitCommit(self, commit_id, idx, pre_load=pre_load)
449
467
@@ -472,6 +490,7 b' class GitRepository(BaseRepository):'
472 """
490 """
473 if self.is_empty():
491 if self.is_empty():
474 raise EmptyRepositoryError("There are no commits yet")
492 raise EmptyRepositoryError("There are no commits yet")
493
475 self._validate_branch_name(branch_name)
494 self._validate_branch_name(branch_name)
476
495
477 if start_id is not None:
496 if start_id is not None:
@@ -479,9 +498,9 b' class GitRepository(BaseRepository):'
479 if end_id is not None:
498 if end_id is not None:
480 self._validate_commit_id(end_id)
499 self._validate_commit_id(end_id)
481
500
482 start_raw_id = self._get_commit_id(start_id)
501 start_raw_id = self._lookup_commit(start_id)
483 start_pos = self._commit_ids[start_raw_id] if start_id else None
502 start_pos = self._commit_ids[start_raw_id] if start_id else None
484 end_raw_id = self._get_commit_id(end_id)
503 end_raw_id = self._lookup_commit(end_id)
485 end_pos = max(0, self._commit_ids[end_raw_id]) if end_id else None
504 end_pos = max(0, self._commit_ids[end_raw_id]) if end_id else None
486
505
487 if None not in [start_id, end_id] and start_pos > end_pos:
506 if None not in [start_id, end_id] and start_pos > end_pos:
@@ -83,14 +83,15 b' class MercurialInMemoryCommit(BaseInMemo'
83
83
84 date, tz = date_to_timestamp_plus_offset(date)
84 date, tz = date_to_timestamp_plus_offset(date)
85
85
86 new_id = self.repository._remote.commitctx(
86 commit_id = self.repository._remote.commitctx(
87 message=message, parents=parent_ids,
87 message=message, parents=parent_ids,
88 commit_time=date, commit_timezone=tz, user=author,
88 commit_time=date, commit_timezone=tz, user=author,
89 files=self.get_paths(), extra=kwargs, removed=removed,
89 files=self.get_paths(), extra=kwargs, removed=removed,
90 updated=updated)
90 updated=updated)
91 if commit_id not in self.repository.commit_ids:
92 self.repository.commit_ids.append(commit_id)
93 self.repository._rebuild_cache(self.repository.commit_ids)
91
94
92 self.repository.commit_ids.append(new_id)
93 self.repository._rebuild_cache(self.repository.commit_ids)
94 self.repository.branches = self.repository._get_branches()
95 self.repository.branches = self.repository._get_branches()
95 tip = self.repository.get_commit()
96 tip = self.repository.get_commit()
96 self.reset()
97 self.reset()
@@ -425,18 +425,20 b' class MercurialRepository(BaseRepository'
425 if commit_id is not None:
425 if commit_id is not None:
426 self._validate_commit_id(commit_id)
426 self._validate_commit_id(commit_id)
427 try:
427 try:
428 # we have cached idx, use it without contacting the remote
428 idx = self._commit_ids[commit_id]
429 idx = self._commit_ids[commit_id]
429 return MercurialCommit(self, commit_id, idx, pre_load=pre_load)
430 return MercurialCommit(self, commit_id, idx, pre_load=pre_load)
430 except KeyError:
431 except KeyError:
431 pass
432 pass
433
432 elif commit_idx is not None:
434 elif commit_idx is not None:
433 self._validate_commit_idx(commit_idx)
435 self._validate_commit_idx(commit_idx)
434 try:
436 try:
435 id_ = self.commit_ids[commit_idx]
437 _commit_id = self.commit_ids[commit_idx]
436 if commit_idx < 0:
438 if commit_idx < 0:
437 commit_idx += len(self.commit_ids)
439 commit_idx = self.commit_ids.index(_commit_id)
438 return MercurialCommit(
440
439 self, id_, commit_idx, pre_load=pre_load)
441 return MercurialCommit(self, _commit_id, commit_idx, pre_load=pre_load)
440 except IndexError:
442 except IndexError:
441 commit_id = commit_idx
443 commit_id = commit_idx
442 else:
444 else:
@@ -448,8 +450,7 b' class MercurialRepository(BaseRepository'
448 try:
450 try:
449 raw_id, idx = self._remote.lookup(commit_id, both=True)
451 raw_id, idx = self._remote.lookup(commit_id, both=True)
450 except CommitDoesNotExistError:
452 except CommitDoesNotExistError:
451 msg = "Commit %s does not exist for %s" % (
453 msg = "Commit %s does not exist for %s" % (commit_id, self.name)
452 commit_id, self)
453 raise CommitDoesNotExistError(msg)
454 raise CommitDoesNotExistError(msg)
454
455
455 return MercurialCommit(self, raw_id, idx, pre_load=pre_load)
456 return MercurialCommit(self, raw_id, idx, pre_load=pre_load)
@@ -2437,6 +2437,7 b' class Repository(Base, BaseModel):'
2437 # control over cache behaviour
2437 # control over cache behaviour
2438 if cache is None and full_cache and not config:
2438 if cache is None and full_cache and not config:
2439 return self._get_instance_cached()
2439 return self._get_instance_cached()
2440 # cache here is sent to the "vcs server"
2440 return self._get_instance(cache=bool(cache), config=config)
2441 return self._get_instance(cache=bool(cache), config=config)
2441
2442
2442 def _get_instance_cached(self):
2443 def _get_instance_cached(self):
@@ -2479,7 +2480,8 b' class Repository(Base, BaseModel):'
2479 with_wire=custom_wire,
2480 with_wire=custom_wire,
2480 create=False,
2481 create=False,
2481 _vcs_alias=self.repo_type)
2482 _vcs_alias=self.repo_type)
2482
2483 if repo is not None:
2484 repo.count() # cache rebuild
2483 return repo
2485 return repo
2484
2486
2485 def __json__(self):
2487 def __json__(self):
@@ -4433,9 +4435,9 b' class Gist(Base, BaseModel):'
4433
4435
4434 def scm_instance(self, **kwargs):
4436 def scm_instance(self, **kwargs):
4435 """
4437 """
4436 Get explicit Mercurial repository used
4438 Get an instance of VCS Repository
4439
4437 :param kwargs:
4440 :param kwargs:
4438 :return:
4439 """
4441 """
4440 from rhodecode.model.gist import GistModel
4442 from rhodecode.model.gist import GistModel
4441 full_repo_path = os.path.join(self.base_path(), self.gist_access_id)
4443 full_repo_path = os.path.join(self.base_path(), self.gist_access_id)
@@ -683,7 +683,6 b' class PullRequestModel(BaseModel):'
683
683
684 # source repo
684 # source repo
685 source_repo = pull_request.source_repo.scm_instance()
685 source_repo = pull_request.source_repo.scm_instance()
686 source_repo.count() # cache rebuild
687
686
688 try:
687 try:
689 source_commit = source_repo.get_commit(commit_id=source_ref_name)
688 source_commit = source_repo.get_commit(commit_id=source_ref_name)
@@ -698,7 +697,6 b' class PullRequestModel(BaseModel):'
698
697
699 # target repo
698 # target repo
700 target_repo = pull_request.target_repo.scm_instance()
699 target_repo = pull_request.target_repo.scm_instance()
701 target_repo.count() # cache rebuild
702
700
703 try:
701 try:
704 target_commit = target_repo.get_commit(commit_id=target_ref_name)
702 target_commit = target_repo.get_commit(commit_id=target_ref_name)
@@ -1342,7 +1340,6 b' class PullRequestModel(BaseModel):'
1342 else:
1340 else:
1343 name_or_id = reference.commit_id
1341 name_or_id = reference.commit_id
1344
1342
1345 vcs_repository.count() # cache rebuild
1346 refreshed_commit = vcs_repository.get_commit(name_or_id)
1343 refreshed_commit = vcs_repository.get_commit(name_or_id)
1347 refreshed_reference = Reference(
1344 refreshed_reference = Reference(
1348 reference.type, reference.name, refreshed_commit.raw_id)
1345 reference.type, reference.name, refreshed_commit.raw_id)
@@ -129,20 +129,19 b' def test_strip_with_single_heads(backend'
129 assert commit_ids['b'] not in rest_commit_ids
129 assert commit_ids['b'] not in rest_commit_ids
130
130
131
131
132 def test_get_nodes_returns_unicode_flat(backend_random):
132 def test_get_nodes_returns_unicode_flat(backend):
133 repo = backend_random.repo
133 repo = backend.repo
134 directories, files = scm.ScmModel().get_nodes(
134 commit_id = repo.get_commit(commit_idx=0).raw_id
135 repo.repo_name, repo.get_commit(commit_idx=0).raw_id,
135 directories, files = scm.ScmModel().get_nodes(repo.repo_name, commit_id, flat=True)
136 flat=True)
137 assert_contains_only_unicode(directories)
136 assert_contains_only_unicode(directories)
138 assert_contains_only_unicode(files)
137 assert_contains_only_unicode(files)
139
138
140
139
141 def test_get_nodes_returns_unicode_non_flat(backend_random):
140 def test_get_nodes_returns_unicode_non_flat(backend):
142 repo = backend_random.repo
141 repo = backend.repo
143 directories, files = scm.ScmModel().get_nodes(
142 commit_id = repo.get_commit(commit_idx=0).raw_id
144 repo.repo_name, repo.get_commit(commit_idx=0).raw_id,
143
145 flat=False)
144 directories, files = scm.ScmModel().get_nodes(repo.repo_name, commit_id, flat=False)
146 # johbo: Checking only the names for now, since that is the critical
145 # johbo: Checking only the names for now, since that is the critical
147 # part.
146 # part.
148 assert_contains_only_unicode([d['name'] for d in directories])
147 assert_contains_only_unicode([d['name'] for d in directories])
@@ -148,7 +148,6 b' def _add_commits_to_repo(repo, commits):'
148 author=unicode(commit['author']),
148 author=unicode(commit['author']),
149 date=commit['date'],
149 date=commit['date'],
150 branch=commit.get('branch'))
150 branch=commit.get('branch'))
151
152 return tip
151 return tip
153
152
154
153
@@ -159,8 +159,7 b' class TestCommitsInNonEmptyRepo(BackendT'
159 parents=[initial],
159 parents=[initial],
160 branch=DEFAULT_BRANCH,)
160 branch=DEFAULT_BRANCH,)
161
161
162 default_branch_commits = self.repo.get_commits(
162 default_branch_commits = self.repo.get_commits(branch_name=DEFAULT_BRANCH)
163 branch_name=DEFAULT_BRANCH)
164 assert docs_branch_commit1 not in list(default_branch_commits)
163 assert docs_branch_commit1 not in list(default_branch_commits)
165 assert docs_branch_commit2 not in list(default_branch_commits)
164 assert docs_branch_commit2 not in list(default_branch_commits)
166
165
@@ -1091,23 +1091,23 b' class TestGitSpecificWithRepo(BackendTes'
1091 self.repo.get_diff(self.repo[0], self.repo[1])
1091 self.repo.get_diff(self.repo[0], self.repo[1])
1092 self.repo.run_git_command.assert_called_once_with(
1092 self.repo.run_git_command.assert_called_once_with(
1093 ['diff', '-U3', '--full-index', '--binary', '-p', '-M',
1093 ['diff', '-U3', '--full-index', '--binary', '-p', '-M',
1094 '--abbrev=40', self.repo._get_commit_id(0),
1094 '--abbrev=40', self.repo._lookup_commit(0),
1095 self.repo._get_commit_id(1)])
1095 self.repo._lookup_commit(1)])
1096
1096
1097 def test_get_diff_runs_git_command_with_str_hashes(self):
1097 def test_get_diff_runs_git_command_with_str_hashes(self):
1098 self.repo.run_git_command = mock.Mock(return_value=['', ''])
1098 self.repo.run_git_command = mock.Mock(return_value=['', ''])
1099 self.repo.get_diff(self.repo.EMPTY_COMMIT, self.repo[1])
1099 self.repo.get_diff(self.repo.EMPTY_COMMIT, self.repo[1])
1100 self.repo.run_git_command.assert_called_once_with(
1100 self.repo.run_git_command.assert_called_once_with(
1101 ['show', '-U3', '--full-index', '--binary', '-p', '-M',
1101 ['show', '-U3', '--full-index', '--binary', '-p', '-M',
1102 '--abbrev=40', self.repo._get_commit_id(1)])
1102 '--abbrev=40', self.repo._lookup_commit(1)])
1103
1103
1104 def test_get_diff_runs_git_command_with_path_if_its_given(self):
1104 def test_get_diff_runs_git_command_with_path_if_its_given(self):
1105 self.repo.run_git_command = mock.Mock(return_value=['', ''])
1105 self.repo.run_git_command = mock.Mock(return_value=['', ''])
1106 self.repo.get_diff(self.repo[0], self.repo[1], 'foo')
1106 self.repo.get_diff(self.repo[0], self.repo[1], 'foo')
1107 self.repo.run_git_command.assert_called_once_with(
1107 self.repo.run_git_command.assert_called_once_with(
1108 ['diff', '-U3', '--full-index', '--binary', '-p', '-M',
1108 ['diff', '-U3', '--full-index', '--binary', '-p', '-M',
1109 '--abbrev=40', self.repo._get_commit_id(0),
1109 '--abbrev=40', self.repo._lookup_commit(0),
1110 self.repo._get_commit_id(1), '--', 'foo'])
1110 self.repo._lookup_commit(1), '--', 'foo'])
1111
1111
1112
1112
1113 @pytest.mark.usefixtures("vcs_repository_support")
1113 @pytest.mark.usefixtures("vcs_repository_support")
General Comments 0
You need to be logged in to leave comments. Login now