blob: 8bc243fb9f680ae41b08c33dc21f9da2a662013b [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 Borowitz3b744b12016-08-19 16:11:10 -040026import java.io.IOException;
27import java.io.Writer;
28import java.util.Map;
29import java.util.Set;
30import javax.annotation.Nullable;
31import javax.servlet.http.HttpServletRequest;
Dave Borowitzcb88d4d2015-10-26 13:58:17 -040032import org.eclipse.jgit.diff.DiffEntry;
33import org.eclipse.jgit.diff.DiffEntry.ChangeType;
Dave Borowitzba1bd222014-07-30 09:50:29 -070034import org.eclipse.jgit.lib.Config;
Dave Borowitzb772cce2012-12-28 13:57:22 -080035import org.eclipse.jgit.lib.ObjectId;
Dave Borowitzb772cce2012-12-28 13:57:22 -080036import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitzb772cce2012-12-28 13:57:22 -080037
Dave Borowitzb772cce2012-12-28 13:57:22 -080038public class LogSoyData {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020039 private static final ImmutableSet<Field> FIELDS =
40 Sets.immutableEnumSet(
41 Field.ABBREV_SHA,
42 Field.SHA,
43 Field.URL,
44 Field.SHORT_MESSAGE,
45 Field.MESSAGE,
46 Field.AUTHOR,
47 Field.COMMITTER,
48 Field.BRANCHES,
49 Field.TAGS);
Michael Moss558f8642014-04-15 09:29:21 -070050 private static final ImmutableSet<Field> VERBOSE_FIELDS = Field.setOf(FIELDS, Field.DIFF_TREE);
Dave Borowitz01551272014-03-16 13:43:16 -070051
Dave Borowitz571b7a02018-02-09 15:18:10 -050052 /** Behavior for the footer link when rendering streaming log data. */
53 public enum FooterBehavior {
54 /** "Next" link that skips commits in the current view. */
55 NEXT,
56
57 /** "More" link that starts from HEAD. */
58 LOG_HEAD;
59 }
60
Dave Borowitzb772cce2012-12-28 13:57:22 -080061 private final HttpServletRequest req;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070062 private final GitilesView view;
Dave Borowitzba1bd222014-07-30 09:50:29 -070063 private final Set<Field> fields;
Dave Borowitzba1bd222014-07-30 09:50:29 -070064 private final String variant;
Gustaf Lundh06a98702015-08-17 17:29:11 +020065 private CommitSoyData csd;
Dave Borowitzb772cce2012-12-28 13:57:22 -080066
Dave Borowitzba1bd222014-07-30 09:50:29 -070067 public LogSoyData(HttpServletRequest req, GitilesAccess access, String pretty)
68 throws IOException {
69 this.req = checkNotNull(req);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070070 this.view = checkNotNull(ViewFilter.getView(req));
Dave Borowitzb15a5862015-09-16 15:14:56 -040071 checkNotNull(pretty);
Dave Borowitzba1bd222014-07-30 09:50:29 -070072 Config config = access.getConfig();
73 fields = config.getBoolean("logFormat", pretty, "verbose", false) ? VERBOSE_FIELDS : FIELDS;
Dave Borowitzc410f962014-09-23 10:49:26 -070074 variant = firstNonNull(config.getString("logFormat", pretty, "variant"), pretty);
Dave Borowitzb772cce2012-12-28 13:57:22 -080075 }
76
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020077 public void renderStreaming(
78 Paginator paginator,
79 @Nullable String revision,
80 Renderer renderer,
81 Writer out,
Dave Borowitz571b7a02018-02-09 15:18:10 -050082 DateFormatter df,
83 FooterBehavior footerBehavior)
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020084 throws IOException {
85 renderer
86 .newRenderer("gitiles.logEntriesHeader")
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070087 .setData(toHeaderSoyData(paginator, revision))
88 .render(out);
89 out.flush();
90
91 SoyTofu.Renderer entryRenderer = renderer.newRenderer("gitiles.logEntryWrapper");
Andrew Bonventreb33426e2015-09-09 18:28:28 -040092 boolean renderedEntries = false;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070093 for (RevCommit c : paginator) {
Andrew Bonventreb33426e2015-09-09 18:28:28 -040094 entryRenderer.setData(toEntrySoyData(paginator, c, df)).render(out);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070095 out.flush();
Andrew Bonventreb33426e2015-09-09 18:28:28 -040096 renderedEntries = true;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070097 }
Andrew Bonventreb33426e2015-09-09 18:28:28 -040098 if (!renderedEntries) {
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070099 renderer.newRenderer("gitiles.emptyLog").render(out);
100 }
101
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200102 renderer
103 .newRenderer("gitiles.logEntriesFooter")
Dave Borowitz571b7a02018-02-09 15:18:10 -0500104 .setData(toFooterSoyData(paginator, revision, footerBehavior))
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700105 .render(out);
Dave Borowitz27fada42013-11-01 11:09:49 -0700106 }
107
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700108 private Map<String, Object> toHeaderSoyData(Paginator paginator, @Nullable String revision) {
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400109 Map<String, Object> data = Maps.newHashMapWithExpectedSize(1);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800110 ObjectId prev = paginator.getPreviousStart();
111 if (prev != null) {
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700112 GitilesView.Builder prevView = copyAndCanonicalizeView(revision);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800113 if (!prevView.getRevision().getId().equals(prev)) {
114 prevView.replaceParam(LogServlet.START_PARAM, prev.name());
115 }
116 data.put("previousUrl", prevView.toUrl());
117 }
118 return data;
119 }
120
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400121 private Map<String, Object> toEntrySoyData(Paginator paginator, RevCommit c, DateFormatter df)
122 throws IOException {
Gustaf Lundh06a98702015-08-17 17:29:11 +0200123 if (csd == null) {
124 csd = new CommitSoyData();
125 }
126
Jonathan Nieder153d1452019-03-07 14:10:57 -0800127 Map<String, Object> entry = csd.toSoyData(req, paginator.getWalk(), c, fields, df);
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400128 DiffEntry rename = paginator.getRename(c);
129 if (rename != null) {
130 entry.put("rename", toRenameSoyData(rename));
131 }
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700132 return ImmutableMap.of(
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700133 "variant", variant,
134 "entry", entry);
135 }
136
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400137 private Map<String, Object> toRenameSoyData(DiffEntry entry) {
138 if (entry == null) {
139 return null;
140 }
141 ChangeType type = entry.getChangeType();
142 if (type != ChangeType.RENAME && type != ChangeType.COPY) {
143 return null;
144 }
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200145 return ImmutableMap.<String, Object>of(
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400146 "changeType", type.toString(),
147 "oldPath", entry.getOldPath(),
148 "newPath", entry.getNewPath(),
149 "score", entry.getScore());
150 }
151
Dave Borowitz571b7a02018-02-09 15:18:10 -0500152 private Map<String, Object> toFooterSoyData(
153 Paginator paginator, @Nullable String revision, FooterBehavior behavior) {
154 switch (behavior) {
155 case NEXT:
156 ObjectId next = paginator.getNextStart();
157 if (next == null) {
158 return ImmutableMap.of();
159 }
160 return ImmutableMap.of(
161 "nextUrl",
162 copyAndCanonicalizeView(revision)
163 .replaceParam(LogServlet.START_PARAM, next.name())
164 .toUrl(),
165 "nextText",
166 "Next");
167
168 case LOG_HEAD:
169 return ImmutableMap.of(
170 "nextUrl", GitilesView.log().copyFrom(view).toUrl(), "nextText", "More");
171 default:
172 throw new IllegalStateException("unknown footer behavior: " + behavior);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700173 }
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700174 }
175
176 private GitilesView.Builder copyAndCanonicalizeView(String revision) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800177 // Canonicalize the view by using full SHAs.
178 GitilesView.Builder copy = GitilesView.log().copyFrom(view);
179 if (view.getRevision() != Revision.NULL) {
180 copy.setRevision(view.getRevision());
Dave Borowitzfa80a6a2018-02-09 15:31:49 -0500181 } else if (revision != null) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800182 copy.setRevision(Revision.named(revision));
Dave Borowitzfa80a6a2018-02-09 15:31:49 -0500183 } else {
184 copy.setRevision(Revision.NULL);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800185 }
186 if (view.getOldRevision() != Revision.NULL) {
187 copy.setOldRevision(view.getOldRevision());
188 }
189 return copy;
190 }
191}