Search in sources :

Example 16 with DiffEntry

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

the class Status method print.

/**
     * Prints the list of changes using the specified options
     * 
     * @param console the output console
     * @param changes an iterator of differences to print
     * @param color the color to use for the changes if color use is enabled
     * @param total the total number of changes
     * @throws IOException
     * @see DiffEntry
     */
private void print(final ConsoleReader console, final Iterator<DiffEntry> changes, final Color color, final long total) throws IOException {
    final int limit = all || this.limit == null ? Integer.MAX_VALUE : this.limit.intValue();
    StringBuilder sb = new StringBuilder();
    Ansi ansi = newAnsi(console.getTerminal(), sb);
    DiffEntry entry;
    ChangeType type;
    String path;
    int cnt = 0;
    if (limit > 0) {
        Iterator<DiffEntry> changesIterator = changes;
        while (changesIterator.hasNext() && cnt < limit) {
            ++cnt;
            entry = changesIterator.next();
            type = entry.changeType();
            path = formatPath(entry);
            sb.setLength(0);
            ansi.a("#      ").fg(color).a(type.toString().toLowerCase()).a("  ").a(path).reset();
            console.println(ansi.toString());
        }
    }
    sb.setLength(0);
    ansi.a("# ").a(String.format("%,d", total)).a(" total.");
    console.println(ansi.toString());
}
Also used : ChangeType(org.locationtech.geogig.api.plumbing.diff.DiffEntry.ChangeType) Ansi(org.fusesource.jansi.Ansi) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 17 with DiffEntry

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

the class OSMHistoryImportTest method test.

@Test
public void test() throws Exception {
    cli.execute("config", "user.name", "Gabriel Roldan");
    cli.execute("config", "user.email", "groldan@boundlessgeo.com");
    cli.execute("osm", "import-history", fakeOsmApiUrl, "--to", "10");
    GeoGIG geogig = cli.getGeogig();
    List<DiffEntry> changes = ImmutableList.copyOf(geogig.command(DiffOp.class).setOldVersion("HEAD~2").setNewVersion("HEAD~1").call());
    assertEquals(1, changes.size());
    DiffEntry entry = changes.get(0);
    assertEquals(ChangeType.MODIFIED, entry.changeType());
    assertEquals("node/20", entry.getOldObject().path());
    assertEquals("node/20", entry.getNewObject().path());
    Optional<RevFeature> oldRevFeature = geogig.command(RevObjectParse.class).setObjectId(entry.getOldObject().objectId()).call(RevFeature.class);
    Optional<RevFeature> newRevFeature = geogig.command(RevObjectParse.class).setObjectId(entry.getNewObject().objectId()).call(RevFeature.class);
    assertTrue(oldRevFeature.isPresent());
    assertTrue(newRevFeature.isPresent());
    Optional<RevFeatureType> type = geogig.command(RevObjectParse.class).setObjectId(entry.getOldObject().getMetadataId()).call(RevFeatureType.class);
    assertTrue(type.isPresent());
    FeatureType featureType = type.get().type();
    CoordinateReferenceSystem expected = CRS.decode("EPSG:4326", true);
    CoordinateReferenceSystem actual = featureType.getCoordinateReferenceSystem();
    assertTrue(actual.toString(), CRS.equalsIgnoreMetadata(expected, actual));
}
Also used : FeatureType(org.opengis.feature.type.FeatureType) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevFeature(org.locationtech.geogig.api.RevFeature) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) GeoGIG(org.locationtech.geogig.api.GeoGIG) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Test(org.junit.Test)

Example 18 with DiffEntry

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

the class Log method run.

/**
     * Runs the command and builds the appropriate response
     * 
     * @param context - the context to use for this command
     * 
     * @throws IllegalArgumentException
     */
@Override
public void run(final CommandContext context) {
    final Context geogig = this.getCommandLocator(context);
    LogOp op = geogig.command(LogOp.class).setFirstParentOnly(firstParentOnly);
    if (skip != null) {
        op.setSkip(skip.intValue());
    }
    if (limit != null) {
        op.setLimit(limit.intValue());
    }
    if (this.sinceTime != null || this.untilTime != null) {
        Date since = new Date(0);
        Date until = new Date();
        if (this.sinceTime != null) {
            since = new Date(geogig.command(ParseTimestamp.class).setString(this.sinceTime).call());
        }
        if (this.untilTime != null) {
            until = new Date(geogig.command(ParseTimestamp.class).setString(this.untilTime).call());
        }
        op.setTimeRange(new Range<Date>(Date.class, since, until));
    }
    if (this.since != null) {
        Optional<ObjectId> since;
        since = geogig.command(RevParse.class).setRefSpec(this.since).call();
        Preconditions.checkArgument(since.isPresent(), "Object not found '%s'", this.since);
        op.setSince(since.get());
    }
    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);
        op.setUntil(until.get());
    }
    if (paths != null && !paths.isEmpty()) {
        for (String path : paths) {
            op.addPath(path);
        }
    }
    final Iterator<RevCommit> log = op.call();
    Iterators.advance(log, page * elementsPerPage);
    if (countChanges) {
        final String pathFilter;
        if (paths != null && !paths.isEmpty()) {
            pathFilter = paths.get(0);
        } else {
            pathFilter = null;
        }
        Function<RevCommit, CommitWithChangeCounts> changeCountFunctor = new Function<RevCommit, CommitWithChangeCounts>() {

            @Override
            public CommitWithChangeCounts apply(RevCommit input) {
                ObjectId parent = ObjectId.NULL;
                if (input.getParentIds().size() > 0) {
                    parent = input.getParentIds().get(0);
                }
                int added = 0;
                int modified = 0;
                int removed = 0;
                // If it's a shallow clone, the commit may not exist
                if (parent.equals(ObjectId.NULL) || geogig.stagingDatabase().exists(parent)) {
                    final Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(parent).setNewVersion(input.getId()).setFilter(pathFilter).call();
                    while (diff.hasNext()) {
                        DiffEntry entry = diff.next();
                        if (entry.changeType() == DiffEntry.ChangeType.ADDED) {
                            added++;
                        } else if (entry.changeType() == DiffEntry.ChangeType.MODIFIED) {
                            modified++;
                        } else {
                            removed++;
                        }
                    }
                }
                return new CommitWithChangeCounts(input, added, modified, removed);
            }
        };
        final Iterator<CommitWithChangeCounts> summarizedLog = Iterators.transform(log, changeCountFunctor);
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeCommitsWithChangeCounts(summarizedLog, elementsPerPage);
                out.finish();
            }
        });
    } else if (summary) {
        if (paths != null && paths.size() > 0) {
            context.setResponseContent(new StreamResponse() {

                @Override
                public void write(Writer out) throws Exception {
                    writeCSV(context.getGeoGIG(), out, log);
                }
            });
        } else {
            throw new CommandSpecException("You must specify a feature type path when getting a summary.");
        }
    } else {
        final boolean rangeLog = returnRange;
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeCommits(log, elementsPerPage, rangeLog);
                out.finish();
            }
        });
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) ObjectId(org.locationtech.geogig.api.ObjectId) LogOp(org.locationtech.geogig.api.porcelain.LogOp) StreamResponse(org.locationtech.geogig.web.api.StreamResponse) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) Date(java.util.Date) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) Function(com.google.common.base.Function) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RevParse(org.locationtech.geogig.api.plumbing.RevParse) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) Writer(java.io.Writer) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 19 with DiffEntry

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

the class BlameOp method _call.

@Override
protected BlameReport _call() {
    String fullPath = (commit != null ? commit.toString() : Ref.HEAD) + ":" + path;
    Optional<ObjectId> id = command(RevParse.class).setRefSpec(fullPath).call();
    if (!id.isPresent()) {
        throw new BlameException(StatusCode.FEATURE_NOT_FOUND);
    }
    TYPE type = command(ResolveObjectType.class).setObjectId(id.get()).call();
    if (!type.equals(TYPE.FEATURE)) {
        throw new BlameException(StatusCode.PATH_NOT_FEATURE);
    }
    Optional<RevFeatureType> featureType = command(ResolveFeatureType.class).setRefSpec(path).call();
    BlameReport report = new BlameReport(featureType.get());
    Iterator<RevCommit> log = command(LogOp.class).addPath(path).setUntil(commit).call();
    RevCommit commit = log.next();
    RevObjectParse revObjectParse = command(RevObjectParse.class);
    DiffOp diffOp = command(DiffOp.class);
    DiffFeature diffFeature = command(DiffFeature.class);
    while (!report.isComplete()) {
        if (!log.hasNext()) {
            String refSpec = commit.getId().toString() + ":" + path;
            RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
            report.setFirstVersion(feature, commit);
            break;
        }
        RevCommit commitB = log.next();
        Iterator<DiffEntry> diffs = diffOp.setNewVersion(commit.getId()).setOldVersion(commitB.getId()).setReportTrees(false).call();
        while (diffs.hasNext()) {
            DiffEntry diff = diffs.next();
            if (path.equals(diff.newPath())) {
                if (diff.isAdd()) {
                    String refSpec = commit.getId().toString() + ":" + path;
                    RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
                    report.setFirstVersion(feature, commit);
                    break;
                }
                FeatureDiff featureDiff = diffFeature.setNewVersion(Suppliers.ofInstance(diff.getNewObject())).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).call();
                Map<PropertyDescriptor, AttributeDiff> attribDiffs = featureDiff.getDiffs();
                Iterator<PropertyDescriptor> iter = attribDiffs.keySet().iterator();
                while (iter.hasNext()) {
                    PropertyDescriptor key = iter.next();
                    Optional<?> value = attribDiffs.get(key).getNewValue();
                    String attribute = key.getName().toString();
                    report.addDiff(attribute, value, commit);
                }
            }
        }
        commit = commitB;
    }
    return report;
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ObjectId(org.locationtech.geogig.api.ObjectId) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) TYPE(org.locationtech.geogig.api.RevObject.TYPE) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 20 with DiffEntry

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

the class DiffOp method _call.

/**
     * Executes the diff operation.
     * 
     * @return an iterator to a set of differences between the two trees
     * @see DiffEntry
     */
@Override
protected Iterator<DiffEntry> _call() {
    checkArgument(cached && oldRefSpec == null || !cached, String.format("compare index allows only one revision to check against, got %s / %s", oldRefSpec, newRefSpec));
    checkArgument(newRefSpec == null || oldRefSpec != null, "If new rev spec is specified then old rev spec is mandatory");
    Iterator<DiffEntry> iterator;
    if (cached) {
        // compare the tree-ish (default to HEAD) and the index
        DiffIndex diffIndex = command(DiffIndex.class).addFilter(this.pathFilter).setReportTrees(reportTrees);
        if (oldRefSpec != null) {
            diffIndex.setOldVersion(oldRefSpec);
        }
        iterator = diffIndex.call();
    } else if (newRefSpec == null) {
        DiffWorkTree workTreeIndexDiff = command(DiffWorkTree.class).setFilter(pathFilter).setReportTrees(reportTrees);
        if (oldRefSpec != null) {
            workTreeIndexDiff.setOldVersion(oldRefSpec);
        }
        iterator = workTreeIndexDiff.call();
    } else {
        iterator = command(DiffTree.class).setOldVersion(oldRefSpec).setNewVersion(newRefSpec).setPathFilter(pathFilter).setReportTrees(reportTrees).call();
    }
    return iterator;
}
Also used : DiffTree(org.locationtech.geogig.api.plumbing.DiffTree) DiffIndex(org.locationtech.geogig.api.plumbing.DiffIndex) DiffWorkTree(org.locationtech.geogig.api.plumbing.DiffWorkTree) 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