Class GitControl

java.lang.Object
org.apache.juneau.server.config.repository.GitControl
All Implemented Interfaces:
AutoCloseable

public class GitControl extends Object implements AutoCloseable
Git repository control operations for cloning, pulling, pushing, and committing.

Provides a simplified wrapper around JGit for server configuration repository management. Used to fetch configuration files from a remote Git repository.

Credentials are never hardcoded in this class. They are either supplied directly by the caller (via the 5-arg constructor) or, preferably, resolved at construction time from a pluggable SecretStore (via the BeanStore/SecretStore constructor) so the password/token can be sourced from an environment variable, an OS keychain, or another secret backend rather than a literal string.

The underlying JGit Repository and Git handles are opened in the constructor and released by close(), so instances must be used with try-with-resources to avoid leaking file/native handles.

See Also:
  • Constructor Details

    • GitControl

      public GitControl(String localPath, String remotePath) throws IOException
      Constructor.

      Equivalent to GitControl(localPath, remotePath, null, null, false): no credentials (anonymous transport) and non-force push.

      Parameters:
      localPath - Local directory path for the repository.
      remotePath - Remote Git repository URI.
      Throws:
      IOException - If the repository cannot be opened.
    • GitControl

      public GitControl(String localPath, String remotePath, String username, String password, boolean forcePush) throws IOException
      Constructor.

      Credentials are supplied by the caller (typically from operator configuration or a secret store) rather than hardcoded. Pass null for username to use anonymous transport.

      Parameters:
      localPath - Local directory path for the repository.
      remotePath - Remote Git repository URI.
      username - Git username, or null for anonymous transport.
      password - Git password/token. Ignored when username is null.
      forcePush - Whether pushToRepo() performs a force-push (destructive remote history rewrite).
      Force-push is opt-in; defaults to false in the other constructors.
      Throws:
      IOException - If the repository cannot be opened.
    • GitControl

      public GitControl(String localPath, String remotePath, String username, String secretKey, BeanStore beanStore, boolean forcePush) throws IOException
      Constructor that resolves the Git password/token from a pluggable SecretStore.

      The active store is resolved from the supplied BeanStore via SecretStores.resolve(BeanStore), defaulting to an InMemorySecretStore when the bean store is null or contributes none. The password/token is looked up under secretKey as a char[] so it is never materialized as a String, and the credentials are zeroed by close(). Pass null for username to use anonymous transport (the secret store is not consulted in that case).

      Parameters:
      localPath - Local directory path for the repository.
      remotePath - Remote Git repository URI.
      username - Git username, or null for anonymous transport.
      secretKey - The key under which the password/token is stored in the resolved SecretStore.
      Ignored when username is null.
      beanStore - The bean store to resolve the SecretStore from. Can be null to use the default InMemorySecretStore.
      forcePush - Whether pushToRepo() performs a force-push (destructive remote history rewrite).
      Force-push is opt-in; defaults to false in the other constructors.
      Throws:
      IOException - If the repository cannot be opened.
  • Method Details

    • close

      public void close()
      Closes the underlying JGit Git and Repository handles.

      The Git instance wraps an externally-supplied Repository (it does not close it on Git.close()), so both are released explicitly here. Any credentials held by the provider are zeroed so a resolved secret does not linger in memory beyond the life of this instance.

      Specified by:
      close in interface AutoCloseable
    • addToRepo

      public void addToRepo() throws org.eclipse.jgit.api.errors.GitAPIException
      Stages all files for commit.
      Throws:
      org.eclipse.jgit.api.errors.GitAPIException - If staging fails.
    • branch

      public void branch(String name) throws org.eclipse.jgit.api.errors.GitAPIException
      Checks out the specified branch from origin.
      Parameters:
      name - Branch name. Must not be null.
      Throws:
      org.eclipse.jgit.api.errors.GitAPIException - If checkout fails.
    • cloneRepo

      public void cloneRepo() throws org.eclipse.jgit.api.errors.GitAPIException
      Clones the remote repository to the local path.
      Throws:
      org.eclipse.jgit.api.errors.GitAPIException - If clone fails.
    • commitToRepo

      public void commitToRepo(String message) throws org.eclipse.jgit.api.errors.JGitInternalException, org.eclipse.jgit.api.errors.GitAPIException
      Commits staged changes with the specified message.
      Parameters:
      message - Commit message.
      Throws:
      org.eclipse.jgit.api.errors.JGitInternalException - If an internal JGit error occurs.
      org.eclipse.jgit.api.errors.GitAPIException - If commit fails.
    • pullFromRepo

      public void pullFromRepo() throws org.eclipse.jgit.api.errors.GitAPIException
      Pulls the latest changes from the remote repository.
      Throws:
      org.eclipse.jgit.api.errors.GitAPIException - If pull fails.
    • pushToRepo

      public void pushToRepo() throws org.eclipse.jgit.api.errors.JGitInternalException, org.eclipse.jgit.api.errors.GitAPIException
      Pushes all committed changes to the remote repository.
      Throws:
      org.eclipse.jgit.api.errors.JGitInternalException - If an internal JGit error occurs.
      org.eclipse.jgit.api.errors.GitAPIException - If push fails.