Merge pull request #8781 from ptamarit:short-commit-id-custom-length

* pr/8781:
  Polish "Use git.commit.id.abbrev if present" contribution
  Use git.commit.id.abbrev if present
This commit is contained in:
Stephane Nicoll 2017-03-31 15:09:54 +02:00
commit 751cd6e0b2
2 changed files with 32 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -54,6 +54,10 @@ public class GitProperties extends InfoProperties {
* @return the short commit id * @return the short commit id
*/ */
public String getShortCommitId() { public String getShortCommitId() {
String shortId = get("commit.id.abbrev");
if (shortId != null) {
return shortId;
}
String id = getCommitId(); String id = getCommitId();
if (id == null) { if (id == null) {
return null; return null;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,7 +32,7 @@ public class GitPropertiesTests {
@Test @Test
public void basicInfo() { public void basicInfo() {
GitProperties properties = new GitProperties( GitProperties properties = new GitProperties(
createProperties("master", "abcdefghijklmno", "1457527123")); createProperties("master", "abcdefghijklmno", "abcdefg", "1457527123"));
assertThat(properties.getBranch()).isEqualTo("master"); assertThat(properties.getBranch()).isEqualTo("master");
assertThat(properties.getCommitId()).isEqualTo("abcdefghijklmno"); assertThat(properties.getCommitId()).isEqualTo("abcdefghijklmno");
assertThat(properties.getShortCommitId()).isEqualTo("abcdefg"); assertThat(properties.getShortCommitId()).isEqualTo("abcdefg");
@ -50,7 +50,7 @@ public class GitPropertiesTests {
@Test @Test
public void coerceEpochSecond() { public void coerceEpochSecond() {
GitProperties properties = new GitProperties( GitProperties properties = new GitProperties(
createProperties("master", "abcdefg", "1457527123")); createProperties("master", "abcdefg", null, "1457527123"));
assertThat(properties.getCommitTime()).isNotNull(); assertThat(properties.getCommitTime()).isNotNull();
assertThat(properties.get("commit.time")).isEqualTo("1457527123000"); assertThat(properties.get("commit.time")).isEqualTo("1457527123000");
assertThat(properties.getCommitTime().getTime()).isEqualTo(1457527123000L); assertThat(properties.getCommitTime().getTime()).isEqualTo(1457527123000L);
@ -59,7 +59,7 @@ public class GitPropertiesTests {
@Test @Test
public void coerceDateString() { public void coerceDateString() {
GitProperties properties = new GitProperties( GitProperties properties = new GitProperties(
createProperties("master", "abcdefg", "2016-03-04T14:36:33+0100")); createProperties("master", "abcdefg", null, "2016-03-04T14:36:33+0100"));
assertThat(properties.getCommitTime()).isNotNull(); assertThat(properties.getCommitTime()).isNotNull();
assertThat(properties.get("commit.time")).isEqualTo("1457098593000"); assertThat(properties.get("commit.time")).isEqualTo("1457098593000");
assertThat(properties.getCommitTime().getTime()).isEqualTo(1457098593000L); assertThat(properties.getCommitTime().getTime()).isEqualTo(1457098593000L);
@ -68,24 +68,43 @@ public class GitPropertiesTests {
@Test @Test
public void coerceUnsupportedFormat() { public void coerceUnsupportedFormat() {
GitProperties properties = new GitProperties( GitProperties properties = new GitProperties(
createProperties("master", "abcdefg", "2016-03-04 15:22:24")); createProperties("master", "abcdefg", null, "2016-03-04 15:22:24"));
assertThat(properties.getCommitTime()).isNull(); assertThat(properties.getCommitTime()).isNull();
assertThat(properties.get("commit.time")).isEqualTo("2016-03-04 15:22:24"); assertThat(properties.get("commit.time")).isEqualTo("2016-03-04 15:22:24");
} }
@Test @Test
public void shortenCommitId() { public void shortCommitUsedIfPresent() {
GitProperties properties = new GitProperties( GitProperties properties = new GitProperties(
createProperties("master", "abc", "1457527123")); createProperties("master", "abcdefghijklmno", "abcdefgh", "1457527123"));
assertThat(properties.getCommitId()).isEqualTo("abcdefghijklmno");
assertThat(properties.getShortCommitId()).isEqualTo("abcdefgh");
}
@Test
public void shortenCommitIdShorterThan7() {
GitProperties properties = new GitProperties(
createProperties("master", "abc", null, "1457527123"));
assertThat(properties.getCommitId()).isEqualTo("abc"); assertThat(properties.getCommitId()).isEqualTo("abc");
assertThat(properties.getShortCommitId()).isEqualTo("abc"); assertThat(properties.getShortCommitId()).isEqualTo("abc");
} }
@Test
public void shortenCommitIdLongerThan7() {
GitProperties properties = new GitProperties(
createProperties("master", "abcdefghijklmno", null, "1457527123"));
assertThat(properties.getCommitId()).isEqualTo("abcdefghijklmno");
assertThat(properties.getShortCommitId()).isEqualTo("abcdefg");
}
private static Properties createProperties(String branch, String commitId, private static Properties createProperties(String branch, String commitId,
String commitTime) { String commitIdAbbrev, String commitTime) {
Properties properties = new Properties(); Properties properties = new Properties();
properties.put("branch", branch); properties.put("branch", branch);
properties.put("commit.id", commitId); properties.put("commit.id", commitId);
if (commitIdAbbrev != null) {
properties.put("commit.id.abbrev", commitIdAbbrev);
}
properties.put("commit.time", commitTime); properties.put("commit.time", commitTime);
return properties; return properties;
} }