Search in sources :

Example 16 with RevObject

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

the class LogServlet method doGetHtml.

@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
    Repository repo = ServletUtils.getRepository(req);
    GitilesView view = getView(req, repo);
    Paginator paginator = null;
    try {
        GitilesAccess access = getAccess(req);
        paginator = newPaginator(repo, view, access);
        if (paginator == null) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }
        DateFormatter df = new DateFormatter(access, Format.DEFAULT);
        // Allow the user to select a logView variant with the "pretty" param.
        String pretty = Iterables.getFirst(view.getParameters().get(PRETTY_PARAM), "default");
        Map<String, Object> data = Maps.newHashMapWithExpectedSize(2);
        if (!view.getRevision().nameIsId()) {
            List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1);
            for (RevObject o : RevisionServlet.listObjects(paginator.getWalk(), view.getRevision())) {
                if (o instanceof RevTag) {
                    tags.add(new TagSoyData(linkifier, req).toSoyData((RevTag) o, df));
                }
            }
            if (!tags.isEmpty()) {
                data.put("tags", tags);
            }
        }
        String title = "Log - ";
        if (view.getOldRevision() != Revision.NULL) {
            title += view.getRevisionRange();
        } else {
            title += view.getRevision().getName();
        }
        data.put("title", title);
        try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.logDetail", data)) {
            Writer w = newWriter(out, res);
            new LogSoyData(req, access, pretty).renderStreaming(paginator, null, renderer, w, df);
            w.flush();
        }
    } catch (RevWalkException e) {
        log.warn("Error in rev walk", e);
        res.setStatus(SC_INTERNAL_SERVER_ERROR);
        return;
    } finally {
        if (paginator != null) {
            paginator.getWalk().close();
        }
    }
}
Also used : RevTag(org.eclipse.jgit.revwalk.RevTag) RevObject(org.eclipse.jgit.revwalk.RevObject) OutputStream(java.io.OutputStream) RevWalkException(org.eclipse.jgit.errors.RevWalkException) Repository(org.eclipse.jgit.lib.Repository) RevObject(org.eclipse.jgit.revwalk.RevObject) Map(java.util.Map) Writer(java.io.Writer)

Example 17 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project che by eclipse.

the class JGitConnection method tagCreate.

@Override
public Tag tagCreate(TagCreateParams params) throws GitException {
    String commit = params.getCommit();
    if (commit == null) {
        commit = Constants.HEAD;
    }
    try {
        RevWalk revWalk = new RevWalk(repository);
        RevObject revObject;
        try {
            revObject = revWalk.parseAny(repository.resolve(commit));
        } finally {
            revWalk.close();
        }
        TagCommand tagCommand = getGit().tag().setName(params.getName()).setObjectId(revObject).setMessage(params.getMessage()).setForceUpdate(params.isForce());
        GitUser tagger = getUser();
        if (tagger != null) {
            tagCommand.setTagger(new PersonIdent(tagger.getName(), tagger.getEmail()));
        }
        Ref revTagRef = tagCommand.call();
        RevTag revTag = revWalk.parseTag(revTagRef.getLeaf().getObjectId());
        return newDto(Tag.class).withName(revTag.getTagName());
    } catch (IOException | GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : RevTag(org.eclipse.jgit.revwalk.RevTag) RevObject(org.eclipse.jgit.revwalk.RevObject) GitException(org.eclipse.che.api.git.exception.GitException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TagCommand(org.eclipse.jgit.api.TagCommand) GitUser(org.eclipse.che.api.git.shared.GitUser) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Ref(org.eclipse.jgit.lib.Ref) PersonIdent(org.eclipse.jgit.lib.PersonIdent) RevTag(org.eclipse.jgit.revwalk.RevTag) Tag(org.eclipse.che.api.git.shared.Tag)

Example 18 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project gitblit by gitblit.

the class JGitUtils method getRefs.

/**
	 * Returns a list of references in the repository matching "refs". If the
	 * repository is null or empty, an empty list is returned.
	 *
	 * @param repository
	 * @param refs
	 *            if unspecified, all refs are returned
	 * @param fullName
	 *            if true, /refs/something/yadayadayada is returned. If false,
	 *            yadayadayada is returned.
	 * @param maxCount
	 *            if < 0, all references are returned
	 * @param offset
	 *            if maxCount provided sets the starting point of the records to return
	 * @return list of references
	 */
private static List<RefModel> getRefs(Repository repository, String refs, boolean fullName, int maxCount, int offset) {
    List<RefModel> list = new ArrayList<RefModel>();
    if (maxCount == 0) {
        return list;
    }
    if (!hasCommits(repository)) {
        return list;
    }
    try {
        Map<String, Ref> map = repository.getRefDatabase().getRefs(refs);
        RevWalk rw = new RevWalk(repository);
        for (Entry<String, Ref> entry : map.entrySet()) {
            Ref ref = entry.getValue();
            RevObject object = rw.parseAny(ref.getObjectId());
            String name = entry.getKey();
            if (fullName && !StringUtils.isEmpty(refs)) {
                name = refs + name;
            }
            list.add(new RefModel(name, ref, object));
        }
        rw.dispose();
        Collections.sort(list);
        Collections.reverse(list);
        if (maxCount > 0 && list.size() > maxCount) {
            if (offset < 0) {
                offset = 0;
            }
            int endIndex = offset + maxCount;
            if (endIndex > list.size()) {
                endIndex = list.size();
            }
            list = new ArrayList<RefModel>(list.subList(offset, endIndex));
        }
    } catch (IOException e) {
        error(e, repository, "{0} failed to retrieve {1}", refs);
    }
    return list;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) RefModel(com.gitblit.models.RefModel) RevObject(org.eclipse.jgit.revwalk.RevObject) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 19 with RevObject

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

the class SubmitOnPushIT method assertTag.

private void assertTag(Project.NameKey project, String branch, PushOneCommit.Tag tag) throws Exception {
    try (Repository repo = repoManager.openRepository(project)) {
        Ref tagRef = repo.findRef(tag.name);
        assertThat(tagRef).isNotNull();
        ObjectId taggedCommit = null;
        if (tag instanceof PushOneCommit.AnnotatedTag) {
            PushOneCommit.AnnotatedTag annotatedTag = (PushOneCommit.AnnotatedTag) tag;
            try (RevWalk rw = new RevWalk(repo)) {
                RevObject object = rw.parseAny(tagRef.getObjectId());
                assertThat(object).isInstanceOf(RevTag.class);
                RevTag tagObject = (RevTag) object;
                assertThat(tagObject.getFullMessage()).isEqualTo(annotatedTag.message);
                assertThat(tagObject.getTaggerIdent()).isEqualTo(annotatedTag.tagger);
                taggedCommit = tagObject.getObject();
            }
        } else {
            taggedCommit = tagRef.getObjectId();
        }
        ObjectId headCommit = repo.exactRef(branch).getObjectId();
        assertThat(taggedCommit).isNotNull();
        assertThat(taggedCommit).isEqualTo(headCommit);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RevTag(org.eclipse.jgit.revwalk.RevTag) ObjectId(org.eclipse.jgit.lib.ObjectId) RevObject(org.eclipse.jgit.revwalk.RevObject) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit)

Example 20 with RevObject

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

the class ConfigChangeIT method readProjectConfig.

private Config readProjectConfig() throws Exception {
    RevWalk rw = testRepo.getRevWalk();
    RevTree tree = rw.parseTree(testRepo.getRepository().resolve("HEAD"));
    RevObject obj = rw.parseAny(testRepo.get(tree, "project.config"));
    ObjectLoader loader = rw.getObjectReader().open(obj);
    String text = new String(loader.getCachedBytes(), UTF_8);
    Config cfg = new Config();
    cfg.fromText(text);
    return cfg;
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject) Config(org.eclipse.jgit.lib.Config) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree)

Aggregations

RevObject (org.eclipse.jgit.revwalk.RevObject)25 RevWalk (org.eclipse.jgit.revwalk.RevWalk)14 IOException (java.io.IOException)10 ObjectId (org.eclipse.jgit.lib.ObjectId)10 RevCommit (org.eclipse.jgit.revwalk.RevCommit)10 Repository (org.eclipse.jgit.lib.Repository)8 Ref (org.eclipse.jgit.lib.Ref)7 RevTag (org.eclipse.jgit.revwalk.RevTag)6 RevTree (org.eclipse.jgit.revwalk.RevTree)6 Map (java.util.Map)4 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)4 TagCommand (org.eclipse.jgit.api.TagCommand)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 PersonIdent (org.eclipse.jgit.lib.PersonIdent)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2