Search in sources :

Example 1 with Region

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);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) Region(com.google.gitiles.blame.cache.Region) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with Region

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;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) Region(com.google.gitiles.blame.cache.Region) SoyListData(com.google.template.soy.data.SoyListData)

Example 3 with Region

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());
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) TypeToken(com.google.gson.reflect.TypeToken) Region(com.google.gitiles.blame.cache.Region) GitilesView(com.google.gitiles.GitilesView) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 4 with Region

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;
}
Also used : ArrayList(java.util.ArrayList) Region(com.google.gitiles.blame.cache.Region) RangeInfo(com.google.gerrit.extensions.common.RangeInfo) BlameInfo(com.google.gerrit.extensions.common.BlameInfo) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

Region (com.google.gitiles.blame.cache.Region)4 ObjectId (org.eclipse.jgit.lib.ObjectId)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 ImmutableList (com.google.common.collect.ImmutableList)1 BlameInfo (com.google.gerrit.extensions.common.BlameInfo)1 RangeInfo (com.google.gerrit.extensions.common.RangeInfo)1 GitilesView (com.google.gitiles.GitilesView)1 TypeToken (com.google.gson.reflect.TypeToken)1 SoyListData (com.google.template.soy.data.SoyListData)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Repository (org.eclipse.jgit.lib.Repository)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1