Package org.craftercms.commons.git.utils
Class GitUtils
java.lang.Object
org.craftercms.commons.git.utils.GitUtils
Common operations related to git
- Since:
- 4.0
- Author:
- joseross, avasquez
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidExecutes a git gc.static org.eclipse.jgit.api.GitcloneRemoteRepository(String remoteName, String remoteUrl, String branch, GitAuthenticationConfigurator authConfigurator, File localFolder, String bigFileThreshold, Integer compression, Boolean fileMode) Clones a remote repository into a specific local folder.protected static voiddeleteFile(Path file) Force delete a file on diskstatic voiddeleteGitIndex(String repoPath) Delete the git index for a given git repository (used to fix a broken repo)static voiddiscardAllChanges(org.eclipse.jgit.api.Git git) Executes a git reset to discard all uncommitted changesstatic List<org.eclipse.jgit.diff.DiffEntry> doDiff(org.eclipse.jgit.api.Git git, org.eclipse.jgit.lib.ObjectReader reader, org.eclipse.jgit.lib.ObjectId fromCommitId, org.eclipse.jgit.lib.ObjectId toCommitId) static org.eclipse.jgit.revwalk.RevTreegetTreeForCommit(org.eclipse.jgit.lib.Repository repo, org.eclipse.jgit.lib.ObjectId commitId) static org.eclipse.jgit.treewalk.AbstractTreeIteratorgetTreeIteratorForCommit(org.eclipse.jgit.api.Git git, org.eclipse.jgit.lib.ObjectReader reader, org.eclipse.jgit.lib.ObjectId commitId) static booleanisRepositoryLocked(String repoPath) Check if a git repository is lockedstatic org.eclipse.jgit.api.GitopenRepository(File localRepositoryFolder) Opens the Git repository at the specified location.static org.eclipse.jgit.api.PullResultpull(org.eclipse.jgit.api.Git git, String remoteName, String remoteUrl, String branch, org.eclipse.jgit.merge.MergeStrategy mergeStrategy, org.eclipse.jgit.merge.ContentMergeStrategy contentMergeStrategy, org.eclipse.jgit.api.MergeCommand.FastForwardMode fastForwardMode, GitAuthenticationConfigurator authConfigurator) Execute a Git pull.static Iterable<org.eclipse.jgit.transport.PushResult> push(org.eclipse.jgit.api.Git git, String remote, boolean pushAll, String localBranch, String remoteBranch, GitAuthenticationConfigurator authConfigurator, boolean force) Executes a git push.static voidUnlock a git repository by deleting the `.lock` file
-
Field Details
-
GIT_FOLDER_NAME
- See Also:
-
GIT_INDEX_NAME
- See Also:
-
GIT_LOCK_NAME
- See Also:
-
CORE_CONFIG_SECTION
- See Also:
-
BIG_FILE_THRESHOLD_CONFIG_PARAM
- See Also:
-
COMPRESSION_CONFIG_PARAM
- See Also:
-
FILE_MODE_CONFIG_PARAM
- See Also:
-
BIG_FILE_THRESHOLD_DEFAULT
- See Also:
-
COMPRESSION_DEFAULT
public static final int COMPRESSION_DEFAULT- See Also:
-
FILE_MODE_DEFAULT
public static final boolean FILE_MODE_DEFAULT- See Also:
-
-
Constructor Details
-
GitUtils
public GitUtils()
-
-
Method Details
-
doDiff
public static List<org.eclipse.jgit.diff.DiffEntry> doDiff(org.eclipse.jgit.api.Git git, org.eclipse.jgit.lib.ObjectReader reader, org.eclipse.jgit.lib.ObjectId fromCommitId, org.eclipse.jgit.lib.ObjectId toCommitId) throws IOException, org.eclipse.jgit.api.errors.GitAPIException - Throws:
IOExceptionorg.eclipse.jgit.api.errors.GitAPIException
-
getTreeIteratorForCommit
public static org.eclipse.jgit.treewalk.AbstractTreeIterator getTreeIteratorForCommit(org.eclipse.jgit.api.Git git, org.eclipse.jgit.lib.ObjectReader reader, org.eclipse.jgit.lib.ObjectId commitId) throws IOException - Throws:
IOException
-
getTreeForCommit
public static org.eclipse.jgit.revwalk.RevTree getTreeForCommit(org.eclipse.jgit.lib.Repository repo, org.eclipse.jgit.lib.ObjectId commitId) throws IOException - Throws:
IOException
-
openRepository
public static org.eclipse.jgit.api.Git openRepository(File localRepositoryFolder) throws IOException Opens the Git repository at the specified location.- Parameters:
localRepositoryFolder- the folder where the Git repository is- Returns:
- the Git instance used to handle the repository
- Throws:
IOException- if an error occurs
-
cloneRemoteRepository
public static org.eclipse.jgit.api.Git cloneRemoteRepository(String remoteName, String remoteUrl, String branch, GitAuthenticationConfigurator authConfigurator, File localFolder, String bigFileThreshold, Integer compression, Boolean fileMode) throws org.eclipse.jgit.api.errors.GitAPIException, IOException Clones a remote repository into a specific local folder.- Parameters:
remoteName- the name of the remoteremoteUrl- the URL of the remote. This should be a legal Git URL.branch- the branch which should be clonedauthConfigurator- theGitAuthenticationConfiguratorclass used to configure the authentication with the remote repositorylocalFolder- the local folder into which the remote repository should be clonedbigFileThreshold- the value of the Gitcore.bigFileThresholdconfig propertycompression- the value of the Gitcore.compressionconfig propertyfileMode- the value of the Gitcore.fileModeconfig property- Returns:
- the Git instance used to handle the cloned repository
- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if a Git related error occursIOException- if an IO error occurs
-
pull
public static org.eclipse.jgit.api.PullResult pull(org.eclipse.jgit.api.Git git, String remoteName, String remoteUrl, String branch, org.eclipse.jgit.merge.MergeStrategy mergeStrategy, org.eclipse.jgit.merge.ContentMergeStrategy contentMergeStrategy, org.eclipse.jgit.api.MergeCommand.FastForwardMode fastForwardMode, GitAuthenticationConfigurator authConfigurator) throws org.eclipse.jgit.api.errors.GitAPIException, URISyntaxException Execute a Git pull.- Parameters:
git- the Git instance used to handle the repositoryremoteName- the name of the remote where to pull fromremoteUrl- the URL of the remote (remote will be set to the URL)branch- the branch to pullmergeStrategy- the merge strategy to useauthConfigurator- theGitAuthenticationConfiguratorclass used to configure the authentication with the remote repository- Returns:
- the result of the pull
- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if a Git related error occursURISyntaxException- if the remote URL is invalid
-
push
public static Iterable<org.eclipse.jgit.transport.PushResult> push(org.eclipse.jgit.api.Git git, String remote, boolean pushAll, String localBranch, String remoteBranch, GitAuthenticationConfigurator authConfigurator, boolean force) throws org.eclipse.jgit.api.errors.GitAPIException Executes a git push.- Parameters:
git- the Git instance used to handle the repositoryremote- remote name or URLpushAll- if the push should push all local branchesremoteBranch- the remote remoteBranch being pushed toauthConfigurator- theGitAuthenticationConfiguratorclass used to configure the authentication with the remote repositoryforce- sets the force preference for the push- Returns:
- the result of the push
- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if a Git related error occurs
-
cleanup
public static void cleanup(String repoPath) throws org.eclipse.jgit.api.errors.GitAPIException, IOException Executes a git gc.- Parameters:
repoPath- full path of the repository- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if there is an error running the commandIOException- if there is an error opening the repository
-
discardAllChanges
public static void discardAllChanges(org.eclipse.jgit.api.Git git) throws org.eclipse.jgit.api.errors.GitAPIException Executes a git reset to discard all uncommitted changes- Parameters:
git- the git repository- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if there is an error performing the reset
-
isRepositoryLocked
Check if a git repository is locked- Parameters:
repoPath- path to repository- Returns:
- true if locked, false otherwise
-
unlock
Unlock a git repository by deleting the `.lock` file- Parameters:
repoPath- path to repository- Throws:
IOException
-
deleteGitIndex
Delete the git index for a given git repository (used to fix a broken repo)- Parameters:
repoPath- path to repository- Throws:
IOException
-
deleteFile
Force delete a file on disk- Parameters:
file- path to file- Throws:
IOException
-