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