| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [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 | |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 19 | import com.google.common.io.BaseEncoding; |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 20 | import com.google.gitiles.CommitData.Field; |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 21 | import com.google.gitiles.DateFormatter.Format; |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 22 | import com.google.gitiles.GitilesRequestFailureException.FailureReason; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 23 | import java.io.IOException; |
| 24 | import java.io.OutputStream; |
| 25 | import java.io.Writer; |
| 26 | import java.util.Arrays; |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | import javax.servlet.http.HttpServletRequest; |
| 30 | import javax.servlet.http.HttpServletResponse; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 31 | import org.eclipse.jgit.diff.DiffFormatter; |
| 32 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 33 | import org.eclipse.jgit.errors.MissingObjectException; |
| 34 | import org.eclipse.jgit.http.server.ServletUtils; |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 35 | import org.eclipse.jgit.lib.FileMode; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 36 | import org.eclipse.jgit.lib.ObjectId; |
| 37 | import org.eclipse.jgit.lib.Repository; |
| 38 | import org.eclipse.jgit.revwalk.RevCommit; |
| 39 | import org.eclipse.jgit.revwalk.RevWalk; |
| 40 | import org.eclipse.jgit.treewalk.AbstractTreeIterator; |
| 41 | import org.eclipse.jgit.treewalk.CanonicalTreeParser; |
| 42 | import org.eclipse.jgit.treewalk.EmptyTreeIterator; |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 43 | import org.eclipse.jgit.treewalk.TreeWalk; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 44 | import org.eclipse.jgit.treewalk.filter.PathFilter; |
| 45 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | /** Serves an HTML page with all the diffs for a commit. */ |
| 47 | public class DiffServlet extends BaseServlet { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 48 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 49 | |
| 50 | private final Linkifier linkifier; |
| 51 | |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 52 | public DiffServlet(GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) { |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 53 | super(renderer, accessFactory); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 54 | this.linkifier = checkNotNull(linkifier, "linkifier"); |
| 55 | } |
| 56 | |
| 57 | @Override |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 58 | protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 59 | GitilesView view = ViewFilter.getView(req); |
| 60 | Repository repo = ServletUtils.getRepository(req); |
| 61 | |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 62 | try (RevWalk walk = new RevWalk(repo); |
| 63 | TreeWalk tw = newTreeWalk(walk, view)) { |
| David Pursehouse | f2f98cf | 2016-06-15 22:08:14 +0900 | [diff] [blame] | 64 | boolean showCommit; |
| 65 | boolean isFile; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 66 | AbstractTreeIterator oldTree; |
| 67 | AbstractTreeIterator newTree; |
| 68 | try { |
| Dave Borowitz | 0d418f6 | 2014-04-29 11:32:25 -0700 | [diff] [blame] | 69 | if (tw == null && !view.getPathPart().isEmpty()) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 70 | throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND); |
| Dave Borowitz | 0d418f6 | 2014-04-29 11:32:25 -0700 | [diff] [blame] | 71 | } |
| 72 | isFile = tw != null && isFile(tw); |
| 73 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 74 | // If we are viewing the diff between a commit and one of its parents, |
| 75 | // include the commit detail in the rendered page. |
| 76 | showCommit = isParentOf(walk, view.getOldRevision(), view.getRevision()); |
| 77 | oldTree = getTreeIterator(walk, view.getOldRevision().getId()); |
| 78 | newTree = getTreeIterator(walk, view.getRevision().getId()); |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 79 | } catch (MissingObjectException e) { |
| 80 | throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND, e); |
| 81 | } catch (IncorrectObjectTypeException e) { |
| 82 | throw new GitilesRequestFailureException(FailureReason.INCORRECT_OBJECT_TYPE, e); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | Map<String, Object> data = getData(req); |
| 86 | data.put("title", "Diff - " + view.getRevisionRange()); |
| 87 | if (showCommit) { |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 88 | Set<Field> fs = CommitSoyData.DEFAULT_FIELDS; |
| 89 | if (isFile) { |
| 90 | fs = Field.setOf(fs, Field.PARENT_BLAME_URL); |
| 91 | } |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 92 | GitilesAccess access = getAccess(req); |
| 93 | DateFormatter df = new DateFormatter(access, Format.DEFAULT); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 94 | data.put( |
| 95 | "commit", |
| 96 | new CommitSoyData() |
| 97 | .setLinkifier(linkifier) |
| 98 | .setArchiveFormat(getArchiveFormat(access)) |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 99 | .toSoyData(req, walk, walk.parseCommit(view.getRevision().getId()), fs, df)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 100 | } |
| 101 | if (!data.containsKey("repositoryName") && (view.getRepositoryName() != null)) { |
| 102 | data.put("repositoryName", view.getRepositoryName()); |
| 103 | } |
| 104 | if (!data.containsKey("breadcrumbs")) { |
| 105 | data.put("breadcrumbs", view.getBreadcrumbs()); |
| 106 | } |
| 107 | |
| Andrew Bonventre | cc3418b | 2016-12-01 20:18:37 -0800 | [diff] [blame] | 108 | setCacheHeaders(req, res); |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 109 | try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.diffDetail", data); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 110 | DiffFormatter diff = new HtmlDiffFormatter(renderer, view, out)) { |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 111 | formatDiff(repo, oldTree, newTree, view.getPathPart(), diff); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 112 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 116 | @Override |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 117 | protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 118 | GitilesView view = ViewFilter.getView(req); |
| 119 | Repository repo = ServletUtils.getRepository(req); |
| 120 | |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 121 | try (RevWalk walk = new RevWalk(repo)) { |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 122 | AbstractTreeIterator oldTree; |
| 123 | AbstractTreeIterator newTree; |
| 124 | try { |
| 125 | oldTree = getTreeIterator(walk, view.getOldRevision().getId()); |
| 126 | newTree = getTreeIterator(walk, view.getRevision().getId()); |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 127 | } catch (MissingObjectException e) { |
| 128 | throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND, e); |
| 129 | } catch (IncorrectObjectTypeException e) { |
| 130 | throw new GitilesRequestFailureException(FailureReason.INCORRECT_OBJECT_TYPE, e); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| Dave Borowitz | 6c6cac1 | 2014-09-17 16:10:28 -0700 | [diff] [blame] | 133 | try (Writer writer = startRenderText(req, res); |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 134 | OutputStream out = BaseEncoding.base64().encodingStream(writer); |
| 135 | DiffFormatter diff = new DiffFormatter(out)) { |
| 136 | formatDiff(repo, oldTree, newTree, view.getPathPart(), diff); |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 137 | } |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| Dave Borowitz | 0d418f6 | 2014-04-29 11:32:25 -0700 | [diff] [blame] | 141 | private static TreeWalk newTreeWalk(RevWalk walk, GitilesView view) throws IOException { |
| 142 | if (view.getPathPart().isEmpty()) { |
| 143 | return null; |
| 144 | } |
| 145 | return TreeWalk.forPath( |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 146 | walk.getObjectReader(), view.getPathPart(), walk.parseTree(view.getRevision().getId())); |
| Dave Borowitz | 0d418f6 | 2014-04-29 11:32:25 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 149 | private static boolean isParentOf(RevWalk walk, Revision oldRevision, Revision newRevision) |
| 150 | throws MissingObjectException, IncorrectObjectTypeException, IOException { |
| 151 | RevCommit newCommit = walk.parseCommit(newRevision.getId()); |
| 152 | if (newCommit.getParentCount() > 0) { |
| 153 | return Arrays.asList(newCommit.getParents()).contains(oldRevision.getId()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 154 | } |
| David Pursehouse | b3b630f | 2016-06-15 21:51:18 +0900 | [diff] [blame] | 155 | return oldRevision == Revision.NULL; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| Dave Borowitz | 0d418f6 | 2014-04-29 11:32:25 -0700 | [diff] [blame] | 158 | private static boolean isFile(TreeWalk tw) { |
| 159 | return (tw.getRawMode(0) & FileMode.TYPE_FILE) > 0; |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 162 | private static void formatDiff( |
| 163 | Repository repo, |
| 164 | AbstractTreeIterator oldTree, |
| 165 | AbstractTreeIterator newTree, |
| 166 | String path, |
| 167 | DiffFormatter diff) |
| 168 | throws IOException { |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 169 | if (!path.isEmpty()) { |
| 170 | diff.setPathFilter(PathFilter.create(path)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 171 | } |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 172 | diff.setRepository(repo); |
| 173 | diff.setDetectRenames(true); |
| 174 | diff.format(oldTree, newTree); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private static AbstractTreeIterator getTreeIterator(RevWalk walk, ObjectId id) |
| 178 | throws IOException { |
| 179 | if (!id.equals(ObjectId.zeroId())) { |
| 180 | CanonicalTreeParser p = new CanonicalTreeParser(); |
| 181 | p.reset(walk.getObjectReader(), walk.parseTree(id)); |
| 182 | return p; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 183 | } |
| David Pursehouse | b3b630f | 2016-06-15 21:51:18 +0900 | [diff] [blame] | 184 | return new EmptyTreeIterator(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 185 | } |
| 186 | } |