blob: dbeb2e996cbb6f4d505cfdb835a0e6f01be1d598 [file] [log] [blame]
Dave Borowitzb772cce2012-12-28 13:57:22 -08001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
17import java.io.IOException;
18import java.util.List;
19import java.util.Map;
20import java.util.Set;
21
22import javax.annotation.Nullable;
23import javax.servlet.http.HttpServletRequest;
24
25import org.eclipse.jgit.lib.AnyObjectId;
26import org.eclipse.jgit.lib.ObjectId;
27import org.eclipse.jgit.lib.Ref;
28import org.eclipse.jgit.lib.Repository;
29import org.eclipse.jgit.revwalk.RevCommit;
30import org.eclipse.jgit.revwalk.RevWalk;
31
32import com.google.common.collect.Lists;
33import com.google.common.collect.Maps;
34import com.google.gitiles.CommitSoyData.KeySet;
35
36public class LogSoyData {
37 private final HttpServletRequest req;
38 private final Repository repo;
39 private final GitilesView view;
40
41 public LogSoyData(HttpServletRequest req, Repository repo, GitilesView view) {
42 this.req = req;
43 this.repo = repo;
44 this.view = view;
45 }
46
47 public Map<String, Object> toSoyData(RevWalk walk, int limit, @Nullable String revision,
48 @Nullable ObjectId start) throws IOException {
49 Map<String, Object> data = Maps.newHashMapWithExpectedSize(3);
50
51 Paginator paginator = new Paginator(walk, limit, start);
52 Map<AnyObjectId, Set<Ref>> refsById = repo.getAllRefsByPeeledObjectId();
53 List<Map<String, Object>> entries = Lists.newArrayListWithCapacity(limit);
54 for (RevCommit c : paginator) {
55 entries.add(new CommitSoyData(null, req, repo, walk, view, refsById)
56 .toSoyData(c, KeySet.SHORTLOG));
57 }
58
59 data.put("entries", entries);
60 ObjectId next = paginator.getNextStart();
61 if (next != null) {
62 data.put("nextUrl", copyAndCanonicalize(view, revision)
63 .replaceParam(LogServlet.START_PARAM, next.name())
64 .toUrl());
65 }
66 ObjectId prev = paginator.getPreviousStart();
67 if (prev != null) {
68 GitilesView.Builder prevView = copyAndCanonicalize(view, revision);
69 if (!prevView.getRevision().getId().equals(prev)) {
70 prevView.replaceParam(LogServlet.START_PARAM, prev.name());
71 }
72 data.put("previousUrl", prevView.toUrl());
73 }
74 return data;
75 }
76
77 private static GitilesView.Builder copyAndCanonicalize(GitilesView view, String revision) {
78 // Canonicalize the view by using full SHAs.
79 GitilesView.Builder copy = GitilesView.log().copyFrom(view);
80 if (view.getRevision() != Revision.NULL) {
81 copy.setRevision(view.getRevision());
82 } else {
83 copy.setRevision(Revision.named(revision));
84 }
85 if (view.getOldRevision() != Revision.NULL) {
86 copy.setOldRevision(view.getOldRevision());
87 }
88 return copy;
89 }
90}