use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class CreatePatchOp method _call.
@Override
protected Patch _call() {
Patch patch = new Patch();
Map<ObjectId, RevFeatureType> featureTypes = Maps.newHashMap();
while (diffs.hasNext()) {
DiffEntry diffEntry = diffs.next();
final NodeRef newObject = diffEntry.getNewObject();
final NodeRef oldObject = diffEntry.getOldObject();
if (diffEntry.changeType() == ChangeType.MODIFIED) {
RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.newObjectId()).call().get();
if (revObject instanceof RevFeature) {
FeatureDiff diff = command(DiffFeature.class).setNewVersion(Suppliers.ofInstance(diffEntry.getNewObject())).setOldVersion(Suppliers.ofInstance(diffEntry.getOldObject())).call();
patch.addModifiedFeature(diff);
} else if (revObject instanceof RevTree) {
RevFeatureType oldFeatureType = command(RevObjectParse.class).setObjectId(diffEntry.getOldObject().getMetadataId()).call(RevFeatureType.class).get();
RevFeatureType newFeatureType = command(RevObjectParse.class).setObjectId(diffEntry.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
patch.addFeatureType(oldFeatureType);
patch.addFeatureType(newFeatureType);
patch.addAlteredTree(diffEntry);
}
} else if (diffEntry.changeType() == ChangeType.ADDED) {
RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.newObjectId()).call().get();
if (revObject instanceof RevFeature) {
RevFeatureType featureType;
if (featureTypes.containsKey(newObject.getMetadataId())) {
featureType = featureTypes.get(newObject.getMetadataId());
} else {
featureType = command(RevObjectParse.class).setObjectId(newObject.getMetadataId()).call(RevFeatureType.class).get();
featureTypes.put(newObject.getMetadataId(), featureType);
}
FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
Feature feature = featureBuilder.build(diffEntry.newObjectId().toString(), (RevFeature) revObject);
String name = diffEntry.newPath();
patch.addAddedFeature(name, feature, featureType);
} else if (revObject instanceof RevTree) {
ObjectId metadataId = diffEntry.getNewObject().getMetadataId();
if (!metadataId.isNull()) {
RevFeatureType featureType = command(RevObjectParse.class).setObjectId(metadataId).call(RevFeatureType.class).get();
patch.addAlteredTree(diffEntry);
patch.addFeatureType(featureType);
}
}
} else if (diffEntry.changeType() == ChangeType.REMOVED) {
RevObject revObject = command(RevObjectParse.class).setObjectId(diffEntry.oldObjectId()).call().get();
if (revObject instanceof RevFeature) {
RevFeatureType featureType;
if (featureTypes.containsKey(oldObject.getMetadataId())) {
featureType = featureTypes.get(oldObject.getMetadataId());
} else {
featureType = command(RevObjectParse.class).setObjectId(oldObject.getMetadataId()).call(RevFeatureType.class).get();
featureTypes.put(oldObject.getMetadataId(), featureType);
}
FeatureBuilder featureBuilder = new FeatureBuilder(featureType);
Feature feature = featureBuilder.build(diffEntry.oldObjectId().toString(), (RevFeature) revObject);
String name = diffEntry.oldPath();
patch.addRemovedFeature(name, feature, featureType);
} else if (revObject instanceof RevTree) {
ObjectId metadataId = diffEntry.getOldObject().getMetadataId();
if (!metadataId.isNull()) {
RevFeatureType featureType = command(RevObjectParse.class).setObjectId(metadataId).call(RevFeatureType.class).get();
patch.addAlteredTree(diffEntry);
patch.addFeatureType(featureType);
}
}
}
}
return patch;
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class CherryPickOp method _call.
/**
* Executes the cherry pick operation.
*
* @return RevCommit the new commit with the changes from the cherry-picked commit
*/
@Override
protected RevCommit _call() {
final Repository repository = repository();
final Optional<Ref> currHead = command(RefParse.class).setName(Ref.HEAD).call();
Preconditions.checkState(currHead.isPresent(), "Repository has no HEAD, can't cherry pick.");
Preconditions.checkState(currHead.get() instanceof SymRef, "Can't cherry pick from detached HEAD");
final SymRef headRef = (SymRef) currHead.get();
Preconditions.checkState(index().isClean() && workingTree().isClean(), "You must have a clean working tree and index to perform a cherry pick.");
getProgressListener().started();
Preconditions.checkArgument(repository.commitExists(commit), "Commit could not be resolved: %s.", commit);
RevCommit commitToApply = repository.getCommit(commit);
ObjectId headId = headRef.getObjectId();
ObjectId parentCommitId = ObjectId.NULL;
if (commitToApply.getParentIds().size() > 0) {
parentCommitId = commitToApply.getParentIds().get(0);
}
ObjectId parentTreeId = ObjectId.NULL;
if (repository.commitExists(parentCommitId)) {
parentTreeId = repository.getCommit(parentCommitId).getTreeId();
}
// get changes
Iterator<DiffEntry> diff = command(DiffTree.class).setOldTree(parentTreeId).setNewTree(commitToApply.getTreeId()).setReportTrees(true).call();
// see if there are conflicts
MergeScenarioReport report = command(ReportCommitConflictsOp.class).setCommit(commitToApply).call();
if (report.getConflicts().isEmpty()) {
// stage changes
index().stage(getProgressListener(), diff, 0);
// write new tree
ObjectId newTreeId = command(WriteTree2.class).call();
RevCommit newCommit = command(CommitOp.class).setCommit(commitToApply).call();
repository.workingTree().updateWorkHead(newTreeId);
repository.index().updateStageHead(newTreeId);
getProgressListener().complete();
return newCommit;
} else {
Iterator<DiffEntry> unconflicted = report.getUnconflicted().iterator();
// stage changes
index().stage(getProgressListener(), unconflicted, 0);
workingTree().updateWorkHead(index().getTree().getId());
command(UpdateRef.class).setName(Ref.CHERRY_PICK_HEAD).setNewValue(commit).call();
command(UpdateRef.class).setName(Ref.ORIG_HEAD).setNewValue(headId).call();
command(ConflictsWriteOp.class).setConflicts(report.getConflicts()).call();
StringBuilder msg = new StringBuilder();
msg.append("error: could not apply ");
msg.append(commitToApply.getId().toString().substring(0, 7));
msg.append(" " + commitToApply.getMessage());
for (Conflict conflict : report.getConflicts()) {
msg.append("\t" + conflict.getPath() + "\n");
}
StringBuilder sb = new StringBuilder();
for (Conflict conflict : report.getConflicts()) {
sb.append("CONFLICT: conflict in " + conflict.getPath() + "\n");
}
sb.append("Fix conflicts and then commit the result using 'geogig commit -c " + commitToApply.getId().toString().substring(0, 7) + "\n");
throw new IllegalStateException(sb.toString());
}
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class ExportDiffOp method getFeatures.
private static Iterator<SimpleFeature> getFeatures(Iterator<DiffEntry> diffs, final boolean old, final ObjectDatabase database, final ObjectId metadataId, final ProgressListener progressListener) {
final SimpleFeatureType featureType = addFidAttribute(database.getFeatureType(metadataId));
final RevFeatureType revFeatureType = RevFeatureTypeImpl.build(featureType);
final SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
Function<DiffEntry, SimpleFeature> asFeature = new Function<DiffEntry, SimpleFeature>() {
@Override
@Nullable
public SimpleFeature apply(final DiffEntry input) {
NodeRef nodeRef = old ? input.getOldObject() : input.getNewObject();
if (nodeRef == null) {
return null;
}
final RevFeature revFeature = database.getFeature(nodeRef.objectId());
ImmutableList<Optional<Object>> values = revFeature.getValues();
for (int i = 0; i < values.size(); i++) {
String name = featureType.getDescriptor(i + 1).getLocalName();
Object value = values.get(i).orNull();
featureBuilder.set(name, value);
}
featureBuilder.set("geogig_fid", nodeRef.name());
Feature feature = featureBuilder.buildFeature(nodeRef.name());
feature.getUserData().put(Hints.USE_PROVIDED_FID, true);
feature.getUserData().put(RevFeature.class, revFeature);
feature.getUserData().put(RevFeatureType.class, revFeatureType);
if (feature instanceof SimpleFeature) {
return (SimpleFeature) feature;
}
return null;
}
};
Iterator<SimpleFeature> asFeatures = Iterators.transform(diffs, asFeature);
UnmodifiableIterator<SimpleFeature> filterNulls = Iterators.filter(asFeatures, Predicates.notNull());
return filterNulls;
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class Reset method runInternal.
/**
* Executes the reset command using the provided options.
*
* @param cli
* @see org.locationtech.geogig.cli.AbstractCommand#runInternal(org.locationtech.geogig.cli.GeogigCLI)
*/
@Override
public void runInternal(GeogigCLI cli) {
final GeoGIG geogig = cli.getGeogig();
ResetMode mode = resolveResetMode();
ResetOp reset = cli.getGeogig().command(ResetOp.class);
try {
for (int i = 0; args != null && i < args.size(); i++) {
reset.addPattern(args.get(i));
}
if (commit != null && commit.size() > 0) {
Optional<ObjectId> commitId = geogig.command(RevParse.class).setRefSpec(commit.get(0)).call();
checkParameter(commitId.isPresent(), "Commit could not be resolved.");
reset.setCommit(Suppliers.ofInstance(commitId.get()));
}
reset.setMode(mode);
reset.call();
} catch (IllegalArgumentException iae) {
throw new CommandFailedException(iae.getMessage(), iae);
} catch (IllegalStateException ise) {
throw new CommandFailedException(ise.getMessage(), ise);
}
if (!geogig.getRepository().workingTree().isClean()) {
try {
Iterator<DiffEntry> unstaged = geogig.command(DiffWorkTree.class).setFilter(null).call();
cli.getConsole().println("Unstaged changes after reset:");
while (unstaged.hasNext()) {
DiffEntry entry = unstaged.next();
ChangeType type = entry.changeType();
switch(type) {
case ADDED:
cli.getConsole().println("A\t" + entry.newPath());
break;
case MODIFIED:
cli.getConsole().println("M\t" + entry.newPath());
break;
case REMOVED:
cli.getConsole().println("D\t" + entry.oldPath());
break;
}
}
} catch (IOException e) {
}
}
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class GeogigTransactionState method composeDefaultCommitMessage.
private String composeDefaultCommitMessage() {
Iterator<DiffEntry> indexDiffs = this.geogigTx.command(DiffIndex.class).call();
int added = 0, removed = 0, modified = 0;
StringBuilder msg = new StringBuilder();
while (indexDiffs.hasNext()) {
DiffEntry entry = indexDiffs.next();
switch(entry.changeType()) {
case ADDED:
added++;
break;
case MODIFIED:
modified++;
break;
case REMOVED:
removed++;
break;
}
if ((added + removed + modified) < 10) {
msg.append("\n ").append(entry.changeType().toString().toLowerCase()).append(' ').append(entry.newPath() == null ? entry.oldName() : entry.newPath());
}
}
int count = added + removed + modified;
if (count > 10) {
msg.append("\n And ").append(count - 10).append(" more changes.");
}
StringBuilder title = new StringBuilder();
if (added > 0) {
title.append("added ").append(added);
}
if (modified > 0) {
if (title.length() > 0) {
title.append(", ");
}
title.append("modified ").append(modified);
}
if (removed > 0) {
if (title.length() > 0) {
title.append(", ");
}
title.append("removed ").append(removed);
}
if (count > 0) {
title.append(" features via unversioned legacy client.\n");
}
msg.insert(0, title);
return msg.toString();
}
Aggregations