4949import java .util .*;
5050import java .util .regex .Pattern ;
5151
52- import static com .google .common .collect .Lists .newArrayList ;
53- import static com .google .common .collect .Lists .newLinkedList ;
54- import static com .google .common .collect .Maps .newHashMap ;
55- import static com .google .common .collect .Sets .newHashSet ;
56-
5752/**
5853 * Implements git's <pre>describe</pre> command.
5954 *
@@ -63,7 +58,7 @@ public class DescribeCommand extends GitCommand<DescribeResult> {
6358
6459 private LoggerBridge loggerBridge ;
6560
66- // todo not yet implemented options:
61+ // TODO not yet implemented options:
6762// private boolean containsFlag = false;
6863// private boolean allFlag = false;
6964// private boolean tagsFlag = false;
@@ -307,7 +302,7 @@ public DescribeResult call() throws GitAPIException {
307302 }
308303
309304 // get commits, up until the nearest tag
310- List <RevCommit > commits = findCommitsUntilSomeTag (repo , headCommit , tagObjectIdToName );
305+ List <RevCommit > commits = new JGitCommon (). findCommitsUntilSomeTag (repo , headCommit , tagObjectIdToName );
311306
312307 // if there is no tags or any tag is not on that branch then return generic describe
313308 if (foundZeroTags (tagObjectIdToName ) || commits .isEmpty ()) {
@@ -317,7 +312,7 @@ public DescribeResult call() throws GitAPIException {
317312
318313 // check how far away from a tag we are
319314
320- int distance = distanceBetween (repo , headCommit , commits .get (0 ));
315+ int distance = new JGitCommon (). distanceBetween (repo , headCommit , commits .get (0 ));
321316 String tagName = tagObjectIdToName .get (commits .get (0 )).iterator ().next ();
322317 Pair <Integer , String > howFarFromWhichTag = Pair .of (distance , tagName );
323318
@@ -398,102 +393,6 @@ RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
398393 }
399394 }
400395
401- private List <RevCommit > findCommitsUntilSomeTag (Repository repo , RevCommit head , @ NotNull Map <ObjectId , List <String >> tagObjectIdToName ) {
402- RevWalk revWalk = new RevWalk (repo );
403- try {
404- revWalk .markStart (head );
405-
406- for (RevCommit commit : revWalk ) {
407- ObjectId objId = commit .getId ();
408- if (tagObjectIdToName .size () > 0 ) {
409- List <String > maybeList = tagObjectIdToName .get (objId );
410- if (maybeList != null && maybeList .get (0 ) != null ) {
411- return Collections .singletonList (commit );
412- }
413- }
414- }
415-
416- return Collections .emptyList ();
417- } catch (Exception e ) {
418- throw new RuntimeException ("Unable to find commits until some tag" , e );
419- }
420- }
421-
422- /**
423- * Calculates the distance (number of commits) between the given parent and child commits.
424- *
425- * @return distance (number of commits) between the given commits
426- * @see <a href="https://github.com/mdonoughe/jgit-describe/blob/master/src/org/mdonoughe/JGitDescribeTask.java">mdonoughe/jgit-describe/blob/master/src/org/mdonoughe/JGitDescribeTask.java</a>
427- */
428- private int distanceBetween (@ NotNull Repository repo , @ NotNull RevCommit child , @ NotNull RevCommit parent ) {
429- RevWalk revWalk = new RevWalk (repo );
430-
431- try {
432- revWalk .markStart (child );
433-
434- Set <ObjectId > seena = newHashSet ();
435- Set <ObjectId > seenb = newHashSet ();
436- Queue <RevCommit > q = newLinkedList ();
437-
438- q .add (revWalk .parseCommit (child ));
439- int distance = 0 ;
440- ObjectId parentId = parent .getId ();
441-
442- while (q .size () > 0 ) {
443- RevCommit commit = q .remove ();
444- ObjectId commitId = commit .getId ();
445-
446- if (seena .contains (commitId )) {
447- continue ;
448- }
449- seena .add (commitId );
450-
451- if (parentId .equals (commitId )) {
452- // don't consider commits that are included in this commit
453- seeAllParents (revWalk , commit , seenb );
454- // remove things we shouldn't have included
455- for (ObjectId oid : seenb ) {
456- if (seena .contains (oid )) {
457- distance --;
458- }
459- }
460- seena .addAll (seenb );
461- continue ;
462- }
463-
464- for (ObjectId oid : commit .getParents ()) {
465- if (!seena .contains (oid )) {
466- q .add (revWalk .parseCommit (oid ));
467- }
468- }
469- distance ++;
470- }
471-
472- return distance ;
473-
474- } catch (Exception e ) {
475- throw new RuntimeException (String .format ("Unable to calculate distance between [%s] and [%s]" , child , parent ), e );
476- } finally {
477- revWalk .dispose ();
478- }
479- }
480-
481- private void seeAllParents (@ NotNull RevWalk revWalk , RevCommit child , @ NotNull Set <ObjectId > seen ) throws IOException {
482- Queue <RevCommit > q = newLinkedList ();
483- q .add (child );
484-
485- while (q .size () > 0 ) {
486- RevCommit commit = q .remove ();
487- for (ObjectId oid : commit .getParents ()) {
488- if (seen .contains (oid )) {
489- continue ;
490- }
491- seen .add (oid );
492- q .add (revWalk .parseCommit (oid ));
493- }
494- }
495- }
496-
497396 // git commit id -> its tag (or tags)
498397 private Map <ObjectId , List <String >> findTagObjectIds (@ NotNull Repository repo , boolean tagsFlag ) {
499398 String matchPattern = createMatchPattern ();
@@ -519,10 +418,5 @@ private String createMatchPattern() {
519418 private void log (Object ... parts ) {
520419 loggerBridge .log (parts );
521420 }
522-
523- private void error (Object ... parts ) {
524- loggerBridge .error (parts );
525- }
526-
527421}
528422
0 commit comments