blob: 85355ffbce67ed55071259bd9bab0b96489fd13b [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 Borowitzc410f962014-09-23 10:49:26 -070017import static com.google.common.base.MoreObjects.firstNonNull;
Dave Borowitzba1bd222014-07-30 09:50:29 -070018import static com.google.common.base.Preconditions.checkNotNull;
19
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070020import com.google.common.collect.ImmutableMap;
Dave Borowitz01551272014-03-16 13:43:16 -070021import com.google.common.collect.ImmutableSet;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080022import com.google.common.collect.Maps;
Dave Borowitz01551272014-03-16 13:43:16 -070023import com.google.common.collect.Sets;
24import com.google.gitiles.CommitData.Field;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070025import com.google.template.soy.tofu.SoyTofu;
Dave Borowitzb772cce2012-12-28 13:57:22 -080026
Dave Borowitzba1bd222014-07-30 09:50:29 -070027import org.eclipse.jgit.lib.Config;
Dave Borowitzb772cce2012-12-28 13:57:22 -080028import org.eclipse.jgit.lib.ObjectId;
Dave Borowitzb772cce2012-12-28 13:57:22 -080029import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitzb772cce2012-12-28 13:57:22 -080030
Dave Borowitz209d0aa2012-12-28 14:28:53 -080031import java.io.IOException;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070032import java.io.Writer;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080033import java.util.Map;
Michael Moss558f8642014-04-15 09:29:21 -070034import java.util.Set;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080035
36import javax.annotation.Nullable;
37import javax.servlet.http.HttpServletRequest;
Dave Borowitzb772cce2012-12-28 13:57:22 -080038
39public class LogSoyData {
Dave Borowitz01551272014-03-16 13:43:16 -070040 private static final ImmutableSet<Field> FIELDS = Sets.immutableEnumSet(Field.ABBREV_SHA,
Michael Moss558f8642014-04-15 09:29:21 -070041 Field.SHA, Field.URL, Field.SHORT_MESSAGE, Field.MESSAGE, Field.AUTHOR, Field.COMMITTER,
42 Field.BRANCHES, Field.TAGS);
43 private static final ImmutableSet<Field> VERBOSE_FIELDS = Field.setOf(FIELDS, Field.DIFF_TREE);
Dave Borowitz01551272014-03-16 13:43:16 -070044
Dave Borowitzb772cce2012-12-28 13:57:22 -080045 private final HttpServletRequest req;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070046 private final GitilesView view;
Dave Borowitzba1bd222014-07-30 09:50:29 -070047 private final Set<Field> fields;
Dave Borowitzba1bd222014-07-30 09:50:29 -070048 private final String variant;
Gustaf Lundh06a98702015-08-17 17:29:11 +020049 private CommitSoyData csd;
Dave Borowitzb772cce2012-12-28 13:57:22 -080050
Dave Borowitzba1bd222014-07-30 09:50:29 -070051 public LogSoyData(HttpServletRequest req, GitilesAccess access, String pretty)
52 throws IOException {
53 this.req = checkNotNull(req);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070054 this.view = checkNotNull(ViewFilter.getView(req));
Dave Borowitzb15a5862015-09-16 15:14:56 -040055 checkNotNull(pretty);
Dave Borowitzba1bd222014-07-30 09:50:29 -070056 Config config = access.getConfig();
57 fields = config.getBoolean("logFormat", pretty, "verbose", false) ? VERBOSE_FIELDS : FIELDS;
Dave Borowitzc410f962014-09-23 10:49:26 -070058 variant = firstNonNull(config.getString("logFormat", pretty, "variant"), pretty);
Dave Borowitzb772cce2012-12-28 13:57:22 -080059 }
60
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070061 public void renderStreaming(Paginator paginator, @Nullable String revision, Renderer renderer,
62 Writer out, DateFormatter df) throws IOException {
63 renderer.newRenderer("gitiles.logEntriesHeader")
64 .setData(toHeaderSoyData(paginator, revision))
65 .render(out);
66 out.flush();
67
68 SoyTofu.Renderer entryRenderer = renderer.newRenderer("gitiles.logEntryWrapper");
Andrew Bonventreb33426e2015-09-09 18:28:28 -040069 boolean renderedEntries = false;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070070 for (RevCommit c : paginator) {
Andrew Bonventreb33426e2015-09-09 18:28:28 -040071 entryRenderer.setData(toEntrySoyData(paginator, c, df)).render(out);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070072 out.flush();
Andrew Bonventreb33426e2015-09-09 18:28:28 -040073 renderedEntries = true;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070074 }
Andrew Bonventreb33426e2015-09-09 18:28:28 -040075 if (!renderedEntries) {
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070076 renderer.newRenderer("gitiles.emptyLog").render(out);
77 }
78
79 renderer.newRenderer("gitiles.logEntriesFooter")
80 .setData(toFooterSoyData(paginator, revision))
81 .render(out);
Dave Borowitz27fada42013-11-01 11:09:49 -070082 }
83
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070084 private Map<String, Object> toHeaderSoyData(Paginator paginator, @Nullable String revision) {
Andrew Bonventreb33426e2015-09-09 18:28:28 -040085 Map<String, Object> data = Maps.newHashMapWithExpectedSize(1);
Dave Borowitzb772cce2012-12-28 13:57:22 -080086 ObjectId prev = paginator.getPreviousStart();
87 if (prev != null) {
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070088 GitilesView.Builder prevView = copyAndCanonicalizeView(revision);
Dave Borowitzb772cce2012-12-28 13:57:22 -080089 if (!prevView.getRevision().getId().equals(prev)) {
90 prevView.replaceParam(LogServlet.START_PARAM, prev.name());
91 }
92 data.put("previousUrl", prevView.toUrl());
93 }
94 return data;
95 }
96
Andrew Bonventreb33426e2015-09-09 18:28:28 -040097 private Map<String, Object> toEntrySoyData(Paginator paginator, RevCommit c, DateFormatter df)
98 throws IOException {
Gustaf Lundh06a98702015-08-17 17:29:11 +020099 if (csd == null) {
100 csd = new CommitSoyData();
101 }
102
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400103 Map<String, Object> entry = csd.setRevWalk(paginator.getWalk()).toSoyData(req, c, fields, df);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700104 return ImmutableMap.of(
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700105 "variant", variant,
106 "entry", entry);
107 }
108
109 private Map<String, Object> toFooterSoyData(Paginator paginator, @Nullable String revision) {
110 Map<String, Object> data = Maps.newHashMapWithExpectedSize(1);
111 ObjectId next = paginator.getNextStart();
112 if (next != null) {
113 data.put("nextUrl", copyAndCanonicalizeView(revision)
114 .replaceParam(LogServlet.START_PARAM, next.name())
115 .toUrl());
116 }
117 return data;
118 }
119
120 private GitilesView.Builder copyAndCanonicalizeView(String revision) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800121 // Canonicalize the view by using full SHAs.
122 GitilesView.Builder copy = GitilesView.log().copyFrom(view);
123 if (view.getRevision() != Revision.NULL) {
124 copy.setRevision(view.getRevision());
125 } else {
126 copy.setRevision(Revision.named(revision));
127 }
128 if (view.getOldRevision() != Revision.NULL) {
129 copy.setOldRevision(view.getOldRevision());
130 }
131 return copy;
132 }
133}