use of org.eclipse.jgit.revwalk.RevObject in project gitiles by GerritCodeReview.
the class RevisionServlet method listObjects.
// TODO(dborowitz): Extract this.
static List<RevObject> listObjects(RevWalk walk, Revision rev) throws MissingObjectException, IOException {
List<RevObject> objects = Lists.newArrayListWithExpectedSize(1);
ObjectId id = rev.getId();
RevObject cur;
while (true) {
cur = walk.parseAny(id);
objects.add(cur);
if (cur.getType() != Constants.OBJ_TAG) {
break;
}
id = ((RevTag) cur).getObject();
}
if (cur.getType() == Constants.OBJ_COMMIT) {
objects.add(walk.parseTree(((RevCommit) cur).getTree()));
}
return objects;
}
use of org.eclipse.jgit.revwalk.RevObject in project gitiles by GerritCodeReview.
the class RevisionServlet method doGetHtml.
@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
GitilesView view = ViewFilter.getView(req);
Repository repo = ServletUtils.getRepository(req);
GitilesAccess access = getAccess(req);
Config cfg = getAccess(req).getConfig();
try (RevWalk walk = new RevWalk(repo)) {
DateFormatter df = new DateFormatter(access, Format.DEFAULT);
List<RevObject> objects = listObjects(walk, view.getRevision());
List<Map<String, ?>> soyObjects = Lists.newArrayListWithCapacity(objects.size());
boolean hasBlob = false;
boolean hasReadme = false;
// TODO(sop): Allow caching commits by SHA-1 when no S cookie is sent.
for (RevObject obj : objects) {
try {
switch(obj.getType()) {
case OBJ_COMMIT:
soyObjects.add(ImmutableMap.of("type", Constants.TYPE_COMMIT, "data", new CommitSoyData().setLinkifier(linkifier).setRevWalk(walk).setArchiveFormat(getArchiveFormat(access)).toSoyData(req, (RevCommit) obj, COMMIT_SOY_FIELDS, df)));
break;
case OBJ_TREE:
Map<String, Object> tree = new TreeSoyData(walk.getObjectReader(), view, cfg, (RevTree) obj, req.getRequestURI()).toSoyData(obj);
soyObjects.add(ImmutableMap.of("type", Constants.TYPE_TREE, "data", tree));
hasReadme = tree.containsKey("readmeHtml");
break;
case OBJ_BLOB:
soyObjects.add(ImmutableMap.of("type", Constants.TYPE_BLOB, "data", new BlobSoyData(walk.getObjectReader(), view).toSoyData(obj)));
hasBlob = true;
break;
case OBJ_TAG:
soyObjects.add(ImmutableMap.of("type", Constants.TYPE_TAG, "data", new TagSoyData(linkifier, req).toSoyData((RevTag) obj, df)));
break;
default:
log.warn("Bad object type for {}: {}", ObjectId.toString(obj.getId()), obj.getType());
res.setStatus(SC_NOT_FOUND);
return;
}
} catch (MissingObjectException e) {
log.warn("Missing object " + ObjectId.toString(obj.getId()), e);
res.setStatus(SC_NOT_FOUND);
return;
} catch (IncorrectObjectTypeException e) {
log.warn("Incorrect object type for " + ObjectId.toString(obj.getId()), e);
res.setStatus(SC_NOT_FOUND);
return;
}
}
renderHtml(req, res, "gitiles.revisionDetail", ImmutableMap.of("title", view.getRevision().getName(), "objects", soyObjects, "hasBlob", hasBlob, "hasReadme", hasReadme));
}
}
use of org.eclipse.jgit.revwalk.RevObject in project gitiles by GerritCodeReview.
the class RootedDocServlet method service.
@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
try (Repository repo = resolver.open(req, null);
RevWalk rw = new RevWalk(repo)) {
ObjectId id = repo.resolve(BRANCH);
if (id == null) {
res.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
RevObject obj = rw.peel(rw.parseAny(id));
if (!(obj instanceof RevCommit)) {
res.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
req.setAttribute(ATTRIBUTE_REPOSITORY, repo);
ViewFilter.setView(req, GitilesView.rootedDoc().setHostName(req.getServerName()).setServletPath(req.getContextPath() + req.getServletPath()).setRevision(BRANCH, obj).setPathPart(req.getPathInfo()).build());
docServlet.service(req, res);
} catch (RepositoryNotFoundException | ServiceNotAuthorizedException | ServiceNotEnabledException e) {
log.error(String.format("cannot open repository for %s", req.getServerName()), e);
res.sendError(HttpServletResponse.SC_NOT_FOUND);
} finally {
ViewFilter.removeView(req);
req.removeAttribute(ATTRIBUTE_REPOSITORY);
}
}
use of org.eclipse.jgit.revwalk.RevObject 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);
}
}
use of org.eclipse.jgit.revwalk.RevObject in project gerrit by GerritCodeReview.
the class ReceiveCommits method validateNewCommits.
private void validateNewCommits(RefControl ctl, ReceiveCommand cmd) {
if (ctl.canForgeAuthor() && ctl.canForgeCommitter() && ctl.canForgeGerritServerIdentity() && ctl.canUploadMerges() && !projectControl.getProjectState().isUseSignedOffBy() && Iterables.isEmpty(rejectCommits) && !RefNames.REFS_CONFIG.equals(ctl.getRefName()) && !(MagicBranch.isMagicBranch(cmd.getRefName()) || NEW_PATCHSET.matcher(cmd.getRefName()).matches())) {
logDebug("Short-circuiting new commit validation");
return;
}
boolean defaultName = Strings.isNullOrEmpty(user.getAccount().getFullName());
RevWalk walk = rp.getRevWalk();
walk.reset();
walk.sort(RevSort.NONE);
try {
RevObject parsedObject = walk.parseAny(cmd.getNewId());
if (!(parsedObject instanceof RevCommit)) {
return;
}
ListMultimap<ObjectId, Ref> existing = changeRefsById();
walk.markStart((RevCommit) parsedObject);
markHeadsAsUninteresting(walk, cmd.getRefName());
int i = 0;
for (RevCommit c; (c = walk.next()) != null; ) {
i++;
if (existing.keySet().contains(c)) {
continue;
} else if (!validCommit(walk, ctl, cmd, c)) {
break;
}
if (defaultName && user.hasEmailAddress(c.getCommitterIdent().getEmailAddress())) {
try {
Account a = db.accounts().get(user.getAccountId());
if (a != null && Strings.isNullOrEmpty(a.getFullName())) {
a.setFullName(c.getCommitterIdent().getName());
db.accounts().update(Collections.singleton(a));
user.getAccount().setFullName(a.getFullName());
accountCache.evict(a.getId());
}
} catch (OrmException e) {
logWarn("Cannot default full_name", e);
} finally {
defaultName = false;
}
}
}
logDebug("Validated {} new commits", i);
} catch (IOException err) {
cmd.setResult(REJECTED_MISSING_OBJECT);
logError("Invalid pack upload; one or more objects weren't sent", err);
}
}
Aggregations