use javahg for log command

This commit is contained in:
Sebastian Sdorra
2012-07-01 20:43:05 +02:00
parent 152943d15b
commit 1df3815a6c
3 changed files with 231 additions and 85 deletions

View File

@@ -30,11 +30,14 @@
*/
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Function;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import sonia.scm.repository.Changeset;
@@ -42,6 +45,8 @@ import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.HgContext;
import sonia.scm.repository.HgPythonScript;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.Modifications;
import sonia.scm.repository.Person;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryException;
import sonia.scm.util.Util;
@@ -52,28 +57,26 @@ import sonia.scm.web.HgUtil;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
*
* @author Sebastian Sdorra
*/
public class HgLogCommand extends AbstractHgCommand implements LogCommand
public class HgLogCommand extends AbstractCommand implements LogCommand
{
/**
* Constructs ...
*
*
* @param handler
* @param context
* @param repository
* @param repositoryDirectory
*/
public HgLogCommand(HgRepositoryHandler handler, HgContext context,
Repository repository, File repositoryDirectory)
HgLogCommand(HgCommandContext context, Repository repository)
{
super(handler, context, repository, repositoryDirectory);
super(context, repository);
}
//~--- get methods ----------------------------------------------------------
@@ -93,16 +96,15 @@ public class HgLogCommand extends AbstractHgCommand implements LogCommand
public Changeset getChangeset(String id)
throws IOException, RepositoryException
{
Map<String, String> env = Maps.newHashMap();
Changeset changeset = null;
com.aragost.javahg.Changeset c = open().changeset(id);
env.put(ENV_REVISION, HgUtil.getRevision(id));
env.put(ENV_PATH, Util.EMPTY_STRING);
env.put(ENV_PAGE_START, "0");
env.put(ENV_PAGE_LIMIT, "1");
env.put(ENV_REVISION_START, Util.EMPTY_STRING);
env.put(ENV_REVISION_END, Util.EMPTY_STRING);
if (c != null)
{
changeset = convert(c);
}
return getResultFromScript(Changeset.class, HgPythonScript.LOG, env);
return changeset;
}
/**
@@ -120,17 +122,155 @@ public class HgLogCommand extends AbstractHgCommand implements LogCommand
public ChangesetPagingResult getChangesets(LogCommandRequest request)
throws IOException, RepositoryException
{
Map<String, String> env = Maps.newHashMap();
com.aragost.javahg.Repository repository = open();
com.aragost.javahg.commands.LogCommand cmd =
com.aragost.javahg.commands.LogCommand.on(repository);
env.put(ENV_REVISION, Util.EMPTY_STRING);
env.put(ENV_PATH, Strings.nullToEmpty(request.getPath()));
env.put(ENV_PAGE_START, String.valueOf(request.getPagingStart()));
env.put(ENV_PAGE_LIMIT, String.valueOf(request.getPagingLimit()));
env.put(ENV_REVISION_START,
Strings.nullToEmpty(request.getStartChangeset()));
env.put(ENV_REVISION_END, Strings.nullToEmpty(request.getEndChangeset()));
cmd.fileStatus();
return getResultFromScript(ChangesetPagingResult.class, HgPythonScript.LOG,
env);
String startChangeset = request.getStartChangeset();
String endChangeset = request.getEndChangeset();
if (!Strings.isNullOrEmpty(startChangeset)
&&!Strings.isNullOrEmpty(endChangeset))
{
cmd.rev(startChangeset.concat(":").concat(endChangeset));
}
else if (!Strings.isNullOrEmpty(endChangeset))
{
cmd.rev("tip:".concat(endChangeset));
}
else if (!Strings.isNullOrEmpty(startChangeset))
{
cmd.rev(startChangeset.concat(":0"));
}
List<com.aragost.javahg.Changeset> changesetList = null;
if (!Strings.isNullOrEmpty(request.getPath()))
{
changesetList = cmd.execute(request.getPath());
}
else
{
changesetList = cmd.execute();
}
int start = request.getPagingStart();
int limit = request.getPagingLimit();
List<Changeset> changesets = null;
if ((start == 0) && (limit < 0))
{
changesets = Lists.transform(changesetList, new ChangesetTransformer());
}
else
{
limit = limit + start;
if ((limit > changesetList.size()) || (limit < 0))
{
limit = changesetList.size();
}
List<com.aragost.javahg.Changeset> sublist = changesetList.subList(start,
limit);
changesets = Lists.transform(sublist, new ChangesetTransformer());
}
return new ChangesetPagingResult(changesetList.size(), changesets);
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param c
*
* @return
*/
private Changeset convert(com.aragost.javahg.Changeset c)
{
Changeset changeset = new Changeset();
changeset.setId(c.getNode());
changeset.setDate(c.getTimestamp().getDate().getTime());
changeset.setDescription(c.getMessage());
String user = c.getUser();
if (!Strings.isNullOrEmpty(user))
{
changeset.setAuthor(Person.toPerson(user));
}
String branch = c.getBranch();
if (!Strings.isNullOrEmpty(branch) &&!branch.equals("default"))
{
changeset.setBranches(Lists.newArrayList(branch));
}
List<String> tags = c.tags();
if (tags != null)
{
changeset.setTags(tags);
}
List<String> parents = Lists.newArrayList();
com.aragost.javahg.Changeset p1 = c.getParent1();
if (p1 != null)
{
parents.add(p1.getNode());
}
com.aragost.javahg.Changeset p2 = c.getParent1();
if (p2 != null)
{
parents.add(p2.getNode());
}
changeset.setParents(parents);
Modifications mods = changeset.getModifications();
mods.getAdded().addAll(c.getAddedFiles());
mods.getModified().addAll(c.getModifiedFiles());
mods.getRemoved().addAll(c.getDeletedFiles());
return changeset;
}
//~--- inner classes --------------------------------------------------------
/**
* Class description
*
*
* @version Enter version here..., 12/07/01
* @author Enter your name here...
*/
private class ChangesetTransformer
implements Function<com.aragost.javahg.Changeset, Changeset>
{
/**
* Method description
*
*
* @param c
*
* @return
*/
@Override
public Changeset apply(com.aragost.javahg.Changeset c)
{
return convert(c);
}
}
}

View File

@@ -82,7 +82,8 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
this.handler = handler;
this.repository = repository;
this.repositoryDirectory = handler.getDirectory(repository);
this.context = new HgCommandContext(handler.getConfig(), repositoryDirectory);
this.context = new HgCommandContext(handler.getConfig(),
repositoryDirectory);
}
//~--- methods --------------------------------------------------------------
@@ -159,8 +160,7 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
@Override
public HgLogCommand getLogCommand()
{
return new HgLogCommand(handler, hgContextProvider.get(), repository,
repositoryDirectory);
return new HgLogCommand(context, repository);
}
/**

View File

@@ -30,6 +30,7 @@
*/
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
@@ -67,10 +68,8 @@ public class HgLogCommandTest extends AbstractHgCommandTestBase
@Test
public void testGetAll() throws IOException, RepositoryException
{
ChangesetPagingResult result = new HgLogCommand(
handler, new HgContext(), repository,
repositoryDirectory).getChangesets(
new LogCommandRequest());
ChangesetPagingResult result =
createComamnd().getChangesets(new LogCommandRequest());
assertNotNull(result);
assertEquals(5, result.getTotal());
@@ -91,52 +90,19 @@ public class HgLogCommandTest extends AbstractHgCommandTestBase
request.setPath("a.txt");
ChangesetPagingResult result =
new HgLogCommand(handler, new HgContext(), repository,
repositoryDirectory).getChangesets(request);
ChangesetPagingResult result = createComamnd().getChangesets(request);
assertNotNull(result);
assertEquals(3, result.getTotal());
assertEquals(3, result.getChangesets().size());
assertEquals("4:2baab8e80280", result.getChangesets().get(0).getId());
assertEquals("2:79b6baf49711", result.getChangesets().get(1).getId());
assertEquals("0:a9bacaf1b7fa", result.getChangesets().get(2).getId());
assertEquals("2baab8e80280ef05a9aa76c49c76feca2872afb7",
result.getChangesets().get(0).getId());
assertEquals("79b6baf49711ae675568e0698d730b97ef13e84a",
result.getChangesets().get(1).getId());
assertEquals("a9bacaf1b7fa0cebfca71fed4e59ed69a6319427",
result.getChangesets().get(2).getId());
}
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testGetAllWithPaging() throws IOException, RepositoryException
{
LogCommandRequest request = new LogCommandRequest();
request.setPagingStart(1);
request.setPagingLimit(2);
ChangesetPagingResult result =
new HgLogCommand(handler, new HgContext(), repository,
repositoryDirectory).getChangesets(request);
assertNotNull(result);
assertEquals(5, result.getTotal());
assertEquals(2, result.getChangesets().size());
Changeset c1 = result.getChangesets().get(0);
assertNotNull(c1);
assertEquals("3:542bf4893dd2", c1.getId());
Changeset c2 = result.getChangesets().get(1);
assertNotNull(c2);
assertEquals("2:79b6baf49711", c2.getId());
}
/**
* Method description
*
@@ -151,9 +117,7 @@ public class HgLogCommandTest extends AbstractHgCommandTestBase
request.setPagingLimit(2);
ChangesetPagingResult result =
new HgLogCommand(handler, new HgContext(), repository,
repositoryDirectory).getChangesets(request);
ChangesetPagingResult result = createComamnd().getChangesets(request);
assertNotNull(result);
assertEquals(5, result.getTotal());
@@ -162,12 +126,44 @@ public class HgLogCommandTest extends AbstractHgCommandTestBase
Changeset c1 = result.getChangesets().get(0);
assertNotNull(c1);
assertEquals("4:2baab8e80280", c1.getId());
assertEquals("2baab8e80280ef05a9aa76c49c76feca2872afb7", c1.getId());
Changeset c2 = result.getChangesets().get(1);
assertNotNull(c2);
assertEquals("3:542bf4893dd2", c2.getId());
assertEquals("542bf4893dd2ff58a0eb719551d75ddeb919608b", c2.getId());
}
/**
* Method description
*
*
* @throws IOException
* @throws RepositoryException
*/
@Test
public void testGetAllWithPaging() throws IOException, RepositoryException
{
LogCommandRequest request = new LogCommandRequest();
request.setPagingStart(1);
request.setPagingLimit(2);
ChangesetPagingResult result = createComamnd().getChangesets(request);
assertNotNull(result);
assertEquals(5, result.getTotal());
assertEquals(2, result.getChangesets().size());
Changeset c1 = result.getChangesets().get(0);
assertNotNull(c1);
assertEquals("542bf4893dd2ff58a0eb719551d75ddeb919608b", c1.getId());
Changeset c2 = result.getChangesets().get(1);
assertNotNull(c2);
assertEquals("79b6baf49711ae675568e0698d730b97ef13e84a", c2.getId());
}
/**
@@ -180,12 +176,12 @@ public class HgLogCommandTest extends AbstractHgCommandTestBase
@Test
public void testGetCommit() throws IOException, RepositoryException
{
HgLogCommand command = new HgLogCommand(handler, new HgContext(),
repository, repositoryDirectory);
Changeset c = command.getChangeset("a9bacaf1b7fa");
HgLogCommand command = createComamnd();
Changeset c =
command.getChangeset("a9bacaf1b7fa0cebfca71fed4e59ed69a6319427");
assertNotNull(c);
assertEquals("0:a9bacaf1b7fa", c.getId());
assertEquals("a9bacaf1b7fa0cebfca71fed4e59ed69a6319427", c.getId());
assertEquals("added a and b files", c.getDescription());
checkDate(c.getDate());
assertEquals("Douglas Adams", c.getAuthor().getName());
@@ -217,9 +213,7 @@ public class HgLogCommandTest extends AbstractHgCommandTestBase
request.setStartChangeset("3049df33fdbb");
request.setEndChangeset("a9bacaf1b7fa");
ChangesetPagingResult result =
new HgLogCommand(handler, new HgContext(), repository,
repositoryDirectory).getChangesets(request);
ChangesetPagingResult result = createComamnd().getChangesets(request);
assertNotNull(result);
assertEquals(2, result.getTotal());
@@ -229,8 +223,20 @@ public class HgLogCommandTest extends AbstractHgCommandTestBase
Changeset c2 = result.getChangesets().get(1);
assertNotNull(c1);
assertEquals("1:3049df33fdbb", c1.getId());
assertEquals("3049df33fdbbded08b707bac3eccd0f7b453c58b", c1.getId());
assertNotNull(c2);
assertEquals("0:a9bacaf1b7fa", c2.getId());
assertEquals("a9bacaf1b7fa0cebfca71fed4e59ed69a6319427", c2.getId());
}
/**
* Method description
*
*
* @return
*/
private HgLogCommand createComamnd()
{
return new HgLogCommand(new HgCommandContext(handler.getConfig(),
repositoryDirectory), repository);
}
}