Search in sources :

Example 26 with Context

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

the class Init method runInternal.

/**
     * Executes the init command.
     */
@Override
public void runInternal(GeogigCLI cli) throws IOException {
    // argument location if provided, or current directory otherwise
    final File targetDirectory;
    {
        File currDir = cli.getPlatform().pwd();
        if (location != null && location.size() == 1) {
            String target = location.get(0);
            File f = new File(target);
            if (!f.isAbsolute()) {
                f = new File(currDir, target).getCanonicalFile();
            }
            targetDirectory = f;
        } else {
            targetDirectory = currDir;
        }
    }
    final boolean repoExisted;
    final Repository repository;
    {
        GeoGIG geogig = cli.getGeogig();
        if (geogig == null) {
            Context geogigInjector = cli.getGeogigInjector();
            geogig = new GeoGIG(geogigInjector);
        }
        repoExisted = determineIfRepoExists(targetDirectory, geogig);
        final Map<String, String> suppliedConfiguration = splitConfig(config);
        try {
            repository = geogig.command(InitOp.class).setConfig(suppliedConfiguration).setTarget(targetDirectory).call();
        } catch (IllegalArgumentException e) {
            throw new CommandFailedException(e.getMessage(), e);
        } finally {
            geogig.close();
        }
    }
    File repoDirectory;
    try {
        repoDirectory = new File(repository.getLocation().toURI());
    } catch (URISyntaxException e) {
        throw new CommandFailedException("Environment home can't be resolved to a directory", e);
    }
    String message;
    if (repoExisted) {
        message = "Reinitialized existing Geogig repository in " + repoDirectory.getAbsolutePath();
    } else {
        message = "Initialized empty Geogig repository in " + repoDirectory.getAbsolutePath();
    }
    cli.getConsole().println(message);
}
Also used : Context(org.locationtech.geogig.api.Context) InitOp(org.locationtech.geogig.api.porcelain.InitOp) Repository(org.locationtech.geogig.repository.Repository) RequiresRepository(org.locationtech.geogig.cli.annotation.RequiresRepository) URISyntaxException(java.net.URISyntaxException) File(java.io.File) Map(java.util.Map) GeoGIG(org.locationtech.geogig.api.GeoGIG) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Example 27 with Context

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

the class CachingModuleTest method setUp.

@Before
public void setUp() throws Exception {
    odbCache = mock(Cache.class);
    indexCache = mock(Cache.class);
    final ObjectDatabaseCacheFactory odbCacheFac = mock(ObjectDatabaseCacheFactory.class);
    when(odbCacheFac.get()).thenReturn(odbCache);
    final StagingDatabaseCacheFactory indexCacheFac = mock(StagingDatabaseCacheFactory.class);
    when(indexCacheFac.get()).thenReturn(indexCache);
    File workingDirectory = tmpFolder.getRoot();
    final Platform platform = new TestPlatform(workingDirectory);
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            bind(Context.class).to(GuiceInjector.class).in(Scopes.SINGLETON);
            Multibinder.newSetBinder(binder(), Decorator.class);
            bind(DecoratorProvider.class).in(Scopes.SINGLETON);
            DataStreamSerializationFactoryV1 sfac = DataStreamSerializationFactoryV1.INSTANCE;
            bind(ObjectSerializingFactory.class).toInstance(sfac);
            bind(ObjectDatabase.class).to(HeapObjectDatabse.class).in(Scopes.SINGLETON);
            bind(StagingDatabase.class).to(HeapStagingDatabase.class).in(Scopes.SINGLETON);
            ConfigDatabase config = new IniFileConfigDatabase(platform);
            bind(ConfigDatabase.class).toInstance(config);
            bind(ObjectDatabaseCacheFactory.class).toInstance(odbCacheFac);
            bind(StagingDatabaseCacheFactory.class).toInstance(indexCacheFac);
        }
    };
    Context injector = Guice.createInjector(Modules.override(new CachingModule()).with(module)).getInstance(org.locationtech.geogig.api.Context.class);
    odb = injector.objectDatabase();
    index = injector.stagingDatabase();
    odb.open();
    index.open();
    odb.put(o1);
    odb.put(o2);
    odb.put(o3);
    index.put(s1);
    index.put(s2);
    index.put(s3);
}
Also used : ObjectSerializingFactory(org.locationtech.geogig.storage.ObjectSerializingFactory) Context(org.locationtech.geogig.api.Context) TestPlatform(org.locationtech.geogig.api.TestPlatform) Platform(org.locationtech.geogig.api.Platform) DecoratorProvider(org.locationtech.geogig.di.DecoratorProvider) IniFileConfigDatabase(org.locationtech.geogig.storage.fs.IniFileConfigDatabase) ConfigDatabase(org.locationtech.geogig.storage.ConfigDatabase) HeapObjectDatabse(org.locationtech.geogig.storage.memory.HeapObjectDatabse) IniFileConfigDatabase(org.locationtech.geogig.storage.fs.IniFileConfigDatabase) AbstractModule(com.google.inject.AbstractModule) GuiceInjector(org.locationtech.geogig.di.GuiceInjector) TestPlatform(org.locationtech.geogig.api.TestPlatform) HeapStagingDatabase(org.locationtech.geogig.storage.memory.HeapStagingDatabase) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) DataStreamSerializationFactoryV1(org.locationtech.geogig.storage.datastream.DataStreamSerializationFactoryV1) File(java.io.File) Cache(com.google.common.cache.Cache) Before(org.junit.Before)

Example 28 with Context

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

the class DepthSearchTest method addTree.

private RevTreeBuilder addTree(RevTreeBuilder root, final String treePath, String... singleNodeNames) {
    Context mockInjector = mock(Context.class);
    when(mockInjector.objectDatabase()).thenReturn(odb);
    CreateTree op = new CreateTree().setIndex(false);
    op.setContext(mockInjector);
    RevTreeBuilder subTreeBuilder = op.call();
    if (singleNodeNames != null) {
        for (String singleNodeName : singleNodeNames) {
            String nodePath = NodeRef.appendChild(treePath, singleNodeName);
            ObjectId fakeFeatureOId = ObjectId.forString(nodePath);
            // forString(treePath);
            ObjectId fakeTypeOId = ObjectId.NULL;
            subTreeBuilder.put(Node.create(singleNodeName, fakeFeatureOId, fakeTypeOId, TYPE.FEATURE, null));
        }
    }
    RevTree subtree = subTreeBuilder.build();
    WriteBack writeBack = fakeGeogig.command(WriteBack.class).setAncestor(root).setChildPath(treePath).setTree(subtree).setMetadataId(fakeTreeMetadataId);
    ObjectId newRootId = writeBack.call();
    return fakeGeogig.command(RevObjectParse.class).setObjectId(newRootId).call(RevTree.class).get().builder(odb);
}
Also used : Context(org.locationtech.geogig.api.Context) WriteBack(org.locationtech.geogig.api.plumbing.WriteBack) CreateTree(org.locationtech.geogig.api.plumbing.CreateTree) ObjectId(org.locationtech.geogig.api.ObjectId) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) RevTree(org.locationtech.geogig.api.RevTree)

Example 29 with Context

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

the class LsTree 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) {
    String ref = null;
    if (refList != null && !refList.isEmpty()) {
        ref = refList.get(0);
    }
    LsTreeOp.Strategy lsStrategy = LsTreeOp.Strategy.CHILDREN;
    if (recursive) {
        if (includeTrees) {
            lsStrategy = LsTreeOp.Strategy.DEPTHFIRST;
        } else if (onlyTrees) {
            lsStrategy = LsTreeOp.Strategy.DEPTHFIRST_ONLY_TREES;
        } else {
            lsStrategy = LsTreeOp.Strategy.DEPTHFIRST_ONLY_FEATURES;
        }
    } else {
        if (onlyTrees) {
            lsStrategy = LsTreeOp.Strategy.TREES_ONLY;
        }
    }
    final Context geogig = this.getCommandLocator(context);
    final Iterator<NodeRef> iter = geogig.command(LsTreeOp.class).setReference(ref).setStrategy(lsStrategy).call();
    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start(true);
            out.writeLsTreeResponse(iter, verbose);
            out.finish();
        }
    });
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) NodeRef(org.locationtech.geogig.api.NodeRef) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) CommandResponse(org.locationtech.geogig.web.api.CommandResponse)

Example 30 with Context

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

the class PullWebOp 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);
    PullOp command = geogig.command(PullOp.class).setAuthor(authorName.orNull(), authorEmail.orNull()).setRemote(remoteName).setAll(fetchAll).addRefSpec(refSpec);
    try {
        final PullResult result = command.call();
        final Iterator<DiffEntry> iter;
        if (result.getOldRef() != null && result.getNewRef() != null && result.getOldRef().equals(result.getNewRef())) {
            iter = null;
        } else {
            if (result.getOldRef() == null) {
                iter = geogig.command(DiffOp.class).setNewVersion(result.getNewRef().getObjectId()).setOldVersion(ObjectId.NULL).call();
            } else {
                iter = geogig.command(DiffOp.class).setNewVersion(result.getNewRef().getObjectId()).setOldVersion(result.getOldRef().getObjectId()).call();
            }
        }
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writePullResponse(result, iter, geogig);
                out.finish();
            }
        });
    } catch (SynchronizationException e) {
        switch(e.statusCode) {
            case HISTORY_TOO_SHALLOW:
            default:
                context.setResponseContent(CommandResponse.error("Unable to pull, the remote history is shallow."));
        }
    } catch (MergeConflictsException e) {
        String[] refs = refSpec.split(":");
        String remoteRef = Ref.REMOTES_PREFIX + remoteName + "/" + refs[0];
        Optional<Ref> sourceRef = geogig.command(RefParse.class).setName(remoteRef).call();
        String destinationref = "";
        if (refs.length == 2) {
            destinationref = refs[1];
        } else {
            final Optional<Ref> currHead = geogig.command(RefParse.class).setName(Ref.HEAD).call();
            if (!currHead.isPresent()) {
                context.setResponseContent(CommandResponse.error("Repository has no HEAD, can't pull."));
            } else if (!(currHead.get() instanceof SymRef)) {
                context.setResponseContent(CommandResponse.error("Can't pull from detached HEAD"));
            }
            final SymRef headRef = (SymRef) currHead.get();
            destinationref = headRef.getTarget();
        }
        Optional<Ref> destRef = geogig.command(RefParse.class).setName(destinationref).call();
        final RevCommit theirs = context.getGeoGIG().getRepository().getCommit(sourceRef.get().getObjectId());
        final RevCommit ours = context.getGeoGIG().getRepository().getCommit(destRef.get().getObjectId());
        final Optional<ObjectId> ancestor = geogig.command(FindCommonAncestor.class).setLeft(ours).setRight(theirs).call();
        context.setResponseContent(new CommandResponse() {

            final MergeScenarioReport report = geogig.command(ReportMergeScenarioOp.class).setMergeIntoCommit(ours).setToMergeCommit(theirs).call();

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                Optional<RevCommit> mergeCommit = Optional.absent();
                out.writeMergeResponse(mergeCommit, report, geogig, ours.getId(), theirs.getId(), ancestor.get());
                out.finish();
            }
        });
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) PullOp(org.locationtech.geogig.api.porcelain.PullOp) Optional(com.google.common.base.Optional) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) MergeScenarioReport(org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport) PullResult(org.locationtech.geogig.api.porcelain.PullResult) MergeConflictsException(org.locationtech.geogig.api.porcelain.MergeConflictsException) SynchronizationException(org.locationtech.geogig.api.porcelain.SynchronizationException) SymRef(org.locationtech.geogig.api.SymRef) MergeConflictsException(org.locationtech.geogig.api.porcelain.MergeConflictsException) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RefParse(org.locationtech.geogig.api.plumbing.RefParse) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) SynchronizationException(org.locationtech.geogig.api.porcelain.SynchronizationException) RevCommit(org.locationtech.geogig.api.RevCommit)

Aggregations

Context (org.locationtech.geogig.api.Context)52 CommandContext (org.locationtech.geogig.web.api.CommandContext)24 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)24 ResponseWriter (org.locationtech.geogig.web.api.ResponseWriter)24 CommandSpecException (org.locationtech.geogig.web.api.CommandSpecException)16 ObjectId (org.locationtech.geogig.api.ObjectId)13 File (java.io.File)9 GeoGIG (org.locationtech.geogig.api.GeoGIG)9 NodeRef (org.locationtech.geogig.api.NodeRef)8 Ref (org.locationtech.geogig.api.Ref)8 Before (org.junit.Before)7 RevCommit (org.locationtech.geogig.api.RevCommit)7 Optional (com.google.common.base.Optional)6 Platform (org.locationtech.geogig.api.Platform)6 RevTree (org.locationtech.geogig.api.RevTree)6 TestPlatform (org.locationtech.geogig.api.TestPlatform)6 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)5 GeogigModule (org.locationtech.geogig.di.GeogigModule)5 MemoryModule (org.locationtech.geogig.api.MemoryModule)4 RevFeature (org.locationtech.geogig.api.RevFeature)4