Example usage for org.eclipse.jgit.transport RemoteConfig update

List of usage examples for org.eclipse.jgit.transport RemoteConfig update

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport RemoteConfig update.

Prototype

public void update(Config rc) 

Source Link

Document

Update this remote's definition within the configuration.

Usage

From source file:com.google.gct.idea.appengine.synchronization.SampleSyncTaskTest.java

License:Apache License

@Override
public void setUp() throws Exception {
    super.setUp();

    mockAndroidRepoPath = System.getProperty("java.io.tmpdir") + "/android/mockAndroidRepo";
    String mockGitHubRepoGitPath = db.getDirectory().getPath();
    mockGitHubRepoPath = mockGitHubRepoGitPath.substring(0, mockGitHubRepoGitPath.lastIndexOf('/'));

    // Configure the mock github repo
    StoredConfig targetConfig = db.getConfig();
    targetConfig.setString("branch", "master", "remote", "origin");
    targetConfig.setString("branch", "master", "merge", "refs/heads/master");

    RemoteConfig config = new RemoteConfig(targetConfig, "origin");
    config.addURI(new URIish(mockGitHubRepoGitPath));
    config.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
    config.update(targetConfig);

    targetConfig.save();/*from   w w w. ja  va  2  s . c o  m*/

    mockGitHubRepo = new Git(db);

    // commit something
    writeTrashFile("Test.txt", "Hello world");
    mockGitHubRepo.add().addFilepattern("Test.txt").call();
    mockGitHubRepo.commit().setMessage("Initial commit").call();
    mockGitHubRepo.tag().setName("tag-initial").setMessage("Tag initial").call();

}

From source file:com.netbeetle.reboot.git.CachedRepository.java

License:Apache License

public void init() throws IOException, URISyntaxException {
    if (exists()) {
        return;/*w w w .  j a  v  a2s.c  o m*/
    }

    repository.create(true);
    StoredConfig config = repository.getConfig();
    RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
    remoteConfig.addURI(new URIish(uri));
    remoteConfig.setMirror(true);
    remoteConfig.addFetchRefSpec(new RefSpec().setForceUpdate(true).setSourceDestination("refs/*", "refs/*"));
    remoteConfig.update(config);
    config.save();
}

From source file:com.rimerosolutions.ant.git.tasks.FetchTask.java

License:Apache License

@Override
public void doExecute() {
    try {//  w w  w. ja v  a 2  s.  co  m
        StoredConfig config = git.getRepository().getConfig();
        List<RemoteConfig> remoteConfigs = RemoteConfig.getAllRemoteConfigs(config);

        if (remoteConfigs.isEmpty()) {
            URIish uri = new URIish(getUri());

            RemoteConfig remoteConfig = new RemoteConfig(config, Constants.DEFAULT_REMOTE_NAME);
            remoteConfig.addURI(uri);
            remoteConfig.addFetchRefSpec(new RefSpec("+" + Constants.R_HEADS + "*:" + Constants.R_REMOTES
                    + Constants.DEFAULT_REMOTE_NAME + "/*"));

            config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
                    ConfigConstants.CONFIG_KEY_REMOTE, Constants.DEFAULT_REMOTE_NAME);
            config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
                    ConfigConstants.CONFIG_KEY_MERGE, Constants.R_HEADS + Constants.MASTER);

            remoteConfig.update(config);
            config.save();
        }

        List<RefSpec> specs = new ArrayList<RefSpec>(3);

        specs.add(new RefSpec(
                "+" + Constants.R_HEADS + "*:" + Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/*"));
        specs.add(new RefSpec("+" + Constants.R_NOTES + "*:" + Constants.R_NOTES + "*"));
        specs.add(new RefSpec("+" + Constants.R_TAGS + "*:" + Constants.R_TAGS + "*"));

        FetchCommand fetchCommand = git.fetch().setDryRun(dryRun).setThin(thinPack).setRemote(getUri())
                .setRefSpecs(specs).setRemoveDeletedRefs(removeDeletedRefs);

        setupCredentials(fetchCommand);

        if (getProgressMonitor() != null) {
            fetchCommand.setProgressMonitor(getProgressMonitor());
        }

        FetchResult fetchResult = fetchCommand.call();

        GitTaskUtils.validateTrackingRefUpdates(FETCH_FAILED_MESSAGE, fetchResult.getTrackingRefUpdates());

        log(fetchResult.getMessages());

    } catch (URISyntaxException e) {
        throw new GitBuildException("Invalid URI syntax: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new GitBuildException("Could not save or get repository configuration: " + e.getMessage(), e);
    } catch (InvalidRemoteException e) {
        throw new GitBuildException("Invalid remote URI: " + e.getMessage(), e);
    } catch (TransportException e) {
        throw new GitBuildException("Communication error: " + e.getMessage(), e);
    } catch (GitAPIException e) {
        throw new GitBuildException("Unexpected exception: " + e.getMessage(), e);
    }
}

From source file:com.rimerosolutions.ant.git.tasks.PushTask.java

License:Apache License

@Override
protected void doExecute() {
    try {//from w ww . j av  a  2 s. co m
        StoredConfig config = git.getRepository().getConfig();
        List<RemoteConfig> remoteConfigs = RemoteConfig.getAllRemoteConfigs(config);

        if (remoteConfigs.isEmpty()) {
            URIish uri = new URIish(getUri());

            RemoteConfig remoteConfig = new RemoteConfig(config, Constants.DEFAULT_REMOTE_NAME);

            remoteConfig.addURI(uri);
            remoteConfig.addFetchRefSpec(new RefSpec(DEFAULT_REFSPEC_STRING));
            remoteConfig.addPushRefSpec(new RefSpec(DEFAULT_REFSPEC_STRING));
            remoteConfig.update(config);

            config.save();
        }

        String currentBranch = git.getRepository().getBranch();
        List<RefSpec> specs = Arrays.asList(new RefSpec(currentBranch + ":" + currentBranch));

        if (deleteRemoteBranch != null) {
            specs = Arrays.asList(new RefSpec(":" + Constants.R_HEADS + deleteRemoteBranch));
        }

        PushCommand pushCommand = git.push().setPushAll().setRefSpecs(specs).setDryRun(false);

        if (getUri() != null) {
            pushCommand.setRemote(getUri());
        }

        setupCredentials(pushCommand);

        if (includeTags) {
            pushCommand.setPushTags();
        }

        if (getProgressMonitor() != null) {
            pushCommand.setProgressMonitor(getProgressMonitor());
        }

        Iterable<PushResult> pushResults = pushCommand.setForce(true).call();

        for (PushResult pushResult : pushResults) {
            log(pushResult.getMessages());
            GitTaskUtils.validateRemoteRefUpdates(PUSH_FAILED_MESSAGE, pushResult.getRemoteUpdates());
            GitTaskUtils.validateTrackingRefUpdates(PUSH_FAILED_MESSAGE, pushResult.getTrackingRefUpdates());
        }
    } catch (Exception e) {
        if (pushFailedProperty != null) {
            getProject().setProperty(pushFailedProperty, e.getMessage());
        }

        throw new GitBuildException(PUSH_FAILED_MESSAGE, e);
    }
}

From source file:com.tasktop.c2c.server.scm.service.GitServiceBean.java

License:Open Source License

@Secured({ Role.Admin })
@Override//from  w ww. ja v a2s .  c  o  m
public void addExternalRepository(String url) {
    try {
        String repoName = getRepoDirNameFromExternalUrl(url);
        File dir = new File(repositoryProvider.getTenantMirroredBaseDir(), repoName);
        Git git = Git.init().setBare(true).setDirectory(dir).call();
        RemoteConfig config = new RemoteConfig(git.getRepository().getConfig(), Constants.DEFAULT_REMOTE_NAME);
        config.addURI(new URIish(url));
        config.update(git.getRepository().getConfig());
        git.getRepository().getConfig().save();
    } catch (JGitInternalException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (GitAPIException e) {
        throw new RuntimeException(e);
    }

}

From source file:edu.wustl.lookingglass.community.CommunityRepository.java

License:Open Source License

private void addRemote(String newName, URL newURL) throws IOException, URISyntaxException {
    assert this.remoteName != null;
    assert this.remoteURL != null;

    boolean remoteExists = false;
    StoredConfig config = this.git.getRepository().getConfig();
    Set<String> remotes = config.getSubsections("remote");
    for (String oldName : remotes) {
        String oldURL = config.getString("remote", oldName, "url");
        if (newName.equals(oldName)) {
            remoteExists = true;//from www  . j a v  a 2s.  co  m
            if (newURL.toExternalForm().equals(oldURL)) {
                break;
            } else {
                Logger.warning("inconsistent remote url " + oldName + " : " + oldURL);
                config.setString("remote", oldName, "url", newURL.toExternalForm());
                config.save();
                break;
            }
        }
    }

    if (!remoteExists) {
        RemoteConfig remoteConfig = new RemoteConfig(config, this.remoteName);
        remoteConfig.addURI(new URIish(this.remoteURL));
        remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + this.remoteName + "/*"));
        remoteConfig.update(config);
        config.save();
    }
}

From source file:eu.cloud4soa.cli.roo.addon.commands.GitManager.java

License:Apache License

public void setConfig() throws URISyntaxException, IOException, GitAPIException {
    StoredConfig config = gitRepository.getConfig();

    if (config.getString("remote", repoName, "url") == null
            || config.getString("remote", repoName, "url") != gitProxyUrl) {
        config.unset("remote", repoName, "url");
        RemoteConfig remoteConfig = new RemoteConfig(config, repoName);
        //cloud@94.75.243.141/proxyname.git
        //proxy<userid><applicationid>.git?
        //        String gitUrl = gitUser+"@"+gitProxyUrl+"/proxy"+this.userId+this.applicationId+".git";
        URIish uri = new URIish(gitProxyUrl);
        remoteConfig.addURI(uri);/*from   w  w  w . j av a 2  s.c  om*/
        config.unset("remote", repoName, "fetch");
        RefSpec spec = new RefSpec("+refs/heads/*:refs/remotes/" + repoName + "/*");
        remoteConfig.addFetchRefSpec(spec);
        remoteConfig.update(config);
    }

    if (config.getString("branch", "master", "remote") == null
            || config.getString("branch", "master", "remote") != repoName) {
        config.unset("branch", "master", "remote");
        config.setString("branch", "master", "remote", repoName);
    }
    if (config.getString("branch", "master", "merge") == null
            || config.getString("branch", "master", "merge") != "refs/heads/master") {
        config.unset("branch", "master", "merge");
        config.setString("branch", "master", "merge", "refs/heads/master");
    }
    config.save();
}

From source file:net.erdfelt.android.sdkfido.git.internal.GitCloneCommand.java

License:Apache License

/**
 * Add a 'remote' configuration.//from  w ww.  j a  va  2  s . com
 * 
 * @param remoteName
 *            the name of the remote
 * @param uri
 *            the uri to the remote
 * @throws URISyntaxException
 *             if unable to process uri
 * @throws IOException
 *             if unable to add remote config
 */
private void addRemoteConfig(String remoteName, URIish uri) throws URISyntaxException, IOException {
    RemoteConfig rc = new RemoteConfig(repo.getConfig(), remoteName);
    rc.addURI(uri);

    String dest = Constants.R_HEADS + "*:" + Constants.R_REMOTES + remoteName + "/*";

    RefSpec refspec = new RefSpec(dest);
    refspec.setForceUpdate(true);
    rc.addFetchRefSpec(refspec);
    rc.update(repo.getConfig());
    repo.getConfig().save();
}

From source file:org.commonjava.gitwrap.BareGitRepository.java

License:Open Source License

protected final void doClone(final String remoteUrl, final String remoteName, final String branch)
        throws GitWrapException {
    final FileRepository repository = getRepository();

    final String branchRef = Constants.R_HEADS + (branch == null ? Constants.MASTER : branch);

    try {/*  www .  j  a  v a  2s. c o  m*/
        final RefUpdate head = repository.updateRef(Constants.HEAD);
        head.disableRefLog();
        head.link(branchRef);

        final RemoteConfig remoteConfig = new RemoteConfig(repository.getConfig(), remoteName);
        remoteConfig.addURI(new URIish(remoteUrl));

        final String remoteRef = Constants.R_REMOTES + remoteName;

        RefSpec spec = new RefSpec();
        spec = spec.setForceUpdate(true);
        spec = spec.setSourceDestination(Constants.R_HEADS + "*", remoteRef + "/*");

        remoteConfig.addFetchRefSpec(spec);

        remoteConfig.update(repository.getConfig());

        repository.getConfig().setString("branch", branch, "remote", remoteName);
        repository.getConfig().setString("branch", branch, "merge", branchRef);

        repository.getConfig().save();

        fetch(remoteName);
        postClone(remoteUrl, branchRef);
    } catch (final IOException e) {
        throw new GitWrapException("Failed to clone from: %s. Reason: %s", e, remoteUrl, e.getMessage());
    } catch (final URISyntaxException e) {
        throw new GitWrapException("Failed to clone from: %s. Reason: %s", e, remoteUrl, e.getMessage());
    }
}

From source file:org.commonjava.gitwrap.BareGitRepository.java

License:Open Source License

public BareGitRepository setPushTarget(final String name, final String uri, final boolean heads,
        final boolean tags) throws GitWrapException {
    try {/*  w w w. j a v  a  2 s. co m*/
        final RemoteConfig remote = new RemoteConfig(repository.getConfig(), name);

        final URIish uriish = new URIish(uri);

        final List<URIish> uris = remote.getURIs();
        if (uris == null || !uris.contains(uriish)) {
            remote.addURI(uriish);
        }

        final List<URIish> pushURIs = remote.getPushURIs();
        if (pushURIs == null || !pushURIs.contains(uriish)) {
            remote.addPushURI(uriish);
        }

        final List<RefSpec> pushRefSpecs = remote.getPushRefSpecs();
        if (heads) {
            final RefSpec headSpec = new RefSpec("+" + Constants.R_HEADS + "*:" + Constants.R_HEADS + "*");
            if (pushRefSpecs == null || !pushRefSpecs.contains(headSpec)) {
                remote.addPushRefSpec(headSpec);
            }
        }

        if (tags) {
            final RefSpec tagSpec = new RefSpec("+" + Constants.R_TAGS + "*:" + Constants.R_TAGS + "*");
            if (pushRefSpecs == null || !pushRefSpecs.contains(tagSpec)) {
                remote.addPushRefSpec(tagSpec);
            }
        }

        remote.update(repository.getConfig());
        repository.getConfig().save();
    } catch (final URISyntaxException e) {
        throw new GitWrapException("Invalid URI-ish: %s. Nested error: %s", e, uri, e.getMessage());
    } catch (final IOException e) {
        throw new GitWrapException("Failed to write Git config: %s", e, e.getMessage());
    }

    return this;
}