All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.databasesandlife.util.GitPropertiesBuildInfo Maven / Gradle / Ivy

There is a newer version: 21.0.1
Show newest version
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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy