1

Given this git command git log ``git describe --tags --abbrev=0``..HEAD --oneline

I'd love to have the equivalent in JGit. I found methods like git.log().addRange() but could not figure out what kind of values it expects or where I can make a call on git describe within the API. I tried git.describe() but the chaining methods did not made any sense for me in regards to the native git CLI API.

xetra11
  • 7,671
  • 14
  • 84
  • 159
  • I am quite fluent with the JGit API but not so much with CLI, can you describe what `git describe --tags --abbrev=0` exactly does? – Rüdiger Herrmann Sep 18 '17 at 19:13
  • It returns the git log/commit history *since* the last created tag. I do that to see all commits which have been made since the last tag – xetra11 Sep 18 '17 at 19:14

1 Answers1

1

I can't make much sense of the DescribeCommand output either. Thus I suggest to work around the DescribeCommand and iterate the history starting from HEAD backward like this:

Collection<Ref> allTags = git.getRepository().getRefDatabase().getRefs( "refs/tags/" ).values();
RevWalk revWalk = new RevWalk( git.getRepository() );
revWalk.markStart( revWalk.parseCommit( git.getRepository().resolve( "HEAD" ) ) );
RevCommit next = revWalk.next();
while( next != null ) {
  // ::pseudo code:: 
  // in real, iterate over allTags and compare each tag.getObjectId() with next
  if( allTags.contains( next ) ) { 
    // remember next and exit while loop
  }
  next = revWalk.next();
}
revWalk.close();

Note, that annotated tags need be unpeeled: List commits associated with a given tag with JGit

Once you have the tagged commit, you can feed the result to the LogCommand like this:

ObjectId headCommitId = git.getRepository().resolve( "HEAD" );
Iterable<RevCommit> commits = git.log().addRange( headCommitId, taggedCommit ).call();

This is a bit vague, but I hope it helps to get you started.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • It brought me nearer to my target. Even tho I could find the IDs of the commit with the last tag - resolving the `RevCommits` at the end is not working and a very big pain. I have no change to debug the `Iterable` since it's overloaded with properties that don't tell me anything about the result of `git.log().addRange(...).call()` – xetra11 Sep 18 '17 at 20:21
  • 1
    You can as well replace the `LogCommand` with a custom `RevWalk` similar to the above that is starting at HEAD (use `markStart()`) and loops until it encounters the tagged commit. Or let us know what exactly isn't working. I.e. what is expected and what is the actual outcome. BTW did you try to swap the `addRange()` arguments? I am always confused about their order. – Rüdiger Herrmann Sep 19 '17 at 05:21