Search code examples
gitjgitgit-describe

JGit equivalent for git commits since last tag command


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.


Solution

  • 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.