use of org.eclipse.jgit.transport.RemoteConfig in project che by eclipse.
the class JGitConnection method remoteList.
@Override
public List<Remote> remoteList(String remoteName, boolean verbose) throws GitException {
StoredConfig config = repository.getConfig();
Set<String> remoteNames = new HashSet<>(config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE));
if (remoteName != null && remoteNames.contains(remoteName)) {
remoteNames.clear();
remoteNames.add(remoteName);
}
List<Remote> result = new ArrayList<>(remoteNames.size());
for (String remote : remoteNames) {
try {
List<URIish> uris = new RemoteConfig(config, remote).getURIs();
result.add(newDto(Remote.class).withName(remote).withUrl(uris.isEmpty() ? null : uris.get(0).toString()));
} catch (URISyntaxException exception) {
throw new GitException(exception.getMessage(), exception);
}
}
return result;
}
use of org.eclipse.jgit.transport.RemoteConfig in project che by eclipse.
the class JGitConnection method remoteAdd.
@Override
public void remoteAdd(RemoteAddParams params) throws GitException {
String remoteName = params.getName();
if (isNullOrEmpty(remoteName)) {
throw new GitException(ERROR_ADD_REMOTE_NAME_MISSING);
}
StoredConfig config = repository.getConfig();
Set<String> remoteNames = config.getSubsections("remote");
if (remoteNames.contains(remoteName)) {
throw new GitException(format(ERROR_ADD_REMOTE_NAME_ALREADY_EXISTS, remoteName));
}
String url = params.getUrl();
if (isNullOrEmpty(url)) {
throw new GitException(ERROR_ADD_REMOTE_URL_MISSING);
}
RemoteConfig remoteConfig;
try {
remoteConfig = new RemoteConfig(config, remoteName);
} catch (URISyntaxException exception) {
// Not happen since it is newly created remote.
throw new GitException(exception.getMessage(), exception);
}
try {
remoteConfig.addURI(new URIish(url));
} catch (URISyntaxException exception) {
throw new GitException("Remote url " + url + " is invalid. ");
}
List<String> branches = params.getBranches();
if (branches.isEmpty()) {
remoteConfig.addFetchRefSpec(new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*").setForceUpdate(true));
} else {
for (String branch : branches) {
remoteConfig.addFetchRefSpec(new RefSpec(Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remoteName + "/" + branch).setForceUpdate(true));
}
}
remoteConfig.update(config);
try {
config.save();
} catch (IOException exception) {
throw new GitException(exception.getMessage(), exception);
}
}
use of org.eclipse.jgit.transport.RemoteConfig in project camel by apache.
the class GitProducer method doRemoteAdd.
protected void doRemoteAdd(Exchange exchange, String operation) throws Exception {
if (ObjectHelper.isEmpty(endpoint.getRemoteName())) {
throw new IllegalArgumentException("Remote Name must be specified to execute " + operation);
}
if (ObjectHelper.isEmpty(endpoint.getRemotePath())) {
throw new IllegalArgumentException("Remote Path must be specified to execute " + operation);
}
RemoteConfig result = null;
try {
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setUri(new URIish(endpoint.getRemotePath()));
remoteAddCommand.setName(endpoint.getRemoteName());
result = remoteAddCommand.call();
} catch (Exception e) {
LOG.error("There was an error in Git " + operation + " operation");
throw e;
}
updateExchange(exchange, result);
}
use of org.eclipse.jgit.transport.RemoteConfig in project camel by apache.
the class GitProducerTest method remoteAddTest.
@Test
public void remoteAddTest() throws Exception {
Repository repository = getTestRepository();
File gitDir = new File(gitLocalRepo, ".git");
assertEquals(gitDir.exists(), true);
Git git = new Git(repository);
List<RemoteConfig> remoteConfigList = git.remoteList().call();
assertTrue(remoteConfigList.size() == 0);
Object result = template.requestBody("direct:remoteAdd", "");
assertTrue(result instanceof RemoteConfig);
RemoteConfig remoteConfig = (RemoteConfig) result;
remoteConfigList = git.remoteList().call();
assertTrue(remoteConfigList.size() == 1);
assertEquals(remoteConfigList.get(0).getName(), remoteConfig.getName());
assertEquals(remoteConfigList.get(0).getURIs(), remoteConfig.getURIs());
git.close();
}
use of org.eclipse.jgit.transport.RemoteConfig in project egit by eclipse.
the class ConfigureFetchAfterCloneTask method execute.
/**
* @param repository the cloned repository
* @param monitor
* @throws CoreException
*/
@Override
public void execute(Repository repository, IProgressMonitor monitor) throws CoreException {
try (Git git = new Git(repository)) {
RemoteConfig configToUse = new RemoteConfig(repository.getConfig(), remoteName);
if (fetchRefSpec != null) {
configToUse.addFetchRefSpec(new RefSpec(fetchRefSpec));
}
configToUse.update(repository.getConfig());
repository.getConfig().save();
git.fetch().setRemote(remoteName).call();
} catch (Exception e) {
Activator.logError(NLS.bind(CoreText.ConfigureFetchAfterCloneTask_couldNotFetch, fetchRefSpec), e);
throw new CoreException(Activator.error(e.getMessage(), e));
}
}
Aggregations