Search in sources :

Example 21 with Remote

use of org.locationtech.geogig.api.Remote in project GeoGig by boundlessgeo.

the class PullOp method _call.

/**
     * Executes the pull operation.
     * 
     * @return {@code null}
     * @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
     */
@Override
protected PullResult _call() {
    if (remote == null) {
        setRemote("origin");
    }
    PullResult result = new PullResult();
    Optional<Remote> remoteRepo = remote.get();
    Preconditions.checkArgument(remoteRepo.isPresent(), "Remote could not be resolved.");
    getProgressListener().started();
    TransferSummary fetchResult = command(FetchOp.class).addRemote(remote).setDepth(depth.or(0)).setFullDepth(fullDepth).setAll(all).setProgressListener(subProgress(80.f)).call();
    result.setFetchResult(fetchResult);
    if (refSpecs.size() == 0) {
        // pull current branch
        final Optional<Ref> currHead = command(RefParse.class).setName(Ref.HEAD).call();
        Preconditions.checkState(currHead.isPresent(), "Repository has no HEAD, can't pull.");
        Preconditions.checkState(currHead.get() instanceof SymRef, "Can't pull from detached HEAD");
        final SymRef headRef = (SymRef) currHead.get();
        final String currentBranch = Ref.localName(headRef.getTarget());
        refSpecs.add(currentBranch + ":" + currentBranch);
    }
    for (String refspec : refSpecs) {
        String[] refs = refspec.split(":");
        Preconditions.checkArgument(refs.length < 3, "Invalid refspec, please use [+]<remoteref>[:<localref>].");
        boolean force = refspec.length() > 0 && refspec.charAt(0) == '+';
        String remoteref = refs[0].substring(force ? 1 : 0);
        Optional<Ref> sourceRef = findRemoteRef(remoteref);
        if (!sourceRef.isPresent()) {
            continue;
        }
        String destinationref = "";
        if (refs.length == 2) {
            destinationref = refs[1];
        } else {
            // pull into current branch
            final Optional<Ref> currHead = command(RefParse.class).setName(Ref.HEAD).call();
            Preconditions.checkState(currHead.isPresent(), "Repository has no HEAD, can't pull.");
            Preconditions.checkState(currHead.get() instanceof SymRef, "Can't pull from detached HEAD");
            final SymRef headRef = (SymRef) currHead.get();
            destinationref = headRef.getTarget();
        }
        Optional<Ref> destRef = command(RefParse.class).setName(destinationref).call();
        if (destRef.isPresent()) {
            if (destRef.get().getObjectId().equals(sourceRef.get().getObjectId()) || sourceRef.get().getObjectId().equals(ObjectId.NULL)) {
                // Already up to date.
                result.setOldRef(destRef.get());
                result.setNewRef(destRef.get());
                continue;
            }
            result.setOldRef(destRef.get());
            if (destRef.get().getObjectId().equals(ObjectId.NULL)) {
                command(UpdateRef.class).setName(destRef.get().getName()).setNewValue(sourceRef.get().getObjectId()).call();
            } else {
                command(CheckoutOp.class).setSource(destinationref).call();
                if (rebase) {
                    command(RebaseOp.class).setUpstream(Suppliers.ofInstance(sourceRef.get().getObjectId())).call();
                } else {
                    try {
                        MergeReport report = command(MergeOp.class).setAuthor(authorName.orNull(), authorEmail.orNull()).addCommit(Suppliers.ofInstance(sourceRef.get().getObjectId())).call();
                        result.setMergeReport(Optional.of(report));
                    } catch (NothingToCommitException e) {
                    // the branch that we are trying to pull has less history than the
                    // branch we are pulling into
                    }
                }
            }
            destRef = command(RefParse.class).setName(destinationref).call();
            result.setNewRef(destRef.get());
        } else {
            // make a new branch
            Ref newRef = command(BranchCreateOp.class).setAutoCheckout(true).setName(destinationref).setSource(sourceRef.get().getObjectId().toString()).call();
            result.setNewRef(newRef);
        }
    }
    getProgressListener().complete();
    result.setRemoteName(remote.get().get().getFetchURL());
    return result;
}
Also used : Remote(org.locationtech.geogig.api.Remote) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) MergeReport(org.locationtech.geogig.api.porcelain.MergeOp.MergeReport) Ref(org.locationtech.geogig.api.Ref) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) SymRef(org.locationtech.geogig.api.SymRef) SymRef(org.locationtech.geogig.api.SymRef) RefParse(org.locationtech.geogig.api.plumbing.RefParse)

Example 22 with Remote

use of org.locationtech.geogig.api.Remote in project GeoGig by boundlessgeo.

the class RemoteAddOp method _call.

/**
     * Executes the remote-add operation.
     * 
     * @return the {@link Remote} that was added.
     */
@Override
protected Remote _call() {
    if (name == null || name.isEmpty()) {
        throw new RemoteException(StatusCode.MISSING_NAME);
    }
    if (url == null || url.isEmpty()) {
        throw new RemoteException(StatusCode.MISSING_URL);
    }
    if (branch == null || branch.isEmpty()) {
        branch = "*";
    }
    ConfigDatabase config = configDatabase();
    List<String> allRemotes = config.getAllSubsections("remote");
    if (allRemotes.contains(name)) {
        throw new RemoteException(StatusCode.REMOTE_ALREADY_EXISTS);
    }
    String configSection = "remote." + name;
    String fetch = "+" + Ref.HEADS_PREFIX + branch + ":" + Ref.REMOTES_PREFIX + name + "/" + branch;
    config.put(configSection + ".url", url);
    config.put(configSection + ".fetch", fetch);
    if (mapped) {
        config.put(configSection + ".mapped", "true");
        config.put(configSection + ".mappedBranch", branch);
    }
    if (username != null) {
        config.put(configSection + ".username", username);
    }
    if (password != null) {
        password = Remote.encryptPassword(password);
        config.put(configSection + ".password", password);
    }
    return new Remote(name, url, url, fetch, mapped, branch, username, password);
}
Also used : ConfigDatabase(org.locationtech.geogig.storage.ConfigDatabase) Remote(org.locationtech.geogig.api.Remote)

Example 23 with Remote

use of org.locationtech.geogig.api.Remote in project GeoGig by boundlessgeo.

the class RemoteRemoveOp method _call.

/**
     * Executes the remote-remove operation.
     * 
     * @return the {@link Remote} that was removed, or {@link Optional#absent()} if the remote
     *         didn't exist.
     */
@Override
protected Remote _call() {
    if (name == null || name.isEmpty()) {
        throw new RemoteException(StatusCode.MISSING_NAME);
    }
    ConfigDatabase config = configDatabase();
    List<String> allRemotes = config.getAllSubsections("remote");
    if (!allRemotes.contains(name)) {
        throw new RemoteException(StatusCode.REMOTE_NOT_FOUND);
    }
    Remote remote = null;
    String remoteSection = "remote." + name;
    Optional<String> remoteFetchURL = config.get(remoteSection + ".url");
    Optional<String> remoteFetch = config.get(remoteSection + ".fetch");
    Optional<String> remotePushURL = Optional.absent();
    Optional<String> remoteMapped = config.get(remoteSection + ".mapped");
    Optional<String> remoteMappedBranch = config.get(remoteSection + ".mappedBranch");
    Optional<String> remoteUserName = config.get(remoteSection + ".username");
    Optional<String> remotePassword = config.get(remoteSection + ".password");
    if (remoteFetchURL.isPresent() && remoteFetch.isPresent()) {
        remotePushURL = config.get(remoteSection + ".pushurl");
    }
    remote = new Remote(name, remoteFetchURL.or(""), remotePushURL.or(remoteFetchURL.or("")), remoteFetch.or(""), remoteMapped.or("false").equals("true"), remoteMappedBranch.orNull(), remoteUserName.orNull(), remotePassword.orNull());
    config.removeSection(remoteSection);
    // Remove refs
    final ImmutableSet<Ref> localRemoteRefs = command(LsRemote.class).retrieveLocalRefs(true).setRemote(Suppliers.ofInstance(Optional.of(remote))).call();
    for (Ref localRef : localRemoteRefs) {
        command(UpdateRef.class).setDelete(true).setName(localRef.getName()).call();
    }
    return remote;
}
Also used : Ref(org.locationtech.geogig.api.Ref) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) ConfigDatabase(org.locationtech.geogig.storage.ConfigDatabase) LsRemote(org.locationtech.geogig.api.plumbing.LsRemote) Remote(org.locationtech.geogig.api.Remote) LsRemote(org.locationtech.geogig.api.plumbing.LsRemote) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef)

Example 24 with Remote

use of org.locationtech.geogig.api.Remote in project GeoGig by boundlessgeo.

the class RemoteRemoveOpTest method testRemoveNonexistentRemote.

@Test
public void testRemoveNonexistentRemote() {
    final RemoteAddOp remoteAdd = geogig.command(RemoteAddOp.class);
    String remoteName = "myremote";
    String remoteURL = "http://test.com";
    Remote remote = remoteAdd.setName(remoteName).setURL(remoteURL).call();
    assertEquals(remoteName, remote.getName());
    assertEquals(remoteURL, remote.getFetchURL());
    assertEquals(remoteURL, remote.getPushURL());
    assertEquals("+refs/heads/*:refs/remotes/" + remoteName + "/*", remote.getFetch());
    final RemoteRemoveOp remoteRemove = geogig.command(RemoteRemoveOp.class);
    exception.expect(RemoteException.class);
    remoteRemove.setName("nonexistent").call();
}
Also used : RemoteRemoveOp(org.locationtech.geogig.api.porcelain.RemoteRemoveOp) RemoteAddOp(org.locationtech.geogig.api.porcelain.RemoteAddOp) Remote(org.locationtech.geogig.api.Remote) Test(org.junit.Test)

Example 25 with Remote

use of org.locationtech.geogig.api.Remote in project GeoGig by boundlessgeo.

the class RemoteAddOpTest method testAddRemoteWithBranch.

@Test
public void testAddRemoteWithBranch() {
    final RemoteAddOp remoteAdd = geogig.command(RemoteAddOp.class);
    String remoteName = "myremote";
    String remoteURL = "http://test.com";
    String branch = "mybranch";
    Remote remote = remoteAdd.setName(remoteName).setURL(remoteURL).setBranch(branch).call();
    assertEquals(remoteName, remote.getName());
    assertEquals(remoteURL, remote.getFetchURL());
    assertEquals(remoteURL, remote.getPushURL());
    assertEquals("+refs/heads/" + branch + ":refs/remotes/" + remoteName + "/" + branch, remote.getFetch());
}
Also used : RemoteAddOp(org.locationtech.geogig.api.porcelain.RemoteAddOp) Remote(org.locationtech.geogig.api.Remote) Test(org.junit.Test)

Aggregations

Remote (org.locationtech.geogig.api.Remote)31 Test (org.junit.Test)14 RemoteAddOp (org.locationtech.geogig.api.porcelain.RemoteAddOp)13 IOException (java.io.IOException)8 Ref (org.locationtech.geogig.api.Ref)8 RemoteRemoveOp (org.locationtech.geogig.api.porcelain.RemoteRemoveOp)6 UpdateRef (org.locationtech.geogig.api.plumbing.UpdateRef)5 RemoteException (org.locationtech.geogig.api.porcelain.RemoteException)5 RemoteListOp (org.locationtech.geogig.api.porcelain.RemoteListOp)5 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)5 CommandSpecException (org.locationtech.geogig.web.api.CommandSpecException)5 ResponseWriter (org.locationtech.geogig.web.api.ResponseWriter)5 ConfigOp (org.locationtech.geogig.api.porcelain.ConfigOp)4 IRemoteRepo (org.locationtech.geogig.remote.IRemoteRepo)4 ConfigDatabase (org.locationtech.geogig.storage.ConfigDatabase)4 SymRef (org.locationtech.geogig.api.SymRef)3 LsRemote (org.locationtech.geogig.api.plumbing.LsRemote)3 ArrayList (java.util.ArrayList)2 ProgressListener (org.locationtech.geogig.api.ProgressListener)2 ChangedRef (org.locationtech.geogig.api.porcelain.TransferSummary.ChangedRef)2