blob: 5b1801df9071409df5fa49470468fc590b80826a [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// 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
15package com.google.gitiles;
16
17import static com.google.common.base.Preconditions.checkNotNull;
Dave Borowitz9de65952012-08-13 16:09:45 -070018
Dave Borowitzea9bba12014-07-09 16:45:40 -070019import com.google.common.io.BaseEncoding;
Dave Borowitzf03fcea2014-04-21 17:20:33 -070020import com.google.gitiles.CommitData.Field;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070021import com.google.gitiles.DateFormatter.Format;
Masaya Suzuki5cecb862019-03-25 17:35:44 -070022import com.google.gitiles.GitilesRequestFailureException.FailureReason;
Dave Borowitz3b744b12016-08-19 16:11:10 -040023import java.io.IOException;
24import java.io.OutputStream;
25import java.io.Writer;
26import java.util.Arrays;
27import java.util.Map;
28import java.util.Set;
29import javax.servlet.http.HttpServletRequest;
30import javax.servlet.http.HttpServletResponse;
Dave Borowitz9de65952012-08-13 16:09:45 -070031import org.eclipse.jgit.diff.DiffFormatter;
32import org.eclipse.jgit.errors.IncorrectObjectTypeException;
33import org.eclipse.jgit.errors.MissingObjectException;
34import org.eclipse.jgit.http.server.ServletUtils;
Dave Borowitzf03fcea2014-04-21 17:20:33 -070035import org.eclipse.jgit.lib.FileMode;
Dave Borowitz9de65952012-08-13 16:09:45 -070036import org.eclipse.jgit.lib.ObjectId;
37import org.eclipse.jgit.lib.Repository;
38import org.eclipse.jgit.revwalk.RevCommit;
39import org.eclipse.jgit.revwalk.RevWalk;
40import org.eclipse.jgit.treewalk.AbstractTreeIterator;
41import org.eclipse.jgit.treewalk.CanonicalTreeParser;
42import org.eclipse.jgit.treewalk.EmptyTreeIterator;
Dave Borowitzf03fcea2014-04-21 17:20:33 -070043import org.eclipse.jgit.treewalk.TreeWalk;
Dave Borowitz9de65952012-08-13 16:09:45 -070044import org.eclipse.jgit.treewalk.filter.PathFilter;
45
Dave Borowitz9de65952012-08-13 16:09:45 -070046/** Serves an HTML page with all the diffs for a commit. */
47public class DiffServlet extends BaseServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080048 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070049
50 private final Linkifier linkifier;
51
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070052 public DiffServlet(GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) {
Dave Borowitz8d6d6872014-03-16 15:18:14 -070053 super(renderer, accessFactory);
Dave Borowitz9de65952012-08-13 16:09:45 -070054 this.linkifier = checkNotNull(linkifier, "linkifier");
55 }
56
57 @Override
Dave Borowitzea9bba12014-07-09 16:45:40 -070058 protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -070059 GitilesView view = ViewFilter.getView(req);
60 Repository repo = ServletUtils.getRepository(req);
61
Shawn Pearceb5ad0a02015-05-24 20:33:17 -070062 try (RevWalk walk = new RevWalk(repo);
63 TreeWalk tw = newTreeWalk(walk, view)) {
David Pursehousef2f98cf2016-06-15 22:08:14 +090064 boolean showCommit;
65 boolean isFile;
Dave Borowitz9de65952012-08-13 16:09:45 -070066 AbstractTreeIterator oldTree;
67 AbstractTreeIterator newTree;
68 try {
Dave Borowitz0d418f62014-04-29 11:32:25 -070069 if (tw == null && !view.getPathPart().isEmpty()) {
Masaya Suzuki5cecb862019-03-25 17:35:44 -070070 throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND);
Dave Borowitz0d418f62014-04-29 11:32:25 -070071 }
72 isFile = tw != null && isFile(tw);
73
Dave Borowitz9de65952012-08-13 16:09:45 -070074 // 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 Suzuki5cecb862019-03-25 17:35:44 -070079 } 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 Borowitz9de65952012-08-13 16:09:45 -070083 }
84
85 Map<String, Object> data = getData(req);
86 data.put("title", "Diff - " + view.getRevisionRange());
87 if (showCommit) {
Dave Borowitzf03fcea2014-04-21 17:20:33 -070088 Set<Field> fs = CommitSoyData.DEFAULT_FIELDS;
89 if (isFile) {
90 fs = Field.setOf(fs, Field.PARENT_BLAME_URL);
91 }
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070092 GitilesAccess access = getAccess(req);
93 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020094 data.put(
95 "commit",
96 new CommitSoyData()
97 .setLinkifier(linkifier)
98 .setArchiveFormat(getArchiveFormat(access))
Jonathan Niederb49306a2019-03-07 14:10:57 -080099 .toSoyData(req, walk, walk.parseCommit(view.getRevision().getId()), fs, df));
Dave Borowitz9de65952012-08-13 16:09:45 -0700100 }
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 Bonventrecc3418b2016-12-01 20:18:37 -0800108 setCacheHeaders(req, res);
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700109 try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.diffDetail", data);
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200110 DiffFormatter diff = new HtmlDiffFormatter(renderer, view, out)) {
Dave Borowitzea9bba12014-07-09 16:45:40 -0700111 formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
Dave Borowitz9de65952012-08-13 16:09:45 -0700112 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700113 }
114 }
115
Dave Borowitzea9bba12014-07-09 16:45:40 -0700116 @Override
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200117 protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
Dave Borowitzea9bba12014-07-09 16:45:40 -0700118 GitilesView view = ViewFilter.getView(req);
119 Repository repo = ServletUtils.getRepository(req);
120
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700121 try (RevWalk walk = new RevWalk(repo)) {
Dave Borowitzea9bba12014-07-09 16:45:40 -0700122 AbstractTreeIterator oldTree;
123 AbstractTreeIterator newTree;
124 try {
125 oldTree = getTreeIterator(walk, view.getOldRevision().getId());
126 newTree = getTreeIterator(walk, view.getRevision().getId());
Masaya Suzuki5cecb862019-03-25 17:35:44 -0700127 } 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 Borowitzea9bba12014-07-09 16:45:40 -0700131 }
132
Dave Borowitz6c6cac12014-09-17 16:10:28 -0700133 try (Writer writer = startRenderText(req, res);
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700134 OutputStream out = BaseEncoding.base64().encodingStream(writer);
135 DiffFormatter diff = new DiffFormatter(out)) {
136 formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
Dave Borowitzea9bba12014-07-09 16:45:40 -0700137 }
Dave Borowitzea9bba12014-07-09 16:45:40 -0700138 }
139 }
140
Dave Borowitz0d418f62014-04-29 11:32:25 -0700141 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 Nienhuysc0200f62016-05-02 17:34:51 +0200146 walk.getObjectReader(), view.getPathPart(), walk.parseTree(view.getRevision().getId()));
Dave Borowitz0d418f62014-04-29 11:32:25 -0700147 }
148
Dave Borowitz9de65952012-08-13 16:09:45 -0700149 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 Borowitz9de65952012-08-13 16:09:45 -0700154 }
David Pursehouseb3b630f2016-06-15 21:51:18 +0900155 return oldRevision == Revision.NULL;
Dave Borowitz9de65952012-08-13 16:09:45 -0700156 }
157
Dave Borowitz0d418f62014-04-29 11:32:25 -0700158 private static boolean isFile(TreeWalk tw) {
159 return (tw.getRawMode(0) & FileMode.TYPE_FILE) > 0;
Dave Borowitzf03fcea2014-04-21 17:20:33 -0700160 }
161
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200162 private static void formatDiff(
163 Repository repo,
164 AbstractTreeIterator oldTree,
165 AbstractTreeIterator newTree,
166 String path,
167 DiffFormatter diff)
168 throws IOException {
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700169 if (!path.isEmpty()) {
170 diff.setPathFilter(PathFilter.create(path));
Dave Borowitz9de65952012-08-13 16:09:45 -0700171 }
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700172 diff.setRepository(repo);
173 diff.setDetectRenames(true);
174 diff.format(oldTree, newTree);
Dave Borowitz9de65952012-08-13 16:09:45 -0700175 }
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 Borowitz9de65952012-08-13 16:09:45 -0700183 }
David Pursehouseb3b630f2016-06-15 21:51:18 +0900184 return new EmptyTreeIterator();
Dave Borowitz9de65952012-08-13 16:09:45 -0700185 }
186}