@@ -51,40 +51,36 @@ public JGitCommon(LoggerBridge log) {
5151 }
5252
5353 public Collection <String > getTags (Repository repo , final ObjectId headId ) throws GitAPIException {
54- RevWalk walk = null ;
55- try {
56- Git git = Git .wrap (repo );
57- walk = new RevWalk (repo );
58- List <Ref > tagRefs = git .tagList ().call ();
59-
60- final RevWalk finalWalk = walk ;
61- Collection <Ref > tagsForHeadCommit = Collections2 .filter (tagRefs , new Predicate <Ref >() {
62- @ Override public boolean apply (Ref tagRef ) {
63- boolean lightweightTag = tagRef .getObjectId ().equals (headId );
64-
65- try {
66- // TODO make this configurable (most users shouldn't really care too much what kind of tag it is though)
67- return lightweightTag || finalWalk .parseTag (tagRef .getObjectId ()).getObject ().getId ().equals (headId ); // or normal tag
68- } catch (IOException e ) {
69- return false ;
70- }
71- }
72- });
73-
74- Collection <String > tags = Collections2 .transform (tagsForHeadCommit , new Function <Ref , String >() {
75- @ Override public String apply (Ref input ) {
76- return input .getName ().replaceAll ("refs/tags/" , "" );
77- }
78- });
79-
80- return tags ;
81- } finally {
82- if (walk != null ) {
54+ try (Git git = Git .wrap (repo )) {
55+ try (RevWalk walk = new RevWalk (repo )) {
56+ Collection <String > tags = getTags (git , headId , walk );
8357 walk .dispose ();
58+ return tags ;
8459 }
8560 }
8661 }
8762
63+ private Collection <String > getTags (final Git git , final ObjectId headId , final RevWalk finalWalk ) throws GitAPIException {
64+ List <Ref > tagRefs = git .tagList ().call ();
65+ Collection <Ref > tagsForHeadCommit = Collections2 .filter (tagRefs , new Predicate <Ref >() {
66+ @ Override public boolean apply (Ref tagRef ) {
67+ boolean lightweightTag = tagRef .getObjectId ().equals (headId );
68+ try {
69+ // TODO make this configurable (most users shouldn't really care too much what kind of tag it is though)
70+ return lightweightTag || finalWalk .parseTag (tagRef .getObjectId ()).getObject ().getId ().equals (headId ); // or normal tag
71+ } catch (IOException e ) {
72+ return false ;
73+ }
74+ }
75+ });
76+ Collection <String > tags = Collections2 .transform (tagsForHeadCommit , new Function <Ref , String >() {
77+ @ Override public String apply (Ref input ) {
78+ return input .getName ().replaceAll ("refs/tags/" , "" );
79+ }
80+ });
81+ return tags ;
82+ }
83+
8884 public String getClosestTagName (@ NotNull Repository repo ){
8985 Map <ObjectId , List <DatedRevTag >> map = getClosestTagAsMap (repo );
9086 for (Map .Entry <ObjectId , List <DatedRevTag >> entry : map .entrySet ()){
@@ -97,12 +93,13 @@ public String getClosestTagCommitCount(@NotNull Repository repo, RevCommit headC
9793 HashMap <ObjectId , List <String >> map = transformRevTagsMapToDateSortedTagNames (getClosestTagAsMap (repo ));
9894 ObjectId obj = (ObjectId ) map .keySet ().toArray ()[0 ];
9995
100- RevWalk walk = new RevWalk (repo );
101- RevCommit commit = walk .lookupCommit (obj );
102- walk .dispose ();
96+ try ( RevWalk walk = new RevWalk (repo )){
97+ RevCommit commit = walk .lookupCommit (obj );
98+ walk .dispose ();
10399
104- int distance = distanceBetween (repo , headCommit , commit );
105- return String .valueOf (distance );
100+ int distance = distanceBetween (repo , headCommit , commit );
101+ return String .valueOf (distance );
102+ }
106103 }
107104
108105 private Map <ObjectId , List <DatedRevTag >> getClosestTagAsMap (@ NotNull Repository repo ){
0 commit comments