blob: 90d51702ded86a44c4ee5f9910f1ae407a3e9dfb [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)) {
Dave Borowitzf03fcea2014-04-21 17:20:33 -070067 boolean showCommit, isFile;
Dave Borowitz9de65952012-08-13 16:09:45 -070068 AbstractTreeIterator oldTree;
69 AbstractTreeIterator newTree;
70 try {
Dave Borowitz0d418f62014-04-29 11:32:25 -070071 if (tw == null && !view.getPathPart().isEmpty()) {
72 res.setStatus(SC_NOT_FOUND);
73 return;
74 }
75 isFile = tw != null && isFile(tw);
76
Dave Borowitz9de65952012-08-13 16:09:45 -070077 // 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 Borowitzf03fcea2014-04-21 17:20:33 -070082 } catch (MissingObjectException | IncorrectObjectTypeException e) {
Dave Borowitz9de65952012-08-13 16:09:45 -070083 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 Borowitzf03fcea2014-04-21 17:20:33 -070090 Set<Field> fs = CommitSoyData.DEFAULT_FIELDS;
91 if (isFile) {
92 fs = Field.setOf(fs, Field.PARENT_BLAME_URL);
93 }
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070094 GitilesAccess access = getAccess(req);
95 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020096 data.put(
97 "commit",
98 new CommitSoyData()
99 .setLinkifier(linkifier)
100 .setArchiveFormat(getArchiveFormat(access))
101 .toSoyData(req, walk.parseCommit(view.getRevision().getId()), fs, df));
Dave Borowitz9de65952012-08-13 16:09:45 -0700102 }
103 if (!data.containsKey("repositoryName") && (view.getRepositoryName() != null)) {
104 data.put("repositoryName", view.getRepositoryName());
105 }
106 if (!data.containsKey("breadcrumbs")) {
107 data.put("breadcrumbs", view.getBreadcrumbs());
108 }
109
Dave Borowitz33d4fda2013-10-22 16:40:20 -0700110 setCacheHeaders(res);
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700111 try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.diffDetail", data);
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200112 DiffFormatter diff = new HtmlDiffFormatter(renderer, view, out)) {
Dave Borowitzea9bba12014-07-09 16:45:40 -0700113 formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
Dave Borowitz9de65952012-08-13 16:09:45 -0700114 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700115 }
116 }
117
Dave Borowitzea9bba12014-07-09 16:45:40 -0700118 @Override
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200119 protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
Dave Borowitzea9bba12014-07-09 16:45:40 -0700120 GitilesView view = ViewFilter.getView(req);
121 Repository repo = ServletUtils.getRepository(req);
122
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700123 try (RevWalk walk = new RevWalk(repo)) {
Dave Borowitzea9bba12014-07-09 16:45:40 -0700124 AbstractTreeIterator oldTree;
125 AbstractTreeIterator newTree;
126 try {
127 oldTree = getTreeIterator(walk, view.getOldRevision().getId());
128 newTree = getTreeIterator(walk, view.getRevision().getId());
129 } catch (MissingObjectException | IncorrectObjectTypeException e) {
130 res.setStatus(SC_NOT_FOUND);
131 return;
132 }
133
Dave Borowitz6c6cac12014-09-17 16:10:28 -0700134 try (Writer writer = startRenderText(req, res);
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700135 OutputStream out = BaseEncoding.base64().encodingStream(writer);
136 DiffFormatter diff = new DiffFormatter(out)) {
137 formatDiff(repo, oldTree, newTree, view.getPathPart(), diff);
Dave Borowitzea9bba12014-07-09 16:45:40 -0700138 }
Dave Borowitzea9bba12014-07-09 16:45:40 -0700139 }
140 }
141
Dave Borowitz0d418f62014-04-29 11:32:25 -0700142 private static TreeWalk newTreeWalk(RevWalk walk, GitilesView view) throws IOException {
143 if (view.getPathPart().isEmpty()) {
144 return null;
145 }
146 return TreeWalk.forPath(
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200147 walk.getObjectReader(), view.getPathPart(), walk.parseTree(view.getRevision().getId()));
Dave Borowitz0d418f62014-04-29 11:32:25 -0700148 }
149
Dave Borowitz9de65952012-08-13 16:09:45 -0700150 private static boolean isParentOf(RevWalk walk, Revision oldRevision, Revision newRevision)
151 throws MissingObjectException, IncorrectObjectTypeException, IOException {
152 RevCommit newCommit = walk.parseCommit(newRevision.getId());
153 if (newCommit.getParentCount() > 0) {
154 return Arrays.asList(newCommit.getParents()).contains(oldRevision.getId());
155 } else {
156 return oldRevision == Revision.NULL;
157 }
158 }
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;
185 } else {
186 return new EmptyTreeIterator();
187 }
188 }
189}