Implement request limit for hg

This commit is contained in:
Rene Pfeuffer
2020-02-18 10:47:20 +01:00
parent 67e58209cf
commit 9e7fd52f9d
4 changed files with 95 additions and 23 deletions

View File

@@ -101,6 +101,9 @@ public class HgBrowseCommand extends AbstractCommand implements BrowseCommand
cmd.disableSubRepositoryDetection();
}
cmd.setLimit(request.getLimit());
cmd.setProceedFrom(request.getProceedFrom());
FileObject file = cmd.execute();
return new BrowserResult(c == null? "tip": c.getNode(), revision, file);
}

View File

@@ -141,6 +141,35 @@ public class HgFileviewCommand extends AbstractCommand
return this;
}
/**
* Limit the number of result files to <code>limit</code> entries.
*
* @param limit The maximal number of files this request shall return.
*
* @return {@code this}
* @since 2.0.0
*/
public HgFileviewCommand setLimit(int limit) {
cmdAppend("-l", limit);
return this;
}
/**
* Proceed the list from the given number on (zero based).
*
* @param proceedFrom The number of the entry, the result should start with (zero based).
* All preceding entries will be omitted.
*
* @return {@code this}
* @since 2.0.0
*/
public HgFileviewCommand setProceedFrom(int proceedFrom) {
cmdAppend("-f", proceedFrom);
return this;
}
/**
* Executes the mercurial command and parses the output.
*
@@ -294,5 +323,4 @@ public class HgFileviewCommand extends AbstractCommand
{
return HgFileviewExtension.NAME;
}
}

View File

@@ -197,37 +197,43 @@ def collect_sub_repositories(revCtx):
class File_Printer:
def __init__(self, ui, repo, revCtx, disableLastCommit, transport):
def __init__(self, ui, repo, revCtx, disableLastCommit, transport, limit, proceedFrom):
self.ui = ui
self.repo = repo
self.revCtx = revCtx
self.disableLastCommit = disableLastCommit
self.transport = transport
self.result_count = -1
self.limit = limit
self.proceedFrom = proceedFrom
def print_directory(self, path):
format = '%s/\n'
if self.transport:
format = 'd%s/\0'
self.ui.write( format % path)
if self.shouldPrintResult():
format = '%s/\n'
if self.transport:
format = 'd%s/\0'
self.ui.write( format % path)
def print_file(self, path):
file = self.revCtx[path]
date = '0 0'
description = 'n/a'
if not self.disableLastCommit:
linkrev = self.repo[file.linkrev()]
date = '%d %d' % _parsedate(linkrev.date())
description = linkrev.description()
format = '%s %i %s %s\n'
if self.transport:
format = 'f%s\n%i %s %s\0'
self.ui.write( format % (file.path(), file.size(), date, description) )
if self.shouldPrintResult():
file = self.revCtx[path]
date = '0 0'
description = 'n/a'
if not self.disableLastCommit:
linkrev = self.repo[file.linkrev()]
date = '%d %d' % _parsedate(linkrev.date())
description = linkrev.description()
format = '%s %i %s %s\n'
if self.transport:
format = 'f%s\n%i %s %s\0'
self.ui.write( format % (file.path(), file.size(), date, description) )
def print_sub_repository(self, path, subrepo):
format = '%s/ %s %s\n'
if self.transport:
format = 's%s/\n%s %s\0'
self.ui.write( format % (path, subrepo.revision, subrepo.url))
if self.shouldPrintResult():
format = '%s/ %s %s\n'
if self.transport:
format = 's%s/\n%s %s\0'
self.ui.write( format % (path, subrepo.revision, subrepo.url))
def visit(self, file):
if file.sub_repository:
@@ -237,6 +243,13 @@ class File_Printer:
else:
self.print_file(file.path)
def shouldPrintResult(self):
# The first result is the selected path (or root if not specified). This
# always has to be printed. Therefore we start counting with -1.
self.result_count += 1
return self.result_count == 0 or self.proceedFrom < self.result_count <= self.limit + self.proceedFrom
class File_Viewer:
def __init__(self, revCtx, visitor):
self.revCtx = revCtx
@@ -271,13 +284,15 @@ class File_Viewer:
('d', 'disableLastCommit', False, 'disables last commit description and date'),
('s', 'disableSubRepositoryDetection', False, 'disables detection of sub repositories'),
('t', 'transport', False, 'format the output for command server'),
('l', 'limit', 1000, 'limit the number of results'),
('f', 'proceedFrom', 0, 'proceed from the given result number (zero based)'),
])
def fileview(ui, repo, **opts):
revCtx = scmutil.revsingle(repo, opts["revision"])
subrepos = {}
if not opts["disableSubRepositoryDetection"]:
subrepos = collect_sub_repositories(revCtx)
printer = File_Printer(ui, repo, revCtx, opts["disableLastCommit"], opts["transport"])
printer = File_Printer(ui, repo, revCtx, opts["disableLastCommit"], opts["transport"], opts["limit"], opts["proceedFrom"])
viewer = File_Viewer(revCtx, printer)
viewer.recursive = opts["recursive"]
viewer.sub_repositories = subrepos

View File

@@ -45,7 +45,6 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
@@ -181,6 +180,33 @@ public class HgBrowseCommandTest extends AbstractHgCommandTestBase {
assertEquals(2, c.getChildren().size());
}
@Test
public void testLimit() throws IOException {
BrowseCommandRequest request = new BrowseCommandRequest();
request.setLimit(2);
BrowserResult result = new HgBrowseCommand(cmdContext, repository).getBrowserResult(request);
FileObject root = result.getFile();
Collection<FileObject> foList = root.getChildren();
assertThat(foList).extracting("name").containsExactlyInAnyOrder("a.txt", "b.txt");
}
@Test
public void testProceedFrom() throws IOException {
BrowseCommandRequest request = new BrowseCommandRequest();
request.setLimit(2);
request.setProceedFrom(2);
BrowserResult result = new HgBrowseCommand(cmdContext, repository).getBrowserResult(request);
FileObject root = result.getFile();
Collection<FileObject> foList = root.getChildren();
assertThat(foList).extracting("name").containsExactlyInAnyOrder("c", "f.txt");
}
//~--- get methods ----------------------------------------------------------
/**