changeset 7145:32e6957d0aac

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.
author domruf <dominikruf@gmail.com>
date Thu, 23 Nov 2017 19:34:49 +0100
parents bfb1ae42bcbb
children cb187a32c8e3
files kallithea/controllers/api/api.py kallithea/lib/vcs/backends/base.py kallithea/lib/vcs/backends/git/repository.py kallithea/lib/vcs/backends/hg/repository.py kallithea/tests/api/api_base.py
diffstat 5 files changed, 23 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/api/api.py	Sat Jan 20 02:24:38 2018 +0100
+++ b/kallithea/controllers/api/api.py	Thu Nov 23 19:34:49 2017 +0100
@@ -2498,7 +2498,7 @@
 
     # 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 @@
                                                  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)
 
--- a/kallithea/lib/vcs/backends/base.py	Sat Jan 20 02:24:38 2018 +0100
+++ b/kallithea/lib/vcs/backends/base.py	Thu Nov 23 19:34:49 2017 +0100
@@ -163,7 +163,7 @@
             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.
--- a/kallithea/lib/vcs/backends/git/repository.py	Sat Jan 20 02:24:38 2018 +0100
+++ b/kallithea/lib/vcs/backends/git/repository.py	Thu Nov 23 19:34:49 2017 +0100
@@ -504,7 +504,7 @@
         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 @@
         # %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:
--- a/kallithea/lib/vcs/backends/hg/repository.py	Sat Jan 20 02:24:38 2018 +0100
+++ b/kallithea/lib/vcs/backends/hg/repository.py	Thu Nov 23 19:34:49 2017 +0100
@@ -508,7 +508,7 @@
         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 @@
         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
--- a/kallithea/tests/api/api_base.py	Sat Jan 20 02:24:38 2018 +0100
+++ b/kallithea/tests/api/api_base.py	Thu Nov 23 19:34:49 2017 +0100
@@ -2503,6 +2503,15 @@
         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'