Class GitControl
- All Implemented Interfaces:
AutoCloseable
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 Summary
ConstructorsConstructorDescriptionGitControl(String localPath, String remotePath) Constructor.GitControl(String localPath, String remotePath, String username, String password, boolean forcePush) Constructor.GitControl(String localPath, String remotePath, String username, String secretKey, BeanStore beanStore, boolean forcePush) Constructor that resolves the Git password/token from a pluggableSecretStore. -
Method Summary
Modifier and TypeMethodDescriptionvoidStages all files for commit.voidChecks out the specified branch from origin.voidClones the remote repository to the local path.voidclose()Closes the underlying JGitGitandRepositoryhandles.voidcommitToRepo(String message) Commits staged changes with the specified message.voidPulls the latest changes from the remote repository.voidPushes all committed changes to the remote repository.
-
Constructor Details
-
GitControl
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
nullforusernameto use anonymous transport.- Parameters:
localPath- Local directory path for the repository.remotePath- Remote Git repository URI.username- Git username, ornull for anonymous transport.password- Git password/token. Ignored whenusernameisnull .forcePush- WhetherpushToRepo()performs a force-push (destructive remote history rewrite).
Force-push is opt-in; defaults tofalse 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 pluggableSecretStore.The active store is resolved from the supplied
BeanStoreviaSecretStores.resolve(BeanStore), defaulting to anInMemorySecretStorewhen the bean store isnull or contributes none. The password/token is looked up undersecretKey as achar[]so it is never materialized as aString, and the credentials are zeroed byclose(). Passnull forusername 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, ornull for anonymous transport.secretKey- The key under which the password/token is stored in the resolvedSecretStore.
Ignored whenusername isnull .beanStore- The bean store to resolve theSecretStorefrom. Can benull to use the defaultInMemorySecretStore.forcePush- WhetherpushToRepo()performs a force-push (destructive remote history rewrite).
Force-push is opt-in; defaults tofalse in the other constructors.- Throws:
IOException- If the repository cannot be opened.
-
-
Method Details
-
close
Closes the underlying JGitGitandRepositoryhandles.The
Gitinstance wraps an externally-suppliedRepository(it does not close it onGit.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:
closein interfaceAutoCloseable
-
addToRepo
Stages all files for commit.- Throws:
org.eclipse.jgit.api.errors.GitAPIException- If staging fails.
-
branch
Checks out the specified branch from origin.- Parameters:
name- Branch name. Must not benull .- Throws:
org.eclipse.jgit.api.errors.GitAPIException- If checkout fails.
-
cloneRepo
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
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.GitAPIExceptionPushes 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.
-