Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show changes/diffs without a parent commit (first commit) with JGit

Tags:

jgit

when I do a git show commit of the first commit in a repository, I see all the files along with the diffs of the files (i.e all the lines being added)

$ git show cb5d132
commit cb5d13286cf9d14782f0e10445456dfe41072f55
Author: tw2 tw2LastName <tw2>
Date:   Thu Oct 23 05:15:09 2014 -0400

   Initial Commit

diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..96d156e
--- /dev/null
+++ b/README.txt
@@ -0,0 +1 @@
+First Line in README file!
\ No newline at end of file

The jgit show doesn't seem to get similar info, How can I get similar output using JGit API? I could use DiffFormatter, but it seems to require the baseTree and commitTree, wondering how I can get the DiffFormatter to work on the first commit in the repository.

ByteArrayOutputStream os = new ByteArrayOutputStream();
DiffFormatter df = new DiffFormatter( os )
RawTextComparator cmp = RawTextComparator.DEFAULT;

df.setRepository(repository);
df.setDiffComparator(cmp);
df.setDetectRenames(true);

// wondering how to use the API if we do not have baseCommit.
List<DiffEntry> diffEntries = df.scan(??baseCommitTree??, firstCommit.getTree());
for (DiffEntry diffEntry : diffEntries) {                
   df.format(diffEntry);
}
System.out.println(df.toString());
like image 293
Vineel Nalla Avatar asked Oct 19 '25 15:10

Vineel Nalla


1 Answers

Use an o.e.jgit.treewalk.EmptyTreeIterator to diff a commit without a parent:

AbstractTreeIterator oldTreeIter = new EmptyTreeIterator();
ObjectReader reader = repository.newObjectReader();
AbstractTreeIterator newTreeIter = new CanonicalTreeParser(null, reader, firstCommit.getTree());
List<DiffEntry> diffEntries = df.scan(oldTreeIter, newTreeIter);
...
like image 112
Rüdiger Herrmann Avatar answered Oct 22 '25 06:10

Rüdiger Herrmann