blob: 4eaf7f36f785428b2d0fd6297801899eab2280d8 [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;
25import com.google.common.util.concurrent.UncheckedExecutionException;
26
27import org.eclipse.jgit.http.server.ServletUtils;
Dave Borowitzd0b7e182013-01-11 15:55:09 -080028import org.eclipse.jgit.lib.AnyObjectId;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080029import org.eclipse.jgit.lib.Constants;
30import org.eclipse.jgit.lib.Ref;
31import org.eclipse.jgit.lib.RefComparator;
32import org.eclipse.jgit.lib.RefDatabase;
33import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitzd0b7e182013-01-11 15:55:09 -080034import org.eclipse.jgit.transport.RefAdvertiser;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080035
36import java.io.IOException;
Dave Borowitzd0b7e182013-01-11 15:55:09 -080037import java.io.PrintWriter;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080038import java.util.Collection;
39import java.util.List;
40import java.util.Map;
41
Dave Borowitze5fead02013-01-07 13:12:59 -080042import javax.annotation.Nullable;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080043import javax.servlet.http.HttpServletRequest;
44import javax.servlet.http.HttpServletResponse;
45
46/** Serves an HTML page with all the refs in a repository. */
47public class RefServlet extends BaseServlet {
48 private static final long serialVersionUID = 1L;
49
50 private final TimeCache timeCache;
51
Dave Borowitz8d6d6872014-03-16 15:18:14 -070052 protected RefServlet(GitilesAccess.Factory accessFactory, Renderer renderer,
53 TimeCache timeCache) {
54 super(renderer, accessFactory);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080055 this.timeCache = checkNotNull(timeCache, "timeCache");
56 }
57
58 @Override
Dave Borowitzd0b7e182013-01-11 15:55:09 -080059 protected void doGetHtml(HttpServletRequest req, HttpServletResponse res)
Dave Borowitze5fead02013-01-07 13:12:59 -080060 throws IOException {
Dave Borowitzdd3c3d92013-03-11 16:38:41 -070061 if (!ViewFilter.getView(req).getPathPart().isEmpty()) {
Dave Borowitzd0b7e182013-01-11 15:55:09 -080062 res.setStatus(SC_NOT_FOUND);
63 return;
64 }
Dave Borowitz209d0aa2012-12-28 14:28:53 -080065 RevWalk walk = new RevWalk(ServletUtils.getRepository(req));
Dave Borowitze5fead02013-01-07 13:12:59 -080066 List<Map<String, Object>> tags;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080067 try {
Dave Borowitzd0b7e182013-01-11 15:55:09 -080068 tags = getTagsSoyData(req, timeCache, walk, 0);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080069 } finally {
70 walk.release();
71 }
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
88 static List<Map<String, Object>> getBranchesSoyData(HttpServletRequest req, int limit)
Dave Borowitz209d0aa2012-12-28 14:28:53 -080089 throws IOException {
Dave Borowitze5fead02013-01-07 13:12:59 -080090 RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase();
91 Ref head = refdb.getRef(Constants.HEAD);
92 Ref headLeaf = head != null && head.isSymbolic() ? head.getLeaf() : null;
Dave Borowitzd0b7e182013-01-11 15:55:09 -080093 return getRefsSoyData(
Dave Borowitze5fead02013-01-07 13:12:59 -080094 refdb,
95 ViewFilter.getView(req),
96 Constants.R_HEADS,
97 branchComparator(headLeaf),
98 headLeaf,
99 limit);
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800100 }
101
Dave Borowitze5fead02013-01-07 13:12:59 -0800102 private static Ordering<Ref> branchComparator(Ref headLeaf) {
103 if (headLeaf == null) {
104 return Ordering.from(RefComparator.INSTANCE);
105 }
106 final String headLeafName = headLeaf.getName();
107 return new Ordering<Ref>() {
108 @Override
109 public int compare(@Nullable Ref left, @Nullable Ref right) {
110 int l = isHead(left) ? 1 : 0;
111 int r = isHead(right) ? 1 : 0;
Dave Borowitz5d11e2d2013-01-08 10:03:58 -0800112 return r - l;
Dave Borowitze5fead02013-01-07 13:12:59 -0800113 }
114
115 private final boolean isHead(Ref ref) {
116 return ref != null && ref.getName().equals(headLeafName);
117 }
118 }.compound(RefComparator.INSTANCE);
119 }
120
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800121 static List<Map<String, Object>> getTagsSoyData(HttpServletRequest req,
Dave Borowitze5fead02013-01-07 13:12:59 -0800122 TimeCache timeCache, RevWalk walk, int limit) throws IOException {
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800123 return getRefsSoyData(
Dave Borowitze5fead02013-01-07 13:12:59 -0800124 ServletUtils.getRepository(req).getRefDatabase(),
125 ViewFilter.getView(req),
126 Constants.R_TAGS,
127 tagComparator(timeCache, walk),
128 null,
129 limit);
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800130 }
131
132 private static Ordering<Ref> tagComparator(final TimeCache timeCache, final RevWalk walk) {
133 return Ordering.natural().onResultOf(new Function<Ref, Long>() {
134 @Override
135 public Long apply(Ref ref) {
136 try {
137 return timeCache.getTime(walk, ref.getObjectId());
138 } catch (IOException e) {
139 throw new UncheckedExecutionException(e);
140 }
141 }
142 }).reverse().compound(RefComparator.INSTANCE);
143 }
144
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800145 private static List<Map<String, Object>> getRefsSoyData(
Dave Borowitze5fead02013-01-07 13:12:59 -0800146 RefDatabase refdb,
147 GitilesView view,
148 String prefix,
149 Ordering<Ref> ordering,
150 @Nullable Ref headLeaf,
151 int limit) throws IOException {
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800152 Collection<Ref> refs = refdb.getRefs(prefix).values();
153 refs = ordering.leastOf(refs, limit > 0 ? limit + 1 : refs.size());
Dave Borowitze5fead02013-01-07 13:12:59 -0800154 List<Map<String, Object>> result = Lists.newArrayListWithCapacity(refs.size());
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800155
156 for (Ref ref : refs) {
157 String name = ref.getName().substring(prefix.length());
158 boolean needPrefix = !ref.getName().equals(refdb.getRef(name).getName());
Dave Borowitze5fead02013-01-07 13:12:59 -0800159 Map<String, Object> value = Maps.newHashMapWithExpectedSize(3);
160 value.put("url", GitilesView.revision().copyFrom(view).setRevision(
161 Revision.unpeeled(needPrefix ? ref.getName() : name, ref.getObjectId())).toUrl());
162 value.put("name", name);
163 if (headLeaf != null) {
164 value.put("isHead", headLeaf.equals(ref));
165 }
166 result.add(value);
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800167 }
168 return result;
169 }
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800170
Dave Borowitzba9c1182013-03-13 14:16:43 -0700171 static String sanitizeRefForText(String refName) {
Dave Borowitzd0b7e182013-01-11 15:55:09 -0800172 return refName.replace("&", "&amp;")
173 .replace("<", "&lt;")
174 .replace(">", "&gt;");
175 }
176
177 private static Map<String, Ref> getRefs(RefDatabase refdb, String path) throws IOException {
178 path = GitilesView.maybeTrimLeadingAndTrailingSlash(path);
179 if (path.isEmpty()) {
180 return refdb.getRefs(RefDatabase.ALL);
181 }
182 path = Constants.R_REFS + path;
183 Ref singleRef = refdb.getRef(path);
184 if (singleRef != null) {
185 return ImmutableMap.of(singleRef.getName(), singleRef);
186 }
187 return refdb.getRefs(path + '/');
188 }
189
190 private static class TextRefAdvertiser extends RefAdvertiser {
191 private final PrintWriter writer;
192
193 private TextRefAdvertiser(PrintWriter writer) {
194 this.writer = writer;
195 }
196
197 @Override
198 public void advertiseId(AnyObjectId id, String refName) throws IOException {
199 super.advertiseId(id, sanitizeRefForText(refName));
200 }
201
202 @Override
203 protected void writeOne(CharSequence line) throws IOException {
204 writer.print(line);
205 }
206
207 @Override
208 public void end() throws IOException {
209 writer.close();
210 }
211 }
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800212}