blob: 34b290f3aa88eda2d027943799697ac312fd726a [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,
Jonathan Niederb911eb32023-08-16 00:42:59 +000050 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
Sven Selbergd99004c2022-01-31 10:24:08 +010096 .newRenderer("com.google.gitiles.templates.LogDetail.logEntriesHeader")
David Pursehouse28726042019-06-27 09:09:30 +090097 .setData(toHeaderSoyData(paginator, revision)),
98 out);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070099
Sven Selbergd99004c2022-01-31 10:24:08 +0100100 SoySauce.Renderer entryRenderer =
101 renderer.newRenderer("com.google.gitiles.templates.LogDetail.logEntryWrapper");
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400102 boolean renderedEntries = false;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700103 for (RevCommit c : paginator) {
David Pursehouse28726042019-06-27 09:09:30 +0900104 renderHtml(entryRenderer.setData(toEntrySoyData(paginator, c, df)), out);
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400105 renderedEntries = true;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700106 }
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400107 if (!renderedEntries) {
Sven Selbergd99004c2022-01-31 10:24:08 +0100108 renderHtml(renderer.newRenderer("com.google.gitiles.templates.LogDetail.emptyLog"), out);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700109 }
110
David Pursehouse28726042019-06-27 09:09:30 +0900111 renderHtml(
112 renderer
Sven Selbergd99004c2022-01-31 10:24:08 +0100113 .newRenderer("com.google.gitiles.templates.LogDetail.logEntriesFooter")
David Pursehouse28726042019-06-27 09:09:30 +0900114 .setData(toFooterSoyData(paginator, revision, footerBehavior)),
115 out);
Dave Borowitz27fada42013-11-01 11:09:49 -0700116 }
117
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700118 private Map<String, Object> toHeaderSoyData(Paginator paginator, @Nullable String revision) {
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400119 Map<String, Object> data = Maps.newHashMapWithExpectedSize(1);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800120 ObjectId prev = paginator.getPreviousStart();
121 if (prev != null) {
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700122 GitilesView.Builder prevView = copyAndCanonicalizeView(revision);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800123 if (!prevView.getRevision().getId().equals(prev)) {
124 prevView.replaceParam(LogServlet.START_PARAM, prev.name());
125 }
126 data.put("previousUrl", prevView.toUrl());
127 }
128 return data;
129 }
130
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400131 private Map<String, Object> toEntrySoyData(Paginator paginator, RevCommit c, DateFormatter df)
132 throws IOException {
Gustaf Lundh06a98702015-08-17 17:29:11 +0200133 if (csd == null) {
134 csd = new CommitSoyData();
135 }
136
Jonathan Niederb49306a2019-03-07 14:10:57 -0800137 Map<String, Object> entry = csd.toSoyData(req, paginator.getWalk(), c, fields, df);
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400138 DiffEntry rename = paginator.getRename(c);
139 if (rename != null) {
140 entry.put("rename", toRenameSoyData(rename));
141 }
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700142 return ImmutableMap.of(
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700143 "variant", variant,
144 "entry", entry);
145 }
146
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400147 private Map<String, Object> toRenameSoyData(DiffEntry entry) {
148 if (entry == null) {
149 return null;
150 }
151 ChangeType type = entry.getChangeType();
152 if (type != ChangeType.RENAME && type != ChangeType.COPY) {
153 return null;
154 }
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200155 return ImmutableMap.<String, Object>of(
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400156 "changeType", type.toString(),
157 "oldPath", entry.getOldPath(),
158 "newPath", entry.getNewPath(),
159 "score", entry.getScore());
160 }
161
Dave Borowitz571b7a02018-02-09 15:18:10 -0500162 private Map<String, Object> toFooterSoyData(
163 Paginator paginator, @Nullable String revision, FooterBehavior behavior) {
164 switch (behavior) {
165 case NEXT:
166 ObjectId next = paginator.getNextStart();
167 if (next == null) {
168 return ImmutableMap.of();
169 }
170 return ImmutableMap.of(
171 "nextUrl",
172 copyAndCanonicalizeView(revision)
173 .replaceParam(LogServlet.START_PARAM, next.name())
174 .toUrl(),
175 "nextText",
176 "Next");
177
178 case LOG_HEAD:
179 return ImmutableMap.of(
180 "nextUrl", GitilesView.log().copyFrom(view).toUrl(), "nextText", "More");
181 default:
182 throw new IllegalStateException("unknown footer behavior: " + behavior);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700183 }
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700184 }
185
186 private GitilesView.Builder copyAndCanonicalizeView(String revision) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800187 // Canonicalize the view by using full SHAs.
188 GitilesView.Builder copy = GitilesView.log().copyFrom(view);
David Pursehousec53de2b2019-06-01 15:27:25 +0900189 if (!Revision.isNull(view.getRevision())) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800190 copy.setRevision(view.getRevision());
Dave Borowitzfa80a6a2018-02-09 15:31:49 -0500191 } else if (revision != null) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800192 copy.setRevision(Revision.named(revision));
Dave Borowitzfa80a6a2018-02-09 15:31:49 -0500193 } else {
194 copy.setRevision(Revision.NULL);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800195 }
David Pursehousec53de2b2019-06-01 15:27:25 +0900196 if (!Revision.isNull(view.getOldRevision())) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800197 copy.setOldRevision(view.getOldRevision());
198 }
199 return copy;
200 }
201}