| 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 | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 26 | import java.io.IOException; |
| 27 | import java.io.Writer; |
| 28 | import java.util.Map; |
| 29 | import java.util.Set; |
| 30 | import javax.annotation.Nullable; |
| 31 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 32 | import org.eclipse.jgit.diff.DiffEntry; |
| 33 | import org.eclipse.jgit.diff.DiffEntry.ChangeType; |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 34 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 35 | import org.eclipse.jgit.lib.ObjectId; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 36 | import org.eclipse.jgit.revwalk.RevCommit; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 37 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 38 | public class LogSoyData { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 39 | 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 Moss | 558f864 | 2014-04-15 09:29:21 -0700 | [diff] [blame] | 50 | 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] | 51 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 52 | private final HttpServletRequest req; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 53 | private final GitilesView view; |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 54 | private final Set<Field> fields; |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 55 | private final String variant; |
| Gustaf Lundh | 06a9870 | 2015-08-17 17:29:11 +0200 | [diff] [blame] | 56 | private CommitSoyData csd; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 57 | |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 58 | public LogSoyData(HttpServletRequest req, GitilesAccess access, String pretty) |
| 59 | throws IOException { |
| 60 | this.req = checkNotNull(req); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 61 | this.view = checkNotNull(ViewFilter.getView(req)); |
| Dave Borowitz | b15a586 | 2015-09-16 15:14:56 -0400 | [diff] [blame] | 62 | checkNotNull(pretty); |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 63 | Config config = access.getConfig(); |
| 64 | fields = config.getBoolean("logFormat", pretty, "verbose", false) ? VERBOSE_FIELDS : FIELDS; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 65 | variant = firstNonNull(config.getString("logFormat", pretty, "variant"), pretty); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 68 | 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 Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 77 | .setData(toHeaderSoyData(paginator, revision)) |
| 78 | .render(out); |
| 79 | out.flush(); |
| 80 | |
| 81 | SoyTofu.Renderer entryRenderer = renderer.newRenderer("gitiles.logEntryWrapper"); |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 82 | boolean renderedEntries = false; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 83 | for (RevCommit c : paginator) { |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 84 | entryRenderer.setData(toEntrySoyData(paginator, c, df)).render(out); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 85 | out.flush(); |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 86 | renderedEntries = true; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 87 | } |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 88 | if (!renderedEntries) { |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 89 | renderer.newRenderer("gitiles.emptyLog").render(out); |
| 90 | } |
| 91 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 92 | renderer |
| 93 | .newRenderer("gitiles.logEntriesFooter") |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 94 | .setData(toFooterSoyData(paginator, revision)) |
| 95 | .render(out); |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 98 | private Map<String, Object> toHeaderSoyData(Paginator paginator, @Nullable String revision) { |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 99 | Map<String, Object> data = Maps.newHashMapWithExpectedSize(1); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 100 | ObjectId prev = paginator.getPreviousStart(); |
| 101 | if (prev != null) { |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 102 | GitilesView.Builder prevView = copyAndCanonicalizeView(revision); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 103 | 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 Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 111 | private Map<String, Object> toEntrySoyData(Paginator paginator, RevCommit c, DateFormatter df) |
| 112 | throws IOException { |
| Gustaf Lundh | 06a9870 | 2015-08-17 17:29:11 +0200 | [diff] [blame] | 113 | if (csd == null) { |
| 114 | csd = new CommitSoyData(); |
| 115 | } |
| 116 | |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 117 | Map<String, Object> entry = csd.setRevWalk(paginator.getWalk()).toSoyData(req, c, fields, df); |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 118 | DiffEntry rename = paginator.getRename(c); |
| 119 | if (rename != null) { |
| 120 | entry.put("rename", toRenameSoyData(rename)); |
| 121 | } |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 122 | return ImmutableMap.of( |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 123 | "variant", variant, |
| 124 | "entry", entry); |
| 125 | } |
| 126 | |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 127 | 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 Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 135 | return ImmutableMap.<String, Object>of( |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 136 | "changeType", type.toString(), |
| 137 | "oldPath", entry.getOldPath(), |
| 138 | "newPath", entry.getNewPath(), |
| 139 | "score", entry.getScore()); |
| 140 | } |
| 141 | |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 142 | 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 Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 146 | data.put( |
| 147 | "nextUrl", |
| 148 | copyAndCanonicalizeView(revision) |
| 149 | .replaceParam(LogServlet.START_PARAM, next.name()) |
| 150 | .toUrl()); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 151 | } |
| 152 | return data; |
| 153 | } |
| 154 | |
| 155 | private GitilesView.Builder copyAndCanonicalizeView(String revision) { |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 156 | // 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 | } |