Search in sources :

Example 1 with RevTag

use of org.eclipse.jgit.revwalk.RevTag in project gitiles by GerritCodeReview.

the class RevisionParser method parse.

Result parse(String path) throws IOException {
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    try (RevWalk walk = new RevWalk(repo)) {
        Revision oldRevision = null;
        StringBuilder b = new StringBuilder();
        boolean first = true;
        for (String part : PathUtil.SPLITTER.split(path)) {
            if (part.isEmpty()) {
                // No valid revision contains empty segments.
                return null;
            }
            if (!first) {
                b.append('/');
            }
            if (oldRevision == null) {
                int dots = part.indexOf("..");
                int firstParent = part.indexOf("^!");
                if (dots == 0 || firstParent == 0) {
                    return null;
                } else if (dots > 0) {
                    b.append(part, 0, dots);
                    String oldName = b.toString();
                    if (!isValidRevision(oldName)) {
                        return null;
                    }
                    RevObject old = resolve(oldName, walk);
                    if (old == null) {
                        return null;
                    }
                    oldRevision = Revision.peel(oldName, old, walk);
                    part = part.substring(dots + 2);
                    b = new StringBuilder();
                } else if (firstParent > 0) {
                    if (firstParent != part.length() - 2) {
                        return null;
                    }
                    b.append(part, 0, part.length() - 2);
                    String name = b.toString();
                    if (!isValidRevision(name)) {
                        return null;
                    }
                    RevObject obj = resolve(name, walk);
                    if (obj == null) {
                        return null;
                    }
                    while (obj instanceof RevTag) {
                        obj = ((RevTag) obj).getObject();
                        walk.parseHeaders(obj);
                    }
                    if (!(obj instanceof RevCommit)) {
                        // Not a commit, ^! is invalid.
                        return null;
                    }
                    RevCommit c = (RevCommit) obj;
                    if (c.getParentCount() > 0) {
                        oldRevision = Revision.peeled(name + "^", c.getParent(0));
                    } else {
                        oldRevision = Revision.NULL;
                    }
                    Result result = new Result(Revision.peeled(name, c), oldRevision, path.substring(name.length() + 2));
                    return isVisible(walk, result) ? result : null;
                }
            }
            b.append(part);
            String name = b.toString();
            if (!isValidRevision(name)) {
                return null;
            }
            RevObject obj = resolve(name, walk);
            if (obj != null) {
                int pathStart;
                if (oldRevision == null) {
                    // foo
                    pathStart = name.length();
                } else {
                    // foo..bar (foo may be empty)
                    pathStart = oldRevision.getName().length() + 2 + name.length();
                }
                Result result = new Result(Revision.peel(name, obj, walk), oldRevision, path.substring(pathStart));
                return isVisible(walk, result) ? result : null;
            }
            first = false;
        }
        return null;
    }
}
Also used : RevTag(org.eclipse.jgit.revwalk.RevTag) RevObject(org.eclipse.jgit.revwalk.RevObject) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with RevTag

use of org.eclipse.jgit.revwalk.RevTag in project gitiles by GerritCodeReview.

the class TimeCache method getTime.

Long getTime(final RevWalk walk, final ObjectId id) throws IOException {
    try {
        return cache.get(id, () -> {
            RevObject o = walk.parseAny(id);
            while (o instanceof RevTag) {
                RevTag tag = (RevTag) o;
                PersonIdent ident = tag.getTaggerIdent();
                if (ident != null) {
                    return ident.getWhen().getTime() / 1000;
                }
                o = tag.getObject();
                walk.parseHeaders(o);
            }
            if (o.getType() == Constants.OBJ_COMMIT) {
                return Long.valueOf(((RevCommit) o).getCommitTime());
            }
            return Long.MIN_VALUE;
        });
    } catch (ExecutionException e) {
        Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
        throw new IOException(e);
    }
}
Also used : RevTag(org.eclipse.jgit.revwalk.RevTag) RevObject(org.eclipse.jgit.revwalk.RevObject) PersonIdent(org.eclipse.jgit.lib.PersonIdent) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with RevTag

use of org.eclipse.jgit.revwalk.RevTag in project gerrit by GerritCodeReview.

the class ListTags method createTagInfo.

public static TagInfo createTagInfo(PermissionBackend.ForRef perm, Ref ref, RevWalk rw) throws MissingObjectException, IOException {
    RevObject object = rw.parseAny(ref.getObjectId());
    boolean canDelete = perm.testOrFalse(RefPermission.DELETE);
    if (object instanceof RevTag) {
        // Annotated or signed tag
        RevTag tag = (RevTag) object;
        PersonIdent tagger = tag.getTaggerIdent();
        return new TagInfo(ref.getName(), tag.getName(), tag.getObject().getName(), tag.getFullMessage().trim(), tagger != null ? CommonConverters.toGitPerson(tag.getTaggerIdent()) : null, canDelete);
    }
    // Lightweight tag
    return new TagInfo(ref.getName(), ref.getObjectId().getName(), canDelete);
}
Also used : RevTag(org.eclipse.jgit.revwalk.RevTag) RevObject(org.eclipse.jgit.revwalk.RevObject) PersonIdent(org.eclipse.jgit.lib.PersonIdent) TagInfo(com.google.gerrit.extensions.api.projects.TagInfo)

Example 4 with RevTag

use of org.eclipse.jgit.revwalk.RevTag in project gitiles by GerritCodeReview.

the class RevisionParserTest method parseTag.

@Test
public void parseTag() throws Exception {
    RevCommit master = repo.branch("master").commit().create();
    RevTag masterTag = repo.update("refs/tags/master-tag", repo.tag("master-tag", master));
    RevTag masterTagTag = repo.update("refs/tags/master-tag-tag", repo.tag("master-tag-tag", master));
    assertThat(parser.parse("master-tag")).isEqualTo(new Result(new Revision("master-tag", masterTag, OBJ_TAG, master, OBJ_COMMIT)));
    assertThat(parser.parse("master-tag-tag")).isEqualTo(new Result(new Revision("master-tag-tag", masterTagTag, OBJ_TAG, master, OBJ_COMMIT)));
    RevBlob blob = repo.update("refs/tags/blob", repo.blob("blob"));
    RevTag blobTag = repo.update("refs/tags/blob-tag", repo.tag("blob-tag", blob));
    assertThat(parser.parse("blob")).isEqualTo(new Result(Revision.peeled("blob", blob)));
    assertThat(parser.parse("blob-tag")).isEqualTo(new Result(new Revision("blob-tag", blobTag, OBJ_TAG, blob, OBJ_BLOB)));
}
Also used : RevTag(org.eclipse.jgit.revwalk.RevTag) RevBlob(org.eclipse.jgit.revwalk.RevBlob) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Result(com.google.gitiles.RevisionParser.Result) Test(org.junit.Test)

Example 5 with RevTag

use of org.eclipse.jgit.revwalk.RevTag in project gitiles by GerritCodeReview.

the class TimeCacheTest method taggedTagTime.

@Test
public void taggedTagTime() throws Exception {
    repo.tick(2);
    RevTag tag = repo.tag("tag", repo.commit().create());
    repo.tick(-1);
    RevTag tagTag = repo.tag("tagtag", tag);
    assertThat(getTime(tag)).isEqualTo(start + 3);
    assertThat(getTime(tagTag)).isEqualTo(start + 2);
}
Also used : RevTag(org.eclipse.jgit.revwalk.RevTag) Test(org.junit.Test)

Aggregations

RevTag (org.eclipse.jgit.revwalk.RevTag)14 RevCommit (org.eclipse.jgit.revwalk.RevCommit)7 RevObject (org.eclipse.jgit.revwalk.RevObject)6 Test (org.junit.Test)5 PersonIdent (org.eclipse.jgit.lib.PersonIdent)4 RevWalk (org.eclipse.jgit.revwalk.RevWalk)4 Ref (org.eclipse.jgit.lib.Ref)3 IOException (java.io.IOException)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 Repository (org.eclipse.jgit.lib.Repository)2 RevBlob (org.eclipse.jgit.revwalk.RevBlob)2 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)1 TagInfo (com.google.gerrit.extensions.api.projects.TagInfo)1 Result (com.google.gitiles.RevisionParser.Result)1 OutputStream (java.io.OutputStream)1 Writer (java.io.Writer)1 Date (java.util.Date)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 GitException (org.eclipse.che.api.git.exception.GitException)1