use of org.eclipse.jgit.treewalk.TreeWalk in project gerrit by GerritCodeReview.
the class FileContentUtil method getContent.
public BinaryResult getContent(Repository repo, ProjectState project, ObjectId revstr, String path) throws IOException, ResourceNotFoundException {
try (RevWalk rw = new RevWalk(repo)) {
RevCommit commit = rw.parseCommit(revstr);
ObjectReader reader = rw.getObjectReader();
TreeWalk tw = TreeWalk.forPath(reader, path, commit.getTree());
if (tw == null) {
throw new ResourceNotFoundException();
}
org.eclipse.jgit.lib.FileMode mode = tw.getFileMode(0);
ObjectId id = tw.getObjectId(0);
if (mode == org.eclipse.jgit.lib.FileMode.GITLINK) {
return BinaryResult.create(id.name()).setContentType(X_GIT_GITLINK).base64();
}
ObjectLoader obj = repo.open(id, OBJ_BLOB);
byte[] raw;
try {
raw = obj.getCachedBytes(MAX_SIZE);
} catch (LargeObjectException e) {
raw = null;
}
String type;
if (mode == org.eclipse.jgit.lib.FileMode.SYMLINK) {
type = X_GIT_SYMLINK;
} else {
type = registry.getMimeType(path, raw).toString();
type = resolveContentType(project, path, FileMode.FILE, type);
}
return asBinaryResult(raw, obj).setContentType(type).base64();
}
}
use of org.eclipse.jgit.treewalk.TreeWalk in project gitiles by GerritCodeReview.
the class TreeSoyData method toSoyData.
public Map<String, Object> toSoyData(ObjectId treeId) throws MissingObjectException, IOException {
TreeWalk tw = new TreeWalk(reader);
tw.addTree(treeId);
tw.setRecursive(false);
return toSoyData(treeId, tw);
}
use of org.eclipse.jgit.treewalk.TreeWalk in project gitiles by GerritCodeReview.
the class ImageLoader method inlineMaybe.
private String inlineMaybe(@Nullable String markdownPath, String imagePath) {
if (config.imageLimit <= 0) {
return null;
}
String path = PathResolver.resolve(markdownPath, imagePath);
if (path == null) {
return null;
}
String type = MimeTypes.getMimeType(path);
if (!ALLOWED_TYPES.contains(type)) {
return null;
}
try {
TreeWalk tw = TreeWalk.forPath(reader, path, root);
if (tw == null || tw.getFileMode(0) != FileMode.REGULAR_FILE) {
return null;
}
ObjectId id = tw.getObjectId(0);
byte[] raw = reader.open(id, Constants.OBJ_BLOB).getCachedBytes(config.imageLimit);
if (raw.length > config.imageLimit) {
return null;
}
return "data:" + type + ";base64," + BaseEncoding.base64().encode(raw);
} catch (LargeObjectException.ExceedsLimit e) {
return null;
} catch (IOException err) {
String repo = view != null ? view.getRepositoryName() : "<unknown>";
log.error(String.format("cannot read repo %s image %s from %s", repo, path, root.name()), err);
return null;
}
}
use of org.eclipse.jgit.treewalk.TreeWalk in project gitiles by GerritCodeReview.
the class DiffServlet method doGetHtml.
@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
GitilesView view = ViewFilter.getView(req);
Repository repo = ServletUtils.getRepository(req);
try (RevWalk walk = new RevWalk(repo);
TreeWalk tw = newTreeWalk(walk, view)) {
boolean showCommit;
boolean isFile;
AbstractTreeIterator oldTree;
AbstractTreeIterator newTree;
try {
if (tw == null && !view.getPathPart().isEmpty()) {
res.setStatus(SC_NOT_FOUND);
return;
}
isFile = tw != null && isFile(tw);
// If we are viewing the diff between a commit and one of its parents,
// include the commit detail in the rendered page.
showCommit = isParentOf(walk, view.getOldRevision(), view.getRevision());
oldTree = getTreeIterator(walk, view.getOldRevision().getId());
newTree = getTreeIterator(walk, view.getRevision().getId());
} catch (MissingObjectException | IncorrectObjectTypeException e) {
res.setStatus(SC_NOT_FOUND);
return;
}
Map<String, Object> data = getData(req);
data.put("title", "Diff - " + view.getRevisionRange());
if (showCommit) {
Set<Field> fs = CommitSoyData.DEFAULT_FIELDS;
if (isFile) {
fs = Field.setOf(fs, Field.PARENT_BLAME_URL);
}
GitilesAccess access = getAccess(req);
DateFormatter df = new DateFormatter(access, Format.DEFAULT);
data.put("commit", new CommitSoyData().setLinkifier(linkifier).setArchiveFormat(getArchiveFormat(access)).toSoyData(req, walk.parseCommit(view.getRevision().getId()), fs, df));
}
if (!data.containsKey("repositoryName") && (view.getRepositoryName() != null)) {
data.put("repositoryName", view.getRepositoryName());
}
if (!data.containsKey("breadcrumbs")) {
data.put("breadcrumbs", view.getBreadcrumbs());
}
setCacheHeaders(req, res);
try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.diffDetail", data);
DiffFormatter diff = new HtmlDiffFormatter(renderer, view, out)) {
formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
}
}
}
use of org.eclipse.jgit.treewalk.TreeWalk in project gerrit by GerritCodeReview.
the class RenameFileModification method getPathEdits.
@Override
public List<DirCacheEditor.PathEdit> getPathEdits(Repository repository, RevCommit baseCommit) throws IOException {
try (RevWalk revWalk = new RevWalk(repository)) {
revWalk.parseHeaders(baseCommit);
try (TreeWalk treeWalk = TreeWalk.forPath(revWalk.getObjectReader(), currentFilePath, baseCommit.getTree())) {
if (treeWalk == null) {
return Collections.emptyList();
}
DirCacheEditor.DeletePath deletePathEdit = new DirCacheEditor.DeletePath(currentFilePath);
AddPath addPathEdit = new AddPath(newFilePath, treeWalk.getFileMode(0), treeWalk.getObjectId(0));
return Arrays.asList(deletePathEdit, addPathEdit);
}
}
}
Aggregations