blob: 959a449623e3b2ee2a7cf542f51103999b1bf004 [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;
18
19import com.google.common.base.Function;
20import com.google.common.collect.ImmutableMap;
21import com.google.common.collect.Lists;
Dave Borowitze5fead02013-01-07 13:12:59 -080022import com.google.common.collect.Maps;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080023import com.google.common.collect.Ordering;
24import com.google.common.util.concurrent.UncheckedExecutionException;
25
26import org.eclipse.jgit.http.server.ServletUtils;
27import org.eclipse.jgit.lib.Constants;
28import org.eclipse.jgit.lib.Ref;
29import org.eclipse.jgit.lib.RefComparator;
30import org.eclipse.jgit.lib.RefDatabase;
31import org.eclipse.jgit.revwalk.RevWalk;
32
33import java.io.IOException;
34import java.util.Collection;
35import java.util.List;
36import java.util.Map;
37
Dave Borowitze5fead02013-01-07 13:12:59 -080038import javax.annotation.Nullable;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080039import javax.servlet.http.HttpServletRequest;
40import javax.servlet.http.HttpServletResponse;
41
42/** Serves an HTML page with all the refs in a repository. */
43public class RefServlet extends BaseServlet {
44 private static final long serialVersionUID = 1L;
45
46 private final TimeCache timeCache;
47
48 protected RefServlet(Renderer renderer, TimeCache timeCache) {
49 super(renderer);
50 this.timeCache = checkNotNull(timeCache, "timeCache");
51 }
52
53 @Override
Dave Borowitze5fead02013-01-07 13:12:59 -080054 protected void doGet(HttpServletRequest req, HttpServletResponse res)
55 throws IOException {
Dave Borowitz209d0aa2012-12-28 14:28:53 -080056 RevWalk walk = new RevWalk(ServletUtils.getRepository(req));
Dave Borowitze5fead02013-01-07 13:12:59 -080057 List<Map<String, Object>> tags;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080058 try {
59 tags = getTags(req, timeCache, walk, 0);
60 } finally {
61 walk.release();
62 }
Dave Borowitze5fead02013-01-07 13:12:59 -080063 render(req, res, "gitiles.refsDetail",
64 ImmutableMap.of("branches", getBranches(req, 0), "tags", tags));
Dave Borowitz209d0aa2012-12-28 14:28:53 -080065 }
66
Dave Borowitze5fead02013-01-07 13:12:59 -080067 static List<Map<String, Object>> getBranches(HttpServletRequest req, int limit)
Dave Borowitz209d0aa2012-12-28 14:28:53 -080068 throws IOException {
Dave Borowitze5fead02013-01-07 13:12:59 -080069 RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase();
70 Ref head = refdb.getRef(Constants.HEAD);
71 Ref headLeaf = head != null && head.isSymbolic() ? head.getLeaf() : null;
72 return getRefs(
73 refdb,
74 ViewFilter.getView(req),
75 Constants.R_HEADS,
76 branchComparator(headLeaf),
77 headLeaf,
78 limit);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080079 }
80
Dave Borowitze5fead02013-01-07 13:12:59 -080081 private static Ordering<Ref> branchComparator(Ref headLeaf) {
82 if (headLeaf == null) {
83 return Ordering.from(RefComparator.INSTANCE);
84 }
85 final String headLeafName = headLeaf.getName();
86 return new Ordering<Ref>() {
87 @Override
88 public int compare(@Nullable Ref left, @Nullable Ref right) {
89 int l = isHead(left) ? 1 : 0;
90 int r = isHead(right) ? 1 : 0;
Dave Borowitz5d11e2d2013-01-08 10:03:58 -080091 return r - l;
Dave Borowitze5fead02013-01-07 13:12:59 -080092 }
93
94 private final boolean isHead(Ref ref) {
95 return ref != null && ref.getName().equals(headLeafName);
96 }
97 }.compound(RefComparator.INSTANCE);
98 }
99
100 static List<Map<String, Object>> getTags(HttpServletRequest req,
101 TimeCache timeCache, RevWalk walk, int limit) throws IOException {
102 return getRefs(
103 ServletUtils.getRepository(req).getRefDatabase(),
104 ViewFilter.getView(req),
105 Constants.R_TAGS,
106 tagComparator(timeCache, walk),
107 null,
108 limit);
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800109 }
110
111 private static Ordering<Ref> tagComparator(final TimeCache timeCache, final RevWalk walk) {
112 return Ordering.natural().onResultOf(new Function<Ref, Long>() {
113 @Override
114 public Long apply(Ref ref) {
115 try {
116 return timeCache.getTime(walk, ref.getObjectId());
117 } catch (IOException e) {
118 throw new UncheckedExecutionException(e);
119 }
120 }
121 }).reverse().compound(RefComparator.INSTANCE);
122 }
123
Dave Borowitze5fead02013-01-07 13:12:59 -0800124 private static List<Map<String, Object>> getRefs(
125 RefDatabase refdb,
126 GitilesView view,
127 String prefix,
128 Ordering<Ref> ordering,
129 @Nullable Ref headLeaf,
130 int limit) throws IOException {
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800131 Collection<Ref> refs = refdb.getRefs(prefix).values();
132 refs = ordering.leastOf(refs, limit > 0 ? limit + 1 : refs.size());
Dave Borowitze5fead02013-01-07 13:12:59 -0800133 List<Map<String, Object>> result = Lists.newArrayListWithCapacity(refs.size());
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800134
135 for (Ref ref : refs) {
136 String name = ref.getName().substring(prefix.length());
137 boolean needPrefix = !ref.getName().equals(refdb.getRef(name).getName());
Dave Borowitze5fead02013-01-07 13:12:59 -0800138 Map<String, Object> value = Maps.newHashMapWithExpectedSize(3);
139 value.put("url", GitilesView.revision().copyFrom(view).setRevision(
140 Revision.unpeeled(needPrefix ? ref.getName() : name, ref.getObjectId())).toUrl());
141 value.put("name", name);
142 if (headLeaf != null) {
143 value.put("isHead", headLeaf.equals(ref));
144 }
145 result.add(value);
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800146 }
147 return result;
148 }
149}