use of org.eclipse.jgit.lib.ObjectLoader 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;
}
use of org.eclipse.jgit.lib.ObjectLoader in project gitiles by GerritCodeReview.
the class BlobSoyData method toSoyData.
public Map<String, Object> toSoyData(String path, ObjectId blobId) throws MissingObjectException, IOException {
Map<String, Object> data = Maps.newHashMapWithExpectedSize(4);
data.put("sha", ObjectId.toString(blobId));
ObjectLoader loader = reader.open(blobId, Constants.OBJ_BLOB);
String content;
try {
byte[] raw = loader.getCachedBytes(MAX_FILE_SIZE);
content = !RawText.isBinary(raw) ? RawParseUtils.decode(raw) : null;
} catch (LargeObjectException.OutOfMemory e) {
throw e;
} catch (LargeObjectException e) {
content = null;
}
if (content != null) {
data.put("lines", prettify(path, content));
if (path != null && path.endsWith(".md")) {
data.put("docUrl", GitilesView.doc().copyFrom(view).toUrl());
}
} else {
data.put("lines", null);
data.put("size", Long.toString(loader.getSize()));
}
if (path != null && view.getRevision().getPeeledType() == OBJ_COMMIT) {
data.put("fileUrl", GitilesView.path().copyFrom(view).toUrl());
data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
data.put("blameUrl", GitilesView.blame().copyFrom(view).toUrl());
}
return data;
}
use of org.eclipse.jgit.lib.ObjectLoader in project gitiles by GerritCodeReview.
the class PathServlet method showSymlink.
private void showSymlink(HttpServletRequest req, HttpServletResponse res, WalkResult wr) throws IOException {
GitilesView view = ViewFilter.getView(req);
Map<String, Object> data = Maps.newHashMap();
ObjectLoader loader = wr.getObjectReader().open(wr.id, OBJ_BLOB);
String target;
try {
target = RawParseUtils.decode(loader.getCachedBytes(TreeSoyData.MAX_SYMLINK_SIZE));
} catch (LargeObjectException.OutOfMemory e) {
throw e;
} catch (LargeObjectException e) {
data.put("sha", ObjectId.toString(wr.id));
data.put("data", null);
data.put("size", Long.toString(loader.getSize()));
renderHtml(req, res, "gitiles.pathDetail", ImmutableMap.of("title", ViewFilter.getView(req).getPathPart(), "breadcrumbs", view.getBreadcrumbs(wr.hasSingleTree), "type", FileType.REGULAR_FILE.toString(), "data", data));
return;
}
String url = resolveTargetUrl(GitilesView.path().copyFrom(view).setPathPart(dirname(view.getPathPart())).build(), target);
data.put("title", view.getPathPart());
data.put("target", target);
if (url != null) {
data.put("targetUrl", url);
}
// TODO(sop): Allow caching files by SHA-1 when no S cookie is sent.
renderHtml(req, res, "gitiles.pathDetail", ImmutableMap.of("title", ViewFilter.getView(req).getPathPart(), "breadcrumbs", view.getBreadcrumbs(wr.hasSingleTree), "type", FileType.SYMLINK.toString(), "data", data));
}
use of org.eclipse.jgit.lib.ObjectLoader in project gitiles by GerritCodeReview.
the class RevisionServlet method doGetText.
@Override
protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
GitilesView view = ViewFilter.getView(req);
Repository repo = ServletUtils.getRepository(req);
try (ObjectReader reader = repo.newObjectReader()) {
ObjectLoader loader = reader.open(view.getRevision().getId());
if (loader.getType() != OBJ_COMMIT) {
res.setStatus(SC_NOT_FOUND);
} else {
PathServlet.setTypeHeader(res, loader.getType());
try (Writer writer = startRenderText(req, res);
OutputStream out = BaseEncoding.base64().encodingStream(writer)) {
loader.copyTo(out);
}
}
}
}
use of org.eclipse.jgit.lib.ObjectLoader in project gerrit by GerritCodeReview.
the class FileContentUtil method downloadContent.
public BinaryResult downloadContent(ProjectState project, ObjectId revstr, String path, @Nullable Integer parent) throws ResourceNotFoundException, IOException {
try (Repository repo = openRepository(project);
RevWalk rw = new RevWalk(repo)) {
String suffix = "new";
RevCommit commit = rw.parseCommit(revstr);
if (parent != null && parent > 0) {
if (commit.getParentCount() == 1) {
suffix = "old";
} else {
suffix = "old" + parent;
}
commit = rw.parseCommit(commit.getParent(parent - 1));
}
ObjectReader reader = rw.getObjectReader();
TreeWalk tw = TreeWalk.forPath(reader, path, commit.getTree());
if (tw == null) {
throw new ResourceNotFoundException();
}
int mode = tw.getFileMode(0).getObjectType();
if (mode != Constants.OBJ_BLOB) {
throw new ResourceNotFoundException();
}
ObjectId id = tw.getObjectId(0);
ObjectLoader obj = repo.open(id, OBJ_BLOB);
byte[] raw;
try {
raw = obj.getCachedBytes(MAX_SIZE);
} catch (LargeObjectException e) {
raw = null;
}
MimeType contentType = registry.getMimeType(path, raw);
return registry.isSafeInline(contentType) ? wrapBlob(path, obj, raw, contentType, suffix) : zipBlob(path, obj, commit, suffix);
}
}
Aggregations