Search in sources :

Example 1 with RemoteConfig

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;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) URIish(org.eclipse.jgit.transport.URIish) GitException(org.eclipse.che.api.git.exception.GitException) ArrayList(java.util.ArrayList) Remote(org.eclipse.che.api.git.shared.Remote) URISyntaxException(java.net.URISyntaxException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) HashSet(java.util.HashSet)

Example 2 with RemoteConfig

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);
    }
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) URIish(org.eclipse.jgit.transport.URIish) RefSpec(org.eclipse.jgit.transport.RefSpec) GitException(org.eclipse.che.api.git.exception.GitException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Example 3 with RemoteConfig

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);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) IOException(java.io.IOException)

Example 4 with RemoteConfig

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();
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) File(java.io.File) Test(org.junit.Test)

Example 5 with RemoteConfig

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));
    }
}
Also used : Git(org.eclipse.jgit.api.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) CoreException(org.eclipse.core.runtime.CoreException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) CoreException(org.eclipse.core.runtime.CoreException)

Aggregations

RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)63 URISyntaxException (java.net.URISyntaxException)23 StoredConfig (org.eclipse.jgit.lib.StoredConfig)21 URIish (org.eclipse.jgit.transport.URIish)21 IOException (java.io.IOException)16 RefSpec (org.eclipse.jgit.transport.RefSpec)14 Repository (org.eclipse.jgit.lib.Repository)13 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)7 Button (org.eclipse.swt.widgets.Button)6 Composite (org.eclipse.swt.widgets.Composite)6 File (java.io.File)5 Git (org.eclipse.jgit.api.Git)5 Ref (org.eclipse.jgit.lib.Ref)5 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)5 SelectionEvent (org.eclipse.swt.events.SelectionEvent)5 Label (org.eclipse.swt.widgets.Label)5 List (java.util.List)4 CoreException (org.eclipse.core.runtime.CoreException)4 UIText (org.eclipse.egit.ui.internal.UIText)4