Search in sources :

Example 1 with SymRef

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();
}
Also used : InputStreamReader(java.io.InputStreamReader) ObjectId(org.locationtech.geogig.api.ObjectId) InputStream(java.io.InputStream) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Ref(org.locationtech.geogig.api.Ref) SymRef(org.locationtech.geogig.api.SymRef) SymRef(org.locationtech.geogig.api.SymRef) HttpURLConnection(java.net.HttpURLConnection) ImmutableSet(com.google.common.collect.ImmutableSet) BufferedReader(java.io.BufferedReader)

Example 2 with SymRef

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;
}
Also used : UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) Ref(org.locationtech.geogig.api.Ref) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) SymRef(org.locationtech.geogig.api.SymRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) SymRef(org.locationtech.geogig.api.SymRef) UpdateSymRef(org.locationtech.geogig.api.plumbing.UpdateSymRef) Repository(org.locationtech.geogig.repository.Repository) DiffTree(org.locationtech.geogig.api.plumbing.DiffTree) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 3 with SymRef

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;
}
Also used : Ref(org.locationtech.geogig.api.Ref) SymRef(org.locationtech.geogig.api.SymRef) SymRef(org.locationtech.geogig.api.SymRef) HttpURLConnection(java.net.HttpURLConnection) XMLStreamReader(javax.xml.stream.XMLStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) FilterInputStream(java.io.FilterInputStream) CountingInputStream(com.google.common.io.CountingInputStream) InputStream(java.io.InputStream) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Example 4 with SymRef

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;
}
Also used : Ref(org.locationtech.geogig.api.Ref) SymRef(org.locationtech.geogig.api.SymRef) SymRef(org.locationtech.geogig.api.SymRef) ObjectId(org.locationtech.geogig.api.ObjectId)

Example 5 with SymRef

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();
}
Also used : Ref(org.locationtech.geogig.api.Ref) ChangedRef(org.locationtech.geogig.api.porcelain.TransferSummary.ChangedRef) SymRef(org.locationtech.geogig.api.SymRef) NodeRef(org.locationtech.geogig.api.NodeRef) SymRef(org.locationtech.geogig.api.SymRef)

Aggregations

SymRef (org.locationtech.geogig.api.SymRef)49 Ref (org.locationtech.geogig.api.Ref)46 RevCommit (org.locationtech.geogig.api.RevCommit)27 UpdateRef (org.locationtech.geogig.api.plumbing.UpdateRef)22 ObjectId (org.locationtech.geogig.api.ObjectId)20 UpdateSymRef (org.locationtech.geogig.api.plumbing.UpdateSymRef)17 NodeRef (org.locationtech.geogig.api.NodeRef)13 RefParse (org.locationtech.geogig.api.plumbing.RefParse)12 Test (org.junit.Test)11 LogOp (org.locationtech.geogig.api.porcelain.LogOp)9 CheckoutOp (org.locationtech.geogig.api.porcelain.CheckoutOp)8 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 ForEachRef (org.locationtech.geogig.api.plumbing.ForEachRef)6 CheckoutResult (org.locationtech.geogig.api.porcelain.CheckoutResult)6 Repository (org.locationtech.geogig.repository.Repository)6 CommitBuilder (org.locationtech.geogig.api.CommitBuilder)5 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)5 PullOp (org.locationtech.geogig.api.porcelain.PullOp)5 GeoGIG (org.locationtech.geogig.api.GeoGIG)4