Getting an old commit using JGit

I am experimenting with JGit for a project, and although it works mostly, getting the oldest (first) commit does not. Here is the code:

RevWalk rw = new RevWalk(new Repository( new File("/path/to/git"))); RevCommit oldest; Iterator<RevCommit> i = rw.iterator(); if (i.hasNext()) oldest = i.next(); Commit c = oldest.asCommit(rw); //oldest is null here, NPE 

Does anyone know what I'm doing wrong?

+6
java git
source share
1 answer

I think I found it. You must undo the commit log and set the start point in order to start viewing it. The following passage does what I was looking for, but somehow I doubt how optimal it is.

  RevWalk rw = new RevWalk(new Repository( new File("/path/to/git"))); RevCommit c = null; AnyObjectId headId; try { headId = git.resolve(Constants.HEAD); RevCommit root = rw.parseCommit(headId); rw.sort(RevSort.REVERSE); rw.markStart(root); c = rw.next(); } catch (IOException e) { e.printStackTrace(); } 
+9
source share

All Articles