blob: 657edf8ced4d1ae484ab98d4222e42d42f419936 [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;
18import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
19
Dave Borowitzea9bba12014-07-09 16:45:40 -070020import com.google.common.io.BaseEncoding;
Dave Borowitzf03fcea2014-04-21 17:20:33 -070021import com.google.gitiles.CommitData.Field;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070022import com.google.gitiles.DateFormatter.Format;
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()) {
70 res.setStatus(SC_NOT_FOUND);
71 return;
72 }
73 isFile = tw != null && isFile(tw);
74
Dave Borowitz9de65952012-08-13 16:09:45 -070075 // If we are viewing the diff between a commit and one of its parents,
76 // include the commit detail in the rendered page.
77 showCommit = isParentOf(walk, view.getOldRevision(), view.getRevision());
78 oldTree = getTreeIterator(walk, view.getOldRevision().getId());
79 newTree = getTreeIterator(walk, view.getRevision().getId());
Dave Borowitzf03fcea2014-04-21 17:20:33 -070080 } catch (MissingObjectException | IncorrectObjectTypeException e) {
Dave Borowitz9de65952012-08-13 16:09:45 -070081 res.setStatus(SC_NOT_FOUND);
82 return;
83 }
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))
99 .toSoyData(req, 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());
127 } catch (MissingObjectException | IncorrectObjectTypeException e) {
128 res.setStatus(SC_NOT_FOUND);
129 return;
130 }
131
Dave Borowitz6c6cac12014-09-17 16:10:28 -0700132 try (Writer writer = startRenderText(req, res);
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700133 OutputStream out = BaseEncoding.base64().encodingStream(writer);
134 DiffFormatter diff = new DiffFormatter(out)) {
135 formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
Dave Borowitzea9bba12014-07-09 16:45:40 -0700136 }
Dave Borowitzea9bba12014-07-09 16:45:40 -0700137 }
138 }
139
Dave Borowitz0d418f62014-04-29 11:32:25 -0700140 private static TreeWalk newTreeWalk(RevWalk walk, GitilesView view) throws IOException {
141 if (view.getPathPart().isEmpty()) {
142 return null;
143 }
144 return TreeWalk.forPath(
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200145 walk.getObjectReader(), view.getPathPart(), walk.parseTree(view.getRevision().getId()));
Dave Borowitz0d418f62014-04-29 11:32:25 -0700146 }
147
Dave Borowitz9de65952012-08-13 16:09:45 -0700148 private static boolean isParentOf(RevWalk walk, Revision oldRevision, Revision newRevision)
149 throws MissingObjectException, IncorrectObjectTypeException, IOException {
150 RevCommit newCommit = walk.parseCommit(newRevision.getId());
151 if (newCommit.getParentCount() > 0) {
152 return Arrays.asList(newCommit.getParents()).contains(oldRevision.getId());
Dave Borowitz9de65952012-08-13 16:09:45 -0700153 }
David Pursehouseb3b630f2016-06-15 21:51:18 +0900154 return oldRevision == Revision.NULL;
Dave Borowitz9de65952012-08-13 16:09:45 -0700155 }
156
Dave Borowitz0d418f62014-04-29 11:32:25 -0700157 private static boolean isFile(TreeWalk tw) {
158 return (tw.getRawMode(0) & FileMode.TYPE_FILE) > 0;
Dave Borowitzf03fcea2014-04-21 17:20:33 -0700159 }
160
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200161 private static void formatDiff(
162 Repository repo,
163 AbstractTreeIterator oldTree,
164 AbstractTreeIterator newTree,
165 String path,
166 DiffFormatter diff)
167 throws IOException {
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700168 if (!path.isEmpty()) {
169 diff.setPathFilter(PathFilter.create(path));
Dave Borowitz9de65952012-08-13 16:09:45 -0700170 }
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700171 diff.setRepository(repo);
172 diff.setDetectRenames(true);
173 diff.format(oldTree, newTree);
Dave Borowitz9de65952012-08-13 16:09:45 -0700174 }
175
176 private static AbstractTreeIterator getTreeIterator(RevWalk walk, ObjectId id)
177 throws IOException {
178 if (!id.equals(ObjectId.zeroId())) {
179 CanonicalTreeParser p = new CanonicalTreeParser();
180 p.reset(walk.getObjectReader(), walk.parseTree(id));
181 return p;
Dave Borowitz9de65952012-08-13 16:09:45 -0700182 }
David Pursehouseb3b630f2016-06-15 21:51:18 +0900183 return new EmptyTreeIterator();
Dave Borowitz9de65952012-08-13 16:09:45 -0700184 }
185}