Commits

Sebastian Sdorra  committed af420fe

fix get range from git log command

  • Participants
  • Parent commits 8e5b100

Comments (0)

Files changed (2)

File scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitLogCommand.java

 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.api.errors.NoHeadException;
+import org.eclipse.jgit.errors.IncorrectObjectTypeException;
+import org.eclipse.jgit.errors.MissingObjectException;
+import org.eclipse.jgit.errors.StopWalkException;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.revwalk.filter.RevFilter;
+import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
+import org.eclipse.jgit.treewalk.filter.PathFilter;
+import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
+import org.eclipse.jgit.treewalk.filter.TreeFilter;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import java.io.IOException;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 
 /**
 
       if (!gr.getAllRefs().isEmpty())
       {
-        converter = new GitChangesetConverter(gr, GitUtil.ID_LENGTH);
-
         int counter = 0;
         int start = request.getPagingStart();
 
 
         List<Changeset> changesetList = Lists.newArrayList();
         int limit = request.getPagingLimit();
-        boolean started = false;
         ObjectId startId = null;
 
         if (!Strings.isNullOrEmpty(request.getStartChangeset()))
           endId = gr.resolve(request.getEndChangeset());
         }
 
-        org.eclipse.jgit.api.LogCommand cmd = new Git(gr).log().all();
+        RevWalk revWalk = new RevWalk(gr);
+
+        converter = new GitChangesetConverter(gr, revWalk, GitUtil.ID_LENGTH);
 
         if (!Strings.isNullOrEmpty(request.getPath()))
         {
-          cmd = cmd.addPath(request.getPath());
+          revWalk.setTreeFilter(
+              AndTreeFilter.create(
+                PathFilter.create(request.getPath()), TreeFilter.ANY_DIFF));
         }
 
-        for (RevCommit commit : cmd.call())
-        { 
-          if (!started && ((startId == null) || commit.getId().equals(startId)))
+        ObjectId head = GitUtil.getRepositoryHead(gr);
+
+        if (head != null)
+        {
+          if (startId != null)
           {
-            started = true;
+            revWalk.markStart(revWalk.lookupCommit(startId));
+          }
+          else
+          {
+            revWalk.markStart(revWalk.lookupCommit(head));
           }
 
-          if (started)
+          Iterator<RevCommit> iterator = revWalk.iterator();
+
+          while (iterator.hasNext())
           {
+            RevCommit commit = iterator.next();
+
             if ((counter >= start)
                 && ((limit < 0) || (counter < start + limit)))
             {
                     repository.getName());
       }
     }
-    catch (NoHeadException ex)
-    {
-      if (logger.isTraceEnabled())
-      {
-        logger.trace("repository seems to be empty", ex);
-      }
-      else if (logger.isWarnEnabled())
-      {
-        logger.warn("repository seems to be empty");
-      }
-
-      changesets = new ChangesetPagingResult(0, new ArrayList<Changeset>());
-    }
-    catch (GitAPIException ex)
+    catch (Exception ex)
     {
       throw new RepositoryException("could not create change log", ex);
     }

File scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitLogCommandTest.java

       createCommand().getChangesets(new LogCommandRequest());
 
     assertNotNull(result);
-    assertEquals(5, result.getTotal());
-    assertEquals(5, result.getChangesets().size());
+    assertEquals(4, result.getTotal());
+    assertEquals(4, result.getChangesets().size());
   }
 
   /**
     ChangesetPagingResult result = createCommand().getChangesets(request);
 
     assertNotNull(result);
-    assertEquals(3, result.getTotal());
-    assertEquals(3, result.getChangesets().size());
+    assertEquals(2, result.getTotal());
+    assertEquals(2, result.getChangesets().size());
     assertEquals("fcd0ef1831e4002ac43e", result.getChangesets().get(0).getId());
-    assertEquals("3f76a12f08a6ba0dc988", result.getChangesets().get(1).getId());
-    assertEquals("435df2f061add3589cb3", result.getChangesets().get(2).getId());
+    assertEquals("435df2f061add3589cb3", result.getChangesets().get(1).getId());
   }
 
   /**
     ChangesetPagingResult result = createCommand().getChangesets(request);
 
     assertNotNull(result);
-    assertEquals(5, result.getTotal());
+    assertEquals(4, result.getTotal());
     assertEquals(2, result.getChangesets().size());
 
     Changeset c1 = result.getChangesets().get(0);
     ChangesetPagingResult result = createCommand().getChangesets(request);
 
     assertNotNull(result);
-    assertEquals(5, result.getTotal());
+    assertEquals(4, result.getTotal());
     assertEquals(2, result.getChangesets().size());
 
     Changeset c1 = result.getChangesets().get(0);
     Changeset c2 = result.getChangesets().get(1);
 
     assertNotNull(c2);
-    assertEquals("3f76a12f08a6ba0dc988", c2.getId());
+    assertEquals("592d797cd36432e59141", c2.getId());
   }
 
   /**