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