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