Skip to content

Commit 3778f3a

Browse files
author
S L
committed
Use a single instance of JGitCommon
1 parent 877afe0 commit 3778f3a

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/main/java/pl/project13/jgit/DescribeCommand.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
public class DescribeCommand extends GitCommand<DescribeResult> {
4949

5050
private LoggerBridge loggerBridge;
51+
private JGitCommon jGitCommon;
5152

5253
// TODO not yet implemented options:
5354
// private boolean containsFlag = false;
@@ -122,6 +123,12 @@ public DescribeCommand withLoggerBridge(LoggerBridge bridge) {
122123
return this;
123124
}
124125

126+
@NotNull
127+
public DescribeCommand withJGitCommon(JGitCommon jGitCommon) {
128+
this.jGitCommon = jGitCommon;
129+
return this;
130+
}
131+
125132
/**
126133
* <pre>--always</pre>
127134
*
@@ -293,7 +300,7 @@ public DescribeResult call() throws GitAPIException {
293300
}
294301

295302
// get commits, up until the nearest tag
296-
List<RevCommit> commits = new JGitCommon().findCommitsUntilSomeTag(repo, headCommit, tagObjectIdToName);
303+
List<RevCommit> commits = jGitCommon.findCommitsUntilSomeTag(repo, headCommit, tagObjectIdToName);
297304

298305
// if there is no tags or any tag is not on that branch then return generic describe
299306
if (foundZeroTags(tagObjectIdToName) || commits.isEmpty()) {
@@ -303,7 +310,7 @@ public DescribeResult call() throws GitAPIException {
303310

304311
// check how far away from a tag we are
305312

306-
int distance = new JGitCommon().distanceBetween(repo, headCommit, commits.get(0));
313+
int distance = jGitCommon.distanceBetween(repo, headCommit, commits.get(0));
307314
String tagName = tagObjectIdToName.get(commits.get(0)).iterator().next();
308315
Pair<Integer, String> howFarFromWhichTag = Pair.of(distance, tagName);
309316

@@ -387,8 +394,8 @@ RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
387394
// git commit id -> its tag (or tags)
388395
private Map<ObjectId, List<String>> findTagObjectIds(@NotNull Repository repo, boolean tagsFlag) {
389396
String matchPattern = createMatchPattern();
390-
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = new JGitCommon().getCommitIdsToTags(loggerBridge, repo, tagsFlag, matchPattern);
391-
Map<ObjectId, List<String>> commitIdsToTagNames = new JGitCommon().transformRevTagsMapToDateSortedTagNames(commitIdsToTags);
397+
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = jGitCommon.getCommitIdsToTags(loggerBridge, repo, tagsFlag, matchPattern);
398+
Map<ObjectId, List<String>> commitIdsToTagNames = jGitCommon.transformRevTagsMapToDateSortedTagNames(commitIdsToTags);
392399
log("Created map: [",commitIdsToTagNames,"] ");
393400

394401
return commitIdsToTagNames;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class JGitProvider extends GitDataProvider {
5454
private ObjectReader objectReader;
5555
private RevWalk revWalk;
5656
private RevCommit headCommit;
57+
private JGitCommon jGitCommon;
5758

5859
@NotNull
5960
public static JGitProvider on(@NotNull File dotGitDirectory, @NotNull LoggerBridge loggerBridge) {
@@ -63,6 +64,7 @@ public static JGitProvider on(@NotNull File dotGitDirectory, @NotNull LoggerBrid
6364
JGitProvider(@NotNull File dotGitDirectory, @NotNull LoggerBridge loggerBridge) {
6465
super(loggerBridge);
6566
this.dotGitDirectory = dotGitDirectory;
67+
this.jGitCommon = new JGitCommon();
6668
}
6769

6870
@NotNull
@@ -189,7 +191,7 @@ protected String getTags() throws MojoExecutionException {
189191
try {
190192
Repository repo = getGitRepository();
191193
ObjectId headId = headCommit.toObjectId();
192-
Collection<String> tags = new JGitCommon().getTags(repo,headId);
194+
Collection<String> tags = jGitCommon.getTags(repo,headId);
193195
return Joiner.on(",").join(tags);
194196
} catch (GitAPIException e) {
195197
loggerBridge.error("Unable to extract tags from commit: " + headCommit.getName() + " (" + e.getClass().getName() + ")");
@@ -201,7 +203,7 @@ protected String getTags() throws MojoExecutionException {
201203
protected String getClosestTagName() throws MojoExecutionException {
202204
Repository repo = getGitRepository();
203205
try {
204-
return new JGitCommon().getClosestTagName(loggerBridge,repo);
206+
return jGitCommon.getClosestTagName(loggerBridge,repo);
205207
} catch (Throwable t) {
206208
// could not find any tags to describe
207209
}
@@ -212,7 +214,7 @@ protected String getClosestTagName() throws MojoExecutionException {
212214
protected String getClosestTagCommitCount() throws MojoExecutionException {
213215
Repository repo = getGitRepository();
214216
try {
215-
return new JGitCommon().getClosestTagCommitCount(loggerBridge,repo,headCommit);
217+
return jGitCommon.getClosestTagCommitCount(loggerBridge,repo,headCommit);
216218
} catch (Throwable t) {
217219
// could not find any tags to describe
218220
}
@@ -232,6 +234,7 @@ protected void finalCleanUp() {
232234
DescribeResult describeResult = DescribeCommand
233235
.on(repository)
234236
.withLoggerBridge(super.loggerBridge)
237+
.withJGitCommon(jGitCommon)
235238
.setVerbose(super.verbose)
236239
.apply(super.gitDescribe)
237240
.call();

0 commit comments

Comments
 (0)