Class JGitUtils


  • public class JGitUtils
    extends java.lang.Object
    JGit utility functions.
    Since:
    1.9
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private JGitUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List<ScmFile> addAllFiles​(org.eclipse.jgit.api.Git git, ScmFileSet fileSet)
      Adds all files in the given fileSet to the repository.
      static void closeRepo​(org.eclipse.jgit.api.Git git)
      Closes the repository wrapped by the passed git object
      static org.eclipse.jgit.transport.CredentialsProvider getCredentials​(GitScmProviderRepository repository)
      Creates a credentials provider from the information passed in the repository.
      static java.util.List<ScmFile> getFilesInCommit​(org.eclipse.jgit.lib.Repository repository, org.eclipse.jgit.revwalk.RevCommit commit)
      get a list of all files in the given commit
      static org.eclipse.jgit.lib.ProgressMonitor getMonitor​(ScmLogger logger)
      Construct a logging ProgressMonitor for all JGit operations.
      static java.util.List<org.eclipse.jgit.revwalk.RevCommit> getRevCommits​(org.eclipse.jgit.lib.Repository repo, org.eclipse.jgit.revwalk.RevSort[] sortings, java.lang.String fromRev, java.lang.String toRev, java.util.Date fromDate, java.util.Date toDate, int maxLines)
      Get a list of commits between two revisions.
      static ScmFileStatus getScmFileStatus​(org.eclipse.jgit.diff.DiffEntry.ChangeType changeType)
      Translate a FileStatus in the matching ScmFileStatus.
      static boolean hasCommits​(org.eclipse.jgit.lib.Repository repo)
      Does the Repository have any commits?
      static org.eclipse.jgit.api.Git openRepo​(java.io.File basedir)
      Opens a JGit repository in the current directory or a parent directory.
      static org.eclipse.jgit.transport.CredentialsProvider prepareSession​(ScmLogger logger, org.eclipse.jgit.api.Git git, GitScmProviderRepository repository)
      Prepares the in memory configuration of git to connect to the configured repository.
      static java.lang.Iterable<org.eclipse.jgit.transport.PushResult> push​(ScmLogger logger, org.eclipse.jgit.api.Git git, GitScmProviderRepository repo, org.eclipse.jgit.transport.RefSpec refSpec)  
      private static java.lang.String relativize​(java.net.URI baseUri, java.io.File f)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JGitUtils

        private JGitUtils()
    • Method Detail

      • openRepo

        public static org.eclipse.jgit.api.Git openRepo​(java.io.File basedir)
                                                 throws java.io.IOException
        Opens a JGit repository in the current directory or a parent directory.
        Parameters:
        basedir - The directory to start with
        Throws:
        java.io.IOException - If the repository cannot be opened
      • closeRepo

        public static void closeRepo​(org.eclipse.jgit.api.Git git)
        Closes the repository wrapped by the passed git object
        Parameters:
        git -
      • getMonitor

        public static org.eclipse.jgit.lib.ProgressMonitor getMonitor​(ScmLogger logger)
        Construct a logging ProgressMonitor for all JGit operations.
        Parameters:
        logger -
        Returns:
        a ProgressMonitor for use
      • prepareSession

        public static org.eclipse.jgit.transport.CredentialsProvider prepareSession​(ScmLogger logger,
                                                                                    org.eclipse.jgit.api.Git git,
                                                                                    GitScmProviderRepository repository)
        Prepares the in memory configuration of git to connect to the configured repository. It configures the following settings in memory:
        • push url
        • fetch url

        Parameters:
        logger - used to log some details
        git - the instance to configure (only in memory, not saved)
        repository - the repo config to be used
        Returns:
        CredentialsProvider in case there are credentials informations configured in the repository.
      • getCredentials

        public static org.eclipse.jgit.transport.CredentialsProvider getCredentials​(GitScmProviderRepository repository)
        Creates a credentials provider from the information passed in the repository. Current implementation supports:
        • UserName/Password

        Parameters:
        repository - the config to get the details from
        Returns:
        null if there is not enough info to create a provider with
      • push

        public static java.lang.Iterable<org.eclipse.jgit.transport.PushResult> push​(ScmLogger logger,
                                                                                     org.eclipse.jgit.api.Git git,
                                                                                     GitScmProviderRepository repo,
                                                                                     org.eclipse.jgit.transport.RefSpec refSpec)
                                                                              throws org.eclipse.jgit.api.errors.GitAPIException,
                                                                                     org.eclipse.jgit.api.errors.InvalidRemoteException,
                                                                                     org.eclipse.jgit.api.errors.TransportException
        Throws:
        org.eclipse.jgit.api.errors.GitAPIException
        org.eclipse.jgit.api.errors.InvalidRemoteException
        org.eclipse.jgit.api.errors.TransportException
      • hasCommits

        public static boolean hasCommits​(org.eclipse.jgit.lib.Repository repo)
        Does the Repository have any commits?
        Parameters:
        repo -
        Returns:
        false if there are no commits
      • getFilesInCommit

        public static java.util.List<ScmFile> getFilesInCommit​(org.eclipse.jgit.lib.Repository repository,
                                                               org.eclipse.jgit.revwalk.RevCommit commit)
                                                        throws org.eclipse.jgit.errors.MissingObjectException,
                                                               org.eclipse.jgit.errors.IncorrectObjectTypeException,
                                                               org.eclipse.jgit.errors.CorruptObjectException,
                                                               java.io.IOException
        get a list of all files in the given commit
        Parameters:
        repository - the repo
        commit - the commit to get the files from
        Returns:
        a list of files included in the commit
        Throws:
        org.eclipse.jgit.errors.MissingObjectException
        org.eclipse.jgit.errors.IncorrectObjectTypeException
        org.eclipse.jgit.errors.CorruptObjectException
        java.io.IOException
      • getScmFileStatus

        public static ScmFileStatus getScmFileStatus​(org.eclipse.jgit.diff.DiffEntry.ChangeType changeType)
        Translate a FileStatus in the matching ScmFileStatus.
        Parameters:
        changeType -
        Returns:
        the matching ScmFileStatus
      • addAllFiles

        public static java.util.List<ScmFile> addAllFiles​(org.eclipse.jgit.api.Git git,
                                                          ScmFileSet fileSet)
                                                   throws org.eclipse.jgit.api.errors.GitAPIException,
                                                          org.eclipse.jgit.api.errors.NoFilepatternException
        Adds all files in the given fileSet to the repository.
        Parameters:
        git - the repo to add the files to
        fileSet - the set of files within the workspace, the files are added relative to the basedir of this fileset
        Returns:
        a list of added files
        Throws:
        org.eclipse.jgit.api.errors.GitAPIException
        org.eclipse.jgit.api.errors.NoFilepatternException
      • relativize

        private static java.lang.String relativize​(java.net.URI baseUri,
                                                   java.io.File f)
      • getRevCommits

        public static java.util.List<org.eclipse.jgit.revwalk.RevCommit> getRevCommits​(org.eclipse.jgit.lib.Repository repo,
                                                                                       org.eclipse.jgit.revwalk.RevSort[] sortings,
                                                                                       java.lang.String fromRev,
                                                                                       java.lang.String toRev,
                                                                                       java.util.Date fromDate,
                                                                                       java.util.Date toDate,
                                                                                       int maxLines)
                                                                                throws java.io.IOException,
                                                                                       org.eclipse.jgit.errors.MissingObjectException,
                                                                                       org.eclipse.jgit.errors.IncorrectObjectTypeException
        Get a list of commits between two revisions.
        Parameters:
        repo - the repository to work on
        sortings - sorting
        fromRev - start revision
        toRev - if null, falls back to head
        fromDate - from which date on
        toDate - until which date
        maxLines - max number of lines
        Returns:
        a list of commits, might be empty, but never null
        Throws:
        java.io.IOException
        org.eclipse.jgit.errors.MissingObjectException
        org.eclipse.jgit.errors.IncorrectObjectTypeException