Search in sources :

Example 31 with DiffEntry

use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.

the class CreatePatchOpTest method testCreatePatchAddNewEmptyPath.

@Test
public void testCreatePatchAddNewEmptyPath() throws Exception {
    insert(points1);
    delete(points1);
    DiffOp op = geogig.command(DiffOp.class).setReportTrees(true);
    Iterator<DiffEntry> diffs = op.call();
    // ArrayList<DiffEntry> list = Lists.newArrayList(diffs);
    Patch patch = geogig.command(CreatePatchOp.class).setDiffs(diffs).call();
    assertEquals(1, patch.getAlteredTrees().size());
}
Also used : DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Test(org.junit.Test)

Example 32 with DiffEntry

use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.

the class ExportDiffOp method _call.

/**
     * Executes the export operation using the parameters that have been specified.
     * 
     * @return a FeatureCollection with the specified features
     */
@Override
protected SimpleFeatureStore _call() {
    final SimpleFeatureStore targetStore = getTargetStore();
    final String refspec = old ? oldRef : newRef;
    final RevTree rootTree = resolveRootTree(refspec);
    final NodeRef typeTreeRef = resolTypeTreeRef(refspec, path, rootTree);
    final ObjectId defaultMetadataId = typeTreeRef.getMetadataId();
    final ProgressListener progressListener = getProgressListener();
    progressListener.started();
    progressListener.setDescription("Exporting diffs for path '" + path + "'... ");
    FeatureCollection<SimpleFeatureType, SimpleFeature> asFeatureCollection = new BaseFeatureCollection<SimpleFeatureType, SimpleFeature>() {

        @Override
        public FeatureIterator<SimpleFeature> features() {
            Iterator<DiffEntry> diffs = command(DiffOp.class).setOldVersion(oldRef).setNewVersion(newRef).setFilter(path).call();
            final Iterator<SimpleFeature> plainFeatures = getFeatures(diffs, old, stagingDatabase(), defaultMetadataId, progressListener);
            Iterator<Optional<Feature>> transformed = Iterators.transform(plainFeatures, ExportDiffOp.this.function);
            Iterator<SimpleFeature> filtered = Iterators.filter(Iterators.transform(transformed, new Function<Optional<Feature>, SimpleFeature>() {

                @Override
                public SimpleFeature apply(Optional<Feature> input) {
                    return (SimpleFeature) (input.isPresent() ? input.get() : null);
                }
            }), Predicates.notNull());
            return new DelegateFeatureIterator<SimpleFeature>(filtered);
        }
    };
    // add the feature collection to the feature store
    final Transaction transaction;
    if (transactional) {
        transaction = new DefaultTransaction("create");
    } else {
        transaction = Transaction.AUTO_COMMIT;
    }
    try {
        targetStore.setTransaction(transaction);
        try {
            targetStore.addFeatures(asFeatureCollection);
            transaction.commit();
        } catch (final Exception e) {
            if (transactional) {
                transaction.rollback();
            }
            Throwables.propagateIfInstanceOf(e, GeoToolsOpException.class);
            throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
        } finally {
            transaction.close();
        }
    } catch (IOException e) {
        throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_ADD);
    }
    progressListener.complete();
    return targetStore;
}
Also used : DelegateFeatureIterator(org.geotools.feature.collection.DelegateFeatureIterator) Optional(com.google.common.base.Optional) ObjectId(org.locationtech.geogig.api.ObjectId) IOException(java.io.IOException) RevFeature(org.locationtech.geogig.api.RevFeature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) DefaultTransaction(org.geotools.data.DefaultTransaction) IOException(java.io.IOException) NodeRef(org.locationtech.geogig.api.NodeRef) Function(com.google.common.base.Function) ProgressListener(org.locationtech.geogig.api.ProgressListener) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) BaseFeatureCollection(org.geotools.feature.collection.BaseFeatureCollection) RevTree(org.locationtech.geogig.api.RevTree) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 33 with DiffEntry

use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.

the class StatisticsWebOp 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);
    final List<FeatureTypeStats> stats = Lists.newArrayList();
    LogOp logOp = geogig.command(LogOp.class).setFirstParentOnly(true);
    final Iterator<RevCommit> log;
    if (since != null && !since.trim().isEmpty()) {
        Date untilTime = new Date();
        Date sinceTime = new Date(geogig.command(ParseTimestamp.class).setString(since).call());
        logOp.setTimeRange(new Range<Date>(Date.class, sinceTime, untilTime));
    }
    if (this.until != null) {
        Optional<ObjectId> until;
        until = geogig.command(RevParse.class).setRefSpec(this.until).call();
        Preconditions.checkArgument(until.isPresent(), "Object not found '%s'", this.until);
        logOp.setUntil(until.get());
    }
    LsTreeOp lsTreeOp = geogig.command(LsTreeOp.class).setStrategy(LsTreeOp.Strategy.TREES_ONLY);
    if (path != null && !path.trim().isEmpty()) {
        lsTreeOp.setReference(path);
        logOp.addPath(path);
    }
    final Iterator<NodeRef> treeIter = lsTreeOp.call();
    while (treeIter.hasNext()) {
        NodeRef node = treeIter.next();
        stats.add(new FeatureTypeStats(node.path(), context.getGeoGIG().getRepository().getTree(node.objectId()).size()));
    }
    log = logOp.call();
    RevCommit firstCommit = null;
    RevCommit lastCommit = null;
    int totalCommits = 0;
    final List<RevPerson> authors = Lists.newArrayList();
    if (log.hasNext()) {
        lastCommit = log.next();
        authors.add(lastCommit.getAuthor());
        totalCommits++;
    }
    while (log.hasNext()) {
        firstCommit = log.next();
        RevPerson newAuthor = firstCommit.getAuthor();
        // If the author isn't defined, use the committer for the purposes of statistics.
        if (!newAuthor.getName().isPresent() && !newAuthor.getEmail().isPresent()) {
            newAuthor = firstCommit.getCommitter();
        }
        if (newAuthor.getName().isPresent() || newAuthor.getEmail().isPresent()) {
            boolean authorFound = false;
            for (RevPerson author : authors) {
                if (newAuthor.getName().equals(author.getName()) && newAuthor.getEmail().equals(author.getEmail())) {
                    authorFound = true;
                    break;
                }
            }
            if (!authorFound) {
                authors.add(newAuthor);
            }
        }
        totalCommits++;
    }
    int addedFeatures = 0;
    int modifiedFeatures = 0;
    int removedFeatures = 0;
    if (since != null && !since.trim().isEmpty() && firstCommit != null && lastCommit != null) {
        final Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(firstCommit.getId()).setNewVersion(lastCommit.getId()).setFilter(path).call();
        while (diff.hasNext()) {
            DiffEntry entry = diff.next();
            if (entry.changeType() == DiffEntry.ChangeType.ADDED) {
                addedFeatures++;
            } else if (entry.changeType() == DiffEntry.ChangeType.MODIFIED) {
                modifiedFeatures++;
            } else {
                removedFeatures++;
            }
        }
    }
    final RevCommit first = firstCommit;
    final RevCommit last = lastCommit;
    final int total = totalCommits;
    final int added = addedFeatures;
    final int modified = modifiedFeatures;
    final int removed = removedFeatures;
    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start(true);
            out.writeStatistics(stats, first, last, total, authors, added, modified, removed);
            out.finish();
        }
    });
}
Also used : ParseTimestamp(org.locationtech.geogig.api.plumbing.ParseTimestamp) LogOp(org.locationtech.geogig.api.porcelain.LogOp) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) NodeRef(org.locationtech.geogig.api.NodeRef) RevPerson(org.locationtech.geogig.api.RevPerson) RevParse(org.locationtech.geogig.api.plumbing.RevParse) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) ObjectId(org.locationtech.geogig.api.ObjectId) Date(java.util.Date) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter)

Example 34 with DiffEntry

use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.

the class FormatPatch method runInternal.

/**
     * Executes the format-patch command with the specified options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(refSpec.size() < 3, "Commit list is too long :%s", refSpec);
    GeoGIG geogig = cli.getGeogig();
    checkParameter(file != null, "Patch file not specified");
    DiffOp diff = geogig.command(DiffOp.class).setReportTrees(true);
    String oldVersion = resolveOldVersion();
    String newVersion = resolveNewVersion();
    diff.setOldVersion(oldVersion).setNewVersion(newVersion).setCompareIndex(cached);
    Iterator<DiffEntry> entries;
    if (paths.isEmpty()) {
        entries = diff.setProgressListener(cli.getProgressListener()).call();
    } else {
        entries = Iterators.emptyIterator();
        for (String path : paths) {
            Iterator<DiffEntry> moreEntries = diff.setFilter(path).setProgressListener(cli.getProgressListener()).call();
            entries = Iterators.concat(entries, moreEntries);
        }
    }
    if (!entries.hasNext()) {
        cli.getConsole().println("No differences found");
        return;
    }
    Patch patch = geogig.command(CreatePatchOp.class).setDiffs(entries).call();
    FileOutputStream fos = new FileOutputStream(file);
    OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
    PatchSerializer.write(out, patch);
}
Also used : DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) GeoGIG(org.locationtech.geogig.api.GeoGIG) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 35 with DiffEntry

use of org.locationtech.geogig.api.plumbing.diff.DiffEntry in project GeoGig by boundlessgeo.

the class Conflicts method printConflictDiff.

private void printConflictDiff(Conflict conflict, ConsoleReader console, GeoGIG geogig) throws IOException {
    FullDiffPrinter diffPrinter = new FullDiffPrinter(false, true);
    console.println("---" + conflict.getPath() + "---");
    ObjectId theirsHeadId;
    Optional<Ref> mergeHead = geogig.command(RefParse.class).setName(Ref.MERGE_HEAD).call();
    if (mergeHead.isPresent()) {
        theirsHeadId = mergeHead.get().getObjectId();
    } else {
        File branchFile = new File(getRebaseFolder(), "branch");
        Preconditions.checkState(branchFile.exists(), "Cannot find merge/rebase head reference");
        try {
            String currentBranch = Files.readFirstLine(branchFile, Charsets.UTF_8);
            Optional<Ref> rebaseHead = geogig.command(RefParse.class).setName(currentBranch).call();
            theirsHeadId = rebaseHead.get().getObjectId();
        } catch (IOException e) {
            throw new IllegalStateException("Cannot read current branch info file");
        }
    }
    Optional<RevCommit> theirsHead = geogig.command(RevObjectParse.class).setObjectId(theirsHeadId).call(RevCommit.class);
    ObjectId oursHeadId = geogig.command(RefParse.class).setName(Ref.ORIG_HEAD).call().get().getObjectId();
    Optional<RevCommit> oursHead = geogig.command(RevObjectParse.class).setObjectId(oursHeadId).call(RevCommit.class);
    Optional<ObjectId> commonAncestor = geogig.command(FindCommonAncestor.class).setLeft(theirsHead.get()).setRight(oursHead.get()).call();
    String ancestorPath = commonAncestor.get().toString() + ":" + conflict.getPath();
    Optional<NodeRef> ancestorNodeRef = geogig.command(FeatureNodeRefFromRefspec.class).setRefspec(ancestorPath).call();
    String path = Ref.ORIG_HEAD + ":" + conflict.getPath();
    Optional<NodeRef> oursNodeRef = geogig.command(FeatureNodeRefFromRefspec.class).setRefspec(path).call();
    DiffEntry diffEntry = new DiffEntry(ancestorNodeRef.orNull(), oursNodeRef.orNull());
    console.println("Ours");
    diffPrinter.print(geogig, console, diffEntry);
    path = theirsHeadId + ":" + conflict.getPath();
    Optional<NodeRef> theirsNodeRef = geogig.command(FeatureNodeRefFromRefspec.class).setRefspec(path).call();
    diffEntry = new DiffEntry(ancestorNodeRef.orNull(), theirsNodeRef.orNull());
    console.println("Theirs");
    diffPrinter.print(geogig, console, diffEntry);
}
Also used : ObjectId(org.locationtech.geogig.api.ObjectId) IOException(java.io.IOException) NodeRef(org.locationtech.geogig.api.NodeRef) Ref(org.locationtech.geogig.api.Ref) NodeRef(org.locationtech.geogig.api.NodeRef) FindCommonAncestor(org.locationtech.geogig.api.plumbing.FindCommonAncestor) File(java.io.File) RevCommit(org.locationtech.geogig.api.RevCommit) 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