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());
}
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;
}
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();
}
});
}
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();
}
});
}
}
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());
}
Aggregations