blob: a4be3885c831d46f840936fe8bc65760a089b239 [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
Dave Borowitz01551272014-03-16 13:43:16 -070017import com.google.common.collect.ImmutableSet;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080018import com.google.common.collect.Lists;
19import com.google.common.collect.Maps;
Dave Borowitz01551272014-03-16 13:43:16 -070020import com.google.common.collect.Sets;
21import com.google.gitiles.CommitData.Field;
Dave Borowitzb772cce2012-12-28 13:57:22 -080022
Dave Borowitzb772cce2012-12-28 13:57:22 -080023import org.eclipse.jgit.lib.ObjectId;
Dave Borowitzb772cce2012-12-28 13:57:22 -080024import org.eclipse.jgit.revwalk.RevCommit;
25import org.eclipse.jgit.revwalk.RevWalk;
26
Dave Borowitz209d0aa2012-12-28 14:28:53 -080027import java.io.IOException;
28import java.util.List;
29import java.util.Map;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080030
31import javax.annotation.Nullable;
32import javax.servlet.http.HttpServletRequest;
Dave Borowitzb772cce2012-12-28 13:57:22 -080033
34public class LogSoyData {
Dave Borowitz01551272014-03-16 13:43:16 -070035 private static final ImmutableSet<Field> FIELDS = Sets.immutableEnumSet(Field.ABBREV_SHA,
Stefan Zagerd2187432014-02-28 02:23:02 -080036 Field.URL, Field.SHORT_MESSAGE, Field.AUTHOR, Field.COMMITTER, Field.BRANCHES, Field.TAGS);
Dave Borowitz01551272014-03-16 13:43:16 -070037
Dave Borowitzb772cce2012-12-28 13:57:22 -080038 private final HttpServletRequest req;
Dave Borowitzb772cce2012-12-28 13:57:22 -080039 private final GitilesView view;
40
Dave Borowitz3b086a72013-07-02 15:03:03 -070041 public LogSoyData(HttpServletRequest req, GitilesView view) {
Dave Borowitzb772cce2012-12-28 13:57:22 -080042 this.req = req;
Dave Borowitzb772cce2012-12-28 13:57:22 -080043 this.view = view;
44 }
45
46 public Map<String, Object> toSoyData(RevWalk walk, int limit, @Nullable String revision,
Dave Borowitz97a70312014-04-28 15:50:23 -070047 @Nullable ObjectId start, DateFormatter df) throws IOException {
Dave Borowitza03760a2014-01-29 16:17:28 -080048 return toSoyData(new Paginator(walk, limit, start), revision, df);
Dave Borowitz27fada42013-11-01 11:09:49 -070049 }
50
Dave Borowitza03760a2014-01-29 16:17:28 -080051 public Map<String, Object> toSoyData(Paginator paginator, @Nullable String revision,
Dave Borowitz97a70312014-04-28 15:50:23 -070052 DateFormatter df) throws IOException {
Dave Borowitzb772cce2012-12-28 13:57:22 -080053 Map<String, Object> data = Maps.newHashMapWithExpectedSize(3);
54
Dave Borowitz27fada42013-11-01 11:09:49 -070055 List<Map<String, Object>> entries = Lists.newArrayListWithCapacity(paginator.getLimit());
Dave Borowitzb772cce2012-12-28 13:57:22 -080056 for (RevCommit c : paginator) {
Dave Borowitz01551272014-03-16 13:43:16 -070057 entries.add(new CommitSoyData().toSoyData(req, c, FIELDS, df));
Dave Borowitzb772cce2012-12-28 13:57:22 -080058 }
59
60 data.put("entries", entries);
61 ObjectId next = paginator.getNextStart();
62 if (next != null) {
63 data.put("nextUrl", copyAndCanonicalize(view, revision)
64 .replaceParam(LogServlet.START_PARAM, next.name())
65 .toUrl());
66 }
67 ObjectId prev = paginator.getPreviousStart();
68 if (prev != null) {
69 GitilesView.Builder prevView = copyAndCanonicalize(view, revision);
70 if (!prevView.getRevision().getId().equals(prev)) {
71 prevView.replaceParam(LogServlet.START_PARAM, prev.name());
72 }
73 data.put("previousUrl", prevView.toUrl());
74 }
75 return data;
76 }
77
78 private static GitilesView.Builder copyAndCanonicalize(GitilesView view, String revision) {
79 // Canonicalize the view by using full SHAs.
80 GitilesView.Builder copy = GitilesView.log().copyFrom(view);
81 if (view.getRevision() != Revision.NULL) {
82 copy.setRevision(view.getRevision());
83 } else {
84 copy.setRevision(Revision.named(revision));
85 }
86 if (view.getOldRevision() != Revision.NULL) {
87 copy.setOldRevision(view.getOldRevision());
88 }
89 return copy;
90 }
91}