]> source.dussan.org Git - jgit.git/commitdiff
Added filter for merge and non-merges commits. 07/70507/4
authorAlcemir Santos <alcemir.santos@gmail.com>
Tue, 12 Apr 2016 18:11:32 +0000 (20:11 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Thu, 19 May 2016 23:05:40 +0000 (01:05 +0200)
Added the option to retrieve either merge or non-merge commits in the
LogCommand.

Change-Id: Ie0e1c515a823f2392783f1a47d385c31230e8167
Signed-off-by: Alcemir Santos <alcemir.santos@gmail.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/LogCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/LogCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/revwalk/filter/RevFilter.java

index 34432c588bcdd8a16473ce87571471793e05e75b..1310625a9b3a60bb47d05aeeacf159e226ff38fc 100644 (file)
@@ -53,7 +53,9 @@ import java.util.List;
 import org.eclipse.jgit.junit.RepositoryTestCase;
 import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.merge.MergeStrategy;
 import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.filter.RevFilter;
 import org.junit.Test;
 
 public class LogCommandTest extends RepositoryTestCase {
@@ -210,4 +212,81 @@ public class LogCommandTest extends RepositoryTestCase {
                assertEquals("commit#2", commit.getShortMessage());
                assertFalse(log.hasNext());
        }
+
+       @Test
+       public void logOnlyMergeCommits() throws Exception {
+               setCommitsAndMerge();
+               Git git = Git.wrap(db);
+
+               Iterable<RevCommit> commits = git.log().all().call();
+               Iterator<RevCommit> i = commits.iterator();
+               RevCommit commit = i.next();
+               assertEquals("merge s0 with m1", commit.getFullMessage());
+               commit = i.next();
+               assertEquals("s0", commit.getFullMessage());
+               commit = i.next();
+               assertEquals("m1", commit.getFullMessage());
+               commit = i.next();
+               assertEquals("m0", commit.getFullMessage());
+               assertFalse(i.hasNext());
+
+               commits = git.log().setRevFilter(RevFilter.ONLY_MERGES).call();
+               i = commits.iterator();
+               commit = i.next();
+               assertEquals("merge s0 with m1", commit.getFullMessage());
+               assertFalse(i.hasNext());
+       }
+
+       @Test
+       public void logNoMergeCommits() throws Exception {
+               setCommitsAndMerge();
+               Git git = Git.wrap(db);
+
+               Iterable<RevCommit> commits = git.log().all().call();
+               Iterator<RevCommit> i = commits.iterator();
+               RevCommit commit = i.next();
+               assertEquals("merge s0 with m1", commit.getFullMessage());
+               commit = i.next();
+               assertEquals("s0", commit.getFullMessage());
+               commit = i.next();
+               assertEquals("m1", commit.getFullMessage());
+               commit = i.next();
+               assertEquals("m0", commit.getFullMessage());
+               assertFalse(i.hasNext());
+
+               commits = git.log().setRevFilter(RevFilter.NO_MERGES).call();
+               i = commits.iterator();
+               commit = i.next();
+               assertEquals("m1", commit.getFullMessage());
+               commit = i.next();
+               assertEquals("s0", commit.getFullMessage());
+               commit = i.next();
+               assertEquals("m0", commit.getFullMessage());
+               assertFalse(i.hasNext());
+       }
+
+       private void setCommitsAndMerge() throws Exception {
+               Git git = Git.wrap(db);
+               writeTrashFile("file1", "1\n2\n3\n4\n");
+               git.add().addFilepattern("file1").call();
+               RevCommit masterCommit0 = git.commit().setMessage("m0").call();
+
+               createBranch(masterCommit0, "refs/heads/side");
+               checkoutBranch("refs/heads/side");
+
+               writeTrashFile("file2", "1\n2\n3\n4\n5\n6\n7\n8\n");
+               git.add().addFilepattern("file2").call();
+               RevCommit c = git.commit().setMessage("s0").call();
+
+               checkoutBranch("refs/heads/master");
+
+               writeTrashFile("file3", "1\n2\n");
+               git.add().addFilepattern("file3").call();
+               git.commit().setMessage("m1").call();
+
+               git.merge().include(c.getId())
+                               .setStrategy(MergeStrategy.RESOLVE)
+                               .setMessage("merge s0 with m1").call();
+       }
+
 }
\ No newline at end of file
index 9690f799c1aa6e1fbe8878577294ac4d3a3601ec..bb1a7381498dc6bd4ee406535594430dc8a3c8ec 100644 (file)
@@ -65,6 +65,7 @@ import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.revwalk.filter.AndRevFilter;
 import org.eclipse.jgit.revwalk.filter.MaxCountRevFilter;
+import org.eclipse.jgit.revwalk.filter.RevFilter;
 import org.eclipse.jgit.revwalk.filter.SkipRevFilter;
 import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
 import org.eclipse.jgit.treewalk.filter.PathFilter;
@@ -104,6 +105,8 @@ public class LogCommand extends GitCommand<Iterable<RevCommit>> {
 
        private boolean startSpecified = false;
 
+       private RevFilter revFilter;
+
        private final List<PathFilter> pathFilters = new ArrayList<PathFilter>();
 
        private int maxCount = -1;
@@ -156,6 +159,11 @@ public class LogCommand extends GitCommand<Iterable<RevCommit>> {
                                                e);
                        }
                }
+
+               if (this.revFilter != null) {
+                       walk.setRevFilter(this.revFilter);
+               }
+
                setCallable(false);
                return walk;
        }
@@ -342,4 +350,21 @@ public class LogCommand extends GitCommand<Iterable<RevCommit>> {
                                        , start), e);
                }
        }
+
+
+       /**
+        * Sets a filter for the <code>LogCommand</code>.
+        *
+        *
+        * @param aFilter
+        *            the filter that this instance of <code>LogCommand</code>
+        *            should use
+        * @return {@code this}
+        * @since 4.4
+        */
+       public LogCommand setRevFilter(RevFilter aFilter) {
+               checkCallable();
+               this.revFilter = aFilter;
+               return this;
+       }
 }
index 2a5cb2b2ac7ee81c5b7b6b00dc38dc9f9f955c8a..14a98a10bf0cfa66ab37dc8e483850c244177b3e 100644 (file)
@@ -82,6 +82,7 @@ import org.eclipse.jgit.revwalk.RevWalk;
  * <b>Merge filters:</b>
  * <ul>
  * <li>Skip all merges: {@link #NO_MERGES}.</li>
+ * <li>Skip all non-merges: {@link #ONLY_MERGES}</li>
  * </ul>
  *
  * <p>
@@ -143,6 +144,37 @@ public abstract class RevFilter {
                }
        }
 
+       /**
+        * Filter including only merge commits, excluding all commits with less than
+        * two parents (thread safe).
+        *
+        * @since 4.4
+        */
+       public static final RevFilter ONLY_MERGES = new OnlyMergesFilter();
+
+       private static final class OnlyMergesFilter extends RevFilter {
+
+               @Override
+               public boolean include(RevWalk walker, RevCommit c) {
+                       return c.getParentCount() >= 2;
+               }
+
+               @Override
+               public RevFilter clone() {
+                       return this;
+               }
+
+               @Override
+               public boolean requiresCommitBody() {
+                       return false;
+               }
+
+               @Override
+               public String toString() {
+                       return "ONLY_MERGES"; //$NON-NLS-1$
+               }
+       }
+
        /** Excludes commits with more than one parent (thread safe). */
        public static final RevFilter NO_MERGES = new NoMergesFilter();