use of org.locationtech.geogig.api.SymRef in project GeoGig by boundlessgeo.
the class HttpMappedRemoteRepo method listRefs.
/**
* List the mapped versions of the remote's {@link Ref refs}. For example, if the remote ref
* points to commit A, the returned ref will point to the commit that A is mapped to.
*
* @param getHeads whether to return refs in the {@code refs/heads} namespace
* @param getTags whether to return refs in the {@code refs/tags} namespace
* @return an immutable set of refs from the remote
*/
@Override
public ImmutableSet<Ref> listRefs(boolean getHeads, boolean getTags) {
HttpURLConnection connection = null;
ImmutableSet.Builder<Ref> builder = new ImmutableSet.Builder<Ref>();
try {
String expanded = repositoryURL.toString() + "/repo/manifest";
connection = (HttpURLConnection) new URL(expanded).openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setDoOutput(true);
// Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
try {
while ((line = rd.readLine()) != null) {
if ((getHeads && line.startsWith("refs/heads")) || (getTags && line.startsWith("refs/tags"))) {
Ref remoteRef = HttpUtils.parseRef(line);
Ref newRef = remoteRef;
if (!(newRef instanceof SymRef) && localRepository.graphDatabase().exists(remoteRef.getObjectId())) {
ObjectId mappedCommit = localRepository.graphDatabase().getMapping(remoteRef.getObjectId());
if (mappedCommit != null) {
newRef = new Ref(remoteRef.getName(), mappedCommit);
}
}
builder.add(newRef);
}
}
} finally {
rd.close();
}
} catch (Exception e) {
throw Throwables.propagate(e);
} finally {
HttpUtils.consumeErrStreamAndCloseConnection(connection);
}
return builder.build();
}
use of org.locationtech.geogig.api.SymRef in project GeoGig by boundlessgeo.
the class ResetOp method _call.
/**
* Executes the reset operation.
*
* @return always {@code true}
*/
@Override
protected Boolean _call() {
Preconditions.checkState(!(patterns.size() > 0 && mode != ResetMode.NONE), "Ambiguous call, cannot specify paths and reset mode.");
final Optional<Ref> currHead = command(RefParse.class).setName(Ref.HEAD).call();
Preconditions.checkState(currHead.isPresent(), "Repository has no HEAD, can't reset.");
Preconditions.checkState(currHead.get() instanceof SymRef, "Can't reset from detached HEAD");
final SymRef headRef = (SymRef) currHead.get();
final String currentBranch = headRef.getTarget();
if (commit == null) {
commit = Suppliers.ofInstance(currHead.get().getObjectId());
}
Preconditions.checkState(!ObjectId.NULL.equals(commit.get()), "Commit could not be resolved.");
Repository repository = repository();
RevCommit oldCommit = repository.getCommit(commit.get());
if (patterns.size() > 0) {
for (String pattern : patterns) {
DiffTree diffOp = command(DiffTree.class).setOldTree(repository.index().getTree().getId()).setNewTree(oldCommit.getTreeId()).setPathFilter(pattern);
Iterator<DiffEntry> diff = diffOp.call();
final long numChanges = Iterators.size(diffOp.call());
if (numChanges == 0) {
// We are reseting to the current version, so there is nothing to do. However,
// if we are in a conflict state, the conflict should be removed and calling
// stage() will not do it, so we do it here
repository.stagingDatabase().removeConflict(null, pattern);
} else {
repository.index().stage(subProgress((1.f / patterns.size()) * 100.f), diff, numChanges);
}
}
} else {
if (mode == ResetMode.NONE) {
mode = ResetMode.MIXED;
}
switch(mode) {
case HARD:
// Update the index and the working tree to the target tree
index().updateStageHead(oldCommit.getTreeId());
workingTree().updateWorkHead(oldCommit.getTreeId());
break;
case SOFT:
// Do not update index or working tree to the target tree
break;
case MIXED:
// Only update the index to the target tree
index().updateStageHead(oldCommit.getTreeId());
break;
default:
throw new UnsupportedOperationException("Unsupported reset mode.");
}
// Update branch head to the specified commit
command(UpdateRef.class).setName(currentBranch).setNewValue(oldCommit.getId()).call();
command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(currentBranch).call();
Optional<Ref> ref = command(RefParse.class).setName(Ref.MERGE_HEAD).call();
if (ref.isPresent()) {
command(UpdateRef.class).setName(Ref.MERGE_HEAD).setDelete(true).call();
}
}
return true;
}
use of org.locationtech.geogig.api.SymRef in project GeoGig by boundlessgeo.
the class HttpUtils method getRemoteRef.
/**
* Retrieves the remote ref that matches the provided refspec.
*
* @param repositoryURL the URL of the repository
* @param refspec the refspec to search for
* @return the remote ref, or {@link Optional#absent()} if it wasn't found
*/
public static Optional<Ref> getRemoteRef(URL repositoryURL, String refspec) {
HttpURLConnection connection = null;
Optional<Ref> remoteRef = Optional.absent();
try {
String expanded = repositoryURL.toString() + "/refparse?name=" + refspec;
connection = connect(expanded);
InputStream inputStream = HttpUtils.getResponseStream(connection);
XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(inputStream);
try {
HttpUtils.readToElementStart(reader, "Ref");
if (reader.hasNext()) {
HttpUtils.readToElementStart(reader, "name");
final String refName = reader.getElementText();
HttpUtils.readToElementStart(reader, "objectId");
final String objectId = reader.getElementText();
HttpUtils.readToElementStart(reader, "target");
String target = null;
if (reader.hasNext()) {
target = reader.getElementText();
}
reader.close();
if (target != null) {
remoteRef = Optional.of((Ref) new SymRef(refName, new Ref(target, ObjectId.valueOf(objectId))));
} else {
remoteRef = Optional.of(new Ref(refName, ObjectId.valueOf(objectId)));
}
}
} finally {
reader.close();
inputStream.close();
}
} catch (Exception e) {
Throwables.propagate(e);
} finally {
HttpUtils.consumeErrStreamAndCloseConnection(connection);
}
return remoteRef;
}
use of org.locationtech.geogig.api.SymRef in project GeoGig by boundlessgeo.
the class HttpUtils method parseRef.
/**
* Parse the provided ref string to a {@link Ref}. The input string should be in the following
* format:
* <p>
* 'NAME HASH' for a normal ref. e.g. 'refs/heads/master abcd1234ef567890dcba'
* <p>
* 'NAME TARGET HASH' for a symbolic ref. e.g. 'HEAD refs/heads/master abcd1234ef567890dcba'
*
* @param refString the string to parse
* @return the parsed ref
*/
public static Ref parseRef(String refString) {
Ref ref = null;
String[] tokens = refString.split(" ");
if (tokens.length == 2) {
// normal ref
// NAME HASH
String name = tokens[0];
ObjectId objectId = ObjectId.valueOf(tokens[1]);
ref = new Ref(name, objectId);
} else {
// symbolic ref
// NAME TARGET HASH
String name = tokens[0];
String targetRef = tokens[1];
ObjectId targetObjectId = ObjectId.valueOf(tokens[2]);
Ref target = new Ref(targetRef, targetObjectId);
ref = new SymRef(name, target);
}
return ref;
}
use of org.locationtech.geogig.api.SymRef in project GeoGig by boundlessgeo.
the class ResponseWriter method writeBranchListResponse.
/**
* Writes the response for the {@link BranchWebOp} command to the stream.
*
* @param localBranches the local branches of the repository
* @param remoteBranches the remote branches of the repository
* @throws XMLStreamException
*/
public void writeBranchListResponse(List<Ref> localBranches, List<Ref> remoteBranches) throws XMLStreamException {
out.writeStartElement("Local");
for (Ref branch : localBranches) {
out.writeStartElement("Branch");
writeElement("name", branch.localName());
out.writeEndElement();
}
out.writeEndElement();
out.writeStartElement("Remote");
for (Ref branch : remoteBranches) {
if (!(branch instanceof SymRef)) {
out.writeStartElement("Branch");
writeElement("remoteName", branch.namespace().replace(Ref.REMOTES_PREFIX + "/", ""));
writeElement("name", branch.localName());
out.writeEndElement();
}
}
out.writeEndElement();
}
Aggregations