Search in sources :

Example 1 with PullResult

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

the class Pull method runInternal.

/**
     * Executes the pull command using the provided options.
     */
@Override
public void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(depth > 0 ? !fulldepth : true, "Cannot specify a depth and full depth.  Use --depth <depth> or --fulldepth.");
    GeoGIG geogig = cli.getGeogig();
    if (depth > 0 || fulldepth) {
        if (!geogig.getRepository().getDepth().isPresent()) {
            throw new CommandFailedException("Depth operations can only be used on a shallow clone.");
        }
    }
    PullOp pull = geogig.command(PullOp.class);
    pull.setProgressListener(cli.getProgressListener());
    pull.setAll(all).setRebase(rebase).setFullDepth(fulldepth);
    pull.setDepth(depth);
    if (args != null) {
        if (args.size() > 0) {
            pull.setRemote(args.get(0));
        }
        for (int i = 1; i < args.size(); i++) {
            pull.addRefSpec(args.get(i));
        }
    }
    try {
        final PullResult result = pull.call();
        ConsoleReader console = cli.getConsole();
        TransferSummary fetchResult = result.getFetchResult();
        FetchResultPrinter.print(fetchResult, console);
        final Ref oldRef = result.getOldRef();
        final Ref newRef = result.getNewRef();
        if (oldRef == null && newRef == null) {
            console.println("Nothing to pull.");
        } else if (Objects.equal(oldRef, newRef)) {
            String name = oldRef == null ? newRef.getName() : oldRef.getName();
            name = Ref.localName(name);
            console.println(name + " already up to date.");
        } else {
            String oldTreeish;
            String newTreeish = newRef.getObjectId().toString();
            if (oldRef == null) {
                console.println("From " + result.getRemoteName());
                console.println(" * [new branch]     " + newRef.localName() + " -> " + newRef.getName());
                oldTreeish = ObjectId.NULL.toString();
            } else {
                oldTreeish = oldRef.getObjectId().toString();
            }
            DiffObjectCount count = geogig.command(DiffCount.class).setOldVersion(oldTreeish).setNewVersion(newTreeish).call();
            long added = count.getFeaturesAdded();
            long removed = count.getFeaturesRemoved();
            long modified = count.getFeaturesChanged();
            console.println(String.format("Features Added: %,d Removed: %,d Modified: %,d", added, removed, modified));
        }
    } catch (SynchronizationException e) {
        switch(e.statusCode) {
            case HISTORY_TOO_SHALLOW:
            default:
                throw new CommandFailedException("Unable to pull, the remote history is shallow.", e);
        }
    }
}
Also used : PullOp(org.locationtech.geogig.api.porcelain.PullOp) Ref(org.locationtech.geogig.api.Ref) DiffObjectCount(org.locationtech.geogig.api.plumbing.diff.DiffObjectCount) ConsoleReader(jline.console.ConsoleReader) TransferSummary(org.locationtech.geogig.api.porcelain.TransferSummary) GeoGIG(org.locationtech.geogig.api.GeoGIG) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) PullResult(org.locationtech.geogig.api.porcelain.PullResult) SynchronizationException(org.locationtech.geogig.api.porcelain.SynchronizationException)

Example 2 with PullResult

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

the class PullWebOp method run.

/**
     * Runs the command and builds the appropriate response.
     * 
     * @param context - the context to use for this command
     */
@Override
public void run(CommandContext context) {
    final Context geogig = this.getCommandLocator(context);
    PullOp command = geogig.command(PullOp.class).setAuthor(authorName.orNull(), authorEmail.orNull()).setRemote(remoteName).setAll(fetchAll).addRefSpec(refSpec);
    try {
        final PullResult result = command.call();
        final Iterator<DiffEntry> iter;
        if (result.getOldRef() != null && result.getNewRef() != null && result.getOldRef().equals(result.getNewRef())) {
            iter = null;
        } else {
            if (result.getOldRef() == null) {
                iter = geogig.command(DiffOp.class).setNewVersion(result.getNewRef().getObjectId()).setOldVersion(ObjectId.NULL).call();
            } else {
                iter = geogig.command(DiffOp.class).setNewVersion(result.getNewRef().getObjectId()).setOldVersion(result.getOldRef().getObjectId()).call();
            }
        }
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writePullResponse(result, iter, geogig);
                out.finish();
            }
        });
    } catch (SynchronizationException e) {
        switch(e.statusCode) {
            case HISTORY_TOO_SHALLOW:
            default:
                context.setResponseContent(CommandResponse.error("Unable to pull, the remote history is shallow."));
        }
    } catch (MergeConflictsException e) {
        String[] refs = refSpec.split(":");
        String remoteRef = Ref.REMOTES_PREFIX + remoteName + "/" + refs[0];
        Optional<Ref> sourceRef = geogig.command(RefParse.class).setName(remoteRef).call();
        String destinationref = "";
        if (refs.length == 2) {
            destinationref = refs[1];
        } else {
            final Optional<Ref> currHead = geogig.command(RefParse.class).setName(Ref.HEAD).call();
            if (!currHead.isPresent()) {
                context.setResponseContent(CommandResponse.error("Repository has no HEAD, can't pull."));
            } else if (!(currHead.get() instanceof SymRef)) {
                context.setResponseContent(CommandResponse.error("Can't pull from detached HEAD"));
            }
            final SymRef headRef = (SymRef) currHead.get();
            destinationref = headRef.getTarget();
        }
        Optional<Ref> destRef = geogig.command(RefParse.class).setName(destinationref).call();
        final RevCommit theirs = context.getGeoGIG().getRepository().getCommit(sourceRef.get().getObjectId());
        final RevCommit ours = context.getGeoGIG().getRepository().getCommit(destRef.get().getObjectId());
        final Optional<ObjectId> ancestor = geogig.command(FindCommonAncestor.class).setLeft(ours).setRight(theirs).call();
        context.setResponseContent(new CommandResponse() {

            final MergeScenarioReport report = geogig.command(ReportMergeScenarioOp.class).setMergeIntoCommit(ours).setToMergeCommit(theirs).call();

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                Optional<RevCommit> mergeCommit = Optional.absent();
                out.writeMergeResponse(mergeCommit, report, geogig, ours.getId(), theirs.getId(), ancestor.get());
                out.finish();
            }
        });
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) PullOp(org.locationtech.geogig.api.porcelain.PullOp) Optional(com.google.common.base.Optional) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) MergeScenarioReport(org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport) PullResult(org.locationtech.geogig.api.porcelain.PullResult) MergeConflictsException(org.locationtech.geogig.api.porcelain.MergeConflictsException) SynchronizationException(org.locationtech.geogig.api.porcelain.SynchronizationException) SymRef(org.locationtech.geogig.api.SymRef) MergeConflictsException(org.locationtech.geogig.api.porcelain.MergeConflictsException) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RefParse(org.locationtech.geogig.api.plumbing.RefParse) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) SynchronizationException(org.locationtech.geogig.api.porcelain.SynchronizationException) RevCommit(org.locationtech.geogig.api.RevCommit)

Aggregations

PullOp (org.locationtech.geogig.api.porcelain.PullOp)2 PullResult (org.locationtech.geogig.api.porcelain.PullResult)2 SynchronizationException (org.locationtech.geogig.api.porcelain.SynchronizationException)2 Optional (com.google.common.base.Optional)1 ConsoleReader (jline.console.ConsoleReader)1 Context (org.locationtech.geogig.api.Context)1 GeoGIG (org.locationtech.geogig.api.GeoGIG)1 Ref (org.locationtech.geogig.api.Ref)1 RevCommit (org.locationtech.geogig.api.RevCommit)1 SymRef (org.locationtech.geogig.api.SymRef)1 RefParse (org.locationtech.geogig.api.plumbing.RefParse)1 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)1 DiffObjectCount (org.locationtech.geogig.api.plumbing.diff.DiffObjectCount)1 MergeScenarioReport (org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport)1 MergeConflictsException (org.locationtech.geogig.api.porcelain.MergeConflictsException)1 TransferSummary (org.locationtech.geogig.api.porcelain.TransferSummary)1 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)1 CommandContext (org.locationtech.geogig.web.api.CommandContext)1 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)1 ResponseWriter (org.locationtech.geogig.web.api.ResponseWriter)1