Search in sources :

Example 6 with GeogigTransaction

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

the class GeoGigDataStore method getCommandLocator.

public Context getCommandLocator(@Nullable Transaction transaction) {
    Context commandLocator = null;
    if (transaction != null && !Transaction.AUTO_COMMIT.equals(transaction)) {
        GeogigTransactionState state;
        state = (GeogigTransactionState) transaction.getState(GeogigTransactionState.class);
        Optional<GeogigTransaction> geogigTransaction = state.getGeogigTransaction();
        if (geogigTransaction.isPresent()) {
            commandLocator = geogigTransaction.get();
        }
    }
    if (commandLocator == null) {
        commandLocator = geogig.getContext();
    }
    return commandLocator;
}
Also used : Context(org.locationtech.geogig.api.Context) GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction)

Example 7 with GeogigTransaction

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

the class EndTransaction method run.

/**
     * Runs the command and builds the appropriate response.
     * 
     * @param context - the context to use for this command
     * 
     * @throws CommandSpecException
     */
@Override
public void run(CommandContext context) {
    if (this.getTransactionId() == null) {
        throw new CommandSpecException("There isn't a transaction to end.");
    }
    final Context transaction = this.getCommandLocator(context);
    TransactionEnd endTransaction = context.getGeoGIG().command(TransactionEnd.class);
    try {
        final boolean closed = endTransaction.setCancel(cancel).setTransaction((GeogigTransaction) transaction).call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                if (closed) {
                    out.writeTransactionId(null);
                } else {
                    out.writeTransactionId(getTransactionId());
                }
                out.finish();
            }
        });
    } catch (MergeConflictsException m) {
        final RevCommit ours = context.getGeoGIG().getRepository().getCommit(m.getOurs());
        final RevCommit theirs = context.getGeoGIG().getRepository().getCommit(m.getTheirs());
        final Optional<ObjectId> ancestor = transaction.command(FindCommonAncestor.class).setLeft(ours).setRight(theirs).call();
        context.setResponseContent(new CommandResponse() {

            final MergeScenarioReport report = transaction.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, transaction, ours.getId(), theirs.getId(), ancestor.get());
                out.finish();
            }
        });
    } catch (RebaseConflictsException r) {
    // TODO: Handle rebase exception
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) Optional(com.google.common.base.Optional) RebaseConflictsException(org.locationtech.geogig.api.porcelain.RebaseConflictsException) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) MergeScenarioReport(org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport) MergeConflictsException(org.locationtech.geogig.api.porcelain.MergeConflictsException) RebaseConflictsException(org.locationtech.geogig.api.porcelain.RebaseConflictsException) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) MergeConflictsException(org.locationtech.geogig.api.porcelain.MergeConflictsException) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) TransactionEnd(org.locationtech.geogig.api.plumbing.TransactionEnd) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 8 with GeogigTransaction

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

the class MergeWebOp method run.

/**
     * Runs the command and builds the appropriate response.
     * 
     * @param context - the context to use for this command
     * 
     * @throws CommandSpecException
     */
@Override
public void run(CommandContext context) {
    if (this.getTransactionId() == null) {
        throw new CommandSpecException("No transaction was specified, merge requires a transaction to preserve the stability of the repository.");
    } else if (this.commit == null) {
        throw new CommandSpecException("No commits were specified for merging.");
    }
    final GeogigTransaction transaction = (GeogigTransaction) this.getCommandLocator(context);
    final Optional<Ref> currHead = transaction.command(RefParse.class).setName(Ref.HEAD).call();
    if (!currHead.isPresent()) {
        throw new CommandSpecException("Repository has no HEAD, can't merge.");
    }
    MergeOp merge = transaction.command(MergeOp.class);
    merge.setAuthor(authorName.orNull(), authorEmail.orNull());
    final Optional<ObjectId> oid = transaction.command(RevParse.class).setRefSpec(commit).call();
    if (oid.isPresent()) {
        merge.addCommit(Suppliers.ofInstance(oid.get()));
    } else {
        throw new CommandSpecException("Couldn't resolve '" + commit + "' to a commit.");
    }
    try {
        final MergeReport report = merge.setNoCommit(noCommit).call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeMergeResponse(Optional.fromNullable(report.getMergeCommit()), report.getReport().get(), transaction, report.getOurs(), report.getPairs().get(0).getTheirs(), report.getPairs().get(0).getAncestor());
                out.finish();
            }
        });
    } catch (Exception e) {
        final RevCommit ours = context.getGeoGIG().getRepository().getCommit(currHead.get().getObjectId());
        final RevCommit theirs = context.getGeoGIG().getRepository().getCommit(oid.get());
        final Optional<ObjectId> ancestor = transaction.command(FindCommonAncestor.class).setLeft(ours).setRight(theirs).call();
        context.setResponseContent(new CommandResponse() {

            final MergeScenarioReport report = transaction.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, transaction, ours.getId(), theirs.getId(), ancestor.get());
                out.finish();
            }
        });
    }
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) MergeOp(org.locationtech.geogig.api.porcelain.MergeOp) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) MergeScenarioReport(org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) MergeReport(org.locationtech.geogig.api.porcelain.MergeOp.MergeReport) Ref(org.locationtech.geogig.api.Ref) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 9 with GeogigTransaction

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

the class PushManager method connectionSucceeded.

/**
     * This is called when the machine at the specified ip address is finished pushing objects to
     * the server. This causes the ref given by {@code refSpec} to be updated to point to the given
     * {@code newCommit} object id, as well as the {@link Ref#WORK_HEAD WORK_HEAD} and
     * {@link Ref#STAGE_HEAD STAGE_HEAD} refs if {@code refSpec} is the current branch.
     * 
     * @param geogig the geogig of the local repository
     * @param ipAddress the remote machine that is pushing objects
     */
public void connectionSucceeded(final GeoGIG geogig, final String ipAddress, final String refspec, final ObjectId newCommit) {
    if (!incomingIPs.remove(ipAddress)) {
        // remove and check for existence in one shot
        throw new RuntimeException("Tried to end a connection that didn't exist.");
    }
    // Do not use the geogig instance after this, but the tx one!
    GeogigTransaction tx = geogig.command(TransactionBegin.class).call();
    try {
        Optional<Ref> oldRef = tx.command(RefParse.class).setName(refspec).call();
        Optional<Ref> headRef = tx.command(RefParse.class).setName(Ref.HEAD).call();
        String refName = refspec;
        if (oldRef.isPresent()) {
            if (oldRef.get().getObjectId().equals(newCommit)) {
                LOGGER.info("ref '{}' -> {} not updated, got same id", refName, newCommit);
                return;
            }
            LOGGER.info("Updating ref '{}'[{}] -> {}", refName, oldRef.get().getObjectId(), newCommit);
            refName = oldRef.get().getName();
        } else {
            LOGGER.info("Creating new ref '{}' -> {}", refName, newCommit);
        }
        if (headRef.isPresent() && headRef.get() instanceof SymRef) {
            if (((SymRef) headRef.get()).getTarget().equals(refName)) {
                Optional<ObjectId> commitTreeId = tx.command(ResolveTreeish.class).setTreeish(newCommit).call();
                checkState(commitTreeId.isPresent(), "Commit %s not found", newCommit);
                tx.command(UpdateRef.class).setName(Ref.WORK_HEAD).setNewValue(commitTreeId.get()).call();
                tx.command(UpdateRef.class).setName(Ref.STAGE_HEAD).setNewValue(commitTreeId.get()).call();
            }
        }
        tx.command(UpdateRef.class).setName(refName).setNewValue(newCommit).call();
        tx.commit();
    } catch (Exception e) {
        tx.abort();
        throw Throwables.propagate(e);
    }
}
Also used : 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) GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) ObjectId(org.locationtech.geogig.api.ObjectId) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef)

Example 10 with GeogigTransaction

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

the class BeginTransaction method run.

/**
     * Runs the command and builds the appropriate response.
     * 
     * @param context - the context to use for this command
     * 
     * @throws CommandSpecException
     */
@Override
public void run(CommandContext context) {
    if (this.getTransactionId() != null) {
        throw new CommandSpecException("Tried to start a transaction within a transaction.");
    }
    final GeoGIG geogig = context.getGeoGIG();
    final GeogigTransaction transaction = geogig.command(TransactionBegin.class).call();
    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start();
            out.writeTransactionId(transaction.getTransactionId());
            out.finish();
        }
    });
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) GeoGIG(org.locationtech.geogig.api.GeoGIG) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Aggregations

GeogigTransaction (org.locationtech.geogig.api.GeogigTransaction)19 TransactionBegin (org.locationtech.geogig.api.plumbing.TransactionBegin)14 Test (org.junit.Test)10 RevCommit (org.locationtech.geogig.api.RevCommit)10 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)7 LogOp (org.locationtech.geogig.api.porcelain.LogOp)6 TransactionEnd (org.locationtech.geogig.api.plumbing.TransactionEnd)5 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 Optional (com.google.common.base.Optional)3 Context (org.locationtech.geogig.api.Context)3 GeoGIG (org.locationtech.geogig.api.GeoGIG)3 Ref (org.locationtech.geogig.api.Ref)3 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)3 CommandSpecException (org.locationtech.geogig.web.api.CommandSpecException)3 ResponseWriter (org.locationtech.geogig.web.api.ResponseWriter)3 ObjectId (org.locationtech.geogig.api.ObjectId)2 UpdateRef (org.locationtech.geogig.api.plumbing.UpdateRef)2 MergeScenarioReport (org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport)2 BranchCreateOp (org.locationtech.geogig.api.porcelain.BranchCreateOp)2