use of com.google.gitiles.blame.cache.Region in project gitiles by GerritCodeReview.
the class BlameServlet method getRegions.
private RegionResult getRegions(GitilesView view, GitilesAccess access, Repository repo, RevWalk rw, HttpServletResponse res) throws IOException {
RevCommit currCommit = rw.parseCommit(view.getRevision().getId());
ObjectId currCommitBlobId = resolveBlob(view, rw, currCommit);
if (currCommitBlobId == null) {
res.setStatus(SC_NOT_FOUND);
return null;
}
ObjectId lastCommit = cache.findLastCommit(repo, currCommit, view.getPathPart());
ObjectId lastCommitBlobId = resolveBlob(view, rw, lastCommit);
if (!Objects.equals(currCommitBlobId, lastCommitBlobId)) {
log.warn(String.format("Blob %s in last modified commit %s for repo %s starting from %s" + " does not match original blob %s", ObjectId.toString(lastCommitBlobId), ObjectId.toString(lastCommit), access.getRepositoryName(), ObjectId.toString(currCommit), ObjectId.toString(currCommitBlobId)));
lastCommitBlobId = currCommitBlobId;
lastCommit = currCommit;
}
List<Region> regions = cache.get(repo, lastCommit, view.getPathPart());
if (regions.isEmpty()) {
res.setStatus(SC_NOT_FOUND);
return null;
}
return new RegionResult(regions, lastCommitBlobId);
}
use of com.google.gitiles.blame.cache.Region in project gitiles by GerritCodeReview.
the class BlameServlet method toSoyData.
private static SoyListData toSoyData(GitilesView view, ObjectReader reader, List<Region> regions, DateFormatter df) throws IOException {
Map<ObjectId, String> abbrevShas = Maps.newHashMap();
SoyListData result = new SoyListData();
for (int i = 0; i < regions.size(); i++) {
Region r = regions.get(i);
int c = i % CLASSES.size();
if (r.getSourceCommit() == null) {
// JGit bug may fail to blame some regions. We should fix this
// upstream, but handle it for now.
result.add(NULLS.get(c));
} else {
String abbrevSha = abbrevShas.get(r.getSourceCommit());
if (abbrevSha == null) {
abbrevSha = reader.abbreviate(r.getSourceCommit()).name();
abbrevShas.put(r.getSourceCommit(), abbrevSha);
}
Map<String, Object> e = Maps.newHashMapWithExpectedSize(6);
e.put("abbrevSha", abbrevSha);
String blameParent = "";
String blameText = "blame";
if (view.getRevision().getName().equals(r.getSourceCommit().name())) {
blameParent = "^";
blameText = "blame^";
}
e.put("blameUrl", GitilesView.blame().copyFrom(view).setRevision(r.getSourceCommit().name() + blameParent).setPathPart(r.getSourcePath()).toUrl());
e.put("blameText", blameText);
e.put("commitUrl", GitilesView.revision().copyFrom(view).setRevision(r.getSourceCommit().name()).toUrl());
e.put("diffUrl", GitilesView.diff().copyFrom(view).setRevision(r.getSourceCommit().name()).setPathPart(r.getSourcePath()).toUrl());
e.put("author", CommitSoyData.toSoyData(r.getSourceAuthor(), df));
e.put("class", CLASSES.get(c));
result.add(e);
}
// single block.
for (int j = 0; j < r.getCount() - 1; j++) {
result.add(NULLS.get(c));
}
}
return result;
}
use of com.google.gitiles.blame.cache.Region in project gitiles by GerritCodeReview.
the class BlameServlet method doGetJson.
@Override
protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
GitilesView view = ViewFilter.getView(req);
Repository repo = ServletUtils.getRepository(req);
try (RevWalk rw = new RevWalk(repo)) {
RegionResult result = getRegions(view, getAccess(req), repo, rw, res);
if (result == null) {
return;
}
// Output from BlameCache is 0-based for lines. We convert to 1-based for
// JSON output later (in RegionAdapter); here we're just filling in the
// transient fields.
int start = 0;
for (Region r : result.regions) {
r.setStart(start);
start += r.getCount();
}
renderJson(req, res, ImmutableMap.of("regions", result.regions), new TypeToken<Map<String, List<Region>>>() {
}.getType());
}
}
use of com.google.gitiles.blame.cache.Region in project gerrit by GerritCodeReview.
the class GetBlame method blame.
private List<BlameInfo> blame(ObjectId id, String path, Repository repository, RevWalk revWalk) throws IOException {
ListMultimap<BlameInfo, RangeInfo> ranges = MultimapBuilder.hashKeys().arrayListValues().build();
List<BlameInfo> result = new ArrayList<>();
if (blameCache.findLastCommit(repository, id, path) == null) {
return result;
}
List<Region> blameRegions = blameCache.get(repository, id, path);
int from = 1;
for (Region region : blameRegions) {
RevCommit commit = revWalk.parseCommit(region.getSourceCommit());
BlameInfo blameInfo = toBlameInfo(commit, region.getSourceAuthor());
ranges.put(blameInfo, new RangeInfo(from, from + region.getCount() - 1));
from += region.getCount();
}
for (BlameInfo key : ranges.keySet()) {
key.ranges = ranges.get(key);
result.add(key);
}
return result;
}
Aggregations