blob: b5a0636dbf2e061193d24363d2be892238be734a [file] [log] [blame]
Dave Borowitz209d0aa2012-12-28 14:28:53 -08001// 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 Borowitzd0b7e182013-01-11 15:55:09 -080018import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080019
20import com.google.common.base.Function;
21import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.Lists;
Dave Borowitze5fead02013-01-07 13:12:59 -080023import com.google.common.collect.Maps;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080024import com.google.common.collect.Ordering;
Dave Borowitz1d94e652014-07-30 12:45:09 -070025import com.google.common.primitives.Ints;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080026import com.google.common.util.concurrent.UncheckedExecutionException;
Kevin Graney77dbea92014-07-08 14:07:40 -040027import com.google.gson.reflect.TypeToken;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080028
29import org.eclipse.jgit.http.server.ServletUtils;
Dave Borowitzd0b7e182013-01-11 15:55:09 -080030import org.eclipse.jgit.lib.AnyObjectId;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080031import org.eclipse.jgit.lib.Constants;
32import org.eclipse.jgit.lib.Ref;
33import org.eclipse.jgit.lib.RefComparator;
34import org.eclipse.jgit.lib.RefDatabase;
35import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitzd0b7e182013-01-11 15:55:09 -080036import org.eclipse.jgit.transport.RefAdvertiser;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080037
38import java.io.IOException;
Dave Borowitz673d1982014-05-02 12:30:49 -070039import java.io.Writer;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080040import java.util.Collection;
Dave Borowitz1d94e652014-07-30 12:45:09 -070041import java.util.LinkedHashMap;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080042import java.util.List;
43import java.util.Map;
44
Dave Borowitze5fead02013-01-07 13:12:59 -080045import javax.annotation.Nullable;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080046import javax.servlet.http.HttpServletRequest;
47import javax.servlet.http.HttpServletResponse;
48
49/** Serves an HTML page with all the refs in a repository. */
50public class RefServlet extends BaseServlet {
51 private static final long serialVersionUID = 1L;
52
53 private final TimeCache timeCache;
54
Dave Borowitz8d6d6872014-03-16 15:18:14 -070055 protected RefServlet(GitilesAccess.Factory accessFactory, Renderer renderer,
56 TimeCache timeCache) {
57 super(renderer, accessFactory);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080058 this.timeCache = checkNotNull(timeCache, "timeCache");
59 }
60
61 @Override
Dave Borowitzd0b7e182013-01-11 15:55:09 -080062 protected void doGetHtml(HttpServletRequest req, HttpServletResponse res)
Dave Borowitze5fead02013-01-07 13:12:59 -080063 throws IOException {
Dave Borowitzdd3c3d92013-03-11 16:38:41 -070064 if (!ViewFilter.getView(req).getPathPart().isEmpty()) {
Dave Borowitzd0b7e182013-01-11 15:55:09 -080065 res.setStatus(SC_NOT_FOUND);
66 return;
67 }
Dave Borowitze5fead02013-01-07 13:12:59 -080068 List<Map<String, Object>> tags;
Shawn Pearceb5ad0a02015-05-24 20:33:17 -070069 try (RevWalk walk = new RevWalk(ServletUtils.getRepository(req))) {
Dave Borowitzd0b7e182013-01-11 15:55:09 -080070 tags = getTagsSoyData(req, timeCache, walk, 0);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080071 }
Dave Borowitzb1c628f2013-01-11 11:28:20 -080072 renderHtml(req, res, "gitiles.refsDetail",
Dave Borowitzd0b7e182013-01-11 15:55:09 -080073 ImmutableMap.of("branches", getBranchesSoyData(req, 0), "tags", tags));
Dave Borowitz209d0aa2012-12-28 14:28:53 -080074 }
75
Dave Borowitzd0b7e182013-01-11 15:55:09 -080076 @Override
77 protected void doGetText(HttpServletRequest req, HttpServletResponse res)
78 throws IOException {
79 GitilesView view = ViewFilter.getView(req);
80 Map<String, Ref> refs = getRefs(ServletUtils.getRepository(req).getRefDatabase(),
Dave Borowitzdd3c3d92013-03-11 16:38:41 -070081 view.getPathPart());
Dave Borowitzd0b7e182013-01-11 15:55:09 -080082 TextRefAdvertiser adv = new TextRefAdvertiser(startRenderText(req, res));
83 adv.setDerefTags(true);
84 adv.send(refs);
85 adv.end();
86 }
87
Kevin Graney77dbea92014-07-08 14:07:40 -040088 @Override
89 protected void doGetJson(HttpServletRequest req, HttpServletResponse res)
90 throws IOException {
91 GitilesView view = ViewFilter.getView(req);
92 Map<String, Ref> refs = getRefs(ServletUtils.getRepository(req).getRefDatabase(),
93 view.getPathPart());
Dave Borowitz96a6f472014-11-04 16:38:20 -080094 Map<String, RefJsonData> jsonRefs = new LinkedHashMap<>();
Kevin Graney77dbea92014-07-08 14:07:40 -040095 for (Map.Entry<String, Ref> ref : refs.entrySet()) {
96 jsonRefs.put(ref.getKey(), new RefJsonData(ref.getValue()));
97 }
98 renderJson(req, res, jsonRefs, new TypeToken<Map<String, RefJsonData>>() {}.getType());
99 }
100
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800101 static List<Map<String, Object>> getBranchesSoyData(HttpServletRequest req, int limit)
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800102 throws IOException {
Dave Borowitze5fead02013-01-07 13:12:59 -0800103 RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase();
104 Ref head = refdb.getRef(Constants.HEAD);
105 Ref headLeaf = head != null && head.isSymbolic() ? head.getLeaf() : null;
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800106 return getRefsSoyData(
Dave Borowitze5fead02013-01-07 13:12:59 -0800107 refdb,
108 ViewFilter.getView(req),
109 Constants.R_HEADS,
110 branchComparator(headLeaf),
111 headLeaf,
112 limit);
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800113 }
114
Dave Borowitze5fead02013-01-07 13:12:59 -0800115 private static Ordering<Ref> branchComparator(Ref headLeaf) {
116 if (headLeaf == null) {
117 return Ordering.from(RefComparator.INSTANCE);
118 }
119 final String headLeafName = headLeaf.getName();
120 return new Ordering<Ref>() {
121 @Override
122 public int compare(@Nullable Ref left, @Nullable Ref right) {
123 int l = isHead(left) ? 1 : 0;
124 int r = isHead(right) ? 1 : 0;
Dave Borowitz5d11e2d2013-01-08 10:03:58 -0800125 return r - l;
Dave Borowitze5fead02013-01-07 13:12:59 -0800126 }
127
128 private final boolean isHead(Ref ref) {
129 return ref != null && ref.getName().equals(headLeafName);
130 }
131 }.compound(RefComparator.INSTANCE);
132 }
133
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800134 static List<Map<String, Object>> getTagsSoyData(HttpServletRequest req,
Dave Borowitze5fead02013-01-07 13:12:59 -0800135 TimeCache timeCache, RevWalk walk, int limit) throws IOException {
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800136 return getRefsSoyData(
Dave Borowitze5fead02013-01-07 13:12:59 -0800137 ServletUtils.getRepository(req).getRefDatabase(),
138 ViewFilter.getView(req),
139 Constants.R_TAGS,
140 tagComparator(timeCache, walk),
141 null,
142 limit);
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800143 }
144
145 private static Ordering<Ref> tagComparator(final TimeCache timeCache, final RevWalk walk) {
146 return Ordering.natural().onResultOf(new Function<Ref, Long>() {
147 @Override
148 public Long apply(Ref ref) {
149 try {
150 return timeCache.getTime(walk, ref.getObjectId());
151 } catch (IOException e) {
152 throw new UncheckedExecutionException(e);
153 }
154 }
155 }).reverse().compound(RefComparator.INSTANCE);
156 }
157
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800158 private static List<Map<String, Object>> getRefsSoyData(
Dave Borowitze5fead02013-01-07 13:12:59 -0800159 RefDatabase refdb,
160 GitilesView view,
161 String prefix,
162 Ordering<Ref> ordering,
163 @Nullable Ref headLeaf,
164 int limit) throws IOException {
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800165 Collection<Ref> refs = refdb.getRefs(prefix).values();
Dave Borowitz1d94e652014-07-30 12:45:09 -0700166 refs = ordering.leastOf(refs, limit > 0 ? Ints.saturatedCast(limit + 1L) : refs.size());
Dave Borowitze5fead02013-01-07 13:12:59 -0800167 List<Map<String, Object>> result = Lists.newArrayListWithCapacity(refs.size());
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800168
169 for (Ref ref : refs) {
170 String name = ref.getName().substring(prefix.length());
171 boolean needPrefix = !ref.getName().equals(refdb.getRef(name).getName());
Dave Borowitze5fead02013-01-07 13:12:59 -0800172 Map<String, Object> value = Maps.newHashMapWithExpectedSize(3);
173 value.put("url", GitilesView.revision().copyFrom(view).setRevision(
174 Revision.unpeeled(needPrefix ? ref.getName() : name, ref.getObjectId())).toUrl());
175 value.put("name", name);
176 if (headLeaf != null) {
177 value.put("isHead", headLeaf.equals(ref));
178 }
179 result.add(value);
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800180 }
181 return result;
182 }
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800183
Dave Borowitzba9c1182013-03-13 14:16:43 -0700184 static String sanitizeRefForText(String refName) {
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800185 return refName.replace("&", "&amp;")
186 .replace("<", "&lt;")
187 .replace(">", "&gt;");
188 }
189
190 private static Map<String, Ref> getRefs(RefDatabase refdb, String path) throws IOException {
191 path = GitilesView.maybeTrimLeadingAndTrailingSlash(path);
192 if (path.isEmpty()) {
193 return refdb.getRefs(RefDatabase.ALL);
194 }
195 path = Constants.R_REFS + path;
196 Ref singleRef = refdb.getRef(path);
197 if (singleRef != null) {
198 return ImmutableMap.of(singleRef.getName(), singleRef);
199 }
200 return refdb.getRefs(path + '/');
201 }
202
203 private static class TextRefAdvertiser extends RefAdvertiser {
Dave Borowitz673d1982014-05-02 12:30:49 -0700204 private final Writer writer;
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800205
Dave Borowitz673d1982014-05-02 12:30:49 -0700206 private TextRefAdvertiser(Writer writer) {
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800207 this.writer = writer;
208 }
209
210 @Override
211 public void advertiseId(AnyObjectId id, String refName) throws IOException {
212 super.advertiseId(id, sanitizeRefForText(refName));
213 }
214
215 @Override
216 protected void writeOne(CharSequence line) throws IOException {
Dave Borowitz673d1982014-05-02 12:30:49 -0700217 writer.append(line);
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800218 }
219
220 @Override
221 public void end() throws IOException {
222 writer.close();
223 }
224 }
Kevin Graney77dbea92014-07-08 14:07:40 -0400225
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700226 static class RefJsonData {
227 RefJsonData(Ref ref) {
Kevin Graney77dbea92014-07-08 14:07:40 -0400228 value = ref.getObjectId().getName();
229 if(ref.getPeeledObjectId() != null) {
230 peeled = ref.getPeeledObjectId().getName();
231 }
232 if (ref.isSymbolic()) {
233 target = ref.getTarget().getName();
234 }
235 }
236
Dave Borowitz32ec5b92014-07-30 07:43:28 -0700237 String value;
238 String peeled;
239 String target;
Kevin Graney77dbea92014-07-08 14:07:40 -0400240 }
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800241}