Search in sources :

Example 16 with Remote

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

the class RemoteWebOp method remoteRemove.

private void remoteRemove(CommandContext context, final Context geogig) {
    if (remoteName == null || remoteName.trim().isEmpty()) {
        throw new CommandSpecException("No remote was specified.");
    }
    final Remote remote;
    try {
        remote = geogig.command(RemoteRemoveOp.class).setName(remoteName).call();
    } catch (RemoteException e) {
        context.setResponseContent(CommandResponse.error(e.statusCode.toString()));
        return;
    } catch (Exception e) {
        context.setResponseContent(CommandResponse.error("Aborting Remote Remove"));
        return;
    }
    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start();
            out.writeElement("name", remote.getName());
            out.finish();
        }
    });
}
Also used : RemoteRemoveOp(org.locationtech.geogig.api.porcelain.RemoteRemoveOp) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) Remote(org.locationtech.geogig.api.Remote) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) RemoteException(org.locationtech.geogig.api.porcelain.RemoteException) IOException(java.io.IOException) RemoteException(org.locationtech.geogig.api.porcelain.RemoteException) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Example 17 with Remote

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

the class RemoteWebOp method remoteUpdate.

private void remoteUpdate(CommandContext context, final Context geogig) {
    if (remoteName == null || remoteName.trim().isEmpty()) {
        throw new CommandSpecException("No remote was specified.");
    } else if (remoteURL == null || remoteURL.trim().isEmpty()) {
        throw new CommandSpecException("No URL was specified.");
    }
    final Remote newRemote;
    try {
        if (newName != null && !newName.trim().isEmpty() && !newName.equals(remoteName)) {
            newRemote = geogig.command(RemoteAddOp.class).setName(newName).setURL(remoteURL).setUserName(username).setPassword(password).call();
            geogig.command(RemoteRemoveOp.class).setName(remoteName).call();
        } else {
            geogig.command(RemoteRemoveOp.class).setName(remoteName).call();
            newRemote = geogig.command(RemoteAddOp.class).setName(remoteName).setURL(remoteURL).setUserName(username).setPassword(password).call();
        }
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeElement("name", newRemote.getName());
                out.finish();
            }
        });
    } catch (RemoteException e) {
        context.setResponseContent(CommandResponse.error(e.statusCode.toString()));
    } catch (Exception e) {
        context.setResponseContent(CommandResponse.error("Aborting Remote Update"));
    }
}
Also used : ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) Remote(org.locationtech.geogig.api.Remote) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) RemoteException(org.locationtech.geogig.api.porcelain.RemoteException) IOException(java.io.IOException) RemoteException(org.locationtech.geogig.api.porcelain.RemoteException) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Example 18 with Remote

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

the class CloneOp method _call.

/**
     * Executes the clone operation.
     * 
     * @return {@code null}
     * @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
     */
@Override
protected Void _call() {
    Preconditions.checkArgument(repositoryURL != null && !repositoryURL.isEmpty(), "No repository specified to clone from.");
    Repository repository = repository();
    if (repository.isSparse()) {
        Preconditions.checkArgument(branch.isPresent(), "No branch specified for sparse clone.");
    }
    ProgressListener progressListener = getProgressListener();
    progressListener.started();
    // Set up origin
    Remote remote = command(RemoteAddOp.class).setName("origin").setURL(repositoryURL).setMapped(repository.isSparse()).setUserName(username).setPassword(password).setBranch(repository.isSparse() ? branch.get() : null).call();
    if (!depth.isPresent()) {
        // See if we are cloning a shallow clone. If so, a depth must be specified.
        Optional<IRemoteRepo> remoteRepo = RemoteUtils.newRemote(GlobalContextBuilder.builder.build(Hints.readOnly()), remote, repository, repository.deduplicationService());
        Preconditions.checkState(remoteRepo.isPresent(), "Failed to connect to the remote.");
        IRemoteRepo remoteRepoInstance = remoteRepo.get();
        try {
            remoteRepoInstance.open();
        } catch (IOException e) {
            Throwables.propagate(e);
        }
        try {
            depth = remoteRepoInstance.getDepth();
        } finally {
            try {
                remoteRepoInstance.close();
            } catch (IOException e) {
                Throwables.propagate(e);
            }
        }
    }
    if (depth.isPresent()) {
        command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName(Repository.DEPTH_CONFIG_KEY).setValue(depth.get().toString()).call();
    }
    // Fetch remote data
    command(FetchOp.class).setDepth(depth.or(0)).setProgressListener(progressListener).call();
    // Set up remote tracking branches
    final ImmutableSet<Ref> remoteRefs = command(LsRemote.class).retrieveTags(false).setRemote(Suppliers.ofInstance(Optional.of(remote))).call();
    boolean emptyRepo = true;
    for (Ref remoteRef : remoteRefs) {
        if (emptyRepo && !remoteRef.getObjectId().isNull()) {
            emptyRepo = false;
        }
        String branchName = remoteRef.localName();
        if (remoteRef instanceof SymRef) {
            continue;
        }
        if (!command(RefParse.class).setName(Ref.HEADS_PREFIX + branchName).call().isPresent()) {
            command(BranchCreateOp.class).setName(branchName).setSource(remoteRef.getObjectId().toString()).call();
        } else {
            command(UpdateRef.class).setName(Ref.HEADS_PREFIX + branchName).setNewValue(remoteRef.getObjectId()).call();
        }
        command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branchName + ".remote").setValue(remote.getName()).call();
        command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branchName + ".merge").setValue(Ref.HEADS_PREFIX + remoteRef.localName()).call();
    }
    if (!emptyRepo) {
        // checkout branch
        if (branch.isPresent()) {
            command(CheckoutOp.class).setForce(true).setSource(branch.get()).call();
        } else {
            // checkout the head
            final Optional<Ref> currRemoteHead = command(RefParse.class).setName(Ref.REMOTES_PREFIX + remote.getName() + "/" + Ref.HEAD).call();
            if (currRemoteHead.isPresent() && currRemoteHead.get() instanceof SymRef) {
                final SymRef remoteHeadRef = (SymRef) currRemoteHead.get();
                final String currentBranch = Ref.localName(remoteHeadRef.getTarget());
                command(CheckoutOp.class).setForce(true).setSource(currentBranch).call();
            } else {
            // just leave at default; should be master since we just initialized the repo.
            }
        }
    }
    progressListener.complete();
    return null;
}
Also used : Remote(org.locationtech.geogig.api.Remote) LsRemote(org.locationtech.geogig.api.plumbing.LsRemote) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) IOException(java.io.IOException) Repository(org.locationtech.geogig.repository.Repository) 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) ProgressListener(org.locationtech.geogig.api.ProgressListener) IRemoteRepo(org.locationtech.geogig.remote.IRemoteRepo) LsRemote(org.locationtech.geogig.api.plumbing.LsRemote)

Example 19 with Remote

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

the class FetchOp method _call.

/**
     * Executes the fetch operation.
     * 
     * @return {@code null}
     * @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
     */
@Override
protected TransferSummary _call() {
    if (all) {
        // Add all remotes to list.
        ImmutableList<Remote> localRemotes = command(RemoteListOp.class).call();
        for (Remote remote : localRemotes) {
            if (!remotes.contains(remote)) {
                remotes.add(remote);
            }
        }
    } else if (remotes.size() == 0) {
        // If no remotes are specified, default to the origin remote
        addRemote("origin");
    }
    final ProgressListener progressListener = getProgressListener();
    progressListener.started();
    Optional<Integer> repoDepth = repository().getDepth();
    if (repoDepth.isPresent()) {
        if (fullDepth) {
            depth = Optional.of(Integer.MAX_VALUE);
        }
        if (depth.isPresent()) {
            if (depth.get() > repoDepth.get()) {
                command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName(Repository.DEPTH_CONFIG_KEY).setValue(depth.get().toString()).call();
                repoDepth = depth;
            }
        }
    } else if (depth.isPresent() || fullDepth) {
        // Ignore depth, this is a full repository
        depth = Optional.absent();
        fullDepth = false;
    }
    TransferSummary result = new TransferSummary();
    for (Remote remote : remotes) {
        final ImmutableSet<Ref> remoteRemoteRefs = command(LsRemote.class).setRemote(Suppliers.ofInstance(Optional.of(remote))).retrieveTags(!remote.getMapped() && (!repoDepth.isPresent() || fullDepth)).call();
        final ImmutableSet<Ref> localRemoteRefs = command(LsRemote.class).retrieveLocalRefs(true).setRemote(Suppliers.ofInstance(Optional.of(remote))).call();
        // If we have specified a depth to pull, we may have more history to pull from existing
        // refs.
        List<ChangedRef> needUpdate = findOutdatedRefs(remote, remoteRemoteRefs, localRemoteRefs, depth);
        if (prune) {
            // Delete local refs that aren't in the remote
            List<Ref> locals = new ArrayList<Ref>();
            // provided a tag so it makes sense not to prune them anyway.
            for (Ref remoteRef : remoteRemoteRefs) {
                Optional<Ref> localRef = findLocal(remoteRef, localRemoteRefs);
                if (localRef.isPresent()) {
                    locals.add(localRef.get());
                }
            }
            for (Ref localRef : localRemoteRefs) {
                if (!locals.contains(localRef)) {
                    // Delete the ref
                    ChangedRef changedRef = new ChangedRef(localRef, null, ChangeTypes.REMOVED_REF);
                    needUpdate.add(changedRef);
                    command(UpdateRef.class).setDelete(true).setName(localRef.getName()).call();
                }
            }
        }
        Optional<IRemoteRepo> remoteRepo = getRemoteRepo(remote, repository().deduplicationService());
        Preconditions.checkState(remoteRepo.isPresent(), "Failed to connect to the remote.");
        IRemoteRepo remoteRepoInstance = remoteRepo.get();
        try {
            remoteRepoInstance.open();
        } catch (IOException e) {
            Throwables.propagate(e);
        }
        try {
            int refCount = 0;
            for (ChangedRef ref : needUpdate) {
                if (ref.getType() != ChangeTypes.REMOVED_REF) {
                    refCount++;
                    Optional<Integer> newFetchLimit = depth;
                    // fetch limit to the current repository depth.
                    if (!newFetchLimit.isPresent() && repoDepth.isPresent() && ref.getType() == ChangeTypes.ADDED_REF) {
                        newFetchLimit = repoDepth;
                    }
                    // Fetch updated data from this ref
                    Ref newRef = ref.getNewRef();
                    remoteRepoInstance.fetchNewData(newRef, newFetchLimit, progressListener);
                    if (repoDepth.isPresent() && !fullDepth) {
                        // Update the repository depth if it is deeper than before.
                        int newDepth;
                        try {
                            newDepth = repository().graphDatabase().getDepth(newRef.getObjectId());
                        } catch (IllegalStateException e) {
                            throw new RuntimeException(ref.toString(), e);
                        }
                        if (newDepth > repoDepth.get()) {
                            command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName(Repository.DEPTH_CONFIG_KEY).setValue(Integer.toString(newDepth)).call();
                            repoDepth = Optional.of(newDepth);
                        }
                    }
                    // Update the ref
                    Ref updatedRef = updateLocalRef(newRef, remote, localRemoteRefs);
                    ref.setNewRef(updatedRef);
                }
            }
            if (needUpdate.size() > 0) {
                result.addAll(remote.getFetchURL(), needUpdate);
            }
            // Update HEAD ref
            if (!remote.getMapped()) {
                Ref remoteHead = remoteRepoInstance.headRef();
                if (remoteHead != null) {
                    updateLocalRef(remoteHead, remote, localRemoteRefs);
                }
            }
        } finally {
            try {
                remoteRepoInstance.close();
            } catch (IOException e) {
                Throwables.propagate(e);
            }
        }
    }
    if (fullDepth) {
        // The full history was fetched, this is no longer a shallow clone
        command(ConfigOp.class).setAction(ConfigAction.CONFIG_UNSET).setScope(ConfigScope.LOCAL).setName(Repository.DEPTH_CONFIG_KEY).call();
    }
    progressListener.complete();
    return result;
}
Also used : ArrayList(java.util.ArrayList) Remote(org.locationtech.geogig.api.Remote) LsRemote(org.locationtech.geogig.api.plumbing.LsRemote) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) IOException(java.io.IOException) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) ChangedRef(org.locationtech.geogig.api.porcelain.TransferSummary.ChangedRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) Ref(org.locationtech.geogig.api.Ref) SymRef(org.locationtech.geogig.api.SymRef) ProgressListener(org.locationtech.geogig.api.ProgressListener) LsRemote(org.locationtech.geogig.api.plumbing.LsRemote) IRemoteRepo(org.locationtech.geogig.remote.IRemoteRepo) ChangedRef(org.locationtech.geogig.api.porcelain.TransferSummary.ChangedRef)

Example 20 with Remote

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

the class PushOp method _call.

/**
     * Executes the push operation.
     * 
     * @return {@code null}
     * @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
     */
@Override
protected TransferSummary _call() {
    final String remoteName = this.remoteName == null ? "origin" : this.remoteName;
    final Remote remote = resolveRemote(remoteName);
    final List<TransferableRef> refsToPush = resolveRefs();
    SendPack sendPack = command(SendPack.class);
    sendPack.setRemote(remote);
    sendPack.setRefs(refsToPush);
    sendPack.setProgressListener(getProgressListener());
    TransferSummary result = sendPack.call();
    return result;
}
Also used : SendPack(org.locationtech.geogig.api.plumbing.SendPack) TransferableRef(org.locationtech.geogig.api.plumbing.SendPack.TransferableRef) Remote(org.locationtech.geogig.api.Remote)

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