| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 17 | import static com.google.common.base.MoreObjects.firstNonNull; |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
| 24 | import com.google.gitiles.CommitData.Field; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 25 | import com.google.template.soy.tofu.SoyTofu; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 26 | |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 27 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 28 | import org.eclipse.jgit.lib.ObjectId; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 29 | import org.eclipse.jgit.revwalk.RevCommit; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 30 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 31 | import java.io.IOException; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 32 | import java.io.Writer; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 33 | import java.util.Map; |
| Michael Moss | 558f864 | 2014-04-15 09:29:21 -0700 | [diff] [blame] | 34 | import java.util.Set; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 35 | |
| 36 | import javax.annotation.Nullable; |
| 37 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 38 | |
| 39 | public class LogSoyData { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 40 | private static final ImmutableSet<Field> FIELDS = Sets.immutableEnumSet(Field.ABBREV_SHA, |
| Michael Moss | 558f864 | 2014-04-15 09:29:21 -0700 | [diff] [blame] | 41 | 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 Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 44 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 45 | private final HttpServletRequest req; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 46 | private final GitilesView view; |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 47 | private final Set<Field> fields; |
| 48 | private final String pretty; |
| 49 | private final String variant; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 50 | |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 51 | public LogSoyData(HttpServletRequest req, GitilesAccess access, String pretty) |
| 52 | throws IOException { |
| 53 | this.req = checkNotNull(req); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 54 | this.view = checkNotNull(ViewFilter.getView(req)); |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 55 | this.pretty = checkNotNull(pretty); |
| 56 | Config config = access.getConfig(); |
| 57 | fields = config.getBoolean("logFormat", pretty, "verbose", false) ? VERBOSE_FIELDS : FIELDS; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 58 | variant = firstNonNull(config.getString("logFormat", pretty, "variant"), pretty); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 61 | 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 Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 84 | private Map<String, Object> toHeaderSoyData(Paginator paginator, @Nullable String revision) { |
| 85 | Map<String, Object> data = Maps.newHashMapWithExpectedSize(5); |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 86 | data.put("logEntryPretty", pretty); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 87 | ObjectId prev = paginator.getPreviousStart(); |
| 88 | if (prev != null) { |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 89 | GitilesView.Builder prevView = copyAndCanonicalizeView(revision); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 90 | 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 Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 98 | 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 Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 120 | // 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 | } |