Example usage for org.eclipse.jgit.transport RefSpec RefSpec

List of usage examples for org.eclipse.jgit.transport RefSpec RefSpec

Introduction

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

Prototype

public RefSpec() 

Source Link

Document

Construct an empty RefSpec.

Usage

From source file:ch.sbb.releasetrain.git.GitRepoImpl.java

License:Apache License

/**
 * Use with care!// w  w  w  .  j  a v  a2s.com
 */
public boolean deleteBranch() {
    if (!branch.startsWith("feature/")) {
        throw new GitException("Can only delete feature branch.");
    }
    try {

        final Git git = gitOpen();
        git.checkout().setCreateBranch(true).setName("feature/temp_" + UUID.randomUUID()).call();
        List<String> deletedBranches = git.branchDelete().setBranchNames(branch).setForce(true).call();
        if (deletedBranches.size() == 1) {
            // delete branch 'branchToDelete' on remote 'origin'
            RefSpec refSpec = new RefSpec().setSource(null).setDestination("refs/heads/" + branch);
            Iterable<PushResult> results = git.push().setCredentialsProvider(credentialsProvider())
                    .setRefSpecs(refSpec).setRemote("origin").call();
            for (PushResult result : results) {
                RemoteRefUpdate myUpdate = result.getRemoteUpdate("refs/heads/" + branch);
                if (myUpdate.isDelete() && myUpdate.getStatus() == RemoteRefUpdate.Status.OK) {
                    return true;
                }
            }
        }
        return false;
    } catch (IOException | GitAPIException e) {
        throw new GitException("Delete branch failed", e);
    }
}

From source file:com.gitblit.tests.TicketReferenceTest.java

License:Apache License

private void assertDeleteBranch(String branchName) throws Exception {

    RefSpec refSpec = new RefSpec().setSource(null).setDestination("refs/heads/" + branchName);

    Iterable<PushResult> results = git.push().setRefSpecs(refSpec).setRemote("origin")
            .setCredentialsProvider(cp).call();

    for (PushResult result : results) {
        RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/" + branchName);
        assertEquals(Status.OK, ref.getStatus());
    }/*from  w  ww.j  ava2  s . c  om*/
}

From source file:com.google.gerrit.server.git.PushReplication.java

License:Apache License

private List<ReplicationConfig> allConfigs(final SitePaths site) throws ConfigInvalidException, IOException {
    final FileBasedConfig cfg = new FileBasedConfig(site.replication_config, FS.DETECTED);

    if (!cfg.getFile().exists()) {
        log.warn("No " + cfg.getFile() + "; not replicating");
        return Collections.emptyList();
    }//ww  w  .  j a  va  2 s .c  om
    if (cfg.getFile().length() == 0) {
        log.info("Empty " + cfg.getFile() + "; not replicating");
        return Collections.emptyList();
    }

    try {
        cfg.load();
    } catch (ConfigInvalidException e) {
        throw new ConfigInvalidException("Config file " + cfg.getFile() + " is invalid: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new IOException("Cannot read " + cfg.getFile() + ": " + e.getMessage(), e);
    }

    final List<ReplicationConfig> r = new ArrayList<ReplicationConfig>();
    for (final RemoteConfig c : allRemotes(cfg)) {
        if (c.getURIs().isEmpty()) {
            continue;
        }

        for (final URIish u : c.getURIs()) {
            if (u.getPath() == null || !u.getPath().contains("${name}")) {
                throw new ConfigInvalidException("remote." + c.getName() + ".url" + " \"" + u
                        + "\" lacks ${name} placeholder in " + cfg.getFile());
            }
        }

        // In case if refspec destination for push is not set then we assume it is
        // equal to source
        for (RefSpec ref : c.getPushRefSpecs()) {
            if (ref.getDestination() == null) {
                ref.setDestination(ref.getSource());
            }
        }

        if (c.getPushRefSpecs().isEmpty()) {
            RefSpec spec = new RefSpec();
            spec = spec.setSourceDestination("refs/*", "refs/*");
            spec = spec.setForceUpdate(true);
            c.addPushRefSpec(spec);
        }

        r.add(new ReplicationConfig(injector, workQueue, c, cfg, database, replicationUserFactory));
    }
    return Collections.unmodifiableList(r);
}

From source file:com.googlesource.gerrit.plugins.github.replication.GitHubDestinations.java

License:Apache License

private List<Destination> getDestinations(File cfgPath) throws ConfigInvalidException, IOException {
    FileBasedConfig cfg = new FileBasedConfig(cfgPath, FS.DETECTED);
    if (!cfg.getFile().exists() || cfg.getFile().length() == 0) {
        return Collections.emptyList();
    }/*  ww w.  j  a v  a  2  s  .  c om*/

    try {
        cfg.load();
    } catch (ConfigInvalidException e) {
        throw new ConfigInvalidException(
                String.format("Config file %s is invalid: %s", cfg.getFile(), e.getMessage()), e);
    } catch (IOException e) {
        throw new IOException(String.format("Cannot read %s: %s", cfg.getFile(), e.getMessage()), e);
    }

    ImmutableList.Builder<Destination> dest = ImmutableList.builder();
    for (RemoteConfig c : allRemotes(cfg)) {
        if (c.getURIs().isEmpty()) {
            continue;
        }

        for (URIish u : c.getURIs()) {
            if (u.getPath() == null || !u.getPath().contains("${name}")) {
                throw new ConfigInvalidException(String.format(
                        "remote.%s.url \"%s\" lacks ${name} placeholder in %s", c.getName(), u, cfg.getFile()));
            }
        }

        // If destination for push is not set assume equal to source.
        for (RefSpec ref : c.getPushRefSpecs()) {
            if (ref.getDestination() == null) {
                ref.setDestination(ref.getSource());
            }
        }

        if (c.getPushRefSpecs().isEmpty()) {
            c.addPushRefSpec(new RefSpec().setSourceDestination("refs/*", "refs/*").setForceUpdate(true));
        }

        dest.add(new Destination(injector, c, cfg, database, replicationUserFactory, pluginUser,
                gitRepositoryManager, groupBackend));
    }
    return dest.build();
}

From source file:com.googlesource.gerrit.plugins.replication.ReplicationFileBasedConfig.java

License:Apache License

private List<Destination> allDestinations() throws ConfigInvalidException, IOException {
    if (!config.getFile().exists()) {
        log.warn("Config file " + config.getFile() + " does not exist; not replicating");
        return Collections.emptyList();
    }/*from ww  w.  j  av a2 s  .  c o m*/
    if (config.getFile().length() == 0) {
        log.info("Config file " + config.getFile() + " is empty; not replicating");
        return Collections.emptyList();
    }

    try {
        config.load();
    } catch (ConfigInvalidException e) {
        throw new ConfigInvalidException(
                String.format("Config file %s is invalid: %s", config.getFile(), e.getMessage()), e);
    } catch (IOException e) {
        throw new IOException(String.format("Cannot read %s: %s", config.getFile(), e.getMessage()), e);
    }

    replicateAllOnPluginStart = config.getBoolean("gerrit", "replicateOnStartup", true);

    defaultForceUpdate = config.getBoolean("gerrit", "defaultForceUpdate", false);

    ImmutableList.Builder<Destination> dest = ImmutableList.builder();
    for (RemoteConfig c : allRemotes(config)) {
        if (c.getURIs().isEmpty()) {
            continue;
        }

        // If destination for push is not set assume equal to source.
        for (RefSpec ref : c.getPushRefSpecs()) {
            if (ref.getDestination() == null) {
                ref.setDestination(ref.getSource());
            }
        }

        if (c.getPushRefSpecs().isEmpty()) {
            c.addPushRefSpec(
                    new RefSpec().setSourceDestination("refs/*", "refs/*").setForceUpdate(defaultForceUpdate));
        }

        Destination destination = new Destination(injector, new DestinationConfiguration(c, config),
                replicationUserFactory, pluginUser, gitRepositoryManager, groupBackend, stateLog,
                groupIncludeCache);

        if (!destination.isSingleProjectMatch()) {
            for (URIish u : c.getURIs()) {
                if (u.getPath() == null || !u.getPath().contains("${name}")) {
                    throw new ConfigInvalidException(
                            String.format("remote.%s.url \"%s\" lacks ${name} placeholder in %s", c.getName(),
                                    u, config.getFile()));
                }
            }
        }

        dest.add(destination);
    }
    return dest.build();
}

From source file:com.hazelcast.simulator.utils.jars.GitSupport.java

License:Open Source License

private void addRepository(StoredConfig config, GitRepository repository) {
    String url = repository.getUrl();
    String name = repository.getName();
    LOGGER.info("Adding a new custom repository " + url);
    config.setString(CONFIG_REMOTE, name, CONFIG_URL, url);
    RefSpec refSpec = new RefSpec().setForceUpdate(true).setSourceDestination(Constants.R_HEADS + '*',
            Constants.R_REMOTES + name + "/*");
    config.setString(CONFIG_REMOTE, name, "fetch", refSpec.toString());
}

From source file:com.maiereni.synchronizer.git.utils.GitDownloaderImpl.java

License:Apache License

private List<Change> update(final Git git, final GitProperties properties, final Ref localBranch,
        final Ref tagRef) throws Exception {
    logger.debug("Fetch from remote");
    List<Change> ret = null;
    FetchCommand cmd = git.fetch();//  ww w  .j a va2s .c  o m
    if (StringUtils.isNotBlank(properties.getUserName()) && StringUtils.isNotBlank(properties.getPassword())) {
        logger.debug("Set credentials");
        cmd.setCredentialsProvider(
                new UsernamePasswordCredentialsProvider(properties.getUserName(), properties.getPassword()));
    }
    if (tagRef != null) {
        RefSpec spec = new RefSpec().setSourceDestination(localBranch.getName(), tagRef.getName());
        List<RefSpec> specs = new ArrayList<RefSpec>();
        specs.add(spec);
        cmd.setRefSpecs(specs);
    }
    FetchResult fr = cmd.call();
    Collection<Ref> refs = fr.getAdvertisedRefs();
    for (Ref ref : refs) {
        if (ref.getName().equals("HEAD")) {
            ret = checkDifferences(git, localBranch, ref);
            logger.debug("Rebase on HEAD");
            RebaseResult rebaseResult = git.rebase().setUpstream(ref.getObjectId()).call();
            if (rebaseResult.getStatus().isSuccessful()) {

            }
        }
    }
    return ret;
}

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 a 2s.  co 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.verigreen.jgit.JGitOperator.java

License:Apache License

@Override
public String fetch(String localBranchName, String remoteBranchName) {

    RefSpec spec = new RefSpec().setSourceDestination(localBranchName, remoteBranchName);
    FetchCommand command = _git.fetch();
    command.setRefSpecs(spec);/*ww w .j ava 2 s.c o  m*/
    FetchResult result = null;
    try {
        result = command.call();
    } catch (Throwable e) {
        throw new RuntimeException(
                String.format("Failed to fetch from [%s] to [%s]", remoteBranchName, localBranchName), e);
    }

    return result.getMessages();
}

From source file:com.verigreen.jgit.JGitOperator.java

License:Apache License

@Override
public boolean push(String sourceBranch, String destinationBranch) {

    PushCommand command = _git.push();/*from   w w w .  j  a v  a  2 s  .co m*/
    boolean ret = true;
    RefSpec refSpec = new RefSpec().setSourceDestination(sourceBranch, destinationBranch);
    command.setRefSpecs(refSpec);
    try {
        List<Ref> remoteBranches = _git.branchList().setListMode(ListMode.REMOTE).call();
        Iterable<PushResult> results = command.call();
        for (PushResult pushResult : results) {
            Collection<RemoteRefUpdate> resultsCollection = pushResult.getRemoteUpdates();
            Map<PushResult, RemoteRefUpdate> resultsMap = new HashMap<>();
            for (RemoteRefUpdate remoteRefUpdate : resultsCollection) {
                resultsMap.put(pushResult, remoteRefUpdate);
            }

            RemoteRefUpdate remoteUpdate = pushResult.getRemoteUpdate(destinationBranch);
            if (remoteUpdate != null) {
                org.eclipse.jgit.transport.RemoteRefUpdate.Status status = remoteUpdate.getStatus();
                ret = status.equals(org.eclipse.jgit.transport.RemoteRefUpdate.Status.OK)
                        || status.equals(org.eclipse.jgit.transport.RemoteRefUpdate.Status.UP_TO_DATE);
            }

            if (remoteUpdate == null && !remoteBranches.toString().contains(destinationBranch)) {

                for (RemoteRefUpdate resultValue : resultsMap.values()) {
                    if (resultValue.toString().contains("REJECTED_OTHER_REASON")) {
                        ret = false;
                    }
                }
            }
        }
    } catch (Throwable e) {
        throw new RuntimeException(
                String.format("Failed to push [%s] into [%s]", sourceBranch, destinationBranch), e);
    }

    return ret;
}