blob: 56ddb8fdfb52092136027309e76dab015ec70444 [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 Borowitzb772cce2012-12-28 13:57:22 -080052 private final HttpServletRequest req;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070053 private final GitilesView view;
Dave Borowitzba1bd222014-07-30 09:50:29 -070054 private final Set<Field> fields;
Dave Borowitzba1bd222014-07-30 09:50:29 -070055 private final String variant;
Gustaf Lundh06a98702015-08-17 17:29:11 +020056 private CommitSoyData csd;
Dave Borowitzb772cce2012-12-28 13:57:22 -080057
Dave Borowitzba1bd222014-07-30 09:50:29 -070058 public LogSoyData(HttpServletRequest req, GitilesAccess access, String pretty)
59 throws IOException {
60 this.req = checkNotNull(req);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070061 this.view = checkNotNull(ViewFilter.getView(req));
Dave Borowitzb15a5862015-09-16 15:14:56 -040062 checkNotNull(pretty);
Dave Borowitzba1bd222014-07-30 09:50:29 -070063 Config config = access.getConfig();
64 fields = config.getBoolean("logFormat", pretty, "verbose", false) ? VERBOSE_FIELDS : FIELDS;
Dave Borowitzc410f962014-09-23 10:49:26 -070065 variant = firstNonNull(config.getString("logFormat", pretty, "variant"), pretty);
Dave Borowitzb772cce2012-12-28 13:57:22 -080066 }
67
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020068 public void renderStreaming(
69 Paginator paginator,
70 @Nullable String revision,
71 Renderer renderer,
72 Writer out,
73 DateFormatter df)
74 throws IOException {
75 renderer
76 .newRenderer("gitiles.logEntriesHeader")
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070077 .setData(toHeaderSoyData(paginator, revision))
78 .render(out);
79 out.flush();
80
81 SoyTofu.Renderer entryRenderer = renderer.newRenderer("gitiles.logEntryWrapper");
Andrew Bonventreb33426e2015-09-09 18:28:28 -040082 boolean renderedEntries = false;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070083 for (RevCommit c : paginator) {
Andrew Bonventreb33426e2015-09-09 18:28:28 -040084 entryRenderer.setData(toEntrySoyData(paginator, c, df)).render(out);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070085 out.flush();
Andrew Bonventreb33426e2015-09-09 18:28:28 -040086 renderedEntries = true;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070087 }
Andrew Bonventreb33426e2015-09-09 18:28:28 -040088 if (!renderedEntries) {
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070089 renderer.newRenderer("gitiles.emptyLog").render(out);
90 }
91
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020092 renderer
93 .newRenderer("gitiles.logEntriesFooter")
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070094 .setData(toFooterSoyData(paginator, revision))
95 .render(out);
Dave Borowitz27fada42013-11-01 11:09:49 -070096 }
97
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070098 private Map<String, Object> toHeaderSoyData(Paginator paginator, @Nullable String revision) {
Andrew Bonventreb33426e2015-09-09 18:28:28 -040099 Map<String, Object> data = Maps.newHashMapWithExpectedSize(1);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800100 ObjectId prev = paginator.getPreviousStart();
101 if (prev != null) {
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700102 GitilesView.Builder prevView = copyAndCanonicalizeView(revision);
Dave Borowitzb772cce2012-12-28 13:57:22 -0800103 if (!prevView.getRevision().getId().equals(prev)) {
104 prevView.replaceParam(LogServlet.START_PARAM, prev.name());
105 }
106 data.put("previousUrl", prevView.toUrl());
107 }
108 return data;
109 }
110
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400111 private Map<String, Object> toEntrySoyData(Paginator paginator, RevCommit c, DateFormatter df)
112 throws IOException {
Gustaf Lundh06a98702015-08-17 17:29:11 +0200113 if (csd == null) {
114 csd = new CommitSoyData();
115 }
116
Andrew Bonventreb33426e2015-09-09 18:28:28 -0400117 Map<String, Object> entry = csd.setRevWalk(paginator.getWalk()).toSoyData(req, c, fields, df);
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400118 DiffEntry rename = paginator.getRename(c);
119 if (rename != null) {
120 entry.put("rename", toRenameSoyData(rename));
121 }
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700122 return ImmutableMap.of(
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700123 "variant", variant,
124 "entry", entry);
125 }
126
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400127 private Map<String, Object> toRenameSoyData(DiffEntry entry) {
128 if (entry == null) {
129 return null;
130 }
131 ChangeType type = entry.getChangeType();
132 if (type != ChangeType.RENAME && type != ChangeType.COPY) {
133 return null;
134 }
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200135 return ImmutableMap.<String, Object>of(
Dave Borowitzcb88d4d2015-10-26 13:58:17 -0400136 "changeType", type.toString(),
137 "oldPath", entry.getOldPath(),
138 "newPath", entry.getNewPath(),
139 "score", entry.getScore());
140 }
141
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700142 private Map<String, Object> toFooterSoyData(Paginator paginator, @Nullable String revision) {
143 Map<String, Object> data = Maps.newHashMapWithExpectedSize(1);
144 ObjectId next = paginator.getNextStart();
145 if (next != null) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200146 data.put(
147 "nextUrl",
148 copyAndCanonicalizeView(revision)
149 .replaceParam(LogServlet.START_PARAM, next.name())
150 .toUrl());
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700151 }
152 return data;
153 }
154
155 private GitilesView.Builder copyAndCanonicalizeView(String revision) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800156 // Canonicalize the view by using full SHAs.
157 GitilesView.Builder copy = GitilesView.log().copyFrom(view);
158 if (view.getRevision() != Revision.NULL) {
159 copy.setRevision(view.getRevision());
160 } else {
161 copy.setRevision(Revision.named(revision));
162 }
163 if (view.getOldRevision() != Revision.NULL) {
164 copy.setOldRevision(view.getOldRevision());
165 }
166 return copy;
167 }
168}