Search in sources :

Example 21 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 22 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 23 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 24 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 25 with DiffEntry

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

the class Merge method runInternal.

/**
     * Executes the merge command using the provided options.
     */
@Override
public void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(commits.size() > 0 || abort, "No commits provided to merge.");
    ConsoleReader console = cli.getConsole();
    final GeoGIG geogig = cli.getGeogig();
    Ansi ansi = newAnsi(console.getTerminal());
    if (abort) {
        Optional<Ref> ref = geogig.command(RefParse.class).setName(Ref.ORIG_HEAD).call();
        if (!ref.isPresent()) {
            throw new CommandFailedException("There is no merge to abort <ORIG_HEAD missing>.");
        }
        geogig.command(ResetOp.class).setMode(ResetMode.HARD).setCommit(Suppliers.ofInstance(ref.get().getObjectId())).call();
        console.println("Merge aborted successfully.");
        return;
    }
    RevCommit commit;
    try {
        MergeOp merge = geogig.command(MergeOp.class);
        merge.setOurs(ours).setTheirs(theirs).setNoCommit(noCommit);
        merge.setMessage(message).setProgressListener(cli.getProgressListener());
        for (String commitish : commits) {
            Optional<ObjectId> commitId;
            commitId = geogig.command(RevParse.class).setRefSpec(commitish).call();
            checkParameter(commitId.isPresent(), "Commit not found '%s'", commitish);
            merge.addCommit(Suppliers.ofInstance(commitId.get()));
        }
        MergeReport report = merge.call();
        commit = report.getMergeCommit();
    } catch (RuntimeException e) {
        if (e instanceof NothingToCommitException || e instanceof IllegalArgumentException || e instanceof IllegalStateException) {
            throw new CommandFailedException(e.getMessage(), e);
        }
        throw e;
    }
    final ObjectId parentId = commit.parentN(0).or(ObjectId.NULL);
    console.println("[" + commit.getId() + "] " + commit.getMessage());
    console.print("Committed, counting objects...");
    Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(parentId).setNewVersion(commit.getId()).call();
    int adds = 0, deletes = 0, changes = 0;
    DiffEntry diffEntry;
    while (diff.hasNext()) {
        diffEntry = diff.next();
        switch(diffEntry.changeType()) {
            case ADDED:
                ++adds;
                break;
            case REMOVED:
                ++deletes;
                break;
            case MODIFIED:
                ++changes;
                break;
        }
    }
    ansi.fg(Color.GREEN).a(adds).reset().a(" features added, ").fg(Color.YELLOW).a(changes).reset().a(" changed, ").fg(Color.RED).a(deletes).reset().a(" deleted.").reset().newline();
    console.print(ansi.toString());
}
Also used : ConsoleReader(jline.console.ConsoleReader) ObjectId(org.locationtech.geogig.api.ObjectId) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) MergeOp(org.locationtech.geogig.api.porcelain.MergeOp) ResetOp(org.locationtech.geogig.api.porcelain.ResetOp) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) MergeReport(org.locationtech.geogig.api.porcelain.MergeOp.MergeReport) Ref(org.locationtech.geogig.api.Ref) RevParse(org.locationtech.geogig.api.plumbing.RevParse) Ansi(org.fusesource.jansi.Ansi) GeoGIG(org.locationtech.geogig.api.GeoGIG) 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