@@ -180,7 +180,7 @@ public DescribeCommand always(boolean always) {
180180 public DescribeCommand forceLongFormat (@ Nullable Boolean forceLongFormat ) {
181181 if (forceLongFormat != null ) {
182182 this .forceLongFormat = forceLongFormat ;
183- log ("--long = %s " , forceLongFormat );
183+ log ("--long =" , forceLongFormat );
184184 }
185185 return this ;
186186 }
@@ -312,21 +312,22 @@ public DescribeResult call() throws GitAPIException {
312312 // check if dirty
313313 boolean dirty = findDirtyState (repo );
314314
315- if (hasTags (headCommit , tagObjectIdToName )) {
315+ if (hasTags (headCommit , tagObjectIdToName ) && ! forceLongFormat ) {
316316 String tagName = tagObjectIdToName .get (headCommit ).iterator ().next ();
317- log ("The commit we're on is a Tag ([" ,tagName ,"]), returning." );
317+ log ("The commit we're on is a Tag ([" ,tagName ,"]) and forceLongFormat == false , returning." );
318318
319319 return new DescribeResult (tagName , dirty , dirtyOption );
320320 }
321321
322- if (foundZeroTags (tagObjectIdToName )) {
322+ // get commits, up until the nearest tag
323+ List <RevCommit > commits = findCommitsUntilSomeTag (repo , headCommit , tagObjectIdToName );
324+
325+ // if there is no tags or any tag is not on that branch then return generic describe
326+ if (foundZeroTags (tagObjectIdToName ) || commits .isEmpty ()) {
323327 return new DescribeResult (objectReader , headCommitId , dirty , dirtyOption )
324328 .withCommitIdAbbrev (abbrev );
325329 }
326330
327- // get commits, up until the nearest tag
328- List <RevCommit > commits = findCommitsUntilSomeTag (repo , headCommit , tagObjectIdToName );
329-
330331 // check how far away from a tag we are
331332
332333 int distance = distanceBetween (repo , headCommit , commits .get (0 ));
@@ -350,7 +351,7 @@ private DescribeResult createDescribeResult(ObjectReader objectReader, ObjectId
350351 .withCommitIdAbbrev (abbrev );
351352
352353 } else if (howFarFromWhichTag .first > 0 || forceLongFormat ) {
353- return new DescribeResult (objectReader , howFarFromWhichTag .second , howFarFromWhichTag .first , headCommitId , dirty , dirtyOption )
354+ return new DescribeResult (objectReader , howFarFromWhichTag .second , howFarFromWhichTag .first , headCommitId , dirty , dirtyOption , forceLongFormat )
354355 .withCommitIdAbbrev (abbrev ); // we're a bit away from a tag
355356
356357 } else if (howFarFromWhichTag .first == 0 ) {
@@ -425,7 +426,7 @@ private List<RevCommit> findCommitsUntilSomeTag(Repository repo, RevCommit head,
425426 }
426427 }
427428
428- throw new RuntimeException ( "Did not find any commits until some tag" );
429+ return Collections . emptyList ( );
429430 } catch (Exception e ) {
430431 throw new RuntimeException ("Unable to find commits until some tag" , e );
431432 }
0 commit comments