use of org.locationtech.geogig.api.Remote in project GeoGig by boundlessgeo.
the class RemoteWebOp method remoteRemove.
private void remoteRemove(CommandContext context, final Context geogig) {
if (remoteName == null || remoteName.trim().isEmpty()) {
throw new CommandSpecException("No remote was specified.");
}
final Remote remote;
try {
remote = geogig.command(RemoteRemoveOp.class).setName(remoteName).call();
} catch (RemoteException e) {
context.setResponseContent(CommandResponse.error(e.statusCode.toString()));
return;
} catch (Exception e) {
context.setResponseContent(CommandResponse.error("Aborting Remote Remove"));
return;
}
context.setResponseContent(new CommandResponse() {
@Override
public void write(ResponseWriter out) throws Exception {
out.start();
out.writeElement("name", remote.getName());
out.finish();
}
});
}
use of org.locationtech.geogig.api.Remote in project GeoGig by boundlessgeo.
the class RemoteWebOp method remoteUpdate.
private void remoteUpdate(CommandContext context, final Context geogig) {
if (remoteName == null || remoteName.trim().isEmpty()) {
throw new CommandSpecException("No remote was specified.");
} else if (remoteURL == null || remoteURL.trim().isEmpty()) {
throw new CommandSpecException("No URL was specified.");
}
final Remote newRemote;
try {
if (newName != null && !newName.trim().isEmpty() && !newName.equals(remoteName)) {
newRemote = geogig.command(RemoteAddOp.class).setName(newName).setURL(remoteURL).setUserName(username).setPassword(password).call();
geogig.command(RemoteRemoveOp.class).setName(remoteName).call();
} else {
geogig.command(RemoteRemoveOp.class).setName(remoteName).call();
newRemote = geogig.command(RemoteAddOp.class).setName(remoteName).setURL(remoteURL).setUserName(username).setPassword(password).call();
}
context.setResponseContent(new CommandResponse() {
@Override
public void write(ResponseWriter out) throws Exception {
out.start();
out.writeElement("name", newRemote.getName());
out.finish();
}
});
} catch (RemoteException e) {
context.setResponseContent(CommandResponse.error(e.statusCode.toString()));
} catch (Exception e) {
context.setResponseContent(CommandResponse.error("Aborting Remote Update"));
}
}
use of org.locationtech.geogig.api.Remote 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.api.Remote in project GeoGig by boundlessgeo.
the class FetchOp method _call.
/**
* Executes the fetch operation.
*
* @return {@code null}
* @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
*/
@Override
protected TransferSummary _call() {
if (all) {
// Add all remotes to list.
ImmutableList<Remote> localRemotes = command(RemoteListOp.class).call();
for (Remote remote : localRemotes) {
if (!remotes.contains(remote)) {
remotes.add(remote);
}
}
} else if (remotes.size() == 0) {
// If no remotes are specified, default to the origin remote
addRemote("origin");
}
final ProgressListener progressListener = getProgressListener();
progressListener.started();
Optional<Integer> repoDepth = repository().getDepth();
if (repoDepth.isPresent()) {
if (fullDepth) {
depth = Optional.of(Integer.MAX_VALUE);
}
if (depth.isPresent()) {
if (depth.get() > repoDepth.get()) {
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName(Repository.DEPTH_CONFIG_KEY).setValue(depth.get().toString()).call();
repoDepth = depth;
}
}
} else if (depth.isPresent() || fullDepth) {
// Ignore depth, this is a full repository
depth = Optional.absent();
fullDepth = false;
}
TransferSummary result = new TransferSummary();
for (Remote remote : remotes) {
final ImmutableSet<Ref> remoteRemoteRefs = command(LsRemote.class).setRemote(Suppliers.ofInstance(Optional.of(remote))).retrieveTags(!remote.getMapped() && (!repoDepth.isPresent() || fullDepth)).call();
final ImmutableSet<Ref> localRemoteRefs = command(LsRemote.class).retrieveLocalRefs(true).setRemote(Suppliers.ofInstance(Optional.of(remote))).call();
// If we have specified a depth to pull, we may have more history to pull from existing
// refs.
List<ChangedRef> needUpdate = findOutdatedRefs(remote, remoteRemoteRefs, localRemoteRefs, depth);
if (prune) {
// Delete local refs that aren't in the remote
List<Ref> locals = new ArrayList<Ref>();
// provided a tag so it makes sense not to prune them anyway.
for (Ref remoteRef : remoteRemoteRefs) {
Optional<Ref> localRef = findLocal(remoteRef, localRemoteRefs);
if (localRef.isPresent()) {
locals.add(localRef.get());
}
}
for (Ref localRef : localRemoteRefs) {
if (!locals.contains(localRef)) {
// Delete the ref
ChangedRef changedRef = new ChangedRef(localRef, null, ChangeTypes.REMOVED_REF);
needUpdate.add(changedRef);
command(UpdateRef.class).setDelete(true).setName(localRef.getName()).call();
}
}
}
Optional<IRemoteRepo> remoteRepo = getRemoteRepo(remote, 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 {
int refCount = 0;
for (ChangedRef ref : needUpdate) {
if (ref.getType() != ChangeTypes.REMOVED_REF) {
refCount++;
Optional<Integer> newFetchLimit = depth;
// fetch limit to the current repository depth.
if (!newFetchLimit.isPresent() && repoDepth.isPresent() && ref.getType() == ChangeTypes.ADDED_REF) {
newFetchLimit = repoDepth;
}
// Fetch updated data from this ref
Ref newRef = ref.getNewRef();
remoteRepoInstance.fetchNewData(newRef, newFetchLimit, progressListener);
if (repoDepth.isPresent() && !fullDepth) {
// Update the repository depth if it is deeper than before.
int newDepth;
try {
newDepth = repository().graphDatabase().getDepth(newRef.getObjectId());
} catch (IllegalStateException e) {
throw new RuntimeException(ref.toString(), e);
}
if (newDepth > repoDepth.get()) {
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL).setName(Repository.DEPTH_CONFIG_KEY).setValue(Integer.toString(newDepth)).call();
repoDepth = Optional.of(newDepth);
}
}
// Update the ref
Ref updatedRef = updateLocalRef(newRef, remote, localRemoteRefs);
ref.setNewRef(updatedRef);
}
}
if (needUpdate.size() > 0) {
result.addAll(remote.getFetchURL(), needUpdate);
}
// Update HEAD ref
if (!remote.getMapped()) {
Ref remoteHead = remoteRepoInstance.headRef();
if (remoteHead != null) {
updateLocalRef(remoteHead, remote, localRemoteRefs);
}
}
} finally {
try {
remoteRepoInstance.close();
} catch (IOException e) {
Throwables.propagate(e);
}
}
}
if (fullDepth) {
// The full history was fetched, this is no longer a shallow clone
command(ConfigOp.class).setAction(ConfigAction.CONFIG_UNSET).setScope(ConfigScope.LOCAL).setName(Repository.DEPTH_CONFIG_KEY).call();
}
progressListener.complete();
return result;
}
use of org.locationtech.geogig.api.Remote in project GeoGig by boundlessgeo.
the class PushOp method _call.
/**
* Executes the push operation.
*
* @return {@code null}
* @see org.locationtech.geogig.api.AbstractGeoGigOp#call()
*/
@Override
protected TransferSummary _call() {
final String remoteName = this.remoteName == null ? "origin" : this.remoteName;
final Remote remote = resolveRemote(remoteName);
final List<TransferableRef> refsToPush = resolveRefs();
SendPack sendPack = command(SendPack.class);
sendPack.setRemote(remote);
sendPack.setRefs(refsToPush);
sendPack.setProgressListener(getProgressListener());
TransferSummary result = sendPack.call();
return result;
}
Aggregations