Search in sources :

Example 1 with RemoteSetUrlCommand

use of org.eclipse.jgit.api.RemoteSetUrlCommand in project studio by craftercms.

the class StudioClusterGlobalRepoSyncTask method addRemoteRepository.

protected void addRemoteRepository(ClusterMember member, String remoteUrl) throws IOException, InvalidRemoteUrlException, ServiceLayerException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(Paths.get(studioConfiguration.getProperty(StudioConfiguration.REPO_BASE_PATH), studioConfiguration.getProperty(StudioConfiguration.GLOBAL_REPO_PATH)).resolve(GIT_ROOT).toFile()).readEnvironment().findGitDir().build();
    try (Git git = new Git(repo)) {
        Config storedConfig = repo.getConfig();
        Set<String> remotes = storedConfig.getSubsections(CONFIG_SECTION_REMOTE);
        if (remotes.contains(member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""))) {
            try {
                studioClusterUtils.removeRemote(git, member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""));
            } catch (GitAPIException e) {
                logger.debug("Error while cleaning remote repositories for global repo", e);
            }
        }
        if (remotes.contains(member.getGitRemoteName())) {
            logger.debug("Remote " + member.getGitRemoteName() + " already exists for global repo");
            String storedRemoteUrl = storedConfig.getString(CONFIG_SECTION_REMOTE, member.getGitRemoteName(), CONFIG_PARAMETER_URL);
            if (!StringUtils.equals(storedRemoteUrl, remoteUrl)) {
                RemoteSetUrlCommand remoteSetUrlCommand = git.remoteSetUrl();
                remoteSetUrlCommand.setName(member.getGitRemoteName());
                remoteSetUrlCommand.setUri(new URIish(remoteUrl));
                remoteSetUrlCommand.call();
            }
        } else {
            logger.debug("Add " + member.getLocalAddress() + " as remote to sandbox");
            RemoteAddCommand remoteAddCommand = git.remoteAdd();
            remoteAddCommand.setName(member.getGitRemoteName());
            remoteAddCommand.setUri(new URIish(remoteUrl));
            remoteAddCommand.call();
        }
    } catch (URISyntaxException e) {
        logger.error("Remote URL is invalid " + remoteUrl, e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException e) {
        logger.error("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for global repo", e);
        throw new ServiceLayerException("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for global repo", e);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Config(org.eclipse.jgit.lib.Config) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) URISyntaxException(java.net.URISyntaxException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand)

Example 2 with RemoteSetUrlCommand

use of org.eclipse.jgit.api.RemoteSetUrlCommand in project studio by craftercms.

the class StudioClusterPublishedRepoSyncTask method addRemoteRepository.

protected void addRemoteRepository(String siteId, ClusterMember member, String remoteUrl) throws IOException, InvalidRemoteUrlException, ServiceLayerException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(buildRepoPath(siteId).resolve(GIT_ROOT).toFile()).readEnvironment().findGitDir().build();
    try (Git git = new Git(repo)) {
        Config storedConfig = repo.getConfig();
        Set<String> remotes = storedConfig.getSubsections(CONFIG_SECTION_REMOTE);
        if (remotes.contains(member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""))) {
            try {
                removeRemote(git, member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""));
            } catch (GitAPIException e) {
                logger.debug("Error while cleaning up remote repository for site " + siteId, e);
            }
        }
        if (remotes.contains(member.getGitRemoteName())) {
            logger.debug("Remote " + member.getGitRemoteName() + " already exists for PUBLISHED repo for site " + siteId);
            String storedRemoteUrl = storedConfig.getString(CONFIG_SECTION_REMOTE, member.getGitRemoteName(), CONFIG_PARAMETER_URL);
            if (!StringUtils.equals(storedRemoteUrl, remoteUrl)) {
                RemoteSetUrlCommand remoteSetUrlCommand = git.remoteSetUrl();
                remoteSetUrlCommand.setName(member.getGitRemoteName());
                remoteSetUrlCommand.setUri(new URIish(remoteUrl));
                remoteSetUrlCommand.call();
            }
        } else {
            logger.debug("Add " + member.getLocalAddress() + " as remote to PUBLISHED");
            RemoteAddCommand remoteAddCommand = git.remoteAdd();
            remoteAddCommand.setName(member.getGitRemoteName());
            remoteAddCommand.setUri(new URIish(remoteUrl));
            remoteAddCommand.call();
        }
    } catch (URISyntaxException e) {
        logger.error("Remote URL is invalid " + remoteUrl, e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException e) {
        logger.error("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
        throw new ServiceLayerException("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Config(org.eclipse.jgit.lib.Config) ServicesConfig(org.craftercms.studio.api.v1.service.configuration.ServicesConfig) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) URISyntaxException(java.net.URISyntaxException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand)

Example 3 with RemoteSetUrlCommand

use of org.eclipse.jgit.api.RemoteSetUrlCommand in project studio by craftercms.

the class StudioClusterSandboxRepoSyncTask method addRemoteRepository.

private void addRemoteRepository(String siteId, ClusterMember member, String remoteUrl) throws IOException, InvalidRemoteUrlException, ServiceLayerException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(buildRepoPath(siteId).resolve(GIT_ROOT).toFile()).readEnvironment().findGitDir().build();
    try (Git git = new Git(repo)) {
        Config storedConfig = repo.getConfig();
        Set<String> remotes = storedConfig.getSubsections(CONFIG_SECTION_REMOTE);
        if (remotes.contains(member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""))) {
            try {
                removeRemote(git, member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""));
            } catch (GitAPIException e) {
                logger.debug("Error while cleaning up remote repository for site " + siteId, e);
            }
        }
        if (remotes.contains(member.getGitRemoteName())) {
            logger.debug("Remote " + member.getGitRemoteName() + " already exists for SANDBOX repo for site " + siteId);
            String storedRemoteUrl = storedConfig.getString(CONFIG_SECTION_REMOTE, member.getGitRemoteName(), CONFIG_PARAMETER_URL);
            if (!StringUtils.equals(storedRemoteUrl, remoteUrl)) {
                RemoteSetUrlCommand remoteSetUrlCommand = git.remoteSetUrl();
                remoteSetUrlCommand.setName(member.getGitRemoteName());
                remoteSetUrlCommand.setUri(new URIish(remoteUrl));
                remoteSetUrlCommand.call();
            }
        } else {
            logger.debug("Add " + member.getLocalAddress() + " as remote to SANDBOX");
            RemoteAddCommand remoteAddCommand = git.remoteAdd();
            remoteAddCommand.setName(member.getGitRemoteName());
            remoteAddCommand.setUri(new URIish(remoteUrl));
            remoteAddCommand.call();
        }
    } catch (URISyntaxException e) {
        logger.error("Remote URL is invalid " + remoteUrl, e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException e) {
        logger.error("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
        throw new ServiceLayerException("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Config(org.eclipse.jgit.lib.Config) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) URISyntaxException(java.net.URISyntaxException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand)

Example 4 with RemoteSetUrlCommand

use of org.eclipse.jgit.api.RemoteSetUrlCommand in project spring-cloud-contract by spring-cloud.

the class AbstractGitTest method setOriginOnProjectToTmp.

void setOriginOnProjectToTmp(File origin, File project, boolean push) throws GitAPIException, IOException, URISyntaxException {
    try (Git git = openGitProject(project)) {
        RemoteRemoveCommand remove = git.remoteRemove();
        remove.setRemoteName("origin");
        remove.call();
        RemoteSetUrlCommand command = git.remoteSetUrl();
        command.setRemoteUri(new URIish(origin.toURI().toURL()));
        command.setRemoteName("origin");
        command.setUriType(RemoteSetUrlCommand.UriType.PUSH);
        command.call();
        StoredConfig config = git.getRepository().getConfig();
        RemoteConfig originConfig = new RemoteConfig(config, "origin");
        originConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
        originConfig.update(config);
        config.save();
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) StoredConfig(org.eclipse.jgit.lib.StoredConfig) Git(org.eclipse.jgit.api.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand) RemoteRemoveCommand(org.eclipse.jgit.api.RemoteRemoveCommand) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Aggregations

Git (org.eclipse.jgit.api.Git)4 RemoteSetUrlCommand (org.eclipse.jgit.api.RemoteSetUrlCommand)4 URIish (org.eclipse.jgit.transport.URIish)4 URISyntaxException (java.net.URISyntaxException)3 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)3 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)3 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)3 RemoteAddCommand (org.eclipse.jgit.api.RemoteAddCommand)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 Config (org.eclipse.jgit.lib.Config)3 Repository (org.eclipse.jgit.lib.Repository)3 FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)3 ServicesConfig (org.craftercms.studio.api.v1.service.configuration.ServicesConfig)1 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)1 RemoteRemoveCommand (org.eclipse.jgit.api.RemoteRemoveCommand)1 StoredConfig (org.eclipse.jgit.lib.StoredConfig)1 RefSpec (org.eclipse.jgit.transport.RefSpec)1 RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)1