blob: 244bbc8fbc76ae7a6f7614ba68bc04a17c50556a [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;
48 private final String pretty;
49 private final String variant;
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 Borowitzba1bd222014-07-30 09:50:29 -070055 this.pretty = checkNotNull(pretty);
56 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");
69 boolean first = true;
70 for (RevCommit c : paginator) {
71 entryRenderer.setData(toEntrySoyData(paginator, c, df, first)).render(out);
72 out.flush();
73 first = false;
74 }
75 if (first) {
76 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) {
85 Map<String, Object> data = Maps.newHashMapWithExpectedSize(5);
Dave Borowitzba1bd222014-07-30 09:50:29 -070086 data.put("logEntryPretty", pretty);
Dave Borowitzb772cce2012-12-28 13:57:22 -080087 ObjectId prev = paginator.getPreviousStart();
88 if (prev != null) {
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070089 GitilesView.Builder prevView = copyAndCanonicalizeView(revision);
Dave Borowitzb772cce2012-12-28 13:57:22 -080090 if (!prevView.getRevision().getId().equals(prev)) {
91 prevView.replaceParam(LogServlet.START_PARAM, prev.name());
92 }
93 data.put("previousUrl", prevView.toUrl());
94 }
95 return data;
96 }
97
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070098 private Map<String, Object> toEntrySoyData(Paginator paginator, RevCommit c, DateFormatter df,
99 boolean first) throws IOException {
100 Map<String, Object> entry = new CommitSoyData().setRevWalk(paginator.getWalk())
101 .toSoyData(req, c, fields, df);
102 return ImmutableMap.of(
103 "firstWithPrevious", first && paginator.getPreviousStart() != null,
104 "variant", variant,
105 "entry", entry);
106 }
107
108 private Map<String, Object> toFooterSoyData(Paginator paginator, @Nullable String revision) {
109 Map<String, Object> data = Maps.newHashMapWithExpectedSize(1);
110 ObjectId next = paginator.getNextStart();
111 if (next != null) {
112 data.put("nextUrl", copyAndCanonicalizeView(revision)
113 .replaceParam(LogServlet.START_PARAM, next.name())
114 .toUrl());
115 }
116 return data;
117 }
118
119 private GitilesView.Builder copyAndCanonicalizeView(String revision) {
Dave Borowitzb772cce2012-12-28 13:57:22 -0800120 // Canonicalize the view by using full SHAs.
121 GitilesView.Builder copy = GitilesView.log().copyFrom(view);
122 if (view.getRevision() != Revision.NULL) {
123 copy.setRevision(view.getRevision());
124 } else {
125 copy.setRevision(Revision.named(revision));
126 }
127 if (view.getOldRevision() != Revision.NULL) {
128 copy.setOldRevision(view.getOldRevision());
129 }
130 return copy;
131 }
132}