| 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; |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 25 | import com.google.template.soy.data.LoggingAdvisingAppendable; |
| 26 | import com.google.template.soy.jbcsrc.api.SoySauce; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 27 | import java.io.IOException; |
| 28 | import java.io.Writer; |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | import javax.annotation.Nullable; |
| 32 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 33 | import org.eclipse.jgit.diff.DiffEntry; |
| 34 | import org.eclipse.jgit.diff.DiffEntry.ChangeType; |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 35 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 36 | import org.eclipse.jgit.lib.ObjectId; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 37 | import org.eclipse.jgit.revwalk.RevCommit; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 38 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 39 | public class LogSoyData { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 40 | private static final ImmutableSet<Field> FIELDS = |
| 41 | Sets.immutableEnumSet( |
| 42 | Field.ABBREV_SHA, |
| 43 | Field.SHA, |
| 44 | Field.URL, |
| 45 | Field.SHORT_MESSAGE, |
| 46 | Field.MESSAGE, |
| 47 | Field.AUTHOR, |
| 48 | Field.COMMITTER, |
| 49 | Field.BRANCHES, |
| Jonathan Nieder | b911eb3 | 2023-08-16 00:42:59 +0000 | [diff] [blame] | 50 | Field.TAGS); |
| Michael Moss | 558f864 | 2014-04-15 09:29:21 -0700 | [diff] [blame] | 51 | 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] | 52 | |
| Dave Borowitz | 571b7a0 | 2018-02-09 15:18:10 -0500 | [diff] [blame] | 53 | /** Behavior for the footer link when rendering streaming log data. */ |
| 54 | public enum FooterBehavior { |
| 55 | /** "Next" link that skips commits in the current view. */ |
| 56 | NEXT, |
| 57 | |
| 58 | /** "More" link that starts from HEAD. */ |
| 59 | LOG_HEAD; |
| 60 | } |
| 61 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 62 | private final HttpServletRequest req; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 63 | private final GitilesView view; |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 64 | private final Set<Field> fields; |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 65 | private final String variant; |
| Gustaf Lundh | 06a9870 | 2015-08-17 17:29:11 +0200 | [diff] [blame] | 66 | private CommitSoyData csd; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 67 | |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 68 | public LogSoyData(HttpServletRequest req, GitilesAccess access, String pretty) |
| 69 | throws IOException { |
| 70 | this.req = checkNotNull(req); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 71 | this.view = checkNotNull(ViewFilter.getView(req)); |
| Dave Borowitz | b15a586 | 2015-09-16 15:14:56 -0400 | [diff] [blame] | 72 | checkNotNull(pretty); |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 73 | Config config = access.getConfig(); |
| 74 | fields = config.getBoolean("logFormat", pretty, "verbose", false) ? VERBOSE_FIELDS : FIELDS; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 75 | variant = firstNonNull(config.getString("logFormat", pretty, "variant"), pretty); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 78 | private void renderHtml(SoySauce.Renderer renderer, LoggingAdvisingAppendable out) |
| 79 | throws IOException { |
| 80 | if (!renderer.renderHtml(out).result().isDone()) { |
| 81 | throw new IOException("failed to render HTML"); |
| 82 | } |
| 83 | } |
| 84 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 85 | public void renderStreaming( |
| 86 | Paginator paginator, |
| 87 | @Nullable String revision, |
| 88 | Renderer renderer, |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 89 | Writer writer, |
| Dave Borowitz | 571b7a0 | 2018-02-09 15:18:10 -0500 | [diff] [blame] | 90 | DateFormatter df, |
| 91 | FooterBehavior footerBehavior) |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 92 | throws IOException { |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 93 | LoggingAdvisingAppendable out = LoggingAdvisingAppendable.delegating(writer); |
| 94 | renderHtml( |
| 95 | renderer |
| Sven Selberg | d99004c | 2022-01-31 10:24:08 +0100 | [diff] [blame] | 96 | .newRenderer("com.google.gitiles.templates.LogDetail.logEntriesHeader") |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 97 | .setData(toHeaderSoyData(paginator, revision)), |
| 98 | out); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 99 | |
| Sven Selberg | d99004c | 2022-01-31 10:24:08 +0100 | [diff] [blame] | 100 | SoySauce.Renderer entryRenderer = |
| 101 | renderer.newRenderer("com.google.gitiles.templates.LogDetail.logEntryWrapper"); |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 102 | boolean renderedEntries = false; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 103 | for (RevCommit c : paginator) { |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 104 | renderHtml(entryRenderer.setData(toEntrySoyData(paginator, c, df)), out); |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 105 | renderedEntries = true; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 106 | } |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 107 | if (!renderedEntries) { |
| Sven Selberg | d99004c | 2022-01-31 10:24:08 +0100 | [diff] [blame] | 108 | renderHtml(renderer.newRenderer("com.google.gitiles.templates.LogDetail.emptyLog"), out); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 111 | renderHtml( |
| 112 | renderer |
| Sven Selberg | d99004c | 2022-01-31 10:24:08 +0100 | [diff] [blame] | 113 | .newRenderer("com.google.gitiles.templates.LogDetail.logEntriesFooter") |
| David Pursehouse | 2872604 | 2019-06-27 09:09:30 +0900 | [diff] [blame] | 114 | .setData(toFooterSoyData(paginator, revision, footerBehavior)), |
| 115 | out); |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 118 | private Map<String, Object> toHeaderSoyData(Paginator paginator, @Nullable String revision) { |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 119 | Map<String, Object> data = Maps.newHashMapWithExpectedSize(1); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 120 | ObjectId prev = paginator.getPreviousStart(); |
| 121 | if (prev != null) { |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 122 | GitilesView.Builder prevView = copyAndCanonicalizeView(revision); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 123 | if (!prevView.getRevision().getId().equals(prev)) { |
| 124 | prevView.replaceParam(LogServlet.START_PARAM, prev.name()); |
| 125 | } |
| 126 | data.put("previousUrl", prevView.toUrl()); |
| 127 | } |
| 128 | return data; |
| 129 | } |
| 130 | |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 131 | private Map<String, Object> toEntrySoyData(Paginator paginator, RevCommit c, DateFormatter df) |
| 132 | throws IOException { |
| Gustaf Lundh | 06a9870 | 2015-08-17 17:29:11 +0200 | [diff] [blame] | 133 | if (csd == null) { |
| 134 | csd = new CommitSoyData(); |
| 135 | } |
| 136 | |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 137 | Map<String, Object> entry = csd.toSoyData(req, paginator.getWalk(), c, fields, df); |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 138 | DiffEntry rename = paginator.getRename(c); |
| 139 | if (rename != null) { |
| 140 | entry.put("rename", toRenameSoyData(rename)); |
| 141 | } |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 142 | return ImmutableMap.of( |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 143 | "variant", variant, |
| 144 | "entry", entry); |
| 145 | } |
| 146 | |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 147 | private Map<String, Object> toRenameSoyData(DiffEntry entry) { |
| 148 | if (entry == null) { |
| 149 | return null; |
| 150 | } |
| 151 | ChangeType type = entry.getChangeType(); |
| 152 | if (type != ChangeType.RENAME && type != ChangeType.COPY) { |
| 153 | return null; |
| 154 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 155 | return ImmutableMap.<String, Object>of( |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 156 | "changeType", type.toString(), |
| 157 | "oldPath", entry.getOldPath(), |
| 158 | "newPath", entry.getNewPath(), |
| 159 | "score", entry.getScore()); |
| 160 | } |
| 161 | |
| Dave Borowitz | 571b7a0 | 2018-02-09 15:18:10 -0500 | [diff] [blame] | 162 | private Map<String, Object> toFooterSoyData( |
| 163 | Paginator paginator, @Nullable String revision, FooterBehavior behavior) { |
| 164 | switch (behavior) { |
| 165 | case NEXT: |
| 166 | ObjectId next = paginator.getNextStart(); |
| 167 | if (next == null) { |
| 168 | return ImmutableMap.of(); |
| 169 | } |
| 170 | return ImmutableMap.of( |
| 171 | "nextUrl", |
| 172 | copyAndCanonicalizeView(revision) |
| 173 | .replaceParam(LogServlet.START_PARAM, next.name()) |
| 174 | .toUrl(), |
| 175 | "nextText", |
| 176 | "Next"); |
| 177 | |
| 178 | case LOG_HEAD: |
| 179 | return ImmutableMap.of( |
| 180 | "nextUrl", GitilesView.log().copyFrom(view).toUrl(), "nextText", "More"); |
| 181 | default: |
| 182 | throw new IllegalStateException("unknown footer behavior: " + behavior); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 183 | } |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | private GitilesView.Builder copyAndCanonicalizeView(String revision) { |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 187 | // Canonicalize the view by using full SHAs. |
| 188 | GitilesView.Builder copy = GitilesView.log().copyFrom(view); |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 189 | if (!Revision.isNull(view.getRevision())) { |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 190 | copy.setRevision(view.getRevision()); |
| Dave Borowitz | fa80a6a | 2018-02-09 15:31:49 -0500 | [diff] [blame] | 191 | } else if (revision != null) { |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 192 | copy.setRevision(Revision.named(revision)); |
| Dave Borowitz | fa80a6a | 2018-02-09 15:31:49 -0500 | [diff] [blame] | 193 | } else { |
| 194 | copy.setRevision(Revision.NULL); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 195 | } |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 196 | if (!Revision.isNull(view.getOldRevision())) { |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 197 | copy.setOldRevision(view.getOldRevision()); |
| 198 | } |
| 199 | return copy; |
| 200 | } |
| 201 | } |