Package org.craftercms.commons.git.utils
Class GitUtils
- java.lang.Object
-
- org.craftercms.commons.git.utils.GitUtils
-
public abstract class GitUtils extends Object
Common operations related to git- Since:
- 4.0
- Author:
- joseross, avasquez
-
-
Field Summary
Fields Modifier and Type Field Description static StringBIG_FILE_THRESHOLD_CONFIG_PARAMstatic StringBIG_FILE_THRESHOLD_DEFAULTstatic StringCOMPRESSION_CONFIG_PARAMstatic intCOMPRESSION_DEFAULTstatic StringCORE_CONFIG_SECTIONstatic StringFILE_MODE_CONFIG_PARAMstatic booleanFILE_MODE_DEFAULTstatic StringGIT_FOLDER_NAMEstatic StringGIT_INDEX_NAMEstatic StringGIT_LOCK_NAME
-
Constructor Summary
Constructors Constructor Description GitUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcleanup(String repoPath)Executes 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, 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(String repoPath)Unlock a git repository by deleting the `.lock` file
-
-
-
Field Detail
-
GIT_FOLDER_NAME
public static final String GIT_FOLDER_NAME
- See Also:
- Constant Field Values
-
GIT_INDEX_NAME
public static final String GIT_INDEX_NAME
- See Also:
- Constant Field Values
-
GIT_LOCK_NAME
public static final String GIT_LOCK_NAME
- See Also:
- Constant Field Values
-
CORE_CONFIG_SECTION
public static final String CORE_CONFIG_SECTION
- See Also:
- Constant Field Values
-
BIG_FILE_THRESHOLD_CONFIG_PARAM
public static final String BIG_FILE_THRESHOLD_CONFIG_PARAM
- See Also:
- Constant Field Values
-
COMPRESSION_CONFIG_PARAM
public static final String COMPRESSION_CONFIG_PARAM
- See Also:
- Constant Field Values
-
FILE_MODE_CONFIG_PARAM
public static final String FILE_MODE_CONFIG_PARAM
- See Also:
- Constant Field Values
-
BIG_FILE_THRESHOLD_DEFAULT
public static final String BIG_FILE_THRESHOLD_DEFAULT
- See Also:
- Constant Field Values
-
COMPRESSION_DEFAULT
public static final int COMPRESSION_DEFAULT
- See Also:
- Constant Field Values
-
FILE_MODE_DEFAULT
public static final boolean FILE_MODE_DEFAULT
- See Also:
- Constant Field Values
-
-
Method Detail
-
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, GitAuthenticationConfigurator authConfigurator) throws org.eclipse.jgit.api.errors.GitAPIException, URISyntaxExceptionExecute 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.GitAPIExceptionExecutes 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
public static boolean isRepositoryLocked(String repoPath)
Check if a git repository is locked- Parameters:
repoPath- path to repository- Returns:
- true if locked, false otherwise
-
unlock
public static void unlock(String repoPath) throws IOException
Unlock a git repository by deleting the `.lock` file- Parameters:
repoPath- path to repository- Throws:
IOException
-
deleteGitIndex
public static void deleteGitIndex(String repoPath) throws IOException
Delete the git index for a given git repository (used to fix a broken repo)- Parameters:
repoPath- path to repository- Throws:
IOException
-
deleteFile
protected static void deleteFile(Path file) throws IOException
Force delete a file on disk- Parameters:
file- path to file- Throws:
IOException
-
-