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;
}
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
}
}
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();
}
});
}
}
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);
}
}
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();
}
});
}
Aggregations