Search in sources :

Example 1 with GeoGIG

use of org.locationtech.geogig.api.GeoGIG in project GeoGig by boundlessgeo.

the class Cat method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(paths.size() < 2, "Only one refspec allowed");
    checkParameter(!paths.isEmpty(), "A refspec must be specified");
    ConsoleReader console = cli.getConsole();
    GeoGIG geogig = cli.getGeogig();
    String path = paths.get(0);
    Optional<RevObject> obj = geogig.command(RevObjectParse.class).setRefSpec(path).call();
    checkParameter(obj.isPresent(), "refspec did not resolve to any object.");
    if (binary) {
        ObjectSerializingFactory factory = DataStreamSerializationFactoryV1.INSTANCE;
        ObjectWriter<RevObject> writer = factory.createObjectWriter(obj.get().getType());
        writer.write(obj.get(), System.out);
    } else {
        CharSequence s = geogig.command(CatObject.class).setObject(Suppliers.ofInstance(obj.get())).call();
        console.println(s);
    }
}
Also used : ObjectSerializingFactory(org.locationtech.geogig.storage.ObjectSerializingFactory) ConsoleReader(jline.console.ConsoleReader) RevObject(org.locationtech.geogig.api.RevObject) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 2 with GeoGIG

use of org.locationtech.geogig.api.GeoGIG in project GeoGig by boundlessgeo.

the class DiffTree method runInternal.

/**
     * Executes the diff-tree command with the specified options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (refSpec.size() > 2) {
        throw new CommandFailedException("Tree refspecs list is too long :" + refSpec);
    }
    if (treeStats && describe) {
        throw new CommandFailedException("Cannot use --describe and --tree-stats simultaneously");
    }
    GeoGIG geogig = cli.getGeogig();
    org.locationtech.geogig.api.plumbing.DiffTree diff = geogig.command(org.locationtech.geogig.api.plumbing.DiffTree.class);
    String oldVersion = resolveOldVersion();
    String newVersion = resolveNewVersion();
    diff.setOldVersion(oldVersion).setNewVersion(newVersion);
    Iterator<DiffEntry> diffEntries;
    if (paths.isEmpty()) {
        diffEntries = diff.setProgressListener(cli.getProgressListener()).call();
    } else {
        diffEntries = Iterators.emptyIterator();
        for (String path : paths) {
            Iterator<DiffEntry> moreEntries = diff.setPathFilter(path).setProgressListener(cli.getProgressListener()).call();
            diffEntries = Iterators.concat(diffEntries, moreEntries);
        }
    }
    DiffEntry diffEntry;
    HashMap<String, Long[]> stats = Maps.newHashMap();
    while (diffEntries.hasNext()) {
        diffEntry = diffEntries.next();
        StringBuilder sb = new StringBuilder();
        String path = diffEntry.newPath() != null ? diffEntry.newPath() : diffEntry.oldPath();
        if (describe) {
            sb.append(diffEntry.changeType().toString().charAt(0)).append(' ').append(path).append(LINE_BREAK);
            if (diffEntry.changeType() == ChangeType.MODIFIED) {
                FeatureDiff featureDiff = geogig.command(DiffFeature.class).setNewVersion(Suppliers.ofInstance(diffEntry.getNewObject())).setOldVersion(Suppliers.ofInstance(diffEntry.getOldObject())).call();
                Map<PropertyDescriptor, AttributeDiff> diffs = featureDiff.getDiffs();
                HashSet<PropertyDescriptor> diffDescriptors = Sets.newHashSet(diffs.keySet());
                NodeRef noderef = diffEntry.changeType() != ChangeType.REMOVED ? diffEntry.getNewObject() : diffEntry.getOldObject();
                RevFeatureType featureType = geogig.command(RevObjectParse.class).setObjectId(noderef.getMetadataId()).call(RevFeatureType.class).get();
                Optional<RevObject> obj = geogig.command(RevObjectParse.class).setObjectId(noderef.objectId()).call();
                RevFeature feature = (RevFeature) obj.get();
                ImmutableList<Optional<Object>> values = feature.getValues();
                ImmutableList<PropertyDescriptor> descriptors = featureType.sortedDescriptors();
                int idx = 0;
                for (PropertyDescriptor descriptor : descriptors) {
                    if (diffs.containsKey(descriptor)) {
                        AttributeDiff ad = diffs.get(descriptor);
                        sb.append(ad.getType().toString().charAt(0) + " " + descriptor.getName().toString() + LINE_BREAK);
                        if (!ad.getType().equals(TYPE.ADDED)) {
                            Object value = ad.getOldValue().orNull();
                            sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                            sb.append(LINE_BREAK);
                        }
                        if (!ad.getType().equals(TYPE.REMOVED)) {
                            Object value = ad.getNewValue().orNull();
                            sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                            sb.append(LINE_BREAK);
                        }
                        diffDescriptors.remove(descriptor);
                    } else {
                        sb.append("U ").append(descriptor.getName().toString()).append(LINE_BREAK);
                        sb.append(TextValueSerializer.asString(values.get(idx))).append(LINE_BREAK);
                    }
                    idx++;
                }
                for (PropertyDescriptor descriptor : diffDescriptors) {
                    AttributeDiff ad = diffs.get(descriptor);
                    sb.append(ad.getType().toString().charAt(0) + " " + descriptor.getName().toString() + LINE_BREAK);
                    if (!ad.getType().equals(TYPE.ADDED)) {
                        Object value = ad.getOldValue().orNull();
                        sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                        sb.append(LINE_BREAK);
                    }
                    if (!ad.getType().equals(TYPE.REMOVED)) {
                        Object value = ad.getNewValue().orNull();
                        sb.append(TextValueSerializer.asString(Optional.fromNullable(value)));
                        sb.append(LINE_BREAK);
                    }
                }
            } else {
                NodeRef noderef = diffEntry.changeType() == ChangeType.ADDED ? diffEntry.getNewObject() : diffEntry.getOldObject();
                RevFeatureType featureType = geogig.command(RevObjectParse.class).setObjectId(noderef.getMetadataId()).call(RevFeatureType.class).get();
                Optional<RevObject> obj = geogig.command(RevObjectParse.class).setObjectId(noderef.objectId()).call();
                RevFeature feature = (RevFeature) obj.get();
                ImmutableList<Optional<Object>> values = feature.getValues();
                int i = 0;
                for (Optional<Object> value : values) {
                    sb.append(diffEntry.changeType().toString().charAt(0));
                    sb.append(' ');
                    sb.append(featureType.sortedDescriptors().get(i).getName().toString());
                    sb.append(LINE_BREAK);
                    sb.append(TextValueSerializer.asString(value));
                    sb.append(LINE_BREAK);
                    i++;
                }
                sb.append(LINE_BREAK);
            }
            sb.append(LINE_BREAK);
            cli.getConsole().println(sb.toString());
        } else if (treeStats) {
            String parent = NodeRef.parentPath(path);
            if (!stats.containsKey(parent)) {
                stats.put(parent, new Long[] { 0l, 0l, 0l });
            }
            Long[] counts = stats.get(parent);
            if (diffEntry.changeType() == ChangeType.ADDED) {
                counts[0]++;
            } else if (diffEntry.changeType() == ChangeType.REMOVED) {
                counts[1]++;
            } else if (diffEntry.changeType() == ChangeType.MODIFIED) {
                counts[2]++;
            }
        } else {
            sb.append(path).append(' ');
            sb.append(diffEntry.oldObjectId().toString());
            sb.append(' ');
            sb.append(diffEntry.newObjectId().toString());
            cli.getConsole().println(sb.toString());
        }
    }
    if (treeStats) {
        for (String path : stats.keySet()) {
            StringBuffer sb = new StringBuffer();
            sb.append(path);
            Long[] counts = stats.get(path);
            for (int i = 0; i < counts.length; i++) {
                sb.append(" " + counts[i].toString());
            }
            cli.getConsole().println(sb.toString());
        }
    }
}
Also used : CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) NodeRef(org.locationtech.geogig.api.NodeRef) RevFeature(org.locationtech.geogig.api.RevFeature) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) RevObject(org.locationtech.geogig.api.RevObject) DiffFeature(org.locationtech.geogig.api.plumbing.DiffFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 3 with GeoGIG

use of org.locationtech.geogig.api.GeoGIG in project GeoGig by boundlessgeo.

the class ShowRef method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    ConsoleReader console = cli.getConsole();
    GeoGIG geogig = cli.getGeogig();
    ForEachRef op = geogig.command(ForEachRef.class);
    Predicate<Ref> filter = new Predicate<Ref>() {

        @Override
        public boolean apply(Ref ref) {
            String name = ref.getName();
            if (!name.startsWith(Ref.REFS_PREFIX)) {
                return false;
            }
            boolean match = patterns.isEmpty() ? true : false;
            for (String pattern : patterns) {
                if (Strings.isNullOrEmpty(pattern)) {
                    match = true;
                } else if (name.endsWith("/" + pattern)) {
                    match = true;
                    break;
                }
            }
            return match;
        }
    };
    op.setFilter(filter);
    ImmutableSet<Ref> refs = op.call();
    for (Ref ref : refs) {
        console.println(ref.getObjectId() + " " + ref.getName());
    }
}
Also used : Ref(org.locationtech.geogig.api.Ref) ForEachRef(org.locationtech.geogig.api.plumbing.ForEachRef) ConsoleReader(jline.console.ConsoleReader) ForEachRef(org.locationtech.geogig.api.plumbing.ForEachRef) GeoGIG(org.locationtech.geogig.api.GeoGIG) Predicate(com.google.common.base.Predicate)

Example 4 with GeoGIG

use of org.locationtech.geogig.api.GeoGIG in project GeoGig by boundlessgeo.

the class Add method runInternal.

/**
     * Executes the add 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) throws IOException {
    final GeoGIG geogig = cli.getGeogig();
    final ConsoleReader console = cli.getConsole();
    String pathFilter = null;
    if (patterns.size() == 1) {
        pathFilter = patterns.get(0);
    } else if (patterns.size() > 1) {
        throw new InvalidParameterException("Only a single path is supported so far");
    }
    List<Conflict> conflicts = geogig.command(ConflictsReadOp.class).call();
    console.print("Counting unstaged elements...");
    console.flush();
    DiffObjectCount unstaged = geogig.getRepository().workingTree().countUnstaged(pathFilter);
    if (0 == unstaged.count() && conflicts.isEmpty()) {
        console.println();
        console.println("No unstaged elements, exiting.");
        return;
    } else {
        console.println(String.valueOf(unstaged.count()));
    }
    console.println("Staging changes...");
    AddOp op = geogig.command(AddOp.class);
    if (patterns.size() == 1) {
        op.addPattern(patterns.get(0));
    }
    WorkingTree workTree = op.setUpdateOnly(updateOnly).setProgressListener(cli.getProgressListener()).call();
    DiffObjectCount staged = geogig.getRepository().index().countStaged(null);
    unstaged = workTree.countUnstaged(null);
    console.println(staged.featureCount() + " features and " + staged.treeCount() + " trees staged for commit");
    console.println(unstaged.featureCount() + " features and " + unstaged.treeCount() + " trees not staged for commit");
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) WorkingTree(org.locationtech.geogig.repository.WorkingTree) DiffObjectCount(org.locationtech.geogig.api.plumbing.diff.DiffObjectCount) ConsoleReader(jline.console.ConsoleReader) ConflictsReadOp(org.locationtech.geogig.api.plumbing.merge.ConflictsReadOp) Conflict(org.locationtech.geogig.api.plumbing.merge.Conflict) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 5 with GeoGIG

use of org.locationtech.geogig.api.GeoGIG in project GeoGig by boundlessgeo.

the class MergeBase method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(commits.size() == 2, "Two commit references must be provided");
    ConsoleReader console = cli.getConsole();
    GeoGIG geogig = cli.getGeogig();
    Optional<RevObject> left = geogig.command(RevObjectParse.class).setRefSpec(commits.get(0)).call();
    checkParameter(left.isPresent(), commits.get(0) + " does not resolve to any object.");
    checkParameter(left.get() instanceof RevCommit, commits.get(0) + " does not resolve to a commit");
    Optional<RevObject> right = geogig.command(RevObjectParse.class).setRefSpec(commits.get(1)).call();
    checkParameter(right.isPresent(), commits.get(1) + " does not resolve to any object.");
    checkParameter(right.get() instanceof RevCommit, commits.get(1) + " does not resolve to a commit");
    Optional<ObjectId> ancestor = geogig.command(FindCommonAncestor.class).setLeft((RevCommit) left.get()).setRight((RevCommit) right.get()).call();
    checkParameter(ancestor.isPresent(), "No common ancestor was found.");
    console.print(ancestor.get().toString());
}
Also used : ConsoleReader(jline.console.ConsoleReader) RevObject(org.locationtech.geogig.api.RevObject) ObjectId(org.locationtech.geogig.api.ObjectId) FindCommonAncestor(org.locationtech.geogig.api.plumbing.FindCommonAncestor) GeoGIG(org.locationtech.geogig.api.GeoGIG) RevCommit(org.locationtech.geogig.api.RevCommit)

Aggregations

GeoGIG (org.locationtech.geogig.api.GeoGIG)83 ConsoleReader (jline.console.ConsoleReader)22 ObjectId (org.locationtech.geogig.api.ObjectId)21 File (java.io.File)19 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)17 RevTree (org.locationtech.geogig.api.RevTree)14 IOException (java.io.IOException)13 NodeRef (org.locationtech.geogig.api.NodeRef)13 RevObject (org.locationtech.geogig.api.RevObject)13 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)12 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)12 RevFeature (org.locationtech.geogig.api.RevFeature)10 Context (org.locationtech.geogig.api.Context)9 Ref (org.locationtech.geogig.api.Ref)9 RevCommit (org.locationtech.geogig.api.RevCommit)9 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)8 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)8 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)8 Repository (org.locationtech.geogig.repository.Repository)7 WorkingTree (org.locationtech.geogig.repository.WorkingTree)7