Skip to content

Commit e1cd88f

Browse files
author
S L
committed
refactoring: extracting some functionality to a new class JGitCommon
1 parent b43afd2 commit e1cd88f

2 files changed

Lines changed: 79 additions & 30 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.jgit;
19+
20+
import java.io.IOException;
21+
import java.util.Collection;
22+
import java.util.List;
23+
24+
import org.eclipse.jgit.api.Git;
25+
import org.eclipse.jgit.api.errors.GitAPIException;
26+
import org.eclipse.jgit.lib.ObjectId;
27+
import org.eclipse.jgit.lib.Ref;
28+
import org.eclipse.jgit.lib.Repository;
29+
import org.eclipse.jgit.revwalk.RevWalk;
30+
31+
import com.google.common.base.Function;
32+
import com.google.common.base.Predicate;
33+
import com.google.common.collect.Collections2;
34+
35+
/**
36+
* @author <a href="mailto:konrad.malawski@java.pl">Konrad 'ktoso' Malawski</a>
37+
*/
38+
public class JGitCommon {
39+
public Collection<String> getTags(Repository repo, final ObjectId headId) throws GitAPIException{
40+
RevWalk walk = null;
41+
try {
42+
Git git = Git.wrap(repo);
43+
walk = new RevWalk(repo);
44+
List<Ref> tagRefs = git.tagList().call();
45+
46+
final RevWalk finalWalk = walk;
47+
Collection<Ref> tagsForHeadCommit = Collections2.filter(tagRefs, new Predicate<Ref>() {
48+
@Override public boolean apply(Ref tagRef) {
49+
boolean lightweightTag = tagRef.getObjectId().equals(headId);
50+
51+
try {
52+
// TODO make this configurable (most users shouldn't really care too much what kind of tag it is though)
53+
return lightweightTag || finalWalk.parseTag(tagRef.getObjectId()).getObject().getId().equals(headId); // or normal tag
54+
} catch (IOException e) {
55+
return false;
56+
}
57+
}
58+
});
59+
60+
Collection<String> tags = Collections2.transform(tagsForHeadCommit, new Function<Ref, String>() {
61+
@Override public String apply(Ref input) {
62+
return input.getName().replaceAll("refs/tags/", "");
63+
}
64+
});
65+
66+
return tags;
67+
} catch (GitAPIException e) {
68+
throw e;
69+
} finally {
70+
if (walk != null) {
71+
walk.dispose();
72+
}
73+
}
74+
}
75+
76+
}

src/main/java/pl/project13/maven/git/JGitProvider.java

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import pl.project13.jgit.DescribeCommand;
2626
import pl.project13.jgit.DescribeResult;
27+
import pl.project13.jgit.JGitCommon;
2728
import pl.project13.maven.git.log.LoggerBridge;
2829

2930
import java.io.File;
@@ -175,42 +176,14 @@ protected String getRemoteOriginUrl() throws MojoExecutionException {
175176

176177
@Override
177178
protected String getTags() throws MojoExecutionException {
178-
RevWalk walk = null;
179179
try {
180180
Repository repo = getGitRepository();
181-
Git git = Git.wrap(repo);
182-
walk = new RevWalk(repo);
183-
List<Ref> tagRefs = git.tagList().call();
184-
185-
final ObjectId headId = headCommit.toObjectId();
186-
final RevWalk finalWalk = walk;
187-
Collection<Ref> tagsForHeadCommit = Collections2.filter(tagRefs, new Predicate<Ref>() {
188-
@Override public boolean apply(Ref tagRef) {
189-
boolean lightweightTag = tagRef.getObjectId().equals(headId);
190-
191-
try {
192-
// TODO make this configurable (most users shouldn't really care too much what kind of tag it is though)
193-
return lightweightTag || finalWalk.parseTag(tagRef.getObjectId()).getObject().getId().equals(headId); // or normal tag
194-
} catch (IOException e) {
195-
return false;
196-
}
197-
}
198-
});
199-
200-
Collection<String> tags = Collections2.transform(tagsForHeadCommit, new Function<Ref, String>() {
201-
@Override public String apply(Ref input) {
202-
return input.getName().replaceAll("refs/tags/", "");
203-
}
204-
});
205-
181+
ObjectId headId = headCommit.toObjectId();
182+
Collection<String> tags = new JGitCommon().getTags(repo,headId);
206183
return Joiner.on(",").join(tags);
207184
} catch (GitAPIException e) {
208185
loggerBridge.error("Unable to extract tags from commit: " + headCommit.getName() + " (" + e.getClass().getName() + ")");
209186
return "";
210-
} finally {
211-
if (walk != null) {
212-
walk.dispose();
213-
}
214187
}
215188
}
216189

0 commit comments

Comments
 (0)