# HG changeset patch # User domruf # Date 2017-11-23 19:34:49 # Node ID 32e6957d0aacdf34a6dce21c5617c1c30576defe # Parent bfb1ae42bcbbfb6daf5f6a1da4cc896a5eee7a28 api: add max_revisions option to get_changesets The returning JSON can become pretty big and hard to parse. Therefore add an option that allows a client to request the changesets in smaller chuncks. diff --git a/kallithea/controllers/api/api.py b/kallithea/controllers/api/api.py --- a/kallithea/controllers/api/api.py +++ b/kallithea/controllers/api/api.py @@ -2498,7 +2498,7 @@ class ApiController(JSONRPCController): # permission check inside def get_changesets(self, repoid, start=None, end=None, start_date=None, - end_date=None, branch_name=None, reverse=False, with_file_list=False): + end_date=None, branch_name=None, reverse=False, with_file_list=False, max_revisions=None): repo = get_repo_or_error(repoid) if not HasRepoPermissionLevel('read')(repo.repo_name): raise JSONRPCError('Access denied to repo %s' % repo.repo_name) @@ -2511,7 +2511,7 @@ class ApiController(JSONRPCController): datetime.strptime(start_date, format) if start_date else None, datetime.strptime(end_date, format) if end_date else None, branch_name, - reverse)] + reverse, max_revisions)] except EmptyRepositoryError as e: raise JSONRPCError(e.message) diff --git a/kallithea/lib/vcs/backends/base.py b/kallithea/lib/vcs/backends/base.py --- a/kallithea/lib/vcs/backends/base.py +++ b/kallithea/lib/vcs/backends/base.py @@ -163,7 +163,7 @@ class BaseRepository(object): yield self.get_changeset(revision) def get_changesets(self, start=None, end=None, start_date=None, - end_date=None, branch_name=None, reverse=False): + end_date=None, branch_name=None, reverse=False, max_revisions=None): """ Returns iterator of ``BaseChangeset`` objects from start to end, both inclusive. diff --git a/kallithea/lib/vcs/backends/git/repository.py b/kallithea/lib/vcs/backends/git/repository.py --- a/kallithea/lib/vcs/backends/git/repository.py +++ b/kallithea/lib/vcs/backends/git/repository.py @@ -504,7 +504,7 @@ class GitRepository(BaseRepository): return changeset def get_changesets(self, start=None, end=None, start_date=None, - end_date=None, branch_name=None, reverse=False): + end_date=None, branch_name=None, reverse=False, max_revisions=None): """ Returns iterator of ``GitChangeset`` objects from start to end (both are inclusive), in ascending date order (unless ``reverse`` is set). @@ -537,6 +537,8 @@ class GitRepository(BaseRepository): # %H at format means (full) commit hash, initial hashes are retrieved # in ascending date order cmd = ['log', '--date-order', '--reverse', '--pretty=format:%H'] + if max_revisions: + cmd += ['--max-count=%s' % max_revisions] if start_date: cmd += ['--since', start_date.strftime('%m/%d/%y %H:%M:%S')] if end_date: diff --git a/kallithea/lib/vcs/backends/hg/repository.py b/kallithea/lib/vcs/backends/hg/repository.py --- a/kallithea/lib/vcs/backends/hg/repository.py +++ b/kallithea/lib/vcs/backends/hg/repository.py @@ -508,7 +508,7 @@ class MercurialRepository(BaseRepository return changeset def get_changesets(self, start=None, end=None, start_date=None, - end_date=None, branch_name=None, reverse=False): + end_date=None, branch_name=None, reverse=False, max_revisions=None): """ Returns iterator of ``MercurialChangeset`` objects from start to end (both are inclusive) @@ -539,13 +539,17 @@ class MercurialRepository(BaseRepository filter_ = [] if branch_name: filter_.append('branch("%s")' % (branch_name)) - if start_date: filter_.append('date(">%s")' % start_date) if end_date: filter_.append('date("<%s")' % end_date) - if filter_: - revspec = ' and '.join(filter_) + if filter_ or max_revisions: + if filter_: + revspec = ' and '.join(filter_) + else: + revspec = 'all()' + if max_revisions: + revspec = 'limit(%s, %s)' % (revspec, max_revisions) revisions = scmutil.revrange(self._repo, [revspec]) else: revisions = self.revisions diff --git a/kallithea/tests/api/api_base.py b/kallithea/tests/api/api_base.py --- a/kallithea/tests/api/api_base.py +++ b/kallithea/tests/api/api_base.py @@ -2503,6 +2503,15 @@ class _BaseTestApi(object): assert 'message' in result[0] assert 'added' not in result[0] + def test_api_get_changesets_with_max_revisions(self): + id_, params = _build_data(self.apikey, 'get_changesets', + repoid=self.REPO, start_date="2011-02-24T00:00:00", max_revisions=10) + response = api_call(self, params) + result = json.loads(response.body)["result"] + assert len(result) == 10 + assert 'message' in result[0] + assert 'added' not in result[0] + def test_api_get_changesets_with_branch(self): if self.REPO == 'vcs_test_hg': branch = 'stable'