Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » How to define a "In between" JGit RevFilter?
How to define a "In between" JGit RevFilter? [message #1729947] Wed, 20 April 2016 12:41 Go to next message
Alcemir Santos is currently offline Alcemir SantosFriend
Messages: 23
Registered: July 2012
Junior Member

I am trying to implement a RevFilter to be used through RevWalk#setRevFilter(). In the following what I have tried:

private class InBetweenRevFilter extends RevFilter {

    private AnyObjectId end;
    private AnyObjectId begin;

    public InBetweenRevFilter(AnyObjectId begin, AnyObjectId end) {
        this.begin = begin;
        this.end = end;
    }

    @Override
    public RevFilter clone() {
        return this;
    }

    @Override
    public boolean include(RevWalk walker, RevCommit c)
            throws StopWalkException, MissingObjectException,
            IncorrectObjectTypeException, IOException {
        RevCommit from = walker.parseCommit(begin);
        RevCommit to = walker.parseCommit(end);
        return (walker.isMergedInto(from, c) && walker.isMergedInto(c, to));
    }
}


The result of the filter should be the commit pushed after the begin and before the end. The problem is that when I set this filter, only the commit marked as the start point with RevWalk.markStart(RevCommit c) is returned by the RevWalk.next(). In the following, I show show how I tried to use the filter:

    RevWalk walk = new RevWalk(getRepository());
    RevCommit beginCommit = walk.parseCommit(getRepository().resolve(start));
    RevCommit endCommit = walk.parseCommit(getRepository().resolve(end));
    walk.setRevFilter(new InBetweenRevFilter(beginCommit.getId(), endCommit.getId()));
    walk.markStart(endCommit);
    for (RevCommit rev : walk) {
        System.out.println(rev.getFullMessage());
    }
    walk.close();


In this example, only the message from endCommit printed in the console.
Right now I am using a recursion to walk in the tree as temporary solution. But I would like to have a more sophisticated one. Any help would be appreciated.

Is there any other way to do this? Or, what am I missing or did wrong?
Re: How to define a "In between" JGit RevFilter? [message #1730093 is a reply to message #1729947] Thu, 21 April 2016 10:58 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 263
Registered: July 2009
Senior Member
Do you want that the commits begin and end to be included in result? One clarification: You say that the result should be "the commit..." but in fact this could a lot of commits which are returned.

I assume for now that you want the semantic of "git log <end>..<begin>".
Imagine you have the following graph:
1--2--3--4
    \
     \
      a--b      

Then the results would be
- begin:4, end:1 -> result=4,3,2
- begin=4, end=b -> result=4,3
- begin=b, end=4 -> result=b,a
- begin=1, end=4 -> empty

This could be implemented without own filters. On the Revwalk use markStart(begin) and markUninteresting(end).

like in
public class RevWalkTests {
	public static void main(String args[]) throws IOException, GitAPIException {
		File tmpDir = new File(System.getProperty("java.io.tmpdir"),
				"JGitTest_DetermineWetherOneCommitIsMergedIntoAnother_" + System.currentTimeMillis());
		tmpDir.mkdirs();
		System.out.println("Working dir: " + tmpDir);

		Git git = Git.init().setDirectory(tmpDir).call();

		RevCommit c1 = git.commit().setMessage("1").call();
		RevCommit c2 = git.commit().setMessage("2").call();
		RevCommit c3 = git.commit().setMessage("3").call();
		RevCommit c4 = git.commit().setMessage("4").call();

		Ref side = git.branchCreate().setName("side").setStartPoint(c2).call();
		git.checkout().setName(side.getName()).call();

		RevCommit ca = git.commit().setMessage("a").call();
		RevCommit cb = git.commit().setMessage("b").call();

		try (RevWalk rw = new RevWalk(git.getRepository())) {
			rw.markStart(rw.lookupCommit(c2));
			rw.markUninteresting(rw.lookupCommit(c4));
			for (RevCommit curr; (curr = rw.next()) != null;)
				System.out.println("Inspecting entry: " + curr.getShortMessage());
		}
	}
}







Ciao
Chris
Re: How to define a "In between" JGit RevFilter? [message #1730240 is a reply to message #1730093] Fri, 22 April 2016 13:36 Go to previous message
Alcemir Santos is currently offline Alcemir SantosFriend
Messages: 23
Registered: July 2012
Junior Member

Yeah. You're right about the result.
Thanks! I will tried that.
Previous Topic:Team Project Set to add local repository
Next Topic:How to JGit blame a file before commit?
Goto Forum:
  


Current Time: Mon May 27 05:17:42 GMT 2019

Powered by FUDForum. Page generated in 0.01305 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top