| 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 | 571b7a0 | 2018-02-09 15:18:10 -0500 | [diff] [blame] | 52 | /** Behavior for the footer link when rendering streaming log data. */ |
| 53 | public enum FooterBehavior { |
| 54 | /** "Next" link that skips commits in the current view. */ |
| 55 | NEXT, |
| 56 | |
| 57 | /** "More" link that starts from HEAD. */ |
| 58 | LOG_HEAD; |
| 59 | } |
| 60 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 61 | private final HttpServletRequest req; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 62 | private final GitilesView view; |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 63 | private final Set<Field> fields; |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 64 | private final String variant; |
| Gustaf Lundh | 06a9870 | 2015-08-17 17:29:11 +0200 | [diff] [blame] | 65 | private CommitSoyData csd; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 66 | |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 67 | public LogSoyData(HttpServletRequest req, GitilesAccess access, String pretty) |
| 68 | throws IOException { |
| 69 | this.req = checkNotNull(req); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 70 | this.view = checkNotNull(ViewFilter.getView(req)); |
| Dave Borowitz | b15a586 | 2015-09-16 15:14:56 -0400 | [diff] [blame] | 71 | checkNotNull(pretty); |
| Dave Borowitz | ba1bd22 | 2014-07-30 09:50:29 -0700 | [diff] [blame] | 72 | Config config = access.getConfig(); |
| 73 | fields = config.getBoolean("logFormat", pretty, "verbose", false) ? VERBOSE_FIELDS : FIELDS; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 74 | variant = firstNonNull(config.getString("logFormat", pretty, "variant"), pretty); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 77 | public void renderStreaming( |
| 78 | Paginator paginator, |
| 79 | @Nullable String revision, |
| 80 | Renderer renderer, |
| 81 | Writer out, |
| Dave Borowitz | 571b7a0 | 2018-02-09 15:18:10 -0500 | [diff] [blame] | 82 | DateFormatter df, |
| 83 | FooterBehavior footerBehavior) |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 84 | throws IOException { |
| 85 | renderer |
| 86 | .newRenderer("gitiles.logEntriesHeader") |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 87 | .setData(toHeaderSoyData(paginator, revision)) |
| 88 | .render(out); |
| 89 | out.flush(); |
| 90 | |
| 91 | SoyTofu.Renderer entryRenderer = renderer.newRenderer("gitiles.logEntryWrapper"); |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 92 | boolean renderedEntries = false; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 93 | for (RevCommit c : paginator) { |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 94 | entryRenderer.setData(toEntrySoyData(paginator, c, df)).render(out); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 95 | out.flush(); |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 96 | renderedEntries = true; |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 97 | } |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 98 | if (!renderedEntries) { |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 99 | renderer.newRenderer("gitiles.emptyLog").render(out); |
| 100 | } |
| 101 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 102 | renderer |
| 103 | .newRenderer("gitiles.logEntriesFooter") |
| Dave Borowitz | 571b7a0 | 2018-02-09 15:18:10 -0500 | [diff] [blame] | 104 | .setData(toFooterSoyData(paginator, revision, footerBehavior)) |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 105 | .render(out); |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 108 | private Map<String, Object> toHeaderSoyData(Paginator paginator, @Nullable String revision) { |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 109 | Map<String, Object> data = Maps.newHashMapWithExpectedSize(1); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 110 | ObjectId prev = paginator.getPreviousStart(); |
| 111 | if (prev != null) { |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 112 | GitilesView.Builder prevView = copyAndCanonicalizeView(revision); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 113 | if (!prevView.getRevision().getId().equals(prev)) { |
| 114 | prevView.replaceParam(LogServlet.START_PARAM, prev.name()); |
| 115 | } |
| 116 | data.put("previousUrl", prevView.toUrl()); |
| 117 | } |
| 118 | return data; |
| 119 | } |
| 120 | |
| Andrew Bonventre | b33426e | 2015-09-09 18:28:28 -0400 | [diff] [blame] | 121 | private Map<String, Object> toEntrySoyData(Paginator paginator, RevCommit c, DateFormatter df) |
| 122 | throws IOException { |
| Gustaf Lundh | 06a9870 | 2015-08-17 17:29:11 +0200 | [diff] [blame] | 123 | if (csd == null) { |
| 124 | csd = new CommitSoyData(); |
| 125 | } |
| 126 | |
| Jonathan Nieder | 153d145 | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 127 | Map<String, Object> entry = csd.toSoyData(req, paginator.getWalk(), c, fields, df); |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 128 | DiffEntry rename = paginator.getRename(c); |
| 129 | if (rename != null) { |
| 130 | entry.put("rename", toRenameSoyData(rename)); |
| 131 | } |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 132 | return ImmutableMap.of( |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 133 | "variant", variant, |
| 134 | "entry", entry); |
| 135 | } |
| 136 | |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 137 | private Map<String, Object> toRenameSoyData(DiffEntry entry) { |
| 138 | if (entry == null) { |
| 139 | return null; |
| 140 | } |
| 141 | ChangeType type = entry.getChangeType(); |
| 142 | if (type != ChangeType.RENAME && type != ChangeType.COPY) { |
| 143 | return null; |
| 144 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 145 | return ImmutableMap.<String, Object>of( |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 146 | "changeType", type.toString(), |
| 147 | "oldPath", entry.getOldPath(), |
| 148 | "newPath", entry.getNewPath(), |
| 149 | "score", entry.getScore()); |
| 150 | } |
| 151 | |
| Dave Borowitz | 571b7a0 | 2018-02-09 15:18:10 -0500 | [diff] [blame] | 152 | private Map<String, Object> toFooterSoyData( |
| 153 | Paginator paginator, @Nullable String revision, FooterBehavior behavior) { |
| 154 | switch (behavior) { |
| 155 | case NEXT: |
| 156 | ObjectId next = paginator.getNextStart(); |
| 157 | if (next == null) { |
| 158 | return ImmutableMap.of(); |
| 159 | } |
| 160 | return ImmutableMap.of( |
| 161 | "nextUrl", |
| 162 | copyAndCanonicalizeView(revision) |
| 163 | .replaceParam(LogServlet.START_PARAM, next.name()) |
| 164 | .toUrl(), |
| 165 | "nextText", |
| 166 | "Next"); |
| 167 | |
| 168 | case LOG_HEAD: |
| 169 | return ImmutableMap.of( |
| 170 | "nextUrl", GitilesView.log().copyFrom(view).toUrl(), "nextText", "More"); |
| 171 | default: |
| 172 | throw new IllegalStateException("unknown footer behavior: " + behavior); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 173 | } |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private GitilesView.Builder copyAndCanonicalizeView(String revision) { |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 177 | // Canonicalize the view by using full SHAs. |
| 178 | GitilesView.Builder copy = GitilesView.log().copyFrom(view); |
| 179 | if (view.getRevision() != Revision.NULL) { |
| 180 | copy.setRevision(view.getRevision()); |
| Dave Borowitz | fa80a6a | 2018-02-09 15:31:49 -0500 | [diff] [blame] | 181 | } else if (revision != null) { |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 182 | copy.setRevision(Revision.named(revision)); |
| Dave Borowitz | fa80a6a | 2018-02-09 15:31:49 -0500 | [diff] [blame] | 183 | } else { |
| 184 | copy.setRevision(Revision.NULL); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 185 | } |
| 186 | if (view.getOldRevision() != Revision.NULL) { |
| 187 | copy.setOldRevision(view.getOldRevision()); |
| 188 | } |
| 189 | return copy; |
| 190 | } |
| 191 | } |