Skip to content

Commit 020662f

Browse files
committed
Merge pull request #111 from szymex/master
Added property 'git.commit.id.short-describe'
2 parents f127f23 + 96167df commit 020662f

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class GitCommitIdMojo extends AbstractMojo {
6666
public static final String COMMIT_ID = "commit.id";
6767
public static final String COMMIT_ID_ABBREV = "commit.id.abbrev";
6868
public static final String COMMIT_DESCRIBE = "commit.id.describe";
69+
public static final String COMMIT_SHORT_DESCRIBE = "commit.id.short-describe";
6970
public static final String BUILD_AUTHOR_NAME = "build.user.name";
7071
public static final String BUILD_AUTHOR_EMAIL = "build.user.email";
7172
public static final String BUILD_TIME = "build.time";
@@ -205,7 +206,7 @@ public class GitCommitIdMojo extends AbstractMojo {
205206
*/
206207
@SuppressWarnings("UnusedDeclaration")
207208
private String prefix;
208-
private String prefixDot;
209+
private String prefixDot = "";
209210

210211
/**
211212
* The date format to be used for any dates exported by this plugin.
@@ -308,6 +309,7 @@ public void execute() throws MojoExecutionException {
308309

309310
loadGitData(properties);
310311
loadBuildTimeData(properties);
312+
loadShortDescribe(properties);
311313
filterNot(properties, excludeProperties);
312314
logProperties(properties);
313315

@@ -419,6 +421,27 @@ void loadBuildTimeData(@NotNull Properties properties) {
419421
SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
420422
put(properties, BUILD_TIME, smf.format(commitDate));
421423
}
424+
425+
void loadShortDescribe(@NotNull Properties properties) {
426+
//removes git hash part from describe
427+
String commitDescribe = properties.getProperty(prefixDot + COMMIT_DESCRIBE);
428+
429+
if (commitDescribe != null) {
430+
int startPos = commitDescribe.indexOf("-g");
431+
if (startPos > 0) {
432+
String commitShortDescribe;
433+
int endPos = commitDescribe.indexOf('-', startPos + 1);
434+
if (endPos < 0) {
435+
commitShortDescribe = commitDescribe.substring(0, startPos);
436+
} else {
437+
commitShortDescribe = commitDescribe.substring(0, startPos) + commitDescribe.substring(endPos);
438+
}
439+
put(properties, COMMIT_SHORT_DESCRIBE, commitShortDescribe);
440+
} else {
441+
put(properties, COMMIT_SHORT_DESCRIBE, commitDescribe);
442+
}
443+
}
444+
}
422445

423446
void loadGitData(@NotNull Properties properties) throws IOException, MojoExecutionException {
424447
Repository git = getGitRepository();

src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ public void shouldGenerateDescribeWithTagOnlyWhenForceLongFormatIsFalse() throws
272272

273273
// then
274274
assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe", "v1.0.0"));
275+
assertThat(targetProject.getProperties()).includes(entry("git.commit.id.short-describe", "v1.0.0"));
275276
}
276277

277278
@Test
@@ -296,8 +297,9 @@ public void shouldGenerateDescribeWithTagAndZeroAndCommitIdWhenForceLongFormatIs
296297

297298
// then
298299
assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe", "v1.0.0-0-gde4db35"));
300+
assertThat(targetProject.getProperties()).includes(entry("git.commit.id.short-describe", "v1.0.0-0"));
299301
}
300-
302+
301303
private void alterMojoSettings(String parameterName, Object parameterValue) {
302304
setInternalState(mojo, parameterName, parameterValue);
303305
}

src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,26 @@ public void shouldUseJenkinsBranchInfoWhenAvailable() throws IOException {
180180
env.put("GIT_BRANCH", "mybranch");
181181
assertThat(detachedHeadSHA1).isEqualTo(mojo.determineBranchName(git, env));
182182
}
183+
184+
@Test
185+
public void loadShortDescribe() {
186+
assertShortDescribe("1.0.2-12-g19471", "1.0.2-12");
187+
assertShortDescribe("1.0.2-12-g19471-DEV", "1.0.2-12-DEV");
188+
assertShortDescribe("V-1.0.2-12-g19471-DEV", "V-1.0.2-12-DEV");
189+
190+
assertShortDescribe(null, null);
191+
assertShortDescribe("12.4.0-1432", "12.4.0-1432");
192+
assertShortDescribe("12.6.0", "12.6.0");
193+
assertShortDescribe("", "");
194+
}
195+
196+
private void assertShortDescribe(String commitDescribe, String expectedShortDescribe) {
197+
GitCommitIdMojo commitIdMojo = new GitCommitIdMojo();
198+
Properties prop = new Properties();
199+
if (commitDescribe != null) {
200+
prop.put(GitCommitIdMojo.COMMIT_DESCRIBE, commitDescribe);
201+
}
202+
commitIdMojo.loadShortDescribe(prop);
203+
assertThat(prop.getProperty(GitCommitIdMojo.COMMIT_SHORT_DESCRIBE)).isEqualTo(expectedShortDescribe);
204+
}
183205
}

0 commit comments

Comments
 (0)