blob: 325068ae326f4deb1c5113269d1c6316b4b73237 [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;
18
19import com.google.common.annotations.VisibleForTesting;
20import com.google.common.base.Strings;
21import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.Lists;
23
24import org.eclipse.jgit.http.server.ServletUtils;
25import org.eclipse.jgit.lib.Constants;
26import org.eclipse.jgit.lib.Ref;
27import org.eclipse.jgit.lib.RefComparator;
28import org.eclipse.jgit.lib.RefDatabase;
29
30import java.io.IOException;
31import java.util.Collection;
32import java.util.List;
33import java.util.Map;
34
35import javax.servlet.http.HttpServletRequest;
36import javax.servlet.http.HttpServletResponse;
37
38/** Serves the index page for a repository, if accessed directly by a browser. */
39public class RepositoryIndexServlet extends BaseServlet {
40 private final GitilesAccess.Factory accessFactory;
41
42 public RepositoryIndexServlet(Renderer renderer, GitilesAccess.Factory accessFactory) {
43 super(renderer);
44 this.accessFactory = checkNotNull(accessFactory, "accessFactory");
45 }
46
47 @Override
48 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
49 render(req, res, "gitiles.repositoryIndex", buildData(req));
50 }
51
52 @VisibleForTesting
53 Map<String, ?> buildData(HttpServletRequest req) throws IOException {
54 RepositoryDescription desc = accessFactory.forRequest(req).getRepositoryDescription();
55 return ImmutableMap.of(
56 "cloneUrl", desc.cloneUrl,
57 "description", Strings.nullToEmpty(desc.description),
58 "branches", getRefs(req, Constants.R_HEADS),
59 "tags", getRefs(req, Constants.R_TAGS));
60 }
61
62 private List<Map<String, String>> getRefs(HttpServletRequest req, String prefix)
63 throws IOException {
64 RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase();
65 String repoName = ViewFilter.getView(req).getRepositoryName();
66 Collection<Ref> refs = RefComparator.sort(refdb.getRefs(prefix).values());
67 List<Map<String, String>> result = Lists.newArrayListWithCapacity(refs.size());
68
69 for (Ref ref : refs) {
70 String name = ref.getName().substring(prefix.length());
71 boolean needPrefix = !ref.getName().equals(refdb.getRef(name).getName());
72 result.add(ImmutableMap.of(
73 "url", GitilesView.log().copyFrom(req).setRevision(
74 Revision.unpeeled(needPrefix ? ref.getName() : name, ref.getObjectId())).toUrl(),
75 "name", name));
76 }
77
78 return result;
79 }
80}