Recognize missing revisions in git

This commit is contained in:
René Pfeuffer
2018-08-31 12:05:39 +02:00
parent d4a8c05c25
commit 6fa2ba7f61
2 changed files with 33 additions and 99 deletions

View File

@@ -37,6 +37,7 @@ package sonia.scm.repository.spi;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
@@ -51,6 +52,7 @@ import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.GitChangesetConverter;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.util.IOUtil;
import java.io.IOException;
@@ -156,15 +158,11 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
* @return
*
* @throws IOException
* @throws RepositoryException
*/
@Override
@SuppressWarnings("unchecked")
public ChangesetPagingResult getChangesets(LogCommandRequest request)
throws IOException
{
if (logger.isDebugEnabled())
{
public ChangesetPagingResult getChangesets(LogCommandRequest request) throws RevisionNotFoundException {
if (logger.isDebugEnabled()) {
logger.debug("fetch changesets for request: {}", request);
}
@@ -172,17 +170,13 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
GitChangesetConverter converter = null;
RevWalk revWalk = null;
try (org.eclipse.jgit.lib.Repository gr = open())
{
if (!gr.getAllRefs().isEmpty())
{
try (org.eclipse.jgit.lib.Repository gr = open()) {
if (!gr.getAllRefs().isEmpty()) {
int counter = 0;
int start = request.getPagingStart();
if (start < 0)
{
if (logger.isErrorEnabled())
{
if (start < 0) {
if (logger.isErrorEnabled()) {
logger.error("start parameter is negative, reset to 0");
}
@@ -193,15 +187,13 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
int limit = request.getPagingLimit();
ObjectId startId = null;
if (!Strings.isNullOrEmpty(request.getStartChangeset()))
{
if (!Strings.isNullOrEmpty(request.getStartChangeset())) {
startId = gr.resolve(request.getStartChangeset());
}
ObjectId endId = null;
if (!Strings.isNullOrEmpty(request.getEndChangeset()))
{
if (!Strings.isNullOrEmpty(request.getEndChangeset())) {
endId = gr.resolve(request.getEndChangeset());
}
@@ -209,57 +201,51 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
converter = new GitChangesetConverter(gr, revWalk);
if (!Strings.isNullOrEmpty(request.getPath()))
{
if (!Strings.isNullOrEmpty(request.getPath())) {
revWalk.setTreeFilter(
AndTreeFilter.create(
PathFilter.create(request.getPath()), TreeFilter.ANY_DIFF));
}
ObjectId head = getBranchOrDefault(gr, request.getBranch());
if (head != null)
{
if (startId != null)
{
if (head != null) {
if (startId != null) {
revWalk.markStart(revWalk.lookupCommit(startId));
}
else
{
} else {
revWalk.markStart(revWalk.lookupCommit(head));
}
Iterator<RevCommit> iterator = revWalk.iterator();
while (iterator.hasNext())
{
while (iterator.hasNext()) {
RevCommit commit = iterator.next();
if ((counter >= start)
&& ((limit < 0) || (counter < start + limit)))
{
&& ((limit < 0) || (counter < start + limit))) {
changesetList.add(converter.createChangeset(commit));
}
counter++;
if ((endId != null) && commit.getId().equals(endId))
{
if ((endId != null) && commit.getId().equals(endId)) {
break;
}
}
}
changesets = new ChangesetPagingResult(counter, changesetList);
}
else if (logger.isWarnEnabled())
{
} else if (logger.isWarnEnabled()) {
logger.warn("the repository {} seems to be empty",
repository.getName());
changesets = new ChangesetPagingResult(0, Collections.EMPTY_LIST);
}
}
catch (MissingObjectException e)
{
throw new RevisionNotFoundException(e.getObjectId().name());
}
catch (Exception ex)
{
throw new InternalRepositoryException("could not create change log", ex);

View File

@@ -35,15 +35,12 @@ package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import org.eclipse.jgit.api.errors.GitAPIException;
import org.junit.Test;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.GitConstants;
import sonia.scm.repository.Modifications;
import java.io.IOException;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -55,7 +52,7 @@ import static org.junit.Assert.assertTrue;
/**
* Unit tests for {@link GitLogCommand}.
*
*
* @author Sebastian Sdorra
*/
public class GitLogCommandTest extends AbstractGitCommandTestBase
@@ -63,13 +60,9 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
/**
* Tests log command with the usage of a default branch.
*
* @throws IOException
* @throws GitAPIException
* @
*/
@Test
public void testGetDefaultBranch() throws IOException, GitAPIException {
public void testGetDefaultBranch() throws Exception {
// without default branch, the repository head should be used
ChangesetPagingResult result = createCommand().getChangesets(new LogCommandRequest());
@@ -79,10 +72,10 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals("86a6645eceefe8b9a247db5eb16e3d89a7e6e6d1", result.getChangesets().get(1).getId());
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(2).getId());
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(3).getId());
// set default branch and fetch again
repository.setProperty(GitConstants.PROPERTY_DEFAULT_BRANCH, "test-branch");
result = createCommand().getChangesets(new LogCommandRequest());
assertNotNull(result);
@@ -91,16 +84,9 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", result.getChangesets().get(1).getId());
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(2).getId());
}
/**
* Method description
*
*
* @throws IOException
* @throws RepositoryException
*/
@Test
public void testGetAll() throws IOException
public void testGetAll() throws Exception
{
ChangesetPagingResult result =
createCommand().getChangesets(new LogCommandRequest());
@@ -110,15 +96,8 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals(4, result.getChangesets().size());
}
/**
* Method description
*
*
* @throws IOException
* @throws RepositoryException
*/
@Test
public void testGetAllByPath() throws IOException
public void testGetAllByPath() throws Exception
{
LogCommandRequest request = new LogCommandRequest();
@@ -133,15 +112,8 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", result.getChangesets().get(1).getId());
}
/**
* Method description
*
*
* @throws IOException
* @throws RepositoryException
*/
@Test
public void testGetAllWithLimit() throws IOException
public void testGetAllWithLimit() throws Exception
{
LogCommandRequest request = new LogCommandRequest();
@@ -164,15 +136,8 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals("86a6645eceefe8b9a247db5eb16e3d89a7e6e6d1", c2.getId());
}
/**
* Method description
*
*
* @throws IOException
* @throws RepositoryException
*/
@Test
public void testGetAllWithPaging() throws IOException
public void testGetAllWithPaging() throws Exception
{
LogCommandRequest request = new LogCommandRequest();
@@ -196,10 +161,6 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals("592d797cd36432e591416e8b2b98154f4f163411", c2.getId());
}
/**
* Method description
*
*/
@Test
public void testGetCommit()
{
@@ -224,15 +185,8 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertThat(mods.getAdded(), contains("a.txt", "b.txt"));
}
/**
* Method description
*
*
* @throws IOException
* @throws RepositoryException
*/
@Test
public void testGetRange() throws IOException
public void testGetRange() throws Exception
{
LogCommandRequest request = new LogCommandRequest();
@@ -254,12 +208,6 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
assertEquals("435df2f061add3589cb326cc64be9b9c3897ceca", c2.getId());
}
/**
* Method description
*
*
* @return
*/
private GitLogCommand createCommand()
{
return new GitLogCommand(createContext(), repository);