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

View File

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