List commits associated with a given tag with JGit

前端 未结 2 984
无人及你
无人及你 2021-01-19 02:33

I need to create a history file that details all tags and for each tag, all its commits.

I\'ve tried to call getTags() on the repository object and use

2条回答
  •  难免孤独
    2021-01-19 03:03

    List all Tags:

    List call = new Git(repository).tagList().call();
    for (Ref ref : call) {
        System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
    }
    

    List commits based on tag:

    I'd use the log-command based on the tag-name with the peeled-magic as noted by Rüdiger:

            LogCommand log = new Git(repository).log();
    
            Ref peeledRef = repository.peel(ref);
            if(peeledRef.getPeeledObjectId() != null) {
                log.add(peeledRef.getPeeledObjectId());
            } else {
                log.add(ref.getObjectId());
            }
    
            Iterable logs = log.call();
            for (RevCommit rev : logs) {
                System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
            }
    

    See also my jgit-cookbook for some related examples.

提交回复
热议问题