Search in sources :

Example 36 with NodeRef

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

the class OSMMapTest method testMappingWithPolygons.

@Test
public void testMappingWithPolygons() throws Exception {
    // test a mapping with a a mapping rule that uses the polygon geometry type
    String filename = OSMImportOp.class.getResource("closed_ways.xml").getFile();
    File file = new File(filename);
    cli.execute("osm", "import", file.getAbsolutePath());
    cli.execute("add");
    cli.execute("commit", "-m", "message");
    Optional<RevTree> tree = cli.getGeogig().command(RevObjectParse.class).setRefSpec("HEAD:node").call(RevTree.class);
    assertTrue(tree.isPresent());
    assertTrue(tree.get().size() > 0);
    tree = cli.getGeogig().command(RevObjectParse.class).setRefSpec("HEAD:way").call(RevTree.class);
    assertTrue(tree.isPresent());
    assertTrue(tree.get().size() > 0);
    String mappingFilename = OSMMap.class.getResource("polygons_mapping.json").getFile();
    File mappingFile = new File(mappingFilename);
    cli.execute("osm", "map", mappingFile.getAbsolutePath());
    Iterator<NodeRef> iter = cli.getGeogig().command(LsTreeOp.class).setReference("HEAD:areas").call();
    assertTrue(iter.hasNext());
    Optional<RevFeatureType> ft = cli.getGeogig().command(ResolveFeatureType.class).setRefSpec("HEAD:" + iter.next().path()).call();
    assertTrue(ft.isPresent());
    assertEquals(Polygon.class, ft.get().sortedDescriptors().get(1).getType().getBinding());
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) OSMImportOp(org.locationtech.geogig.osm.internal.OSMImportOp) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) File(java.io.File) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Example 37 with NodeRef

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

the class CheckoutOp method _call.

/**
     * @return the id of the new work tree
     */
@Override
protected CheckoutResult _call() {
    checkState(branchOrCommit != null || !paths.isEmpty(), "No branch, tree, or path were specified");
    checkArgument(!(ours && theirs), "Cannot use both --ours and --theirs.");
    checkArgument((ours == theirs) || branchOrCommit == null, "--ours/--theirs is incompatible with switching branches.");
    CheckoutResult result = new CheckoutResult();
    List<Conflict> conflicts = stagingDatabase().getConflicts(null, null);
    if (!paths.isEmpty()) {
        result.setResult(CheckoutResult.Results.UPDATE_OBJECTS);
        Optional<RevTree> tree = Optional.absent();
        List<String> unmerged = lookForUnmerged(conflicts, paths);
        if (!unmerged.isEmpty()) {
            if (!(force || ours || theirs)) {
                StringBuilder msg = new StringBuilder();
                for (String path : unmerged) {
                    msg.append("error: path " + path + " is unmerged.\n");
                }
                throw new CheckoutException(msg.toString(), StatusCode.UNMERGED_PATHS);
            }
        }
        if (branchOrCommit != null) {
            Optional<ObjectId> id = command(ResolveTreeish.class).setTreeish(branchOrCommit).call();
            checkState(id.isPresent(), "'" + branchOrCommit + "' not found in repository.");
            tree = command(RevObjectParse.class).setObjectId(id.get()).call(RevTree.class);
        } else {
            tree = Optional.of(index().getTree());
        }
        Optional<RevTree> mainTree = tree;
        for (String st : paths) {
            if (unmerged.contains(st)) {
                if (ours || theirs) {
                    String refspec = ours ? Ref.ORIG_HEAD : Ref.MERGE_HEAD;
                    Optional<ObjectId> treeId = command(ResolveTreeish.class).setTreeish(refspec).call();
                    if (treeId.isPresent()) {
                        tree = command(RevObjectParse.class).setObjectId(treeId.get()).call(RevTree.class);
                    }
                } else {
                    // --force
                    continue;
                }
            } else {
                tree = mainTree;
            }
            Optional<NodeRef> node = command(FindTreeChild.class).setParent(tree.get()).setIndex(true).setChildPath(st).call();
            if ((ours || theirs) && !node.isPresent()) {
                // remove the node.
                command(RemoveOp.class).addPathToRemove(st).call();
            } else {
                checkState(node.isPresent(), "pathspec '" + st + "' didn't match a feature in the tree");
                if (node.get().getType() == TYPE.TREE) {
                    RevTreeBuilder treeBuilder = new RevTreeBuilder(stagingDatabase(), workingTree().getTree());
                    treeBuilder.remove(st);
                    treeBuilder.put(node.get().getNode());
                    RevTree newRoot = treeBuilder.build();
                    stagingDatabase().put(newRoot);
                    workingTree().updateWorkHead(newRoot.getId());
                } else {
                    ObjectId metadataId = ObjectId.NULL;
                    Optional<NodeRef> parentNode = command(FindTreeChild.class).setParent(workingTree().getTree()).setChildPath(node.get().getParentPath()).setIndex(true).call();
                    RevTreeBuilder treeBuilder = null;
                    if (parentNode.isPresent()) {
                        metadataId = parentNode.get().getMetadataId();
                        Optional<RevTree> parsed = command(RevObjectParse.class).setObjectId(parentNode.get().getNode().getObjectId()).call(RevTree.class);
                        checkState(parsed.isPresent(), "Parent tree couldn't be found in the repository.");
                        treeBuilder = new RevTreeBuilder(stagingDatabase(), parsed.get());
                        treeBuilder.remove(node.get().getNode().getName());
                    } else {
                        treeBuilder = new RevTreeBuilder(stagingDatabase());
                    }
                    treeBuilder.put(node.get().getNode());
                    ObjectId newTreeId = command(WriteBack.class).setAncestor(workingTree().getTree().builder(stagingDatabase())).setChildPath(node.get().getParentPath()).setToIndex(true).setTree(treeBuilder.build()).setMetadataId(metadataId).call();
                    workingTree().updateWorkHead(newTreeId);
                }
            }
        }
    } else {
        if (!conflicts.isEmpty()) {
            if (!(force)) {
                StringBuilder msg = new StringBuilder();
                for (Conflict conflict : conflicts) {
                    msg.append("error: " + conflict.getPath() + " needs merge.\n");
                }
                msg.append("You need to resolve your index first.\n");
                throw new CheckoutException(msg.toString(), StatusCode.UNMERGED_PATHS);
            }
        }
        Optional<Ref> targetRef = Optional.absent();
        Optional<ObjectId> targetCommitId = Optional.absent();
        Optional<ObjectId> targetTreeId = Optional.absent();
        targetRef = command(RefParse.class).setName(branchOrCommit).call();
        if (targetRef.isPresent()) {
            ObjectId commitId = targetRef.get().getObjectId();
            if (targetRef.get().getName().startsWith(Ref.REMOTES_PREFIX)) {
                String remoteName = targetRef.get().getName();
                remoteName = remoteName.substring(Ref.REMOTES_PREFIX.length(), targetRef.get().getName().lastIndexOf("/"));
                if (branchOrCommit.contains(remoteName + '/')) {
                    RevCommit commit = command(RevObjectParse.class).setObjectId(commitId).call(RevCommit.class).get();
                    targetTreeId = Optional.of(commit.getTreeId());
                    targetCommitId = Optional.of(commit.getId());
                    targetRef = Optional.absent();
                } else {
                    Ref branch = command(BranchCreateOp.class).setName(targetRef.get().localName()).setSource(commitId.toString()).call();
                    command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branch.localName() + ".remote").setValue(remoteName).call();
                    command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branch.localName() + ".merge").setValue(targetRef.get().getName()).call();
                    targetRef = Optional.of(branch);
                    result.setResult(CheckoutResult.Results.CHECKOUT_REMOTE_BRANCH);
                    result.setRemoteName(remoteName);
                }
            }
            if (commitId.isNull()) {
                targetTreeId = Optional.of(ObjectId.NULL);
                targetCommitId = Optional.of(ObjectId.NULL);
            } else {
                Optional<RevCommit> parsed = command(RevObjectParse.class).setObjectId(commitId).call(RevCommit.class);
                checkState(parsed.isPresent());
                checkState(parsed.get() instanceof RevCommit);
                RevCommit commit = parsed.get();
                targetCommitId = Optional.of(commit.getId());
                targetTreeId = Optional.of(commit.getTreeId());
            }
        } else {
            final Optional<ObjectId> addressed = command(RevParse.class).setRefSpec(branchOrCommit).call();
            checkArgument(addressed.isPresent(), "source '" + branchOrCommit + "' not found in repository");
            RevCommit commit = command(RevObjectParse.class).setObjectId(addressed.get()).call(RevCommit.class).get();
            targetTreeId = Optional.of(commit.getTreeId());
            targetCommitId = Optional.of(commit.getId());
        }
        if (targetTreeId.isPresent()) {
            if (!force) {
                if (!index().isClean() || !workingTree().isClean()) {
                    throw new CheckoutException(StatusCode.LOCAL_CHANGES_NOT_COMMITTED);
                }
            }
            // update work tree
            ObjectId treeId = targetTreeId.get();
            workingTree().updateWorkHead(treeId);
            index().updateStageHead(treeId);
            result.setNewTree(treeId);
            if (targetRef.isPresent()) {
                // update HEAD
                Ref target = targetRef.get();
                String refName;
                if (target instanceof SymRef) {
                    // beware of cyclic refs, peel symrefs
                    refName = ((SymRef) target).getTarget();
                } else {
                    refName = target.getName();
                }
                command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(refName).call();
                result.setNewRef(targetRef.get());
                result.setOid(targetCommitId.get());
                result.setResult(CheckoutResult.Results.CHECKOUT_LOCAL_BRANCH);
            } else {
                // set HEAD to a dettached state
                ObjectId commitId = targetCommitId.get();
                command(UpdateRef.class).setName(Ref.HEAD).setNewValue(commitId).call();
                result.setOid(commitId);
                result.setResult(CheckoutResult.Results.DETACHED_HEAD);
            }
            Optional<Ref> ref = command(RefParse.class).setName(Ref.MERGE_HEAD).call();
            if (ref.isPresent()) {
                command(UpdateRef.class).setName(Ref.MERGE_HEAD).setDelete(true).call();
            }
            return result;
        }
    }
    result.setNewTree(workingTree().getTree().getId());
    return result;
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) SymRef(org.locationtech.geogig.api.SymRef) RefParse(org.locationtech.geogig.api.plumbing.RefParse) RevCommit(org.locationtech.geogig.api.RevCommit) ObjectId(org.locationtech.geogig.api.ObjectId) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) Ref(org.locationtech.geogig.api.Ref) SymRef(org.locationtech.geogig.api.SymRef) NodeRef(org.locationtech.geogig.api.NodeRef) Conflict(org.locationtech.geogig.api.plumbing.merge.Conflict) CanRunDuringConflict(org.locationtech.geogig.di.CanRunDuringConflict) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevTree(org.locationtech.geogig.api.RevTree)

Example 38 with NodeRef

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

the class ApplyPatchOp method applyPatch.

private void applyPatch(Patch patch) {
    final WorkingTree workTree = workingTree();
    final StagingDatabase indexDb = stagingDatabase();
    if (reverse) {
        patch = patch.reversed();
    }
    List<FeatureInfo> removed = patch.getRemovedFeatures();
    for (FeatureInfo feature : removed) {
        workTree.delete(NodeRef.parentPath(feature.getPath()), NodeRef.nodeFromPath(feature.getPath()));
    }
    List<FeatureInfo> added = patch.getAddedFeatures();
    for (FeatureInfo feature : added) {
        workTree.insert(NodeRef.parentPath(feature.getPath()), feature.getFeature());
    }
    List<FeatureDiff> diffs = patch.getModifiedFeatures();
    for (FeatureDiff diff : diffs) {
        String path = diff.getPath();
        DepthSearch depthSearch = new DepthSearch(indexDb);
        Optional<NodeRef> noderef = depthSearch.find(workTree.getTree(), path);
        RevFeatureType oldRevFeatureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
        String refSpec = Ref.WORK_HEAD + ":" + path;
        RevFeature feature = command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class).get();
        RevFeatureType newRevFeatureType = getFeatureType(diff, feature, oldRevFeatureType);
        ImmutableList<Optional<Object>> values = feature.getValues();
        ImmutableList<PropertyDescriptor> oldDescriptors = oldRevFeatureType.sortedDescriptors();
        ImmutableList<PropertyDescriptor> newDescriptors = newRevFeatureType.sortedDescriptors();
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) newRevFeatureType.type());
        Map<Name, Optional<?>> attrs = Maps.newHashMap();
        for (int i = 0; i < oldDescriptors.size(); i++) {
            PropertyDescriptor descriptor = oldDescriptors.get(i);
            if (newDescriptors.contains(descriptor)) {
                Optional<Object> value = values.get(i);
                attrs.put(descriptor.getName(), value);
            }
        }
        Set<Entry<PropertyDescriptor, AttributeDiff>> featureDiffs = diff.getDiffs().entrySet();
        for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = featureDiffs.iterator(); iterator.hasNext(); ) {
            Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
            if (!entry.getValue().getType().equals(TYPE.REMOVED)) {
                Optional<?> oldValue = attrs.get(entry.getKey().getName());
                attrs.put(entry.getKey().getName(), entry.getValue().applyOn(oldValue));
            }
        }
        Set<Entry<Name, Optional<?>>> entries = attrs.entrySet();
        for (Iterator<Entry<Name, Optional<?>>> iterator = entries.iterator(); iterator.hasNext(); ) {
            Entry<Name, Optional<?>> entry = iterator.next();
            featureBuilder.set(entry.getKey(), entry.getValue().orNull());
        }
        SimpleFeature featureToInsert = featureBuilder.buildFeature(NodeRef.nodeFromPath(path));
        workTree.insert(NodeRef.parentPath(path), featureToInsert);
    }
    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());
        }
    }
}
Also used : FeatureInfo(org.locationtech.geogig.api.FeatureInfo) Name(org.opengis.feature.type.Name) WorkingTree(org.locationtech.geogig.repository.WorkingTree) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) NodeRef(org.locationtech.geogig.api.NodeRef) Entry(java.util.Map.Entry) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) StagingDatabase(org.locationtech.geogig.storage.StagingDatabase) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DepthSearch(org.locationtech.geogig.repository.DepthSearch) FeatureTypeDiff(org.locationtech.geogig.api.plumbing.diff.FeatureTypeDiff) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 39 with NodeRef

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

the class FeatureNodeRefFromRefspec method getFeatureFromRefSpec.

private Optional<RevFeature> getFeatureFromRefSpec() {
    Optional<RevObject> revObject = command(RevObjectParse.class).setRefSpec(ref).call(RevObject.class);
    if (!revObject.isPresent()) {
        // let's try to see if it is a feature in the working tree
        NodeRef.checkValidPath(ref);
        Optional<NodeRef> elementRef = command(FindTreeChild.class).setParent(workingTree().getTree()).setChildPath(ref).setIndex(true).call();
        Preconditions.checkArgument(elementRef.isPresent(), "Invalid reference: %s", ref);
        ObjectId id = elementRef.get().objectId();
        revObject = command(RevObjectParse.class).setObjectId(id).call(RevObject.class);
    }
    if (revObject.isPresent()) {
        Preconditions.checkArgument(TYPE.FEATURE.equals(revObject.get().getType()), "%s does not resolve to a feature", ref);
        return Optional.of(RevFeature.class.cast(revObject.get()));
    } else {
        return Optional.absent();
    }
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse)

Example 40 with NodeRef

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

the class CommitOpTest method testCommitEmptyTreeOnNonEmptyRepo.

@Test
public void testCommitEmptyTreeOnNonEmptyRepo() throws Exception {
    insertAndAdd(points1, points2);
    geogig.command(CommitOp.class).call();
    // insertAndAdd(lines1, lines2);
    WorkingTree workingTree = geogig.getRepository().workingTree();
    final String emptyTreeName = "emptyTree";
    workingTree.createTypeTree(emptyTreeName, pointsType);
    {
        List<DiffEntry> unstaged = toList(workingTree.getUnstaged(null));
        assertEquals(unstaged.toString(), 1, unstaged.size());
        // assertEquals(NodeRef.ROOT, unstaged.get(0).newName());
        assertEquals(emptyTreeName, unstaged.get(0).newName());
    }
    geogig.command(AddOp.class).call();
    {
        StagingArea index = geogig.getRepository().index();
        List<DiffEntry> staged = toList(index.getStaged(null));
        assertEquals(staged.toString(), 1, staged.size());
        // assertEquals(NodeRef.ROOT, staged.get(0).newName());
        assertEquals(emptyTreeName, staged.get(0).newName());
    }
    CommitOp commitCommand = geogig.command(CommitOp.class);
    RevCommit commit = commitCommand.call();
    assertNotNull(commit);
    RevTree head = geogig.command(RevObjectParse.class).setObjectId(commit.getTreeId()).call(RevTree.class).get();
    Optional<NodeRef> ref = geogig.command(FindTreeChild.class).setChildPath(emptyTreeName).setParent(head).call();
    assertTrue(ref.isPresent());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) WorkingTree(org.locationtech.geogig.repository.WorkingTree) NodeRef(org.locationtech.geogig.api.NodeRef) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) StagingArea(org.locationtech.geogig.repository.StagingArea) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) RevTree(org.locationtech.geogig.api.RevTree) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Aggregations

NodeRef (org.locationtech.geogig.api.NodeRef)161 ObjectId (org.locationtech.geogig.api.ObjectId)91 RevTree (org.locationtech.geogig.api.RevTree)67 Test (org.junit.Test)62 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)40 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)27 RevFeature (org.locationtech.geogig.api.RevFeature)25 Node (org.locationtech.geogig.api.Node)24 RevTreeBuilder (org.locationtech.geogig.api.RevTreeBuilder)24 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)23 FindTreeChild (org.locationtech.geogig.api.plumbing.FindTreeChild)22 RevObject (org.locationtech.geogig.api.RevObject)21 RevCommit (org.locationtech.geogig.api.RevCommit)19 Map (java.util.Map)15 SimpleFeature (org.opengis.feature.simple.SimpleFeature)15 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)14 Feature (org.opengis.feature.Feature)13 Optional (com.google.common.base.Optional)12 GeoGIG (org.locationtech.geogig.api.GeoGIG)11 LsTreeOp (org.locationtech.geogig.api.plumbing.LsTreeOp)11