Class GitUtils


  • public abstract class GitUtils
    extends Object
    Common operations related to git
    Since:
    4.0
    Author:
    joseross, avasquez
    • Constructor Detail

      • GitUtils

        public GitUtils()
    • 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:
        IOException
        org.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 remote
        remoteUrl - the URL of the remote. This should be a legal Git URL.
        branch - the branch which should be cloned
        authConfigurator - the GitAuthenticationConfigurator class used to configure the authentication with the remote repository
        localFolder - the local folder into which the remote repository should be cloned
        bigFileThreshold - the value of the Git core.bigFileThreshold config property
        compression - the value of the Git core.compression config property
        fileMode - the value of the Git core.fileMode config property
        Returns:
        the Git instance used to handle the cloned repository
        Throws:
        org.eclipse.jgit.api.errors.GitAPIException - if a Git related error occurs
        IOException - 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,
                                                           URISyntaxException
        Execute a Git pull.
        Parameters:
        git - the Git instance used to handle the repository
        remoteName - the name of the remote where to pull from
        remoteUrl - the URL of the remote (remote will be set to the URL)
        branch - the branch to pull
        mergeStrategy - the merge strategy to use
        authConfigurator - the GitAuthenticationConfigurator class 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 occurs
        URISyntaxException - 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 repository
        remote - remote name or URL
        pushAll - if the push should push all local branches
        remoteBranch - the remote remoteBranch being pushed to
        authConfigurator - the GitAuthenticationConfigurator class used to configure the authentication with the remote repository
        force - 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 command
        IOException - 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

        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