blob: a0fea728f1574dcfe1609cb2f3bd7b52df9d1a74 [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 Borowitz209d0aa2012-12-28 14:28:53 -080021import com.google.common.collect.Maps;
Dave Borowitz9de65952012-08-13 16:09:45 -070022
23import org.eclipse.jgit.http.server.ServletUtils;
24import org.eclipse.jgit.lib.Constants;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080025import org.eclipse.jgit.lib.ObjectId;
26import org.eclipse.jgit.lib.Repository;
27import org.eclipse.jgit.revwalk.RevCommit;
28import org.eclipse.jgit.revwalk.RevObject;
Dave Borowitz14ce8282012-12-20 14:08:25 -080029import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitz9de65952012-08-13 16:09:45 -070030
31import java.io.IOException;
Dave Borowitz9de65952012-08-13 16:09:45 -070032import 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 {
Chad Horohoead23f142012-11-12 09:45:39 -080040 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070041
Dave Borowitz209d0aa2012-12-28 14:28:53 -080042 static final int REF_LIMIT = 10;
43 private static final int LOG_LIMIT = 20;
44
Dave Borowitz14ce8282012-12-20 14:08:25 -080045 private final GitilesAccess.Factory accessFactory;
46 private final TimeCache timeCache;
47
48 public RepositoryIndexServlet(Renderer renderer, GitilesAccess.Factory accessFactory,
49 TimeCache timeCache) {
Dave Borowitz9de65952012-08-13 16:09:45 -070050 super(renderer);
51 this.accessFactory = checkNotNull(accessFactory, "accessFactory");
Dave Borowitz14ce8282012-12-20 14:08:25 -080052 this.timeCache = checkNotNull(timeCache, "timeCache");
Dave Borowitz9de65952012-08-13 16:09:45 -070053 }
54
55 @Override
56 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
57 render(req, res, "gitiles.repositoryIndex", buildData(req));
58 }
59
60 @VisibleForTesting
61 Map<String, ?> buildData(HttpServletRequest req) throws IOException {
Dave Borowitz209d0aa2012-12-28 14:28:53 -080062 GitilesView view = ViewFilter.getView(req);
63 Repository repo = ServletUtils.getRepository(req);
Dave Borowitz9de65952012-08-13 16:09:45 -070064 RepositoryDescription desc = accessFactory.forRequest(req).getRepositoryDescription();
Dave Borowitz209d0aa2012-12-28 14:28:53 -080065 RevWalk walk = new RevWalk(repo);
Dave Borowitz14ce8282012-12-20 14:08:25 -080066 List<Map<String, String>> tags;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080067 Map<String, Object> data;
Dave Borowitz14ce8282012-12-20 14:08:25 -080068 try {
Dave Borowitz209d0aa2012-12-28 14:28:53 -080069 tags = RefServlet.getTags(req, timeCache, walk, REF_LIMIT);
70 ObjectId headId = repo.resolve(Constants.HEAD);
71 if (headId != null) {
72 RevObject head = walk.parseAny(repo.resolve(Constants.HEAD));
73 if (head.getType() == Constants.OBJ_COMMIT) {
74 walk.reset();
75 walk.markStart((RevCommit) head);
76 data = new LogSoyData(req, repo, view).toSoyData(walk, LOG_LIMIT, "HEAD", null);
77 } else {
78 // TODO(dborowitz): Handle non-commit or missing HEAD?
79 data = Maps.newHashMapWithExpectedSize(6);
80 }
81 } else {
82 data = Maps.newHashMapWithExpectedSize(6);
83 }
Dave Borowitz14ce8282012-12-20 14:08:25 -080084 } finally {
85 walk.release();
86 }
Dave Borowitz209d0aa2012-12-28 14:28:53 -080087 List<Map<String, String>> branches = RefServlet.getBranches(req, REF_LIMIT);
Dave Borowitz9de65952012-08-13 16:09:45 -070088
Dave Borowitz209d0aa2012-12-28 14:28:53 -080089 data.put("cloneUrl", desc.cloneUrl);
90 data.put("mirroredFromUrl", Strings.nullToEmpty(desc.mirroredFromUrl));
91 data.put("description", Strings.nullToEmpty(desc.description));
92 data.put("branches", trim(branches));
93 if (branches.size() > REF_LIMIT) {
94 data.put("moreBranchesUrl", GitilesView.refs().copyFrom(view).toUrl());
Dave Borowitz9de65952012-08-13 16:09:45 -070095 }
Dave Borowitz209d0aa2012-12-28 14:28:53 -080096 data.put("tags", trim(tags));
97 if (tags.size() > REF_LIMIT) {
98 data.put("moreTagsUrl", GitilesView.refs().copyFrom(view).toUrl());
99 }
100 return data;
Dave Borowitz9de65952012-08-13 16:09:45 -0700101 }
Dave Borowitz14ce8282012-12-20 14:08:25 -0800102
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800103 private static <T> List<T> trim(List<T> list) {
104 return list.size() > REF_LIMIT ? list.subList(0, REF_LIMIT) : list;
Dave Borowitz14ce8282012-12-20 14:08:25 -0800105 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700106}