Class GitRepositoryHelper

java.lang.Object
org.craftercms.studio.api.v2.utils.GitRepositoryHelper
All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean

public class GitRepositoryHelper extends Object implements org.springframework.beans.factory.DisposableBean
  • Field Details

  • Constructor Details

    • GitRepositoryHelper

      public GitRepositoryHelper()
  • Method Details

    • destroy

      public void destroy() throws Exception
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
      Throws:
      Exception
    • getRepoCacheKey

      protected String getRepoCacheKey(String siteId, GitRepositories type)
    • getRepository

      public org.eclipse.jgit.lib.Repository getRepository(String siteId, GitRepositories gitRepository)
    • getRepository

      public org.eclipse.jgit.lib.Repository getRepository(String siteId, GitRepositories repoType, String sandboxBranch)
    • buildSiteRepo

      public boolean buildSiteRepo(String siteId)
    • buildRepoPath

      public Path buildRepoPath(GitRepositories repoType)
      Builds repository path
      Parameters:
      repoType - repository type
      Returns:
      repository path
    • buildRepoPath

      public Path buildRepoPath(GitRepositories repoType, String siteId)
      Builds repository path
      Parameters:
      repoType - repository type
      siteId - site Id (if empty it is global repository)
      Returns:
      repository path
    • openRepository

      public org.eclipse.jgit.lib.Repository openRepository(Path repositoryPath) throws IOException
      Opens a git repository
      Parameters:
      repositoryPath - path to repository to open (including .git)
      Returns:
      repository object if successful
      Throws:
      IOException - IO error
    • isRemoteValid

      public boolean isRemoteValid(org.eclipse.jgit.api.Git git, String remote, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey) throws org.craftercms.commons.crypto.CryptoException, IOException, ServiceLayerException, org.eclipse.jgit.api.errors.GitAPIException
      Throws:
      org.craftercms.commons.crypto.CryptoException
      IOException
      ServiceLayerException
      org.eclipse.jgit.api.errors.GitAPIException
    • setAuthenticationForCommand

      public void setAuthenticationForCommand(org.eclipse.jgit.api.TransportCommand<?,?> gitCommand, String authenticationType, String username, String password, String token, String privateKey, Path tempKey, boolean decrypt) throws org.craftercms.commons.crypto.CryptoException, ServiceLayerException, IOException
      Throws:
      org.craftercms.commons.crypto.CryptoException
      ServiceLayerException
      IOException
    • getGitPath

      public String getGitPath(String path)
    • getGitPaths

      public String[] getGitPaths(String... paths)
    • getAuthorIdent

      public org.eclipse.jgit.lib.PersonIdent getAuthorIdent(User user)
      Return the author identity as a jgit PersonIdent
      Parameters:
      user - author
      Returns:
      author user as a PersonIdent
    • getTreeForCommit

      public org.eclipse.jgit.revwalk.RevTree getTreeForCommit(org.eclipse.jgit.lib.Repository repository, String commitId) throws IOException
      Throws:
      IOException
    • getTreeForCommit

      public org.eclipse.jgit.revwalk.RevTree getTreeForCommit(org.eclipse.jgit.lib.Repository repository, org.eclipse.jgit.lib.ObjectId commitId) throws IOException
      Throws:
      IOException
    • getTreeForLastCommit

      public org.eclipse.jgit.revwalk.RevTree getTreeForLastCommit(org.eclipse.jgit.lib.Repository repository) throws IOException
      Throws:
      IOException
    • getDiffEntry

      public org.eclipse.jgit.diff.DiffEntry getDiffEntry(org.eclipse.jgit.lib.Repository repository, org.eclipse.jgit.lib.ObjectId commitId, String gitPath) throws NoChangesForPathException, IOException, org.eclipse.jgit.api.errors.GitAPIException
      Get the diff entry for a given commit and path. The result value corresponds to the diff found for the given path between the given commit and its (first) parent (or empty tree for initial commits).
      Parameters:
      repository - the repository
      commitId - the commit ID
      gitPath - the git path of the content item
      Returns:
      the diff entry
      Throws:
      NoChangesForPathException
      IOException
      org.eclipse.jgit.api.errors.GitAPIException
    • getFilesInCommit

      public List<String> getFilesInCommit(org.eclipse.jgit.lib.Repository repository, org.eclipse.jgit.revwalk.RevCommit commit)
    • createSandboxRepository

      public boolean createSandboxRepository(String site, String sandboxBranch)
      Create a site sandbox git repository from scratch
      Parameters:
      site - site to create
      sandboxBranch - sandbox branch name
      Returns:
      true if successful, false otherwise
    • createPublishedRepository

      public boolean createPublishedRepository(String siteId, String sandboxBranch)
      Create a site published git repository from scratch
      Parameters:
      siteId - site to create
      sandboxBranch - sandbox branch name
      Returns:
      true if successful, false otherwise
    • createGitRepository

      public org.eclipse.jgit.lib.Repository createGitRepository(Path path)
    • getCommitMessage

      public String getCommitMessage(String commitMessageKey)
    • copyContentFromBlueprint

      public boolean copyContentFromBlueprint(String blueprintLocation, String site)
    • updateSiteNameConfigVar

      public boolean updateSiteNameConfigVar(String site)
    • replaceSiteNameVariable

      protected boolean replaceSiteNameVariable(String site, Path path)
    • replaceParameters

      public boolean replaceParameters(String siteId, Map<String,String> parameters)
    • addGitIgnoreFiles

      public boolean addGitIgnoreFiles(String siteId)
    • performInitialCommit

      public boolean performInitialCommit(String site, String message, String sandboxBranch, String creator)
      Perform an initial commit after large changes to a site. Will not work against the global config repo.
      Parameters:
      site - site identifier
      message - commit message
      sandboxBranch - sandbox branch name
      creator - site creator
      Returns:
      true if successful, false otherwise
    • createSiteCloneRemoteGitRepo

      public boolean createSiteCloneRemoteGitRepo(String siteId, String sandboxBranch, String remoteName, String remoteUrl, String remoteBranch, boolean singleBranch, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey, boolean createAsOrphan, String creator) throws InvalidRemoteRepositoryException, InvalidRemoteRepositoryCredentialsException, RemoteRepositoryNotFoundException, ServiceLayerException
      Throws:
      InvalidRemoteRepositoryException
      InvalidRemoteRepositoryCredentialsException
      RemoteRepositoryNotFoundException
      ServiceLayerException
    • buildGlobalRepo

      public boolean buildGlobalRepo() throws IOException
      Build the global repository as part of system startup and caches it
      Returns:
      true if successful, false otherwise
      Throws:
      IOException - IO error
    • createGlobalRepo

      public boolean createGlobalRepo()
    • deleteSiteGitRepo

      public boolean deleteSiteGitRepo(String site)
    • writeFile

      public boolean writeFile(org.eclipse.jgit.lib.Repository repo, String site, String path, InputStream content)
    • addFiles

      public boolean addFiles(org.eclipse.jgit.lib.Repository repo, String site, String... paths)
    • commitFiles

      public String commitFiles(org.eclipse.jgit.lib.Repository repo, String site, String comment, org.eclipse.jgit.lib.PersonIdent user, String... paths)
      Commit files to a site SANDBOX git repository (or GLOBAL if site is empty)
      Parameters:
      repo - the repository
      site - the site
      comment - the commit message
      user - author of the commit
      paths - the paths to commit
      Returns:
      commit id
    • getCurrentUserIdent

      public org.eclipse.jgit.lib.PersonIdent getCurrentUserIdent() throws ServiceLayerException, UserNotFoundException
      Return the current user identity as a jgit PersonIdent
      Returns:
      current user as a PersonIdent
      Throws:
      ServiceLayerException - general service error
      UserNotFoundException - user not found
    • getAuthorIdent

      public org.eclipse.jgit.lib.PersonIdent getAuthorIdent(String author) throws ServiceLayerException, UserNotFoundException
      Return the author identity as a jgit PersonIdent
      Parameters:
      author - author
      Returns:
      author user as a PersonIdent
      Throws:
      ServiceLayerException - general service error
      UserNotFoundException - user not found error
    • getSandboxRepoLockKey

      public String getSandboxRepoLockKey(String site)
      Returns the key to use when locking Git operations for a site's sandbox repo
      Parameters:
      site - the site name
      Returns:
      the lock key to use with the lock service
    • getSandboxRepoLockKey

      public String getSandboxRepoLockKey(String site, boolean ifSiteEmptyUseGlobalRepoLockKey)
      Returns the key to use when locking Git operations for a site's sandbox or global repo
      Parameters:
      site - the site name
      ifSiteEmptyUseGlobalRepoLockKey - `true` to use global repo lock key if site empty
      Returns:
      the lock key to use with the lock service
    • getPublishedRepoLockKey

      public String getPublishedRepoLockKey(String site)
      Returns the key to use when locking Git operations for a site's published repo
      Parameters:
      site - the site name
      Returns:
      the lock key to use with the lock service
    • getStudioConfiguration

      public StudioConfiguration getStudioConfiguration()
    • setStudioConfiguration

      public void setStudioConfiguration(StudioConfiguration studioConfiguration)
    • setEncryptor

      public void setEncryptor(org.craftercms.commons.crypto.TextEncryptor encryptor)
    • getSecurityService

      public SecurityService getSecurityService()
    • setSecurityService

      public void setSecurityService(SecurityService securityService)
    • getUserServiceInternal

      public UserServiceInternal getUserServiceInternal()
    • setUserServiceInternal

      public void setUserServiceInternal(UserServiceInternal userServiceInternal)
    • getGeneralLockService

      public GeneralLockService getGeneralLockService()
    • setGeneralLockService

      public void setGeneralLockService(GeneralLockService generalLockService)
    • getRetryingRepositoryOperationFacade

      public RetryingRepositoryOperationFacade getRetryingRepositoryOperationFacade()
    • setRetryingRepositoryOperationFacade

      public void setRetryingRepositoryOperationFacade(RetryingRepositoryOperationFacade retryingRepositoryOperationFacade)
    • setAuthConfiguratorFactory

      public void setAuthConfiguratorFactory(org.craftercms.commons.git.utils.AuthConfiguratorFactory authConfiguratorFactory)
    • setGitCli

      public void setGitCli(GitCli gitCli)
    • setGitCliEnabled

      public void setGitCliEnabled(boolean gitCliEnabled)
    • checkoutBranch

      public void checkoutBranch(org.eclipse.jgit.lib.Repository repository, String sourceBranch, String targetBranch, boolean create) throws org.eclipse.jgit.api.errors.GitAPIException
      Checkout a branch, optionally creating it if it doesn't exist
      Parameters:
      repository - the repository
      sourceBranch - starting point of the branch to checkout
      targetBranch - the branch to checkout
      create - if the branch should be created if it doesn't exist
      Throws:
      org.eclipse.jgit.api.errors.GitAPIException - if an error occurs