List of commits associated with this tag with JGit

I need to create a history file that describes all the tags and for each tag, all its fixations.

I tried calling getTags() on the repository object and using these object identifiers, but they are not commit identifiers.

I also tried using getAllRefsByPeeledObjectId() in the repository, and this will return the commits, but I cannot link them to the tags.

Any ideas?

+5
source share
2 answers

List of all tags

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

Tag based commit list:

I would use the log command based on the tag name with refined magic, as indicated 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<RevCommit> 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.

+4
source

To get a list of tags, you can use Repository#getTags() or ListTagCommand .

Git has annotated and unannotated tags. While tags without authorization indicate a commit on which they were placed, an annotated tag points to a git object that contains - among other metadata, such as a message - a commit identifier.

The training test below illustrates this:

 public class TagLearningTest { @Rule public final TemporaryFolder tempFolder = new TemporaryFolder(); private Git git; @Test public void testUnannotatedTag() throws Exception { RevCommit commit = git.commit().setMessage( "Tag Me!" ).call(); Ref tagRef = git.tag().setAnnotated( false ).setName( "Unannotated_Tag" ).call(); assertEquals( commit.getId(), tagRef.getObjectId() ); assertNull( git.getRepository().peel( tagRef ).getPeeledObjectId() ); } @Test public void testAnnotatedTag() throws Exception { RevCommit commit = git.commit().setMessage( "Tag Me!" ).call(); Ref tagRef = git.tag().setAnnotated( true ).setName( "Annotated_Tag" ).call(); assertEquals( commit, git.getRepository().peel( tagRef ).getPeeledObjectId() ); ObjectReader objectReader = git.getRepository().newObjectReader(); ObjectLoader objectLoader = objectReader.open( tagRef.getObjectId() ); RevTag tag = RevTag.parse( objectLoader.getBytes() ); objectReader.release(); assertEquals( commit.getId(), tag.getObject() ); } @Before public void setUp() throws GitAPIException { git = Git.init().setDirectory( tempFolder.getRoot() ).call(); } } 

In JGit, the annotated tag is represented by RevTag , which is stored under the identifier that the ref tag points to.

To pass one form to another, you can clear the ref, and then check to see if its getPeeledObjectId() not null.

 Ref peeledRef = git.getRepository().peel( tagRef ); boolean annotatedTag = peeledRef.getPeeledObjectId() != null; 

The identifier of the cleared object is the one that indicates the commit on which the annotated tag was created.

+3
source

Source: https://habr.com/ru/post/1207738/


All Articles