Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » How to use Git diff options
How to use Git diff options [message #1791169] Mon, 25 June 2018 12:43 Go to next message
Muhammad Umar Ashraf is currently offline Muhammad Umar AshrafFriend
Messages: 1
Registered: June 2018
Junior Member
I'm using JGit Api's to get diff between commits, so how can I make use of Git diff options with JGit Api's e.g. like git diff --unified=n?

Here's the code snippet:


File gitWorkDir = new File("C:/temp/code/");
Git git = Git.open(gitWorkDir);

String oldHash = "d7db296cc2730ca562f91cfa539d6955a21284b6";

ObjectId headId = git.getRepository().resolve("HEAD^{tree}");
ObjectId oldId = git.getRepository().resolve(oldHash + "^{tree}");

ObjectReader reader = git.getRepository().newObjectReader();

CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldId);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, headId);

List<DiffEntry> diffs= git.diff()
.setNewTree(newTreeIter)
.setOldTree(oldTreeIter)
.call();

ByteArrayOutputStream out = new ByteArrayOutputStream();
DiffFormatter df = new DiffFormatter(out);
df.setRepository(git.getRepository());

for(DiffEntry diff : diffs)
{
df.format(diff);
diff.getOldId();
String diffText = out.toString("UTF-8");
System.out.println(diffText);
out.reset();
}



Thanks
Re: How to use Git diff options [message #1791597 is a reply to message #1791169] Mon, 02 July 2018 16:55 Go to previous message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 80
Registered: August 2016
Member
DiffFormatter.setContext() is your friend.
Previous Topic:ssh connection using an ECDSA key fails on second attempt (auth failed)
Next Topic:Help: My Push was rejected
Goto Forum:
  


Current Time: Sat Sep 22 22:51:18 GMT 2018

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

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

Back to the top