Package pl.project13.core
Class GitDataProvider
- java.lang.Object
-
- pl.project13.core.GitDataProvider
-
- All Implemented Interfaces:
GitProvider
- Direct Known Subclasses:
JGitProvider,NativeGitProvider
public abstract class GitDataProvider extends java.lang.Object implements GitProvider
An abstract Interface that dictates what functionality aGitProviderneeds to fullfill. As of now this project implements two variants of aGitProvider. NamelyNativeGitProviderwhich uses the native git binaries andJGitProviderwhich uses a java git implementation (jgit).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceGitDataProvider.SupplierEx<T>Represents a supplier of results that is allowed to throw aGitCommitIdExecutionException.
-
Field Summary
Fields Modifier and Type Field Description protected intabbrevLengthThecommit abbreviation lengthprotected CommitIdGenerationModecommitIdGenerationModeThegit commit id generation mode.protected java.lang.StringdateFormatThedate formatfor all generated properties.protected java.lang.StringdateFormatTimeZoneThedate format time zonefor all generated properties.protected java.lang.StringevaluateOnCommitAllow to tell the plugin what commit should be used as reference to generate the properties from.protected java.util.List<java.lang.String>excludePropertiesCan be used to exclude certain properties from being emitted (e.g.protected GitDescribeConfiggitDescribeTheGitDescribeConfig.protected java.util.List<java.lang.String>includeOnlyPropertiesCan be used to include only certain properties into the resulting file (e.g.protected LogInterfacelogLogging provider which will be used to log events.protected booleanofflineWhen set totrue, the plugin will not try to contact any remote repositories.protected java.lang.StringprefixDotTheprefixused for all generated properties.protected booleanuseBranchNameFromBuildEnvironmentWhen set totruethis plugin will try to use the branch name from build environment.
-
Constructor Summary
Constructors Constructor Description GitDataProvider(LogInterface log)Constructor to encapsulates all references required to dertermine all git-data.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.StringdetermineBranchName(java.util.Map<java.lang.String,java.lang.String> env)If running within Jenkins/Hudson, honor the branch name passed via GIT_BRANCH env var.protected java.text.SimpleDateFormatgetSimpleDateFormatWithTimeZone()protected voidloadGitData(java.lang.String evaluateOnCommit, java.util.Map<java.lang.String,java.lang.String> env, java.util.Properties properties)Main function that will attempt to load the desired properties from the git repository.protected voidloadShortDescribe(java.util.Properties properties)protected voidmaybePut(java.util.Properties properties, java.lang.String key, GitDataProvider.SupplierEx<java.lang.String> value)static <T> GitDataProvider.SupplierEx<T>memoize(GitDataProvider.SupplierEx<T> delegate)GitDataProvidersetAbbrevLength(int abbrevLength)Sets thecommit abbreviation lengthfor theGitProviderGitDataProvidersetCommitIdGenerationMode(CommitIdGenerationMode commitIdGenerationMode)Sets thegit commit id generation modefor theGitProviderGitDataProvidersetDateFormat(java.lang.String dateFormat)Sets thedate formatfor all generated properties for theGitProviderGitDataProvidersetDateFormatTimeZone(java.lang.String dateFormatTimeZone)Sets thedate format time zonefor all generated properties for theGitProviderGitDataProvidersetExcludeProperties(java.util.List<java.lang.String> excludeProperties)Can be used to exclude certain properties from being emitted (e.g.GitDataProvidersetGitDescribe(GitDescribeConfig gitDescribe)Sets theGitDescribeConfigfor theGitProviderGitDataProvidersetIncludeOnlyProperties(java.util.List<java.lang.String> includeOnlyProperties)Can be used to include only certain properties into the resulting file (e.g.GitDataProvidersetOffline(boolean offline)When set totrue, the plugin will not try to contact any remote repositories.GitDataProvidersetPrefixDot(java.lang.String prefixDot)Sets theprefixused for all generated properties for theGitProviderGitDataProvidersetUseBranchNameFromBuildEnvironment(boolean useBranchNameFromBuildEnvironment)Sets the Indicator if the branch name should be obtained from the build environment, or by the plugin.protected java.lang.StringstripCredentialsFromOriginUrl(java.lang.String gitRemoteString)If the git remote value is a URI and contains a user info component, strip the password from it if it exists.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface pl.project13.core.GitProvider
finalCleanUp, getAbbrevCommitId, getAheadBehind, getBranchName, getBuildAuthorEmail, getBuildAuthorName, getClosestTagCommitCount, getClosestTagName, getCommitAuthorEmail, getCommitAuthorName, getCommitAuthorTime, getCommitCommitterTime, getCommitId, getCommitMessageFull, getCommitMessageShort, getCommitTime, getGitDescribe, getRemoteOriginUrl, getTags, getTotalCommitCount, init, isDirty, prepareGitToExtractMoreDetailedRepoInformation
-
-
-
-
Field Detail
-
log
@Nonnull protected final LogInterface log
Logging provider which will be used to log events.
-
prefixDot
protected java.lang.String prefixDot
Theprefixused for all generated properties.
-
abbrevLength
protected int abbrevLength
Thecommit abbreviation length
-
dateFormat
protected java.lang.String dateFormat
Thedate formatfor all generated properties.
-
dateFormatTimeZone
protected java.lang.String dateFormatTimeZone
Thedate format time zonefor all generated properties.
-
gitDescribe
protected GitDescribeConfig gitDescribe
TheGitDescribeConfig.
-
commitIdGenerationMode
protected CommitIdGenerationMode commitIdGenerationMode
Thegit commit id generation mode.
-
evaluateOnCommit
protected java.lang.String evaluateOnCommit
Allow to tell the plugin what commit should be used as reference to generate the properties from. Defaults toHEAD.
-
useBranchNameFromBuildEnvironment
protected boolean useBranchNameFromBuildEnvironment
When set totruethis plugin will try to use the branch name from build environment. Set tofalseto use JGit/GIT to get current branch name which can be useful when using the JGitflow maven plugin. See https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/24
-
excludeProperties
protected java.util.List<java.lang.String> excludeProperties
Can be used to exclude certain properties from being emitted (e.g. filter out properties that you *don't* want to expose). May be useful when you want to hidegit.remote.origin.url(maybe because it contains your repo password?), or the email of the committer etc.
-
includeOnlyProperties
protected java.util.List<java.lang.String> includeOnlyProperties
Can be used to include only certain properties into the resulting file (e.g. include only properties that you *want* to expose). This feature was implemented to avoid big exclude properties tag when we only want very few specific properties. The inclusion rules, will be overruled by the exclude rules (e.g. you can write an inclusion rule that applies for multiple properties and then exclude a subset of them).
-
offline
protected boolean offline
When set totrue, the plugin will not try to contact any remote repositories. Any operations will only use the local state of the repo. If set tofalse, it will executegit fetchoperations e.g. to determine theaheadandbehindbranch information.
-
-
Constructor Detail
-
GitDataProvider
public GitDataProvider(@Nonnull LogInterface log)Constructor to encapsulates all references required to dertermine all git-data.- Parameters:
log- logging provider which will be used to log events
-
-
Method Detail
-
setGitDescribe
public GitDataProvider setGitDescribe(GitDescribeConfig gitDescribe)
Sets theGitDescribeConfigfor theGitProvider- Parameters:
gitDescribe- TheGitDescribeConfig- Returns:
- The
GitProviderwith the correspondingGitDescribeConfig.
-
setPrefixDot
public GitDataProvider setPrefixDot(java.lang.String prefixDot)
Sets theprefixused for all generated properties for theGitProvider- Parameters:
prefixDot- Theprefixused for all generated properties- Returns:
- The
GitProviderwith the correspondingprefix.
-
setAbbrevLength
public GitDataProvider setAbbrevLength(int abbrevLength)
Sets thecommit abbreviation lengthfor theGitProvider- Parameters:
abbrevLength- Thecommit abbreviation length- Returns:
- The
GitProviderwith the correspondingcommit abbreviation length.
-
setDateFormat
public GitDataProvider setDateFormat(java.lang.String dateFormat)
Sets thedate formatfor all generated properties for theGitProvider- Parameters:
dateFormat- Thedate formatfor all generated properties.- Returns:
- The
GitProviderwith the correspondingdate format.
-
setCommitIdGenerationMode
public GitDataProvider setCommitIdGenerationMode(CommitIdGenerationMode commitIdGenerationMode)
Sets thegit commit id generation modefor theGitProvider- Parameters:
commitIdGenerationMode- Thegit commit id generation mode- Returns:
- The
GitProviderwith the correspondinggit commit id generation mode.
-
setDateFormatTimeZone
public GitDataProvider setDateFormatTimeZone(java.lang.String dateFormatTimeZone)
Sets thedate format time zonefor all generated properties for theGitProvider- Parameters:
dateFormatTimeZone- Thedate format time zonefor all generated properties.- Returns:
- The
GitProviderwith the correspondingdate format time zone.
-
setUseBranchNameFromBuildEnvironment
public GitDataProvider setUseBranchNameFromBuildEnvironment(boolean useBranchNameFromBuildEnvironment)
Sets the Indicator if the branch name should be obtained from the build environment, or by the plugin.- Parameters:
useBranchNameFromBuildEnvironment- When set totruethis plugin will try to use the branch name from build environment. Set tofalseto use JGit/GIT to get current branch name which can be useful when using the JGitflow maven plugin. See https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/24- Returns:
- The
GitProviderwith the corresponding Indicator set.
-
setExcludeProperties
public GitDataProvider setExcludeProperties(java.util.List<java.lang.String> excludeProperties)
Can be used to exclude certain properties from being emitted (e.g. filter out properties that you *don't* want to expose). May be useful when you want to hidegit.remote.origin.url(maybe because it contains your repo password?), or the email of the committer etc.- Parameters:
excludeProperties- The properties that shall be excluded from generation.- Returns:
- The
GitProviderwith the correspondingproperties exclusion list.
-
setIncludeOnlyProperties
public GitDataProvider setIncludeOnlyProperties(java.util.List<java.lang.String> includeOnlyProperties)
Can be used to include only certain properties into the resulting file (e.g. include only properties that you *want* to expose). This feature was implemented to avoid big exclude properties tag when we only want very few specific properties. The inclusion rules, will be overruled by the exclude rules (e.g. you can write an inclusion rule that applies for multiple properties and then exclude a subset of them).- Parameters:
includeOnlyProperties- The properties that shall only be included in the generation.- Returns:
- The
GitProviderwith the correspondingproperties inclusion only list.
-
setOffline
public GitDataProvider setOffline(boolean offline)
When set totrue, the plugin will not try to contact any remote repositories. Any operations will only use the local state of the repo. If set tofalse, it will executegit fetchoperations e.g. to determine theaheadandbehindbranch information.- Parameters:
offline- Indicator if the plugin should operate inofflineoronline-mode.- Returns:
- The
GitProviderwith the corresponding Indicator set.
-
loadGitData
protected void loadGitData(@Nonnull java.lang.String evaluateOnCommit, @Nonnull java.util.Map<java.lang.String,java.lang.String> env, @Nonnull java.util.Properties properties) throws GitCommitIdExecutionExceptionMain function that will attempt to load the desired properties from the git repository.- Parameters:
evaluateOnCommit- The commit that should be used as reference to generate the properties from. Defaults toHEAD.env- unmodifiable string map view of the current system environmentSystem.getenv(java.lang.String).properties- The Properties-Set that should be enriched by the generated one.- Throws:
GitCommitIdExecutionException- In case any problem occurred during loading of the properties from the git repository.
-
loadShortDescribe
protected void loadShortDescribe(@Nonnull java.util.Properties properties) throws GitCommitIdExecutionException- Throws:
GitCommitIdExecutionException
-
determineBranchName
protected java.lang.String determineBranchName(@Nonnull java.util.Map<java.lang.String,java.lang.String> env) throws GitCommitIdExecutionExceptionIf running within Jenkins/Hudson, honor the branch name passed via GIT_BRANCH env var. This is necessary because Jenkins/Hudson always invoke build in a detached head state.- Parameters:
env- unmodifiable string map view of the current system environmentSystem.getenv(java.lang.String).- Returns:
- results of getBranchName() or, if in Jenkins/Hudson, value of GIT_BRANCH
- Throws:
GitCommitIdExecutionException- the branch name could not be determined
-
getSimpleDateFormatWithTimeZone
protected java.text.SimpleDateFormat getSimpleDateFormatWithTimeZone()
-
maybePut
protected void maybePut(@Nonnull java.util.Properties properties, java.lang.String key, GitDataProvider.SupplierEx<java.lang.String> value) throws GitCommitIdExecutionException- Throws:
GitCommitIdExecutionException
-
memoize
public static <T> GitDataProvider.SupplierEx<T> memoize(GitDataProvider.SupplierEx<T> delegate)
-
stripCredentialsFromOriginUrl
protected java.lang.String stripCredentialsFromOriginUrl(java.lang.String gitRemoteString) throws GitCommitIdExecutionExceptionIf the git remote value is a URI and contains a user info component, strip the password from it if it exists. Note that this method will return an empty string if any failure occurred, while stripping the password from the credentials. This merely serves as save-guard to avoid any potential password exposure inside generated properties. This method further operates on the assumption that a valid URL schema follows the rules outlined in RFC-2396 in section "3.2.2. Server-based Naming Authority" which declares the following as valid URL schema:<userinfo>@<host>:<port>
The "userinfo" part is declared in the same section allowing the following pattern:userinfo = *( unreserved | escaped | ";" | ":" | "&" | "=" | "+" | "$" | "," )The "unreserved" part is declared in section "2.3. Unreserved Characters" as the following:unreserved = alphanum | mark mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" alphanum = alpha | digit digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" alpha = lowalpha | upalpha lowalpha = "a" | "b" | "c" | ... | "x" | "y" | "z" upalpha = "A" | "B" | "C" | ... | "X" | "Y" | "Z"- Parameters:
gitRemoteString- The value of the git remote- Returns:
- returns the gitRemoteUri with stripped password (might be used in http or https)
- Throws:
GitCommitIdExecutionException- Exception when URI is invalid
-
-