use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class ReportCommitConflictsOp method _call.
@Override
protected MergeScenarioReport _call() {
MergeScenarioReport report = new MergeScenarioReport();
ObjectId parentCommitId = ObjectId.NULL;
if (commit.getParentIds().size() > 0) {
parentCommitId = commit.getParentIds().get(0);
}
ObjectId parentTreeId = ObjectId.NULL;
Repository repository = repository();
if (repository.commitExists(parentCommitId)) {
parentTreeId = repository.getCommit(parentCommitId).getTreeId();
}
// get changes
Iterator<DiffEntry> diffs = command(DiffTree.class).setOldTree(parentTreeId).setNewTree(commit.getTreeId()).setReportTrees(true).call();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
String path = diff.oldPath() == null ? diff.newPath() : diff.oldPath();
Optional<RevObject> obj = command(RevObjectParse.class).setRefSpec(Ref.HEAD + ":" + path).call();
switch(diff.changeType()) {
case ADDED:
if (obj.isPresent()) {
TYPE type = command(ResolveObjectType.class).setObjectId(diff.getNewObject().objectId()).call();
if (TYPE.TREE.equals(type)) {
NodeRef headVersion = command(FindTreeChild.class).setChildPath(path).setParent(repository.getOrCreateHeadTree()).call().get();
if (!headVersion.getMetadataId().equals(diff.getNewObject().getMetadataId())) {
report.addConflict(new Conflict(path, ObjectId.NULL, diff.getNewObject().getMetadataId(), headVersion.getMetadataId()));
}
} else {
if (!obj.get().getId().equals(diff.newObjectId())) {
report.addConflict(new Conflict(path, ObjectId.NULL, diff.newObjectId(), obj.get().getId()));
}
}
} else {
report.addUnconflicted(diff);
}
break;
case REMOVED:
if (obj.isPresent()) {
if (obj.get().getId().equals(diff.oldObjectId())) {
report.addUnconflicted(diff);
} else {
report.addConflict(new Conflict(path, diff.oldObjectId(), ObjectId.NULL, obj.get().getId()));
}
}
break;
case MODIFIED:
TYPE type = command(ResolveObjectType.class).setObjectId(diff.getNewObject().objectId()).call();
if (TYPE.TREE.equals(type)) {
// one
if (!diff.isChange()) {
report.addUnconflicted(diff);
}
} else {
String refSpec = Ref.HEAD + ":" + path;
obj = command(RevObjectParse.class).setRefSpec(refSpec).call();
if (!obj.isPresent()) {
// git reports this as a conflict but does not mark as conflicted, just adds
// the missing file.
// We add it and consider it unconflicted
report.addUnconflicted(diff);
break;
}
RevFeature feature = (RevFeature) obj.get();
DepthSearch depthSearch = new DepthSearch(repository.objectDatabase());
Optional<NodeRef> noderef = depthSearch.find(this.workingTree().getTree(), path);
RevFeatureType featureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
FeatureDiff featureDiff = command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).setNewVersion(Suppliers.ofInstance(diff.getNewObject())).call();
Set<Entry<PropertyDescriptor, AttributeDiff>> attrDiffs = featureDiff.getDiffs().entrySet();
RevFeature newFeature = command(RevObjectParse.class).setObjectId(diff.newObjectId()).call(RevFeature.class).get();
boolean ok = true;
for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = attrDiffs.iterator(); iterator.hasNext() && ok; ) {
Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
AttributeDiff attrDiff = entry.getValue();
PropertyDescriptor descriptor = entry.getKey();
switch(attrDiff.getType()) {
case ADDED:
if (descriptors.contains(descriptor)) {
ok = false;
}
break;
case REMOVED:
case MODIFIED:
if (!descriptors.contains(descriptor)) {
ok = false;
break;
}
for (int i = 0; i < descriptors.size(); i++) {
if (descriptors.get(i).equals(descriptor)) {
Optional<Object> value = feature.getValues().get(i);
Optional<Object> newValue = newFeature.getValues().get(i);
if (!newValue.equals(value)) {
// check
if (!attrDiff.canBeAppliedOn(value)) {
ok = false;
}
break;
}
}
}
}
}
if (ok) {
report.addUnconflicted(diff);
} else {
report.addConflict(new Conflict(path, diff.oldObjectId(), diff.newObjectId(), obj.get().getId()));
}
}
break;
}
}
return report;
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class ReportMergeScenarioOp method _call.
@Override
protected MergeScenarioReport _call() {
Optional<ObjectId> ancestor = command(FindCommonAncestor.class).setLeft(toMerge).setRight(mergeInto).call();
Preconditions.checkState(ancestor.isPresent(), "No ancestor commit could be found.");
Map<String, DiffEntry> mergeIntoDiffs = Maps.newHashMap();
MergeScenarioReport report = new MergeScenarioReport();
Iterator<DiffEntry> diffs = command(DiffTree.class).setOldTree(ancestor.get()).setReportTrees(true).setNewTree(mergeInto.getId()).call();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
String path = diff.oldPath() == null ? diff.newPath() : diff.oldPath();
mergeIntoDiffs.put(path, diff);
}
Iterator<DiffEntry> toMergeDiffs = command(DiffTree.class).setOldTree(ancestor.get()).setReportTrees(true).setNewTree(toMerge.getId()).call();
while (toMergeDiffs.hasNext()) {
DiffEntry toMergeDiff = toMergeDiffs.next();
String path = toMergeDiff.oldPath() == null ? toMergeDiff.newPath() : toMergeDiff.oldPath();
if (mergeIntoDiffs.containsKey(path)) {
RevCommit ancestorCommit = command(RevObjectParse.class).setRefSpec(ancestor.get().toString()).call(RevCommit.class).get();
RevTree ancestorTree = command(RevObjectParse.class).setObjectId(ancestorCommit.getTreeId()).call(RevTree.class).get();
Optional<NodeRef> ancestorVersion = command(FindTreeChild.class).setChildPath(path).setParent(ancestorTree).call();
ObjectId ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getNode().getObjectId() : ObjectId.NULL;
ObjectId theirs = toMergeDiff.getNewObject() == null ? ObjectId.NULL : toMergeDiff.getNewObject().objectId();
DiffEntry mergeIntoDiff = mergeIntoDiffs.get(path);
ObjectId ours = mergeIntoDiff.getNewObject() == null ? ObjectId.NULL : mergeIntoDiff.getNewObject().objectId();
if (!mergeIntoDiff.changeType().equals(toMergeDiff.changeType())) {
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
continue;
}
switch(toMergeDiff.changeType()) {
case ADDED:
if (toMergeDiff.getNewObject().equals(mergeIntoDiff.getNewObject())) {
// already added in current branch, no need to do anything
} else {
TYPE type = command(ResolveObjectType.class).setObjectId(toMergeDiff.getNewObject().objectId()).call();
if (TYPE.TREE.equals(type)) {
boolean conflict = !toMergeDiff.getNewObject().getMetadataId().equals(mergeIntoDiff.getNewObject().getMetadataId());
if (conflict) {
// In this case, we store the metadata id, not the element id
ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getMetadataId() : ObjectId.NULL;
ours = mergeIntoDiff.getNewObject().getMetadataId();
theirs = toMergeDiff.getNewObject().getMetadataId();
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
}
// if the metadata ids match, it means both branches have added the same
// tree, maybe with different content, but there is no need to do
// anything. The correct tree is already there and the merge can be run
// safely, so we do not add it neither as a conflicted change nor as an
// unconflicted one
} else {
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
}
}
break;
case REMOVED:
// removed by both histories => no conflict and no need to do anything
break;
case MODIFIED:
TYPE type = command(ResolveObjectType.class).setObjectId(toMergeDiff.getNewObject().objectId()).call();
if (TYPE.TREE.equals(type)) {
boolean conflict = !toMergeDiff.getNewObject().getMetadataId().equals(mergeIntoDiff.getNewObject().getMetadataId());
if (conflict) {
// In this case, we store the metadata id, not the element id
ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getMetadataId() : ObjectId.NULL;
ours = mergeIntoDiff.getNewObject().getMetadataId();
theirs = toMergeDiff.getNewObject().getMetadataId();
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
}
} else {
FeatureDiff toMergeFeatureDiff = command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(toMergeDiff.getOldObject())).setNewVersion(Suppliers.ofInstance(toMergeDiff.getNewObject())).call();
FeatureDiff mergeIntoFeatureDiff = command(DiffFeature.class).setOldVersion(Suppliers.ofInstance(mergeIntoDiff.getOldObject())).setNewVersion(Suppliers.ofInstance(mergeIntoDiff.getNewObject())).call();
if (toMergeFeatureDiff.conflicts(mergeIntoFeatureDiff)) {
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
} else {
// try to perform automerge
if (!toMergeDiff.getNewObject().getMetadataId().equals(mergeIntoDiff.getNewObject().getMetadataId())) {
report.addConflict(new Conflict(path, ancestorVersionId, ours, theirs));
} else if (!toMergeFeatureDiff.equals(mergeIntoFeatureDiff)) {
Feature mergedFeature = command(MergeFeaturesOp.class).setFirstFeature(mergeIntoDiff.getNewObject()).setSecondFeature(toMergeDiff.getNewObject()).setAncestorFeature(mergeIntoDiff.getOldObject()).call();
RevFeature revFeature = RevFeatureBuilder.build(mergedFeature);
if (revFeature.getId().equals(toMergeDiff.newObjectId())) {
// the resulting merged feature equals the feature to merge from
// the branch, which means that it exists in the repo and there
// is no need to add it
report.addUnconflicted(toMergeDiff);
} else {
RevFeatureType featureType = command(RevObjectParse.class).setObjectId(mergeIntoDiff.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
FeatureInfo merged = new FeatureInfo(mergedFeature, featureType, path);
report.addMerged(merged);
}
}
}
}
break;
}
} else {
// modified in the other branch under it.
if (ChangeType.REMOVED.equals(toMergeDiff.changeType())) {
TYPE type = command(ResolveObjectType.class).setObjectId(toMergeDiff.oldObjectId()).call();
if (TYPE.TREE.equals(type)) {
String parentPath = toMergeDiff.oldPath();
Set<Entry<String, DiffEntry>> entries = mergeIntoDiffs.entrySet();
boolean conflict = false;
for (Entry<String, DiffEntry> entry : entries) {
if (entry.getKey().startsWith(parentPath)) {
if (!ChangeType.REMOVED.equals(entry.getValue().changeType())) {
RevCommit ancestorCommit = command(RevObjectParse.class).setRefSpec(ancestor.get().toString()).call(RevCommit.class).get();
RevTree ancestorTree = command(RevObjectParse.class).setObjectId(ancestorCommit.getTreeId()).call(RevTree.class).get();
Optional<NodeRef> ancestorVersion = command(FindTreeChild.class).setChildPath(path).setParent(ancestorTree).call();
ObjectId ancestorVersionId = ancestorVersion.isPresent() ? ancestorVersion.get().getNode().getObjectId() : ObjectId.NULL;
ObjectId theirs = toMergeDiff.getNewObject() == null ? ObjectId.NULL : toMergeDiff.getNewObject().objectId();
String oursRefSpec = mergeInto.getId().toString() + ":" + parentPath;
Optional<ObjectId> ours = command(RevParse.class).setRefSpec(oursRefSpec).call();
report.addConflict(new Conflict(path, ancestorVersionId, ours.get(), theirs));
conflict = true;
break;
}
}
}
if (!conflict) {
report.addUnconflicted(toMergeDiff);
}
} else {
report.addUnconflicted(toMergeDiff);
}
} else {
report.addUnconflicted(toMergeDiff);
}
}
}
return report;
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class AddOp method stage.
/**
* Stages the object addressed by {@code pathFilter}, or all unstaged objects if
* {@code pathFilter == null} to be added, if it is/they are marked as an unstaged change. Does
* nothing otherwise.
* <p>
* To stage changes not yet staged, a diff tree walk is performed using the current staged
* {@link RevTree} as the old object and the current unstaged {@link RevTree} as the new object.
* Then all the differences are traversed and the staged tree is updated with the changes
* reported by the diff walk (neat).
* </p>
*
* @param progress the progress listener for this process
* @param pathFilter the filter to use
*/
public void stage(final ProgressListener progress, @Nullable final String pathFilter) {
// working tree, so it's just a matter of updating the index ref to working tree RevTree id
if (null == pathFilter && !index().getStaged(null).hasNext() && !updateOnly && index().countConflicted(null) == 0) {
progress.started();
Optional<ObjectId> workHead = command(RevParse.class).setRefSpec(Ref.WORK_HEAD).call();
if (workHead.isPresent()) {
command(UpdateRef.class).setName(Ref.STAGE_HEAD).setNewValue(workHead.get()).call();
}
progress.setProgress(100f);
progress.complete();
return;
}
final long numChanges = workingTree().countUnstaged(pathFilter).count();
Iterator<DiffEntry> unstaged = workingTree().getUnstaged(pathFilter);
if (updateOnly) {
unstaged = Iterators.filter(unstaged, new Predicate<DiffEntry>() {
@Override
public boolean apply(@Nullable DiffEntry input) {
// HACK: avoid reporting changed trees
if (input.isChange() && input.getOldObject().getType().equals(TYPE.TREE)) {
return false;
}
return input.getOldObject() != null;
}
});
}
index().stage(progress, unstaged, numChanges);
List<Conflict> conflicts = index().getConflicted(pathFilter);
for (Conflict conflict : conflicts) {
// if we are staging unmerged files, the conflict should get solved. However, if the
// working index object is the same as the staging area one (for instance, after running
// checkout --ours), it will not be reported by the getUnstaged method. We solve that
// here.
stagingDatabase().removeConflict(null, conflict.getPath());
}
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.
the class WriteTree2 method applyChanges.
private RevTree applyChanges(@Nullable final NodeRef leftTreeRef, @Nullable final NodeRef rightTreeRef) {
Preconditions.checkArgument(leftTreeRef != null || rightTreeRef != null, "either left or right tree shall be non null");
final ObjectDatabase repositoryDatabase = objectDatabase();
final String treePath = rightTreeRef == null ? leftTreeRef.path() : rightTreeRef.path();
final List<String> strippedPathFilters = stripParentAndFiltersThatDontApply(this.pathFilters, treePath);
// find the diffs that apply to the path filters
final ObjectId leftTreeId = leftTreeRef == null ? RevTree.EMPTY_TREE_ID : leftTreeRef.objectId();
final ObjectId rightTreeId = rightTreeRef == null ? RevTree.EMPTY_TREE_ID : rightTreeRef.objectId();
final Predicate<Bounded> existsFilter = new Predicate<Bounded>() {
private final ObjectDatabase targetDb = repositoryDatabase;
@Override
public boolean apply(Bounded input) {
ObjectId id = null;
if (input instanceof Node && TYPE.TREE.equals(((Node) input).getType())) {
id = ((Node) input).getObjectId();
} else if (input instanceof Bucket) {
Bucket b = (Bucket) input;
id = b.id();
}
if (id != null) {
if (targetDb.exists(id)) {
LOGGER.trace("Ignoring {}. Already exists in target database.", input);
return false;
}
}
return true;
}
};
DiffTree diffs = command(DiffTree.class).setRecursive(false).setReportTrees(false).setOldTree(leftTreeId).setNewTree(rightTreeId).setPathFilter(strippedPathFilters).setCustomFilter(existsFilter);
// move new blobs from the index to the repository (note: this could be parallelized)
Supplier<Iterator<Node>> nodesToMove = asNodeSupplierOfNewContents(diffs, strippedPathFilters);
command(DeepMove.class).setObjects(nodesToMove).call();
final StagingDatabase stagingDatabase = stagingDatabase();
final RevTree currentLeftTree = stagingDatabase.getTree(leftTreeId);
final RevTreeBuilder builder = currentLeftTree.builder(repositoryDatabase);
// remove the exists filter, we need to create the new trees taking into account all the
// nodes
diffs.setCustomFilter(null);
Iterator<DiffEntry> iterator = diffs.get();
if (!strippedPathFilters.isEmpty()) {
final Set<String> expected = Sets.newHashSet(strippedPathFilters);
iterator = Iterators.filter(iterator, new Predicate<DiffEntry>() {
@Override
public boolean apply(DiffEntry input) {
boolean applies;
if (input.isDelete()) {
applies = expected.contains(input.oldName());
} else {
applies = expected.contains(input.newName());
}
return applies;
}
});
}
for (; iterator.hasNext(); ) {
final DiffEntry diff = iterator.next();
if (diff.isDelete()) {
builder.remove(diff.oldName());
} else {
NodeRef newObject = diff.getNewObject();
Node node = newObject.getNode();
builder.put(node);
}
}
final RevTree newTree = builder.build();
repositoryDatabase.put(newTree);
return newTree;
}
use of org.locationtech.geogig.api.plumbing.diff.DiffEntry 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();
}
});
}
}
Aggregations