Skip to main content



      Home
Home » Eclipse Projects » EGit / JGit » Jgit diff and triple dot(Listing remote changes using jgit)
Jgit diff and triple dot [message #1692900] Mon, 20 April 2015 09:43 Go to next message
Eclipse UserFriend
This question is just a matter of translation, I know the solution in pure git but don't know how to do the same using jgit. How do I call a command like the one below?

git diff --name-status HEAD...origin/master


From my point of view:, the branches are taken this way:
      ObjectId head = repo.resolve("HEAD^{tree}");

      RevCommit commit = CommitUtils.getCommit(repo, "refs/remotes/origin/master");     
      ObjectId remoteMaster = commit.getTree().getId();


and then

      ObjectReader reader = repo.newObjectReader();
      CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
      oldTreeIter.reset(reader, head);
      CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
      newTreeIter.reset(reader, remoteMaster);
      List<DiffEntry> diffs = git.diff().setShowNameAndStatusOnly(true).setNewTree(newTreeIter)
.setOldTree(oldTreeIter).call();


Just the triple dot that I didn't find the "translation"
Re: Jgit diff and triple dot [message #1692906 is a reply to message #1692900] Mon, 20 April 2015 10:03 Go to previous messageGo to next message
Eclipse UserFriend
I don't know what the command "git diff HEAD...origin/master" should do. I know the triple dot notation from "git log A...B" where it means: print those commits reachable either from A or from B but do not list those commits reachable from A and B. The result is list of commits. But what should a "git diff --name-status <potentially-long-list-of-commits>" mean? Can you explain what you want to achieve in the end?
Re: Jgit diff and triple dot [message #1692916 is a reply to message #1692906] Mon, 20 April 2015 11:02 Go to previous messageGo to next message
Eclipse UserFriend
I want to list the changes that occurred remotely, but with a simple diff between head and fetch_head it shows wrong results. If I add a file in local it shows as if I removed it from remote. The same logic if I remove a file in local. I want to ignore these "reverse" results

[Updated on: Mon, 20 April 2015 11:02] by Moderator

Re: Jgit diff and triple dot [message #1692998 is a reply to message #1692916] Tue, 21 April 2015 03:29 Go to previous messageGo to next message
Eclipse UserFriend
Ahh, thanks, I learned something new. I didn't know about the triple dot notation for "git diff". I was reading only the synopsis part of the "git-diff" man page where this variant is not listed. But the man page [1] gives a hint how to do this with JGit. It says that "git diff A..B" is equivalent to "git diff $(git-merge-base A B) B": first determine the merge base between A and B and then diff that to B. Determining the merge-base is a task which can be done with JGits RevWalk. JGit even has a command-line command in MergeBase.java [2] where you could look up how to do it. This snippet (which has no error handling at all - don't copy it to productive code) works for me:

		try (Git git = Git.open(new File("/home/chris/git/dondalfi"));
				ObjectReader reader = git.getRepository().newObjectReader();
				RevWalk rw = new RevWalk(git.getRepository())) {
			Repository repo = git.getRepository();
			RevCommit commitA = rw.parseCommit(repo
					.resolve("refs/remotes/origin/Multiplyer"));
			RevCommit commitB = rw
					.parseCommit(repo.resolve("refs/remotes/origin/Adder"));
			rw.markStart(commitA);
			rw.markStart(commitB);
			rw.setRevFilter(RevFilter.MERGE_BASE);
			RevCommit base = rw.parseCommit(rw.next());  // Be carefull, commits may have multiple merge bases where diff A...B is complicated

			CanonicalTreeParser aParser = new CanonicalTreeParser();
			aParser.reset(reader, base.getTree());
			CanonicalTreeParser bParser = new CanonicalTreeParser();
			bParser.reset(reader, commitB.getTree());
			for (DiffEntry diff : git.diff().setOldTree(aParser)
					.setNewTree(bParser).call()) {
				System.out.println(diff.toString());
			}
		}


[1] http://git-scm.com/docs/git-diff#_description
[2] https://github.com/eclipse/jgit/blob/master/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/MergeBase.java
Re: Jgit diff and triple dot [message #1693217 is a reply to message #1692998] Wed, 22 April 2015 08:58 Go to previous message
Eclipse UserFriend
Thank you Christian, it works like a breeze!
Previous Topic:Remote Tracking - new branches
Next Topic:Commiting project
Goto Forum:
  


Current Time: Sat Nov 08 06:14:08 EST 2025

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

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

Back to the top