Search in sources :

Example 1 with BlameException

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

the class Blame method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(paths.size() < 2, "Only one path allowed");
    checkParameter(!paths.isEmpty(), "A path must be specified");
    ConsoleReader console = cli.getConsole();
    GeoGIG geogig = cli.getGeogig();
    String path = paths.get(0);
    try {
        BlameReport report = geogig.command(BlameOp.class).setPath(path).call();
        Map<String, ValueAndCommit> changes = report.getChanges();
        Iterator<String> iter = changes.keySet().iterator();
        while (iter.hasNext()) {
            String attrib = iter.next();
            ValueAndCommit valueAndCommit = changes.get(attrib);
            RevCommit commit = valueAndCommit.commit;
            Optional<?> value = valueAndCommit.value;
            if (porcelain) {
                StringBuilder sb = new StringBuilder();
                sb.append(attrib).append(' ');
                sb.append(commit.getId().toString()).append(' ');
                sb.append(commit.getAuthor().getName().or("")).append(' ');
                sb.append(commit.getAuthor().getEmail().or("")).append(' ');
                sb.append(Long.toString(commit.getAuthor().getTimestamp())).append(' ');
                sb.append(Integer.toString(commit.getAuthor().getTimeZoneOffset()));
                if (!noValues) {
                    sb.append(" ").append(TextValueSerializer.asString(Optional.of((Object) value.orNull())));
                }
                console.println(sb.toString());
            } else {
                Ansi ansi = newAnsi(console.getTerminal());
                ansi.fg(GREEN).a(attrib + ": ").reset();
                if (!noValues) {
                    String s = value.isPresent() ? value.get().toString() : "NULL";
                    ansi.fg(YELLOW).a(s).a(" ").reset();
                }
                ansi.a(commit.getId().toString().substring(0, 7)).a(" ");
                ansi.a(commit.getAuthor().getName().or("")).a(" ");
                ansi.a(commit.getAuthor().getEmail().or("")).a(" ");
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String date = formatter.format(new Date(commit.getAuthor().getTimestamp() + commit.getAuthor().getTimeZoneOffset()));
                ansi.a(date);
                console.println(ansi.toString());
            }
        }
    } catch (BlameException e) {
        switch(e.statusCode) {
            case FEATURE_NOT_FOUND:
                throw new InvalidParameterException("The supplied path does not exist", e);
            case PATH_NOT_FEATURE:
                throw new InvalidParameterException("The supplied path does not resolve to a feature", e);
        }
    }
}
Also used : BlameException(org.locationtech.geogig.api.porcelain.BlameException) ConsoleReader(jline.console.ConsoleReader) BlameReport(org.locationtech.geogig.api.porcelain.BlameReport) Date(java.util.Date) ValueAndCommit(org.locationtech.geogig.api.porcelain.ValueAndCommit) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) Ansi(org.fusesource.jansi.Ansi) SimpleDateFormat(java.text.SimpleDateFormat) GeoGIG(org.locationtech.geogig.api.GeoGIG) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 2 with BlameException

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

the class BlameWebOp 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);
    Optional<ObjectId> commit = Optional.absent();
    if (branchOrCommit != null) {
        commit = geogig.command(RevParse.class).setRefSpec(branchOrCommit).call();
        if (!commit.isPresent()) {
            throw new CommandSpecException("Could not resolve branch or commit");
        }
    }
    try {
        final BlameReport report = geogig.command(BlameOp.class).setPath(path).setCommit(commit.orNull()).call();
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                try {
                    out.writeBlameReport(report);
                } catch (XMLStreamException e) {
                    throw new CommandSpecException("Error writing stream.");
                }
                out.finish();
            }
        });
    } catch (BlameException e) {
        switch(e.statusCode) {
            case PATH_NOT_FEATURE:
                throw new CommandSpecException("The supplied path does not resolve to a feature");
            case FEATURE_NOT_FOUND:
                throw new CommandSpecException("The supplied path does not exist");
        }
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) BlameOp(org.locationtech.geogig.api.porcelain.BlameOp) BlameException(org.locationtech.geogig.api.porcelain.BlameException) ObjectId(org.locationtech.geogig.api.ObjectId) BlameReport(org.locationtech.geogig.api.porcelain.BlameReport) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) BlameException(org.locationtech.geogig.api.porcelain.BlameException) XMLStreamException(javax.xml.stream.XMLStreamException) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) XMLStreamException(javax.xml.stream.XMLStreamException) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RevParse(org.locationtech.geogig.api.plumbing.RevParse) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Example 3 with BlameException

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

the class BlameOpTest method testBlameRemovedAndAdded.

@Test
public void testBlameRemovedAndAdded() throws Exception {
    insertAndAdd(points1);
    RevCommit firstCommit = geogig.command(CommitOp.class).call();
    deleteAndAdd(points1);
    RevCommit secondCommit = geogig.command(CommitOp.class).call();
    insertAndAdd(points1);
    RevCommit thirdCommit = geogig.command(CommitOp.class).call();
    String path = NodeRef.appendChild(pointsName, idP1);
    BlameReport report = geogig.command(BlameOp.class).setPath(path).call();
    Map<String, ValueAndCommit> changes = report.getChanges();
    assertEquals(3, changes.size());
    Collection<ValueAndCommit> commits = changes.values();
    for (ValueAndCommit valueAndCommit : commits) {
        assertEquals(thirdCommit, valueAndCommit.commit);
    }
    try {
        report = geogig.command(BlameOp.class).setPath(path).setCommit(secondCommit.getId()).call();
        fail();
    } catch (BlameException e) {
        assertTrue(e.statusCode == StatusCode.FEATURE_NOT_FOUND);
    }
    report = geogig.command(BlameOp.class).setPath(path).setCommit(firstCommit.getId()).call();
    changes = report.getChanges();
    assertEquals(3, changes.size());
    commits = changes.values();
    for (ValueAndCommit valueAndCommit : commits) {
        assertEquals(firstCommit, valueAndCommit.commit);
    }
}
Also used : BlameException(org.locationtech.geogig.api.porcelain.BlameException) ValueAndCommit(org.locationtech.geogig.api.porcelain.ValueAndCommit) BlameReport(org.locationtech.geogig.api.porcelain.BlameReport) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Aggregations

BlameException (org.locationtech.geogig.api.porcelain.BlameException)3 BlameReport (org.locationtech.geogig.api.porcelain.BlameReport)3 RevCommit (org.locationtech.geogig.api.RevCommit)2 ValueAndCommit (org.locationtech.geogig.api.porcelain.ValueAndCommit)2 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ConsoleReader (jline.console.ConsoleReader)1 Ansi (org.fusesource.jansi.Ansi)1 Test (org.junit.Test)1 Context (org.locationtech.geogig.api.Context)1 GeoGIG (org.locationtech.geogig.api.GeoGIG)1 ObjectId (org.locationtech.geogig.api.ObjectId)1 RevParse (org.locationtech.geogig.api.plumbing.RevParse)1 BlameOp (org.locationtech.geogig.api.porcelain.BlameOp)1 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)1 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)1 CommandContext (org.locationtech.geogig.web.api.CommandContext)1 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)1 CommandSpecException (org.locationtech.geogig.web.api.CommandSpecException)1