Search in sources :

Example 1 with IRemoteRepo

use of org.locationtech.geogig.remote.IRemoteRepo in project GeoGig by boundlessgeo.

the class LsRemote method _call.

/**
     * Lists all refs for the given remote.
     * 
     * @return an immutable set of the refs for the given remote
     */
@Override
protected ImmutableSet<Ref> _call() {
    Preconditions.checkState(remote.get().isPresent(), "Remote was not provided");
    final Remote remoteConfig = remote.get().get();
    if (local) {
        return locallyKnownRefs(remoteConfig);
    }
    getProgressListener().setDescription("Obtaining remote " + remoteConfig.getName());
    Optional<IRemoteRepo> remoteRepo = getRemoteRepo(remoteConfig);
    Preconditions.checkState(remoteRepo.isPresent(), "Remote could not be opened.");
    getProgressListener().setDescription("Connecting to remote " + remoteConfig.getName());
    try {
        remoteRepo.get().open();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    getProgressListener().setDescription("Connected to remote " + remoteConfig.getName() + ". Retrieving references");
    ImmutableSet<Ref> remoteRefs;
    try {
        remoteRefs = remoteRepo.get().listRefs(getHeads, getTags);
    } finally {
        try {
            remoteRepo.get().close();
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }
    return remoteRefs;
}
Also used : Ref(org.locationtech.geogig.api.Ref) IRemoteRepo(org.locationtech.geogig.remote.IRemoteRepo) Remote(org.locationtech.geogig.api.Remote) IOException(java.io.IOException)

Example 2 with IRemoteRepo

use of org.locationtech.geogig.remote.IRemoteRepo in project GeoGig by boundlessgeo.

the class SendPack method _call.

@Override
protected TransferSummary _call() {
    checkState(remote != null, "no remote specified");
    checkState(!refsToPush.isEmpty(), "no refs to push specified");
    final IRemoteRepo remoteRepo = openRemoteRepo(remote);
    TransferSummary transferResult;
    try {
        transferResult = callInternal(remoteRepo);
        checkState(transferResult != null);
    } finally {
        try {
            remoteRepo.close();
        } catch (IOException e) {
            Throwables.propagate(e);
        }
    }
    return transferResult;
}
Also used : IRemoteRepo(org.locationtech.geogig.remote.IRemoteRepo) TransferSummary(org.locationtech.geogig.api.porcelain.TransferSummary) IOException(java.io.IOException)

Example 3 with IRemoteRepo

use of org.locationtech.geogig.remote.IRemoteRepo 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 4 with IRemoteRepo

use of org.locationtech.geogig.remote.IRemoteRepo 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 5 with IRemoteRepo

use of org.locationtech.geogig.remote.IRemoteRepo in project GeoGig by boundlessgeo.

the class SendPack method openRemoteRepo.

private IRemoteRepo openRemoteRepo(final Remote remote) {
    final IRemoteRepo remoteRepo;
    Optional<IRemoteRepo> resolvedRemoteRepo = getRemoteRepo(remote);
    checkState(resolvedRemoteRepo.isPresent(), "Failed to connect to the remote.");
    remoteRepo = resolvedRemoteRepo.get();
    try {
        remoteRepo.open();
    } catch (IOException e) {
        Throwables.propagate(e);
    }
    return remoteRepo;
}
Also used : IRemoteRepo(org.locationtech.geogig.remote.IRemoteRepo) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)6 IRemoteRepo (org.locationtech.geogig.remote.IRemoteRepo)6 Ref (org.locationtech.geogig.api.Ref)4 Remote (org.locationtech.geogig.api.Remote)4 ProgressListener (org.locationtech.geogig.api.ProgressListener)2 SymRef (org.locationtech.geogig.api.SymRef)2 LsRemote (org.locationtech.geogig.api.plumbing.LsRemote)2 UpdateRef (org.locationtech.geogig.api.plumbing.UpdateRef)2 ArrayList (java.util.ArrayList)1 UpdateSymRef (org.locationtech.geogig.api.plumbing.UpdateSymRef)1 RemoteException (org.locationtech.geogig.api.porcelain.RemoteException)1 TransferSummary (org.locationtech.geogig.api.porcelain.TransferSummary)1 ChangedRef (org.locationtech.geogig.api.porcelain.TransferSummary.ChangedRef)1 Repository (org.locationtech.geogig.repository.Repository)1 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)1 CommandSpecException (org.locationtech.geogig.web.api.CommandSpecException)1 ResponseWriter (org.locationtech.geogig.web.api.ResponseWriter)1