@Test public void testAddEmptyFeatureTypePatch() throws Exception { Patch patch = new Patch(); RevFeatureType featureType = RevFeatureTypeBuilder.build(pointsType); patch.addFeatureType(featureType); patch.addAlteredTree(new FeatureTypeDiff(pointsName, null, featureType.getId())); testPatch(patch); }
FeatureTypeDiff diff = new FeatureTypeDiff(headerTokens[0], ObjectId.valueOf(headerTokens[1]), ObjectId.valueOf(headerTokens[2])); patch.addAlteredTree(diff); } else {// feature String element = Joiner.on("\n").join(lines.subList(1, lines.size())); patch.addModifiedFeature(featureDiff); } else if (operation.equals("A") || operation.equals("R")) { String fullPath = headerTokens[1].trim(); RevFeature revFeature = (RevFeature) serializer.read(null, stream); if (operation.equals("R")) { patch.addRemovedFeature(fullPath, revFeature, revFeatureType); } else { patch.addAddedFeature(fullPath, revFeature, revFeatureType);
patch = patch.reversed(); console.println(patch.toString()); } else if (check) { VerifyPatchResults verify = cli.getGeogig().command(VerifyPatchOp.class).setPatch(patch) Patch toReject = verify.getToReject(); Patch toApply = verify.getToApply(); if (toReject.isEmpty()) { console.println("Patch can be applied."); } else { console.println("Error: Patch cannot be applied\n"); console.println("Applicable entries:\n"); console.println(toApply.toString()); console.println("\nConflicting entries:\n"); console.println(toReject.toString()); .setApplyPartial(reject).call(); if (reject) { if (rejected.isEmpty()) { console.println("Patch applied succesfully"); } else { int accepted = patch.count() - rejected.count(); StringBuilder sb = new StringBuilder(); File file = new File(patchFile.getAbsolutePath() + ".rej"); sb.append("Patch applied only partially.\n"); sb.append(Integer.toString(accepted) + " changes were applied.\n"); sb.append(Integer.toString(rejected.count()) + " changes were rejected.\n"); BufferedWriter writer = Files.newWriter(file, Charsets.UTF_8);
Preconditions.checkNotNull(reader); Patch patch = new Patch(); List<String> subset = Lists.newArrayList(); Map<String, RevFeatureType> featureTypes = Maps.newHashMap(); .hasNext();) { Entry<String, RevFeatureType> entry = iterator.next(); patch.addFeatureType(entry.getValue());
Patch toReject = verify.getToReject(); Patch toApply = verify.getToApply(); if (toReject.isEmpty()) { console.println("Patch can be applied."); } else { console.println("Error: Patch cannot be applied\n"); console.println("Applicable entries:\n"); console.println(toApply.toString()); console.println("\nConflicting entries:\n"); console.println(toReject.toString());
Patch patch = reverse ? this.patch.reversed() : this.patch; Map<ObjectId, RevFeatureType> typeCache = patch.featureTypes(); Patch toApply = new Patch(typeCache); Patch toReject = new Patch(typeCache); List<FeatureDiff> diffs = patch.getModifiedFeatures(); for (FeatureDiff diff : diffs) { path = diff.getPath(); obj = command(RevObjectParse.class).setRefSpec(refSpec).call(); if (!obj.isPresent()) { toReject.addModifiedFeature(diff); break; toReject.addModifiedFeature(diff); } else { toApply.addModifiedFeature(diff); List<FeatureInfo> added = patch.getAddedFeatures(); for (FeatureInfo feature : added) { String refSpec = Ref.WORK_HEAD + ":" + feature.getPath(); obj = command(RevObjectParse.class).setRefSpec(refSpec).call(); if (obj.isPresent()) { toReject.addAddedFeature(feature.getPath(), feature.getFeature(), getType(feature.getFeatureTypeId(), typeCache)); } else { toApply.addAddedFeature(feature.getPath(), feature.getFeature(),
@Given("^I have a patch file$") public void I_have_a_patch_file() throws Throwable { Patch patch = new Patch(); String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID()); Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap(); Object oldValue = points1.getProperty("sp").getValue(); GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, "new"); map.put(pointsType.getDescriptor("sp"), diff); FeatureDiff feaureDiff = new FeatureDiff(path, map, RevFeatureTypeBuilder.build(pointsType), RevFeatureTypeBuilder.build(pointsType)); patch.addModifiedFeature(feaureDiff); File file = new File(localRepo.platform.pwd(), "test.patch"); BufferedWriter writer = Files.newWriter(file, Charsets.UTF_8); PatchSerializer.write(writer, patch); writer.flush(); writer.close(); }
@Test public void testReversedPatch() throws Exception { insert(points1, points2); Patch patch = new Patch(); String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID()); Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap(); Object oldValue = points1.getProperty("sp").getValue(); GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, "new"); map.put(pointsType.getDescriptor("sp"), diff); FeatureDiff feaureDiff = new FeatureDiff(path, map, RevFeatureTypeBuilder.build(pointsType), RevFeatureTypeBuilder.build(pointsType)); patch.addModifiedFeature(feaureDiff); String removedPath = NodeRef.appendChild(pointsName, points2.getIdentifier().getID()); patch.addRemovedFeature(removedPath, RevFeatureBuilder.build(points2), RevFeatureTypeBuilder.build(pointsType)); String addedPath = NodeRef.appendChild(pointsName, points3.getIdentifier().getID()); patch.addAddedFeature(addedPath, RevFeatureBuilder.build(points3), RevFeatureTypeBuilder.build(pointsType)); geogig.command(ApplyPatchOp.class).setPatch(patch).call(); geogig.command(ApplyPatchOp.class).setPatch(patch.reversed()).call(); RevTree root = repo.workingTree().getTree(); Optional<Node> featureBlobId = findTreeChild(root, removedPath); assertTrue(featureBlobId.isPresent()); featureBlobId = findTreeChild(root, addedPath); assertFalse(featureBlobId.isPresent()); Optional<RevFeature> feature = geogig.command(RevObjectParse.class) .setRefSpec("WORK_HEAD:" + path).call(RevFeature.class); assertTrue(feature.isPresent()); assertEquals(oldValue, feature.get().getValues().get(0).get()); }
@Override protected Patch _call() { Patch patch = new Patch(); Map<ObjectId, RevFeatureType> featureTypes = Maps.newHashMap(); while (diffs.hasNext()) { .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.getNewObject().getMetadataId()) .call(RevFeatureType.class).get(); patch.addFeatureType(oldFeatureType); patch.addFeatureType(newFeatureType); patch.addAlteredTree(diffEntry); patch.addAddedFeature(name, (RevFeature) revObject, featureType); } else if (revObject instanceof RevTree) { ObjectId metadataId = diffEntry.getNewObject().getMetadataId(); RevFeatureType featureType = command(RevObjectParse.class) .setObjectId(metadataId).call(RevFeatureType.class).get(); patch.addAlteredTree(diffEntry); patch.addFeatureType(featureType); patch.addRemovedFeature(name, (RevFeature) revObject, featureType); } else if (revObject instanceof RevTree) { ObjectId metadataId = diffEntry.getOldObject().getMetadataId();
@Test public void testCreatePatch() throws Exception { insertAndAdd(points1, points2); geogig.command(CommitOp.class).setAll(true).call(); final String featureId = points1.getIdentifier().getID(); final Feature modifiedFeature = feature((SimpleFeatureType) points1.getType(), featureId, "changedProp", new Integer(1500), "POINT (2 2)"); insert(modifiedFeature); insert(points3); delete(points2); try (AutoCloseableIterator<DiffEntry> diffs = geogig.command(DiffOp.class).call()) { Patch patch = geogig.command(CreatePatchOp.class).setDiffs(diffs).call(); assertEquals(3, patch.count()); assertEquals(1, patch.getAddedFeatures().size()); assertEquals(1, patch.getRemovedFeatures().size()); assertEquals(1, patch.getModifiedFeatures().size()); assertEquals(RevFeatureTypeBuilder.build(pointsType), patch.getFeatureTypes().get(0)); assertEquals(NodeRef.appendChild(pointsName, idP2), patch.getRemovedFeatures().get(0).getPath()); assertEquals(NodeRef.appendChild(pointsName, idP3), patch.getAddedFeatures().get(0).getPath()); } }
final ObjectStore indexDb = objectDatabase(); if (reverse) { patch = patch.reversed(); objectDatabase().putAll(patch.getFeatureTypes().iterator()); List<FeatureInfo> removed = patch.getRemovedFeatures(); for (FeatureInfo feature : removed) { workTree.delete(feature.getPath()); List<FeatureInfo> added = patch.getAddedFeatures(); for (FeatureInfo feature : added) { workTree.insert(feature); List<FeatureDiff> diffs = patch.getModifiedFeatures(); for (FeatureDiff diff : diffs) { String path = diff.getPath(); ImmutableList<FeatureTypeDiff> alteredTrees = patch.getAlteredTrees(); for (FeatureTypeDiff diff : alteredTrees) { Optional<RevFeatureType> featureType; if (diff.getOldFeatureType().isNull()) { featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType()); workTree.createTypeTree(diff.getPath(), featureType.get().type()); } else if (diff.getNewFeatureType().isNull()) { workTree.delete(diff.getPath()); } else { featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType()); workTree.updateTypeTree(diff.getPath(), featureType.get().type());
@Test public void testRemoveFeatureAttributePatch() throws Exception { Patch patch = new Patch(); String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID()); Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap(); Object oldValue = points1B.getProperty("extra").getValue(); GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, null); map.put(modifiedPointsType.getDescriptor("extra"), diff); FeatureDiff featureDiff = new FeatureDiff(path, map, RevFeatureTypeBuilder.build(modifiedPointsType), RevFeatureTypeBuilder.build(pointsType)); patch.addModifiedFeature(featureDiff); patch.addFeatureType(RevFeatureTypeBuilder.build(pointsType)); testPatch(patch); }
@Test public void testPartialApplication() throws Exception { insert(points1, points2); Patch patch = new Patch(); String pathRemove = NodeRef.appendChild(pointsName, points2.getIdentifier().getID()); patch.addRemovedFeature(pathRemove, RevFeatureBuilder.build(points2), RevFeatureTypeBuilder.build(pointsType)); String pathModify = NodeRef.appendChild(pointsName, points1B.getIdentifier().getID()); Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap(); Object oldValue = points1B.getProperty("extra").getValue(); GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, null); map.put(modifiedPointsType.getDescriptor("extra"), diff); FeatureDiff featureDiff = new FeatureDiff(pathModify, map, RevFeatureTypeBuilder.build(modifiedPointsType), RevFeatureTypeBuilder.build(pointsType)); patch.addModifiedFeature(featureDiff); Patch rejected = geogig.command(ApplyPatchOp.class).setPatch(patch).setApplyPartial(true) .call(); assertFalse(rejected.isEmpty()); RevTree root = repo.workingTree().getTree(); assertNotNull(root); Optional<Node> featureBlobId = findTreeChild(root, pathRemove); assertFalse(featureBlobId.isPresent()); // now we take the rejected patch and apply it, and the new rejected should be identical to // it Patch newRejected = geogig.command(ApplyPatchOp.class).setPatch(rejected) .setApplyPartial(true).call(); assertEquals(rejected, newRejected); }
@Test public void testAddFeaturePatch() throws Exception { Patch patch = new Patch(); String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID()); patch.addAddedFeature(path, RevFeatureBuilder.build(points1), RevFeatureTypeBuilder.build(pointsType)); testPatch(patch); }
@Test public void testRemoveFeaturePatch() throws Exception { Patch patch = new Patch(); String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID()); patch.addRemovedFeature(path, RevFeatureBuilder.build(points1), RevFeatureTypeBuilder.build(pointsType)); testPatch(patch); }
/** * Returns the reversed version of the current patch. Applying this reversed patch has the * opposite effect to applying the original one, and can be used to undo changes * * @return a reversed version of this patch */ public Patch reversed() { Patch patch = new Patch(); patch.removedFeatures = addedFeatures; patch.addedFeatures = removedFeatures; for (FeatureDiff diff : modifiedFeatures) { patch.modifiedFeatures.add(diff.reversed()); } for (FeatureTypeDiff diff : alteredTrees) { patch.alteredTrees.add(diff.reversed()); } return patch; }
public CannotApplyPatchException(Patch patch) { super("Error: Patch cannot be applied\n\nConflicting entries:\n\n" + patch.toString()); this.patch = patch; }
/** * Executes the apply command, applying the given patch If it cannot be applied and no partial * application is allowed, a {@link CannotApplyPatchException} exception is thrown. Returns a * patch with rejected entries, in case partial application is allowed * * @return the modified {@link WorkingTree working tree}. */ @Override protected Patch _call() throws RuntimeException { Preconditions.checkArgument(patch != null, "No patch file provided"); VerifyPatchResults verify = command(VerifyPatchOp.class).setPatch(patch).setReverse(reverse) .call(); Patch toReject = verify.getToReject(); Patch toApply = verify.getToApply(); if (!applyPartial) { if (!toReject.isEmpty()) { throw new CannotApplyPatchException(toReject); } applyPatch(toApply); return null; } else { applyPatch(toApply); return toReject; } }
/** * Adds an element to the list of modified ones * * @param diff */ public void addModifiedFeature(FeatureDiff diff) { modifiedFeatures.add(diff); addFeatureType(diff.getNewFeatureType()); addFeatureType(diff.getOldFeatureType()); }
@Test public void testCreatePatchWithNoChanges() throws Exception { insertAndAdd(points1, points2); geogig.command(CommitOp.class).setAll(true).call(); try (AutoCloseableIterator<DiffEntry> diffs = geogig.command(DiffOp.class).call()) { Patch patch = geogig.command(CreatePatchOp.class).setDiffs(diffs).call(); assertEquals(0, patch.count()); } }