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);
}
}
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);
}
}
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);
}
}
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();
}
}
Aggregations