blob: a07a649a491fffd1562dc41da8cc055fb77c65c9 [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;
Dave Borowitz5db0eed2012-12-28 15:08:39 -080021import com.google.common.collect.ImmutableList;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080022import com.google.common.collect.Maps;
Dave Borowitz9de65952012-08-13 16:09:45 -070023
24import org.eclipse.jgit.http.server.ServletUtils;
25import org.eclipse.jgit.lib.Constants;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080026import org.eclipse.jgit.lib.ObjectId;
27import org.eclipse.jgit.lib.Repository;
28import org.eclipse.jgit.revwalk.RevCommit;
29import org.eclipse.jgit.revwalk.RevObject;
Dave Borowitz14ce8282012-12-20 14:08:25 -080030import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitz9de65952012-08-13 16:09:45 -070031
32import java.io.IOException;
Dave Borowitz9de65952012-08-13 16:09:45 -070033import java.util.List;
34import java.util.Map;
35
36import javax.servlet.http.HttpServletRequest;
37import javax.servlet.http.HttpServletResponse;
38
39/** Serves the index page for a repository, if accessed directly by a browser. */
40public class RepositoryIndexServlet extends BaseServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080041 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070042
Dave Borowitz209d0aa2012-12-28 14:28:53 -080043 static final int REF_LIMIT = 10;
44 private static final int LOG_LIMIT = 20;
45
Dave Borowitz14ce8282012-12-20 14:08:25 -080046 private final GitilesAccess.Factory accessFactory;
47 private final TimeCache timeCache;
48
49 public RepositoryIndexServlet(Renderer renderer, GitilesAccess.Factory accessFactory,
50 TimeCache timeCache) {
Dave Borowitz9de65952012-08-13 16:09:45 -070051 super(renderer);
52 this.accessFactory = checkNotNull(accessFactory, "accessFactory");
Dave Borowitz14ce8282012-12-20 14:08:25 -080053 this.timeCache = checkNotNull(timeCache, "timeCache");
Dave Borowitz9de65952012-08-13 16:09:45 -070054 }
55
56 @Override
57 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
Dave Borowitzb1c628f2013-01-11 11:28:20 -080058 renderHtml(req, res, "gitiles.repositoryIndex", buildData(req));
Dave Borowitz9de65952012-08-13 16:09:45 -070059 }
60
61 @VisibleForTesting
62 Map<String, ?> buildData(HttpServletRequest req) throws IOException {
Dave Borowitz209d0aa2012-12-28 14:28:53 -080063 GitilesView view = ViewFilter.getView(req);
64 Repository repo = ServletUtils.getRepository(req);
Dave Borowitz9de65952012-08-13 16:09:45 -070065 RepositoryDescription desc = accessFactory.forRequest(req).getRepositoryDescription();
Dave Borowitz209d0aa2012-12-28 14:28:53 -080066 RevWalk walk = new RevWalk(repo);
Dave Borowitze5fead02013-01-07 13:12:59 -080067 List<Map<String, Object>> tags;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080068 Map<String, Object> data;
Dave Borowitz14ce8282012-12-20 14:08:25 -080069 try {
Dave Borowitzd0b7e182013-01-11 15:55:09 -080070 tags = RefServlet.getTagsSoyData(req, timeCache, walk, REF_LIMIT);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080071 ObjectId headId = repo.resolve(Constants.HEAD);
72 if (headId != null) {
Dave Borowitz5db0eed2012-12-28 15:08:39 -080073 RevObject head = walk.parseAny(headId);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080074 if (head.getType() == Constants.OBJ_COMMIT) {
75 walk.reset();
76 walk.markStart((RevCommit) head);
Dave Borowitz3b086a72013-07-02 15:03:03 -070077 data =
78 new LogSoyData(req, view).toSoyData(walk, LOG_LIMIT, "HEAD", null);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080079 } else {
80 // TODO(dborowitz): Handle non-commit or missing HEAD?
81 data = Maps.newHashMapWithExpectedSize(6);
82 }
83 } else {
84 data = Maps.newHashMapWithExpectedSize(6);
85 }
Dave Borowitz14ce8282012-12-20 14:08:25 -080086 } finally {
87 walk.release();
88 }
Dave Borowitz5db0eed2012-12-28 15:08:39 -080089 if (!data.containsKey("entries")) {
90 data.put("entries", ImmutableList.of());
91 }
Dave Borowitzd0b7e182013-01-11 15:55:09 -080092 List<Map<String, Object>> branches = RefServlet.getBranchesSoyData(req, REF_LIMIT);
Dave Borowitz9de65952012-08-13 16:09:45 -070093
Dave Borowitz209d0aa2012-12-28 14:28:53 -080094 data.put("cloneUrl", desc.cloneUrl);
95 data.put("mirroredFromUrl", Strings.nullToEmpty(desc.mirroredFromUrl));
96 data.put("description", Strings.nullToEmpty(desc.description));
97 data.put("branches", trim(branches));
98 if (branches.size() > REF_LIMIT) {
99 data.put("moreBranchesUrl", GitilesView.refs().copyFrom(view).toUrl());
Dave Borowitz9de65952012-08-13 16:09:45 -0700100 }
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800101 data.put("tags", trim(tags));
102 if (tags.size() > REF_LIMIT) {
103 data.put("moreTagsUrl", GitilesView.refs().copyFrom(view).toUrl());
104 }
105 return data;
Dave Borowitz9de65952012-08-13 16:09:45 -0700106 }
Dave Borowitz14ce8282012-12-20 14:08:25 -0800107
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800108 private static <T> List<T> trim(List<T> list) {
109 return list.size() > REF_LIMIT ? list.subList(0, REF_LIMIT) : list;
Dave Borowitz14ce8282012-12-20 14:08:25 -0800110 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700111}