# HG changeset patch # User Marcin Kuzminski # Date 1368189302 -7200 # Node ID 42981614c6249ff192204dccd44b46b701d79222 # Parent 2ad55d8ba11de69803128411de8ac657e969d615 vcs: fixed issues with calling get_changesets method doesn't throws EmptyRepositoryError when called on empty repos diff -r 2ad55d8ba11d -r 42981614c624 rhodecode/lib/vcs/backends/git/repository.py --- a/rhodecode/lib/vcs/backends/git/repository.py Thu May 09 22:51:53 2013 +0200 +++ b/rhodecode/lib/vcs/backends/git/repository.py Fri May 10 14:35:02 2013 +0200 @@ -81,6 +81,18 @@ except KeyError: return None + @property + def _empty(self): + """ + Checks if repository is empty ie. without any changesets + """ + + try: + self.revisions[0] + except (KeyError, IndexError): + return True + return False + @LazyProperty def revisions(self): """ @@ -250,9 +262,7 @@ is_null = lambda o: len(o) == revision.count('0') - try: - self.revisions[0] - except (KeyError, IndexError): + if self._empty: raise EmptyRepositoryError("There are no changesets yet") if revision in (None, '', 'tip', 'HEAD', 'head', -1): @@ -492,6 +502,11 @@ if branch_name and branch_name not in self.branches: raise BranchDoesNotExistError("Branch '%s' not found" \ % branch_name) + # actually we should check now if it's not an empty repo to not spaw + # subprocess commands + if self._empty: + raise EmptyRepositoryError("There are no changesets yet") + # %H at format means (full) commit hash, initial hashes are retrieved # in ascending date order cmd_template = 'log --date-order --reverse --pretty=format:"%H"' diff -r 2ad55d8ba11d -r 42981614c624 rhodecode/lib/vcs/backends/hg/repository.py --- a/rhodecode/lib/vcs/backends/hg/repository.py Thu May 09 22:51:53 2013 +0200 +++ b/rhodecode/lib/vcs/backends/hg/repository.py Fri May 10 14:35:02 2013 +0200 @@ -78,7 +78,7 @@ @property def _empty(self): """ - Checks if repository is empty without any changesets + Checks if repository is empty ie. without any changesets """ # TODO: Following raises errors when using InMemoryChangeset... # return len(self._repo.changelog) == 0 diff -r 2ad55d8ba11d -r 42981614c624 rhodecode/tests/vcs/test_changesets.py --- a/rhodecode/tests/vcs/test_changesets.py Thu May 09 22:51:53 2013 +0200 +++ b/rhodecode/tests/vcs/test_changesets.py Fri May 10 14:35:02 2013 +0200 @@ -1,5 +1,6 @@ from __future__ import with_statement +import time import datetime from rhodecode.lib import vcs from rhodecode.tests.vcs.base import BackendTestMixin @@ -12,9 +13,10 @@ ) from rhodecode.lib.vcs.exceptions import ( BranchDoesNotExistError, ChangesetDoesNotExistError, - RepositoryError + RepositoryError, EmptyRepositoryError ) from rhodecode.lib.vcs.utils.compat import unittest +from rhodecode.tests.vcs.conf import get_new_dir class TestBaseChangeset(unittest.TestCase): @@ -197,6 +199,14 @@ changesets = list(self.repo.get_changesets(start=2, end=3)) self.assertEqual(len(changesets), 2) + def test_get_changesets_on_empty_repo_raises_EmptyRepository_error(self): + Backend = self.get_backend() + repo_path = get_new_dir(str(time.time())) + repo = Backend(repo_path, create=True) + + with self.assertRaises(EmptyRepositoryError): + list(repo.get_changesets(start='foobar')) + def test_get_changesets_includes_end_changeset(self): second_id = self.repo.revisions[1] changesets = list(self.repo.get_changesets(end=second_id))