com.databasesandlife.util.GitPropertiesBuildInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
package com.databasesandlife.util;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.ResourceBundle;
/**
* Reads from "git.properties" as produced by git-commit-id-plugin v2.2.4.
* Use useNativeGit always otherwise dirty property is always set to true.
*/
public class GitPropertiesBuildInfo {
protected static final GitPropertiesBuildInfo instance = new GitPropertiesBuildInfo();
private GitPropertiesBuildInfo() { }
public static @Nonnull GitPropertiesBuildInfo get() { return instance; }
protected @Nonnull ResourceBundle getResourceBundle() {
return ResourceBundle.getBundle("git");
}
/** @return "123abcde-dirty" */
public @Nonnull String getGitRevision() {
var dirty = Boolean.parseBoolean(getResourceBundle().getString("git.dirty"));
var hash = getResourceBundle().getString("git.commit.id.abbrev");
return hash + (dirty ? "-dirty" : "");
}
/**
* Parses the most recent commit message like "1.2.3: Foo" and extracts the "1.2.3".
* Returns null if the commit message doesn't have this sort of form.
*/
public @CheckForNull String getCommitMessagePrefix() {
var commitMessage = getResourceBundle().getString("git.commit.message.full");
var colon = commitMessage.indexOf(":");
if (colon == -1) return null;
return commitMessage.substring(0, colon);
}
/** @return "Version 1.2.3" or "Build 123abcde-dirty" */
public @Nonnull String getVersionFromCommitPrefixOrGitRevision() {
var versionFromCommit = getCommitMessagePrefix();
if (versionFromCommit != null) return "Version " + versionFromCommit;
else return "Build " + getGitRevision();
}
}