| 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)) { |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 67 | boolean showCommit, isFile; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 68 | AbstractTreeIterator oldTree; |
| 69 | AbstractTreeIterator newTree; |
| 70 | try { |
| Dave Borowitz | 0d418f6 | 2014-04-29 11:32:25 -0700 | [diff] [blame] | 71 | if (tw == null && !view.getPathPart().isEmpty()) { |
| 72 | res.setStatus(SC_NOT_FOUND); |
| 73 | return; |
| 74 | } |
| 75 | isFile = tw != null && isFile(tw); |
| 76 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 77 | // If we are viewing the diff between a commit and one of its parents, |
| 78 | // include the commit detail in the rendered page. |
| 79 | showCommit = isParentOf(walk, view.getOldRevision(), view.getRevision()); |
| 80 | oldTree = getTreeIterator(walk, view.getOldRevision().getId()); |
| 81 | newTree = getTreeIterator(walk, view.getRevision().getId()); |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 82 | } catch (MissingObjectException | IncorrectObjectTypeException e) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 83 | res.setStatus(SC_NOT_FOUND); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | Map<String, Object> data = getData(req); |
| 88 | data.put("title", "Diff - " + view.getRevisionRange()); |
| 89 | if (showCommit) { |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 90 | Set<Field> fs = CommitSoyData.DEFAULT_FIELDS; |
| 91 | if (isFile) { |
| 92 | fs = Field.setOf(fs, Field.PARENT_BLAME_URL); |
| 93 | } |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 94 | GitilesAccess access = getAccess(req); |
| 95 | DateFormatter df = new DateFormatter(access, Format.DEFAULT); |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 96 | data.put("commit", new CommitSoyData() |
| 97 | .setLinkifier(linkifier) |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 98 | .setArchiveFormat(getArchiveFormat(access)) |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 99 | .toSoyData(req, 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 | |
| Dave Borowitz | 33d4fda | 2013-10-22 16:40:20 -0700 | [diff] [blame] | 108 | setCacheHeaders(res); |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 109 | try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.diffDetail", data); |
| 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 |
| 117 | protected void doGetText(HttpServletRequest req, HttpServletResponse res) |
| 118 | throws IOException { |
| 119 | GitilesView view = ViewFilter.getView(req); |
| 120 | Repository repo = ServletUtils.getRepository(req); |
| 121 | |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 122 | try (RevWalk walk = new RevWalk(repo)) { |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 123 | AbstractTreeIterator oldTree; |
| 124 | AbstractTreeIterator newTree; |
| 125 | try { |
| 126 | oldTree = getTreeIterator(walk, view.getOldRevision().getId()); |
| 127 | newTree = getTreeIterator(walk, view.getRevision().getId()); |
| 128 | } catch (MissingObjectException | IncorrectObjectTypeException e) { |
| 129 | res.setStatus(SC_NOT_FOUND); |
| 130 | return; |
| 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( |
| 146 | walk.getObjectReader(), |
| 147 | view.getPathPart(), |
| 148 | walk.parseTree(view.getRevision().getId())); |
| 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()); |
| 156 | } else { |
| 157 | return oldRevision == Revision.NULL; |
| 158 | } |
| 159 | } |
| 160 | |
| Dave Borowitz | 0d418f6 | 2014-04-29 11:32:25 -0700 | [diff] [blame] | 161 | private static boolean isFile(TreeWalk tw) { |
| 162 | return (tw.getRawMode(0) & FileMode.TYPE_FILE) > 0; |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| Dave Borowitz | ea9bba1 | 2014-07-09 16:45:40 -0700 | [diff] [blame] | 165 | private static void formatDiff(Repository repo, AbstractTreeIterator oldTree, |
| 166 | AbstractTreeIterator newTree, String path, DiffFormatter diff) throws IOException { |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 167 | if (!path.isEmpty()) { |
| 168 | diff.setPathFilter(PathFilter.create(path)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 169 | } |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 170 | diff.setRepository(repo); |
| 171 | diff.setDetectRenames(true); |
| 172 | diff.format(oldTree, newTree); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private static AbstractTreeIterator getTreeIterator(RevWalk walk, ObjectId id) |
| 176 | throws IOException { |
| 177 | if (!id.equals(ObjectId.zeroId())) { |
| 178 | CanonicalTreeParser p = new CanonicalTreeParser(); |
| 179 | p.reset(walk.getObjectReader(), walk.parseTree(id)); |
| 180 | return p; |
| 181 | } else { |
| 182 | return new EmptyTreeIterator(); |
| 183 | } |
| 184 | } |
| 185 | } |