Search in sources :

Example 11 with Repository

use of org.locationtech.geogig.repository.Repository in project GeoGig by boundlessgeo.

the class CloneOp method _call.

/**
     * Executes the clone operation.
     * 
     * @return {@code null}
     * @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
     */
@Override
protected Void _call() {
    Preconditions.checkArgument(repositoryURL != null && !repositoryURL.isEmpty(), "No repository specified to clone from.");
    Repository repository = repository();
    if (repository.isSparse()) {
        Preconditions.checkArgument(branch.isPresent(), "No branch specified for sparse clone.");
    }
    ProgressListener progressListener = getProgressListener();
    progressListener.started();
    // Set up origin
    Remote remote = command(RemoteAddOp.class).setName("origin").setURL(repositoryURL).setMapped(repository.isSparse()).setUserName(username).setPassword(password).setBranch(repository.isSparse() ? branch.get() : null).call();
    if (!depth.isPresent()) {
        // See if we are cloning a shallow clone. If so, a depth must be specified.
        Optional<IRemoteRepo> remoteRepo = RemoteUtils.newRemote(GlobalContextBuilder.builder.build(Hints.readOnly()), remote, repository, repository.deduplicationService());
        Preconditions.checkState(remoteRepo.isPresent(), "Failed to connect to the remote.");
        IRemoteRepo remoteRepoInstance = remoteRepo.get();
        try {
            remoteRepoInstance.open();
        } catch (IOException e) {
            Throwables.propagate(e);
        }
        try {
            depth = remoteRepoInstance.getDepth();
        } finally {
            try {
                remoteRepoInstance.close();
            } catch (IOException e) {
                Throwables.propagate(e);
            }
        }
    }
    if (depth.isPresent()) {
        command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName(Repository.DEPTH_CONFIG_KEY).setValue(depth.get().toString()).call();
    }
    // Fetch remote data
    command(FetchOp.class).setDepth(depth.or(0)).setProgressListener(progressListener).call();
    // Set up remote tracking branches
    final ImmutableSet<Ref> remoteRefs = command(LsRemote.class).retrieveTags(false).setRemote(Suppliers.ofInstance(Optional.of(remote))).call();
    boolean emptyRepo = true;
    for (Ref remoteRef : remoteRefs) {
        if (emptyRepo && !remoteRef.getObjectId().isNull()) {
            emptyRepo = false;
        }
        String branchName = remoteRef.localName();
        if (remoteRef instanceof SymRef) {
            continue;
        }
        if (!command(RefParse.class).setName(Ref.HEADS_PREFIX + branchName).call().isPresent()) {
            command(BranchCreateOp.class).setName(branchName).setSource(remoteRef.getObjectId().toString()).call();
        } else {
            command(UpdateRef.class).setName(Ref.HEADS_PREFIX + branchName).setNewValue(remoteRef.getObjectId()).call();
        }
        command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branchName + ".remote").setValue(remote.getName()).call();
        command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName("branches." + branchName + ".merge").setValue(Ref.HEADS_PREFIX + remoteRef.localName()).call();
    }
    if (!emptyRepo) {
        // checkout branch
        if (branch.isPresent()) {
            command(CheckoutOp.class).setForce(true).setSource(branch.get()).call();
        } else {
            // checkout the head
            final Optional<Ref> currRemoteHead = command(RefParse.class).setName(Ref.REMOTES_PREFIX + remote.getName() + "/" + Ref.HEAD).call();
            if (currRemoteHead.isPresent() && currRemoteHead.get() instanceof SymRef) {
                final SymRef remoteHeadRef = (SymRef) currRemoteHead.get();
                final String currentBranch = Ref.localName(remoteHeadRef.getTarget());
                command(CheckoutOp.class).setForce(true).setSource(currentBranch).call();
            } else {
            // just leave at default; should be master since we just initialized the repo.
            }
        }
    }
    progressListener.complete();
    return null;
}
Also used : Remote(org.locationtech.geogig.api.Remote) LsRemote(org.locationtech.geogig.api.plumbing.LsRemote) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) IOException(java.io.IOException) Repository(org.locationtech.geogig.repository.Repository) Ref(org.locationtech.geogig.api.Ref) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) SymRef(org.locationtech.geogig.api.SymRef) SymRef(org.locationtech.geogig.api.SymRef) ProgressListener(org.locationtech.geogig.api.ProgressListener) IRemoteRepo(org.locationtech.geogig.remote.IRemoteRepo) LsRemote(org.locationtech.geogig.api.plumbing.LsRemote)

Example 12 with Repository

use of org.locationtech.geogig.repository.Repository in project GeoGig by boundlessgeo.

the class InitOp method callInternal.

private Repository callInternal() {
    final Platform platform = platform();
    final File workingDirectory = platform.pwd();
    final Optional<URL> repoUrl = new ResolveGeogigDir(platform).call();
    final boolean repoExisted = repoUrl.isPresent();
    final File envHome;
    if (repoExisted) {
        // we're at either the repo working dir or a subdirectory of it
        try {
            envHome = new File(repoUrl.get().toURI());
        } catch (URISyntaxException e) {
            throw Throwables.propagate(e);
        }
    } else {
        envHome = new File(workingDirectory, ".geogig");
        if (!envHome.mkdirs()) {
            throw new RuntimeException("Unable to create geogig environment at '" + envHome.getAbsolutePath() + "'");
        }
    }
    Map<String, String> effectiveConfigBuilder = Maps.newTreeMap();
    addDefaults(defaults, effectiveConfigBuilder);
    if (config != null) {
        effectiveConfigBuilder.putAll(config);
    }
    if (filterFile != null) {
        try {
            final String FILTER_FILE = "filter.ini";
            File oldFilterFile = new File(filterFile);
            if (!oldFilterFile.exists()) {
                throw new FileNotFoundException("No filter file found at " + filterFile + ".");
            }
            Optional<URL> envHomeURL = new ResolveGeogigDir(platform).call();
            Preconditions.checkState(envHomeURL.isPresent(), "Not inside a geogig directory");
            final URL url = envHomeURL.get();
            if (!"file".equals(url.getProtocol())) {
                throw new UnsupportedOperationException("Sparse clone works only against file system repositories. " + "Repository location: " + url.toExternalForm());
            }
            File repoDir;
            try {
                repoDir = new File(url.toURI());
            } catch (URISyntaxException e) {
                throw new IllegalStateException("Unable to access directory " + url.toExternalForm(), e);
            }
            File newFilterFile = new File(repoDir, FILTER_FILE);
            Files.copy(oldFilterFile, newFilterFile);
            effectiveConfigBuilder.put("sparse.filter", FILTER_FILE);
        } catch (Exception e) {
            throw new IllegalStateException("Unable to copy filter file at path " + filterFile + " to the new repository.", e);
        }
    }
    try {
        Preconditions.checkState(envHome.toURI().toURL().equals(new ResolveGeogigDir(platform).call().get()));
    } catch (MalformedURLException e) {
        Throwables.propagate(e);
    }
    Repository repository;
    try {
        if (!repoExisted) {
            ConfigDatabase configDB = context.configDatabase();
            try {
                for (Entry<String, String> pair : effectiveConfigBuilder.entrySet()) {
                    String key = pair.getKey();
                    String value = pair.getValue();
                    configDB.put(key, value);
                }
                repository = repository();
                repository.configure();
            } catch (RepositoryConnectionException e) {
                throw new IllegalStateException("Unable to initialize repository for the first time: " + e.getMessage(), e);
            }
        } else {
            repository = repository();
        }
        try {
            repository.open();
            // make sure the repo has the empty tree
            ObjectDatabase objectDatabase = repository.objectDatabase();
            objectDatabase.put(RevTree.EMPTY);
        } catch (RepositoryConnectionException e) {
            throw new IllegalStateException("Error opening repository databases: " + e.getMessage(), e);
        }
        createSampleHooks(envHome);
    } catch (ConfigException e) {
        throw e;
    } catch (RuntimeException e) {
        Throwables.propagateIfInstanceOf(e, IllegalStateException.class);
        throw new IllegalStateException("Can't access repository at '" + envHome.getAbsolutePath() + "'", e);
    }
    if (!repoExisted) {
        try {
            createDefaultRefs();
        } catch (IllegalStateException e) {
            Throwables.propagate(e);
        }
    }
    return repository;
}
Also used : MalformedURLException(java.net.MalformedURLException) Platform(org.locationtech.geogig.api.Platform) ConfigDatabase(org.locationtech.geogig.storage.ConfigDatabase) FileNotFoundException(java.io.FileNotFoundException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) RepositoryConnectionException(org.locationtech.geogig.repository.RepositoryConnectionException) RepositoryConnectionException(org.locationtech.geogig.repository.RepositoryConnectionException) Repository(org.locationtech.geogig.repository.Repository) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) ResolveGeogigDir(org.locationtech.geogig.api.plumbing.ResolveGeogigDir) File(java.io.File)

Example 13 with Repository

use of org.locationtech.geogig.repository.Repository in project GeoGig by boundlessgeo.

the class GeoGigDataStoreFactory method createNewDataStore.

/**
     * @see org.geotools.data.DataStoreFactorySpi#createNewDataStore(java.util.Map)
     */
@Override
public GeoGigDataStore createNewDataStore(Map<String, Serializable> params) throws IOException {
    String defaultNamespace = (String) DEFAULT_NAMESPACE.lookUp(params);
    File repositoryRoot = new File((String) REPOSITORY.lookUp(params));
    if (!repositoryRoot.isDirectory()) {
        if (repositoryRoot.exists()) {
            throw new IOException(repositoryRoot.getAbsolutePath() + " is not a directory");
        }
        repositoryRoot.mkdirs();
    }
    GeoGIG geogig = new GeoGIG(repositoryRoot);
    try {
        Repository repository = geogig.getOrCreateRepository();
        Preconditions.checkState(repository != null);
    } catch (RuntimeException e) {
        throw new IOException(e);
    }
    GeoGigDataStore store = new GeoGigDataStore(geogig);
    if (defaultNamespace != null) {
        store.setNamespaceURI(defaultNamespace);
    }
    return store;
}
Also used : Repository(org.locationtech.geogig.repository.Repository) IOException(java.io.IOException) File(java.io.File) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 14 with Repository

use of org.locationtech.geogig.repository.Repository in project GeoGig by boundlessgeo.

the class GeoGigDataStoreFactory method createDataStore.

@Override
public GeoGigDataStore createDataStore(Map<String, Serializable> params) throws IOException {
    @Nullable final String lookUpClass = (String) RESOLVER_CLASS_NAME.lookUp(params);
    RepositoryLookup resolver = resolver(lookUpClass);
    final String repositoryLocation = (String) REPOSITORY.lookUp(params);
    @Nullable final String defaultNamespace = (String) DEFAULT_NAMESPACE.lookUp(params);
    @Nullable final String branch = (String) BRANCH.lookUp(params);
    @Nullable final String head = (String) HEAD.lookUp(params);
    @Nullable final String effectiveHead = (head == null) ? branch : head;
    @Nullable final Boolean create = (Boolean) CREATE.lookUp(params);
    final File repositoryDirectory = resolver.resolve(repositoryLocation);
    if (create != null && create.booleanValue()) {
        if (!repositoryDirectory.exists()) {
            return createNewDataStore(params);
        }
    }
    GeoGIG geogig;
    try {
        geogig = new GeoGIG(repositoryDirectory);
    } catch (RuntimeException e) {
        throw new IOException(e.getMessage(), e);
    }
    Repository repository = geogig.getRepository();
    if (null == repository) {
        if (create != null && create.booleanValue()) {
            return createNewDataStore(params);
        }
        throw new IOException(String.format("Directory is not a geogig repository: '%s'", repositoryDirectory.getAbsolutePath()));
    }
    GeoGigDataStore store = new GeoGigDataStore(geogig);
    if (defaultNamespace != null) {
        store.setNamespaceURI(defaultNamespace);
    }
    if (effectiveHead != null) {
        store.setHead(effectiveHead);
    }
    return store;
}
Also used : Repository(org.locationtech.geogig.repository.Repository) IOException(java.io.IOException) File(java.io.File) Nullable(javax.annotation.Nullable) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 15 with Repository

use of org.locationtech.geogig.repository.Repository 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)

Aggregations

Repository (org.locationtech.geogig.repository.Repository)30 ObjectId (org.locationtech.geogig.api.ObjectId)13 RevCommit (org.locationtech.geogig.api.RevCommit)11 File (java.io.File)10 IOException (java.io.IOException)9 Ref (org.locationtech.geogig.api.Ref)9 UpdateRef (org.locationtech.geogig.api.plumbing.UpdateRef)9 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)8 GeoGIG (org.locationtech.geogig.api.GeoGIG)7 UpdateSymRef (org.locationtech.geogig.api.plumbing.UpdateSymRef)7 SymRef (org.locationtech.geogig.api.SymRef)6 ArrayList (java.util.ArrayList)5 NodeRef (org.locationtech.geogig.api.NodeRef)5 Conflict (org.locationtech.geogig.api.plumbing.merge.Conflict)5 CommitBuilder (org.locationtech.geogig.api.CommitBuilder)4 Platform (org.locationtech.geogig.api.Platform)4 RevObject (org.locationtech.geogig.api.RevObject)4 CanRunDuringConflict (org.locationtech.geogig.di.CanRunDuringConflict)4 LinkedList (java.util.LinkedList)3 RevTree (org.locationtech.geogig.api.RevTree)3