Search in sources :

Example 26 with RemoteConfig

use of org.eclipse.jgit.transport.RemoteConfig in project jbosstools-openshift by jbosstools.

the class EGitUtils method addRemoteTo.

/**
 * Adds the given uri of a remote repository to the given repository by the
 * given name.
 *
 * @param remoteName
 *            the name to use for the remote repository
 * @param uri
 *            the uri of the remote repository
 * @param repository
 *            the repository to add the remote to
 * @throws URISyntaxException
 *             the uRI syntax exception
 * @throws MalformedURLException
 *             the malformed url exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void addRemoteTo(String remoteName, URIish uri, Repository repository) throws URISyntaxException, MalformedURLException, IOException {
    StoredConfig config = repository.getConfig();
    RemoteConfig remoteConfig = new RemoteConfig(config, remoteName);
    remoteConfig.addURI(uri);
    remoteConfig.update(config);
    config.save();
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Example 27 with RemoteConfig

use of org.eclipse.jgit.transport.RemoteConfig in project jbosstools-openshift by jbosstools.

the class EGitUtilsTest method canGetFromSeveralRemoteConfig.

@Test
public void canGetFromSeveralRemoteConfig() throws CoreException, MalformedURLException, URISyntaxException, IOException {
    String repo2RemoteName = "repo2";
    testRepositoryClone.addRemoteTo(repo2RemoteName, testRepository2.getRepository());
    List<RemoteConfig> allRemoteConfigs = EGitUtils.getAllRemoteConfigs(testRepositoryClone.getRepository());
    assertNotNull(allRemoteConfigs);
    // clone already has repo1 as origin
    assertEquals(2, allRemoteConfigs.size());
    RemoteConfig repo2Config = EGitUtils.getRemoteConfig(repo2RemoteName, allRemoteConfigs);
    assertNotNull(repo2Config);
}
Also used : RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) Test(org.junit.Test)

Example 28 with RemoteConfig

use of org.eclipse.jgit.transport.RemoteConfig in project gitea-plugin by jenkinsci.

the class GiteaCreateSCMEvent method isMatch.

/**
 * {@inheritDoc}
 */
@Override
public boolean isMatch(@NonNull SCM scm) {
    URIish uri;
    try {
        uri = new URIish(getPayload().getRepository().getHtmlUrl());
    } catch (URISyntaxException e) {
        return false;
    }
    String ref = getPayload().getRef();
    ref = ref.startsWith(Constants.R_HEADS) ? ref.substring(Constants.R_HEADS.length()) : ref;
    if (scm instanceof GitSCM) {
        GitSCM git = (GitSCM) scm;
        if (git.getExtensions().get(IgnoreNotifyCommit.class) != null) {
            return false;
        }
        for (RemoteConfig repository : git.getRepositories()) {
            for (URIish remoteURL : repository.getURIs()) {
                if (GitStatus.looselyMatches(uri, remoteURL)) {
                    for (BranchSpec branchSpec : git.getBranches()) {
                        if (branchSpec.getName().contains("$")) {
                            // If the branchspec is parametrized, always run the polling
                            return true;
                        } else {
                            if (branchSpec.matches(repository.getName() + "/" + ref)) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) BranchSpec(hudson.plugins.git.BranchSpec) IgnoreNotifyCommit(hudson.plugins.git.extensions.impl.IgnoreNotifyCommit) URISyntaxException(java.net.URISyntaxException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) GitSCM(hudson.plugins.git.GitSCM)

Example 29 with RemoteConfig

use of org.eclipse.jgit.transport.RemoteConfig in project che by eclipse.

the class JGitConnection method remoteUpdate.

@Override
public void remoteUpdate(RemoteUpdateParams params) throws GitException {
    String remoteName = params.getName();
    if (isNullOrEmpty(remoteName)) {
        throw new GitException(ERROR_UPDATE_REMOTE_NAME_MISSING);
    }
    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE);
    if (!remoteNames.contains(remoteName)) {
        throw new GitException("Remote " + remoteName + " not found. ");
    }
    RemoteConfig remoteConfig;
    try {
        remoteConfig = new RemoteConfig(config, remoteName);
    } catch (URISyntaxException e) {
        throw new GitException(e.getMessage(), e);
    }
    List<String> branches = params.getBranches();
    if (!branches.isEmpty()) {
        if (!params.isAddBranches()) {
            remoteConfig.setFetchRefSpecs(Collections.emptyList());
            remoteConfig.setPushRefSpecs(Collections.emptyList());
        } else {
            // Replace wildcard refSpec if any.
            remoteConfig.removeFetchRefSpec(new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*").setForceUpdate(true));
            remoteConfig.removeFetchRefSpec(new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*"));
        }
        // Add new refSpec.
        for (String branch : branches) {
            remoteConfig.addFetchRefSpec(new RefSpec(Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remoteName + "/" + branch).setForceUpdate(true));
        }
    }
    // Remove URLs first.
    for (String url : params.getRemoveUrl()) {
        try {
            remoteConfig.removeURI(new URIish(url));
        } catch (URISyntaxException e) {
            LOG.debug(ERROR_UPDATE_REMOTE_REMOVE_INVALID_URL);
        }
    }
    // Add new URLs.
    for (String url : params.getAddUrl()) {
        try {
            remoteConfig.addURI(new URIish(url));
        } catch (URISyntaxException e) {
            throw new GitException("Remote url " + url + " is invalid. ");
        }
    }
    // Remove URLs for pushing.
    for (String url : params.getRemovePushUrl()) {
        try {
            remoteConfig.removePushURI(new URIish(url));
        } catch (URISyntaxException e) {
            LOG.debug(ERROR_UPDATE_REMOTE_REMOVE_INVALID_URL);
        }
    }
    // Add URLs for pushing.
    for (String url : params.getAddPushUrl()) {
        try {
            remoteConfig.addPushURI(new URIish(url));
        } catch (URISyntaxException e) {
            throw new GitException("Remote push url " + url + " is invalid. ");
        }
    }
    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 30 with RemoteConfig

use of org.eclipse.jgit.transport.RemoteConfig in project camel by apache.

the class GitProducerTest method remoteListTest.

@Test
public void remoteListTest() throws Exception {
    Repository repository = getTestRepository();
    File gitDir = new File(gitLocalRepo, ".git");
    assertEquals(gitDir.exists(), true);
    Git git = new Git(repository);
    RemoteAddCommand remoteAddCommand = git.remoteAdd();
    remoteAddCommand.setName("origin");
    remoteAddCommand.setUri(new URIish(remoteUriTest));
    remoteAddCommand.call();
    List<RemoteConfig> gitRemoteConfigs = git.remoteList().call();
    Object result = template.requestBody("direct:remoteList", "");
    assertTrue(result instanceof List);
    List<RemoteConfig> remoteConfigs = (List<RemoteConfig>) result;
    assertEquals(gitRemoteConfigs.size(), remoteConfigs.size());
    assertEquals(gitRemoteConfigs.get(0).getName(), remoteConfigs.get(0).getName());
    assertEquals(gitRemoteConfigs.get(0).getURIs(), remoteConfigs.get(0).getURIs());
    git.close();
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) List(java.util.List) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) File(java.io.File) Test(org.junit.Test)

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