| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame^] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import java.io.IOException; |
| 18 | import java.util.List; |
| 19 | import java.util.Map; |
| 20 | import java.util.Set; |
| 21 | |
| 22 | import javax.annotation.Nullable; |
| 23 | import javax.servlet.http.HttpServletRequest; |
| 24 | |
| 25 | import org.eclipse.jgit.lib.AnyObjectId; |
| 26 | import org.eclipse.jgit.lib.ObjectId; |
| 27 | import org.eclipse.jgit.lib.Ref; |
| 28 | import org.eclipse.jgit.lib.Repository; |
| 29 | import org.eclipse.jgit.revwalk.RevCommit; |
| 30 | import org.eclipse.jgit.revwalk.RevWalk; |
| 31 | |
| 32 | import com.google.common.collect.Lists; |
| 33 | import com.google.common.collect.Maps; |
| 34 | import com.google.gitiles.CommitSoyData.KeySet; |
| 35 | |
| 36 | public 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 | } |