use of org.eclipse.jgit.lib.Repository in project gitiles by GerritCodeReview.
the class RepositoryIndexServlet 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);
RepositoryDescription desc = access.getRepositoryDescription();
try (RevWalk walk = new RevWalk(repo)) {
Paginator paginator = null;
Map<String, Object> data = Maps.newHashMapWithExpectedSize(7);
List<Map<String, Object>> tags = RefServlet.getTagsSoyData(req, timeCache, walk, REF_LIMIT);
ObjectId headId = repo.resolve(Constants.HEAD);
if (headId != null) {
RevObject head = walk.parseAny(headId);
int limit = LOG_LIMIT;
Map<String, Object> readme = renderReadme(req, walk, view, access.getConfig(), head);
if (readme != null) {
data.putAll(readme);
limit = LOG_WITH_README_LIMIT;
}
// TODO(dborowitz): Handle non-commit or missing HEAD?
if (head.getType() == Constants.OBJ_COMMIT) {
walk.reset();
walk.markStart((RevCommit) head);
paginator = new Paginator(walk, limit, null);
}
}
if (!data.containsKey("entries")) {
data.put("entries", ImmutableList.of());
}
List<Map<String, Object>> branches = RefServlet.getBranchesSoyData(req, REF_LIMIT);
data.put("cloneUrl", desc.cloneUrl);
data.put("mirroredFromUrl", Strings.nullToEmpty(desc.mirroredFromUrl));
data.put("description", Strings.nullToEmpty(desc.description));
data.put("branches", trim(branches));
if (branches.size() > REF_LIMIT) {
data.put("moreBranchesUrl", GitilesView.refs().copyFrom(view).toUrl());
}
data.put("tags", trim(tags));
data.put("hasLog", paginator != null);
if (tags.size() > REF_LIMIT) {
data.put("moreTagsUrl", GitilesView.refs().copyFrom(view).toUrl());
}
GitilesConfig.putVariant(getAccess(req).getConfig(), "logEntry", "logEntryVariant", data);
if (paginator != null) {
DateFormatter df = new DateFormatter(access, Format.DEFAULT);
try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.repositoryIndex", data)) {
Writer w = newWriter(out, res);
new LogSoyData(req, access, "oneline").renderStreaming(paginator, "HEAD", renderer, w, df);
w.flush();
}
} else {
renderHtml(req, res, "gitiles.repositoryIndex", data);
}
}
}
use of org.eclipse.jgit.lib.Repository 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.lib.Repository in project gitiles by GerritCodeReview.
the class DocServlet method doGetHtml.
@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
MarkdownConfig cfg = MarkdownConfig.get(getAccess(req).getConfig());
if (!cfg.render) {
res.setStatus(SC_NOT_FOUND);
return;
}
GitilesView view = ViewFilter.getView(req);
Repository repo = ServletUtils.getRepository(req);
try (RevWalk rw = new RevWalk(repo)) {
ObjectReader reader = rw.getObjectReader();
String path = view.getPathPart();
RevTree root;
try {
root = rw.parseTree(view.getRevision().getId());
} catch (IncorrectObjectTypeException e) {
res.setStatus(SC_NOT_FOUND);
return;
}
MarkdownFile srcmd = findFile(rw, root, path);
if (srcmd == null) {
res.setStatus(SC_NOT_FOUND);
return;
}
MarkdownFile navmd = findFile(rw, root, NAVBAR_MD);
String curEtag = etag(srcmd, navmd);
if (etagMatch(req, curEtag)) {
res.setStatus(SC_NOT_MODIFIED);
return;
}
view = view.toBuilder().setPathPart(srcmd.path).build();
try {
srcmd.read(reader, cfg);
if (navmd != null) {
navmd.read(reader, cfg);
}
} catch (LargeObjectException.ExceedsLimit errBig) {
fileTooBig(res, view, errBig);
return;
} catch (IOException err) {
readError(res, view, err);
return;
}
MarkdownToHtml.Builder fmt = MarkdownToHtml.builder().setConfig(cfg).setGitilesView(view).setRequestUri(req.getRequestURI()).setReader(reader).setRootTree(root);
res.setHeader(HttpHeaders.ETAG, curEtag);
showDoc(req, res, view, cfg, fmt, navmd, srcmd);
}
}
use of org.eclipse.jgit.lib.Repository 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.lib.Repository in project gitiles by GerritCodeReview.
the class PathServlet method doGetText.
@Override
protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
GitilesView view = ViewFilter.getView(req);
Repository repo = ServletUtils.getRepository(req);
try (RevWalk rw = new RevWalk(repo);
WalkResult wr = WalkResult.forPath(rw, view, false)) {
if (wr == null) {
res.setStatus(SC_NOT_FOUND);
return;
}
// which would be bad.
switch(wr.type) {
case SYMLINK:
case REGULAR_FILE:
case EXECUTABLE_FILE:
writeBlobText(req, res, wr);
break;
case TREE:
writeTreeText(req, res, wr);
break;
case GITLINK:
default:
renderTextError(req, res, SC_NOT_FOUND, "Not a file");
break;
}
} catch (LargeObjectException e) {
res.setStatus(SC_INTERNAL_SERVER_ERROR);
}
}
Aggregations