blob: dc782abe1cde155b9d33b087c3c909afa23272e4 [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;
David Pursehouse28726042019-06-27 09:09:30 +090025import com.google.template.soy.data.LoggingAdvisingAppendable;
26import com.google.template.soy.jbcsrc.api.SoySauce;
Dave Borowitz3b744b12016-08-19 16:11:10 -040027import java.io.IOException;
28import java.io.Writer;
29import java.util.Map;
30import java.util.Set;
31import javax.annotation.Nullable;
32import javax.servlet.http.HttpServletRequest;
Dave Borowitzcb88d4d2015-10-26 13:58:17 -040033import org.eclipse.jgit.diff.DiffEntry;
34import org.eclipse.jgit.diff.DiffEntry.ChangeType;
Dave Borowitzba1bd222014-07-30 09:50:29 -070035import org.eclipse.jgit.lib.Config;
Dave Borowitzb772cce2012-12-28 13:57:22 -080036import org.eclipse.jgit.lib.ObjectId;
Dave Borowitzb772cce2012-12-28 13:57:22 -080037import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitzb772cce2012-12-28 13:57:22 -080038
Dave Borowitzb772cce2012-12-28 13:57:22 -080039public class LogSoyData {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020040 private static final ImmutableSet<Field> FIELDS =
41 Sets.immutableEnumSet(
42 Field.ABBREV_SHA,
43 Field.SHA,
44 Field.URL,
45 Field.SHORT_MESSAGE,
46 Field.MESSAGE,
47 Field.AUTHOR,
48 Field.COMMITTER,
49 Field.BRANCHES,
50 Field.TAGS);
Michael Moss558f8642014-04-15 09:29:21 -070051 private static final ImmutableSet<Field> VERBOSE_FIELDS = Field.setOf(FIELDS, Field.DIFF_TREE);
Dave Borowitz01551272014-03-16 13:43:16 -070052
Dave Borowitz571b7a02018-02-09 15:18:10 -050053 /** Behavior for the footer link when rendering streaming log data. */
54 public enum FooterBehavior {
55 /** "Next" link that skips commits in the current view. */
56 NEXT,
57
58 /** "More" link that starts from HEAD. */
59 LOG_HEAD;
60 }
61
Dave Borowitzb772cce2012-12-28 13:57:22 -080062 private final HttpServletRequest req;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070063 private final GitilesView view;
Dave Borowitzba1bd222014-07-30 09:50:29 -070064 private final Set<Field> fields;
Dave Borowitzba1bd222014-07-30 09:50:29 -070065 private final String variant;
Gustaf Lundh06a98702015-08-17 17:29:11 +020066 private CommitSoyData csd;
Dave Borowitzb772cce2012-12-28 13:57:22 -080067
Dave Borowitzba1bd222014-07-30 09:50:29 -070068 public LogSoyData(HttpServletRequest req, GitilesAccess access, String pretty)
69 throws IOException {
70 this.req = checkNotNull(req);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070071 this.view = checkNotNull(ViewFilter.getView(req));
Dave Borowitzb15a5862015-09-16 15:14:56 -040072 checkNotNull(pretty);
Dave Borowitzba1bd222014-07-30 09:50:29 -070073 Config config = access.getConfig();
74 fields = config.getBoolean("logFormat", pretty, "verbose", false) ? VERBOSE_FIELDS : FIELDS;
Dave Borowitzc410f962014-09-23 10:49:26 -070075 variant = firstNonNull(config.getString("logFormat", pretty, "variant"), pretty);
Dave Borowitzb772cce2012-12-28 13:57:22 -080076 }
77
David Pursehouse28726042019-06-27 09:09:30 +090078 private void renderHtml(SoySauce.Renderer renderer, LoggingAdvisingAppendable out)
79 throws IOException {
80 if (!renderer.renderHtml(out).result().isDone()) {
81 throw new IOException("failed to render HTML");
82 }
83 }
84
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020085 public void renderStreaming(
86 Paginator paginator,
87 @Nullable String revision,
88 Renderer renderer,
David Pursehouse28726042019-06-27 09:09:30 +090089 Writer writer,
Dave Borowitz571b7a02018-02-09 15:18:10 -050090 DateFormatter df,
91 FooterBehavior footerBehavior)
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020092 throws IOException {
David Pursehouse28726042019-06-27 09:09:30 +090093 LoggingAdvisingAppendable out = LoggingAdvisingAppendable.delegating(writer);
94 renderHtml(
95 renderer
96 .newRenderer("gitiles.logEntriesHeader")
97 .setData(toHeaderSoyData(paginator, revision)),
98 out);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070099
David Pursehouse28726042019-06-27 09:09:30 +0900100 SoySauce.Renderer entryRenderer = renderer.newRenderer("gitiles.logEntryWrapper");
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400101 boolean renderedEntries = false;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700102 for (RevCommit c : paginator) {
David Pursehouse28726042019-06-27 09:09:30 +0900103 renderHtml(entryRenderer.setData(toEntrySoyData(paginator, c, df)), out);
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400104 renderedEntries = true;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700105 }
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400106 if (!renderedEntries) {
David Pursehouse28726042019-06-27 09:09:30 +0900107 renderHtml(renderer.newRenderer("gitiles.emptyLog"), out);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700108 }
109
David Pursehouse28726042019-06-27 09:09:30 +0900110 renderHtml(
111 renderer
112 .newRenderer("gitiles.logEntriesFooter")
113 .setData(toFooterSoyData(paginator, revision, footerBehavior)),
114 out);
Dave Borowitz27fada42013-11-01 11:09:49 -0700115 }
116
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700117 private Map<String, Object> toHeaderSoyData(Paginator paginator, @Nullable String revision) {
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400118 Map<String, Object> data = Maps.newHashMapWithExpectedSize(1);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800119 ObjectId prev = paginator.getPreviousStart();
120 if (prev != null) {
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700121 GitilesView.Builder prevView = copyAndCanonicalizeView(revision);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800122 if (!prevView.getRevision().getId().equals(prev)) {
123 prevView.replaceParam(LogServlet.START_PARAM, prev.name());
124 }
125 data.put("previousUrl", prevView.toUrl());
126 }
127 return data;
128 }
129
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400130 private Map<String, Object> toEntrySoyData(Paginator paginator, RevCommit c, DateFormatter df)
131 throws IOException {
Gustaf Lundh06a98702015-08-17 17:29:11 +0200132 if (csd == null) {
133 csd = new CommitSoyData();
134 }
135
Jonathan Niederb49306a2019-03-07 14:10:57 -0800136 Map<String, Object> entry = csd.toSoyData(req, paginator.getWalk(), c, fields, df);
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400137 DiffEntry rename = paginator.getRename(c);
138 if (rename != null) {
139 entry.put("rename", toRenameSoyData(rename));
140 }
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700141 return ImmutableMap.of(
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700142 "variant", variant,
143 "entry", entry);
144 }
145
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400146 private Map<String, Object> toRenameSoyData(DiffEntry entry) {
147 if (entry == null) {
148 return null;
149 }
150 ChangeType type = entry.getChangeType();
151 if (type != ChangeType.RENAME && type != ChangeType.COPY) {
152 return null;
153 }
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200154 return ImmutableMap.<String, Object>of(
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400155 "changeType", type.toString(),
156 "oldPath", entry.getOldPath(),
157 "newPath", entry.getNewPath(),
158 "score", entry.getScore());
159 }
160
Dave Borowitz571b7a02018-02-09 15:18:10 -0500161 private Map<String, Object> toFooterSoyData(
162 Paginator paginator, @Nullable String revision, FooterBehavior behavior) {
163 switch (behavior) {
164 case NEXT:
165 ObjectId next = paginator.getNextStart();
166 if (next == null) {
167 return ImmutableMap.of();
168 }
169 return ImmutableMap.of(
170 "nextUrl",
171 copyAndCanonicalizeView(revision)
172 .replaceParam(LogServlet.START_PARAM, next.name())
173 .toUrl(),
174 "nextText",
175 "Next");
176
177 case LOG_HEAD:
178 return ImmutableMap.of(
179 "nextUrl", GitilesView.log().copyFrom(view).toUrl(), "nextText", "More");
180 default:
181 throw new IllegalStateException("unknown footer behavior: " + behavior);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700182 }
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700183 }
184
185 private GitilesView.Builder copyAndCanonicalizeView(String revision) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800186 // Canonicalize the view by using full SHAs.
187 GitilesView.Builder copy = GitilesView.log().copyFrom(view);
David Pursehousec53de2b2019-06-01 15:27:25 +0900188 if (!Revision.isNull(view.getRevision())) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800189 copy.setRevision(view.getRevision());
Dave Borowitzfa80a6a2018-02-09 15:31:49 -0500190 } else if (revision != null) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800191 copy.setRevision(Revision.named(revision));
Dave Borowitzfa80a6a2018-02-09 15:31:49 -0500192 } else {
193 copy.setRevision(Revision.NULL);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800194 }
David Pursehousec53de2b2019-06-01 15:27:25 +0900195 if (!Revision.isNull(view.getOldRevision())) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800196 copy.setOldRevision(view.getOldRevision());
197 }
198 return copy;
199 }
200}