blob: 27b9852becd38ea2f0e952628a7d0b189adfe298 [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 Borowitz9de65952012-08-13 16:09:45 -070023
24import org.eclipse.jgit.diff.DiffFormatter;
25import org.eclipse.jgit.errors.IncorrectObjectTypeException;
26import org.eclipse.jgit.errors.MissingObjectException;
27import org.eclipse.jgit.http.server.ServletUtils;
Dave Borowitzf03fcea2014-04-21 17:20:33 -070028import org.eclipse.jgit.lib.FileMode;
Dave Borowitz9de65952012-08-13 16:09:45 -070029import org.eclipse.jgit.lib.ObjectId;
30import org.eclipse.jgit.lib.Repository;
31import org.eclipse.jgit.revwalk.RevCommit;
32import org.eclipse.jgit.revwalk.RevWalk;
33import org.eclipse.jgit.treewalk.AbstractTreeIterator;
34import org.eclipse.jgit.treewalk.CanonicalTreeParser;
35import org.eclipse.jgit.treewalk.EmptyTreeIterator;
Dave Borowitzf03fcea2014-04-21 17:20:33 -070036import org.eclipse.jgit.treewalk.TreeWalk;
Dave Borowitz9de65952012-08-13 16:09:45 -070037import org.eclipse.jgit.treewalk.filter.PathFilter;
38
39import java.io.IOException;
40import java.io.OutputStream;
Dave Borowitz6c6cac12014-09-17 16:10:28 -070041import java.io.Writer;
Dave Borowitz9de65952012-08-13 16:09:45 -070042import java.util.Arrays;
43import java.util.Map;
Dave Borowitzf03fcea2014-04-21 17:20:33 -070044import java.util.Set;
Dave Borowitz9de65952012-08-13 16:09:45 -070045
46import javax.servlet.http.HttpServletRequest;
47import javax.servlet.http.HttpServletResponse;
48
49/** Serves an HTML page with all the diffs for a commit. */
50public class DiffServlet extends BaseServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080051 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070052
53 private final Linkifier linkifier;
54
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070055 public DiffServlet(GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) {
Dave Borowitz8d6d6872014-03-16 15:18:14 -070056 super(renderer, accessFactory);
Dave Borowitz9de65952012-08-13 16:09:45 -070057 this.linkifier = checkNotNull(linkifier, "linkifier");
58 }
59
60 @Override
Dave Borowitzea9bba12014-07-09 16:45:40 -070061 protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -070062 GitilesView view = ViewFilter.getView(req);
63 Repository repo = ServletUtils.getRepository(req);
64
Shawn Pearceb5ad0a02015-05-24 20:33:17 -070065 try (RevWalk walk = new RevWalk(repo);
66 TreeWalk tw = newTreeWalk(walk, view)) {
David Pursehousef2f98cf2016-06-15 22:08:14 +090067 boolean showCommit;
68 boolean isFile;
Dave Borowitz9de65952012-08-13 16:09:45 -070069 AbstractTreeIterator oldTree;
70 AbstractTreeIterator newTree;
71 try {
Dave Borowitz0d418f62014-04-29 11:32:25 -070072 if (tw == null && !view.getPathPart().isEmpty()) {
73 res.setStatus(SC_NOT_FOUND);
74 return;
75 }
76 isFile = tw != null && isFile(tw);
77
Dave Borowitz9de65952012-08-13 16:09:45 -070078 // 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 Borowitzf03fcea2014-04-21 17:20:33 -070083 } catch (MissingObjectException | IncorrectObjectTypeException e) {
Dave Borowitz9de65952012-08-13 16:09:45 -070084 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 Borowitzf03fcea2014-04-21 17:20:33 -070091 Set<Field> fs = CommitSoyData.DEFAULT_FIELDS;
92 if (isFile) {
93 fs = Field.setOf(fs, Field.PARENT_BLAME_URL);
94 }
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070095 GitilesAccess access = getAccess(req);
96 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020097 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 Borowitz9de65952012-08-13 16:09:45 -0700103 }
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 Borowitz33d4fda2013-10-22 16:40:20 -0700111 setCacheHeaders(res);
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700112 try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.diffDetail", data);
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200113 DiffFormatter diff = new HtmlDiffFormatter(renderer, view, out)) {
Dave Borowitzea9bba12014-07-09 16:45:40 -0700114 formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
Dave Borowitz9de65952012-08-13 16:09:45 -0700115 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700116 }
117 }
118
Dave Borowitzea9bba12014-07-09 16:45:40 -0700119 @Override
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200120 protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
Dave Borowitzea9bba12014-07-09 16:45:40 -0700121 GitilesView view = ViewFilter.getView(req);
122 Repository repo = ServletUtils.getRepository(req);
123
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700124 try (RevWalk walk = new RevWalk(repo)) {
Dave Borowitzea9bba12014-07-09 16:45:40 -0700125 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 Borowitz6c6cac12014-09-17 16:10:28 -0700135 try (Writer writer = startRenderText(req, res);
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700136 OutputStream out = BaseEncoding.base64().encodingStream(writer);
137 DiffFormatter diff = new DiffFormatter(out)) {
138 formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
Dave Borowitzea9bba12014-07-09 16:45:40 -0700139 }
Dave Borowitzea9bba12014-07-09 16:45:40 -0700140 }
141 }
142
Dave Borowitz0d418f62014-04-29 11:32:25 -0700143 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 Nienhuysc0200f62016-05-02 17:34:51 +0200148 walk.getObjectReader(), view.getPathPart(), walk.parseTree(view.getRevision().getId()));
Dave Borowitz0d418f62014-04-29 11:32:25 -0700149 }
150
Dave Borowitz9de65952012-08-13 16:09:45 -0700151 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 Borowitz9de65952012-08-13 16:09:45 -0700156 }
David Pursehouseb3b630f2016-06-15 21:51:18 +0900157 return oldRevision == Revision.NULL;
Dave Borowitz9de65952012-08-13 16:09:45 -0700158 }
159
Dave Borowitz0d418f62014-04-29 11:32:25 -0700160 private static boolean isFile(TreeWalk tw) {
161 return (tw.getRawMode(0) & FileMode.TYPE_FILE) > 0;
Dave Borowitzf03fcea2014-04-21 17:20:33 -0700162 }
163
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200164 private static void formatDiff(
165 Repository repo,
166 AbstractTreeIterator oldTree,
167 AbstractTreeIterator newTree,
168 String path,
169 DiffFormatter diff)
170 throws IOException {
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700171 if (!path.isEmpty()) {
172 diff.setPathFilter(PathFilter.create(path));
Dave Borowitz9de65952012-08-13 16:09:45 -0700173 }
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700174 diff.setRepository(repo);
175 diff.setDetectRenames(true);
176 diff.format(oldTree, newTree);
Dave Borowitz9de65952012-08-13 16:09:45 -0700177 }
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 Borowitz9de65952012-08-13 16:09:45 -0700185 }
David Pursehouseb3b630f2016-06-15 21:51:18 +0900186 return new EmptyTreeIterator();
Dave Borowitz9de65952012-08-13 16:09:45 -0700187 }
188}