Search in sources :

Example 41 with DiffEntry

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;
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) NodeRef(org.locationtech.geogig.api.NodeRef) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 42 with DiffEntry

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());
    }
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) MergeScenarioReport(org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport) Repository(org.locationtech.geogig.repository.Repository) Ref(org.locationtech.geogig.api.Ref) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) SymRef(org.locationtech.geogig.api.SymRef) SymRef(org.locationtech.geogig.api.SymRef) Conflict(org.locationtech.geogig.api.plumbing.merge.Conflict) WriteTree2(org.locationtech.geogig.api.plumbing.WriteTree2) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 43 with DiffEntry

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;
}
Also used : Optional(com.google.common.base.Optional) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Function(com.google.common.base.Function) NodeRef(org.locationtech.geogig.api.NodeRef) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 44 with DiffEntry

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) {
        }
    }
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) IOException(java.io.IOException) ResetOp(org.locationtech.geogig.api.porcelain.ResetOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) ChangeType(org.locationtech.geogig.api.plumbing.diff.DiffEntry.ChangeType) ResetMode(org.locationtech.geogig.api.porcelain.ResetOp.ResetMode) GeoGIG(org.locationtech.geogig.api.GeoGIG) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 45 with DiffEntry

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();
}
Also used : DiffIndex(org.locationtech.geogig.api.plumbing.DiffIndex) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Aggregations

DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)83 ObjectId (org.locationtech.geogig.api.ObjectId)38 Test (org.junit.Test)31 RevCommit (org.locationtech.geogig.api.RevCommit)31 NodeRef (org.locationtech.geogig.api.NodeRef)24 DiffOp (org.locationtech.geogig.api.porcelain.DiffOp)17 RevFeature (org.locationtech.geogig.api.RevFeature)15 RevTree (org.locationtech.geogig.api.RevTree)15 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)14 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)14 RevObject (org.locationtech.geogig.api.RevObject)11 Patch (org.locationtech.geogig.api.plumbing.diff.Patch)11 Feature (org.opengis.feature.Feature)10 Optional (com.google.common.base.Optional)9 GeoGIG (org.locationtech.geogig.api.GeoGIG)8 Repository (org.locationtech.geogig.repository.Repository)8 ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)8 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)8 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 IOException (java.io.IOException)5