Class GitDataProvider

java.lang.Object
pl.project13.core.GitDataProvider
All Implemented Interfaces:
GitProvider
Direct Known Subclasses:
JGitProvider, NativeGitProvider

public abstract class GitDataProvider extends Object implements GitProvider
An abstract Interface that dictates what functionality a GitProvider needs to fullfill. As of now this project implements two variants of a GitProvider. Namely NativeGitProvider which uses the native git binaries and JGitProvider which uses a java git implementation (jgit).
  • Field Details

    • log

      @Nonnull protected final LogInterface log
      Logging provider which will be used to log events.
    • prefixDot

      protected String prefixDot
      The prefix used for all generated properties.
    • abbrevLength

      protected int abbrevLength
      The commit abbreviation length
    • dateFormat

      protected String dateFormat
      The date format for all generated properties.
    • dateFormatTimeZone

      protected String dateFormatTimeZone
      The date format time zone for all generated properties.
    • gitDescribe

      protected GitDescribeConfig gitDescribe
    • commitIdGenerationMode

      protected CommitIdGenerationMode commitIdGenerationMode
      The git commit id generation mode.
    • evaluateOnCommit

      protected String evaluateOnCommit
      Allow to tell the plugin what commit should be used as reference to generate the properties from. Defaults to HEAD.
    • useBranchNameFromBuildEnvironment

      protected boolean useBranchNameFromBuildEnvironment
      When set to true this plugin will try to use the branch name from build environment. Set to false to 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 List<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 hide git.remote.origin.url (maybe because it contains your repo password?), or the email of the committer etc.
    • includeOnlyProperties

      protected List<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 to true, the plugin will not try to contact any remote repositories. Any operations will only use the local state of the repo. If set to false, it will execute git fetch operations e.g. to determine the ahead and behind branch information.
  • Constructor Details

    • 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 Details

    • setGitDescribe

      public GitDataProvider setGitDescribe(GitDescribeConfig gitDescribe)
      Sets the GitDescribeConfig for the GitProvider
      Parameters:
      gitDescribe - The GitDescribeConfig
      Returns:
      The GitProvider with the corresponding GitDescribeConfig.
    • setPrefixDot

      public GitDataProvider setPrefixDot(String prefixDot)
      Sets the prefix used for all generated properties for the GitProvider
      Parameters:
      prefixDot - The prefix used for all generated properties
      Returns:
      The GitProvider with the corresponding prefix.
    • setAbbrevLength

      public GitDataProvider setAbbrevLength(int abbrevLength)
      Sets the commit abbreviation length for the GitProvider
      Parameters:
      abbrevLength - The commit abbreviation length
      Returns:
      The GitProvider with the corresponding commit abbreviation length.
    • setDateFormat

      public GitDataProvider setDateFormat(String dateFormat)
      Sets the date format for all generated properties for the GitProvider
      Parameters:
      dateFormat - The date format for all generated properties.
      Returns:
      The GitProvider with the corresponding date format.
    • setCommitIdGenerationMode

      public GitDataProvider setCommitIdGenerationMode(CommitIdGenerationMode commitIdGenerationMode)
      Sets the git commit id generation mode for the GitProvider
      Parameters:
      commitIdGenerationMode - The git commit id generation mode
      Returns:
      The GitProvider with the corresponding git commit id generation mode.
    • setDateFormatTimeZone

      public GitDataProvider setDateFormatTimeZone(String dateFormatTimeZone)
      Sets the date format time zone for all generated properties for the GitProvider
      Parameters:
      dateFormatTimeZone - The date format time zone for all generated properties.
      Returns:
      The GitProvider with the corresponding date 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 to true this plugin will try to use the branch name from build environment. Set to false to 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 GitProvider with the corresponding Indicator set.
    • setExcludeProperties

      public GitDataProvider setExcludeProperties(List<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 hide git.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 GitProvider with the corresponding properties exclusion list.
    • setIncludeOnlyProperties

      public GitDataProvider setIncludeOnlyProperties(List<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 GitProvider with the corresponding properties inclusion only list.
    • setOffline

      public GitDataProvider setOffline(boolean offline)
      When set to true, the plugin will not try to contact any remote repositories. Any operations will only use the local state of the repo. If set to false, it will execute git fetch operations e.g. to determine the ahead and behind branch information.
      Parameters:
      offline - Indicator if the plugin should operate in offline or online-mode.
      Returns:
      The GitProvider with the corresponding Indicator set.
    • loadGitData

      protected void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Map<String,String> env, @Nonnull Properties properties) throws GitCommitIdExecutionException
      Main 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 to HEAD.
      env - unmodifiable string map view of the current system environment System.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 Properties properties) throws GitCommitIdExecutionException
      Throws:
      GitCommitIdExecutionException
    • determineBranchName

      protected String determineBranchName(@Nonnull Map<String,String> env) throws GitCommitIdExecutionException
      If 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 environment System.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 SimpleDateFormat getSimpleDateFormatWithTimeZone()
    • maybePut

      protected void maybePut(@Nonnull Properties properties, String key, GitDataProvider.SupplierEx<String> value) throws GitCommitIdExecutionException
      Throws:
      GitCommitIdExecutionException
    • memoize

      public static <T> GitDataProvider.SupplierEx<T> memoize(GitDataProvider.SupplierEx<T> delegate)
    • stripCredentialsFromOriginUrl

      protected String stripCredentialsFromOriginUrl(String gitRemoteString) throws GitCommitIdExecutionException
      If 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