blob: 7dde342ec4b2fef34176b83faaeb0bcb4f45a4ea [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 Borowitz2b2f34b2014-04-29 16:47:20 -070023import com.google.gitiles.DateFormatter.Format;
Dave Borowitz9de65952012-08-13 16:09:45 -070024
25import org.eclipse.jgit.http.server.ServletUtils;
26import org.eclipse.jgit.lib.Constants;
Dave Borowitz209d0aa2012-12-28 14:28:53 -080027import org.eclipse.jgit.lib.ObjectId;
28import org.eclipse.jgit.lib.Repository;
29import org.eclipse.jgit.revwalk.RevCommit;
30import org.eclipse.jgit.revwalk.RevObject;
Dave Borowitz14ce8282012-12-20 14:08:25 -080031import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitz9de65952012-08-13 16:09:45 -070032
33import java.io.IOException;
Dave Borowitz9de65952012-08-13 16:09:45 -070034import java.util.List;
35import java.util.Map;
36
37import javax.servlet.http.HttpServletRequest;
38import javax.servlet.http.HttpServletResponse;
39
40/** Serves the index page for a repository, if accessed directly by a browser. */
41public class RepositoryIndexServlet extends BaseServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080042 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070043
Dave Borowitz209d0aa2012-12-28 14:28:53 -080044 static final int REF_LIMIT = 10;
45 private static final int LOG_LIMIT = 20;
46
Dave Borowitz14ce8282012-12-20 14:08:25 -080047 private final TimeCache timeCache;
48
Dave Borowitz8d6d6872014-03-16 15:18:14 -070049 public RepositoryIndexServlet(GitilesAccess.Factory accessFactory, Renderer renderer,
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070050 TimeCache timeCache) {
Dave Borowitz8d6d6872014-03-16 15:18:14 -070051 super(renderer, 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 {
Dave Borowitzb1c628f2013-01-11 11:28:20 -080057 renderHtml(req, res, "gitiles.repositoryIndex", buildData(req));
Dave Borowitz9de65952012-08-13 16:09:45 -070058 }
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 Borowitz2b2f34b2014-04-29 16:47:20 -070064 GitilesAccess access = getAccess(req);
65 RepositoryDescription desc = access.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 Borowitz2b2f34b2014-04-29 16:47:20 -070077 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Dave Borowitza03760a2014-01-29 16:17:28 -080078 data = new LogSoyData(req, view).toSoyData(walk, LOG_LIMIT, "HEAD", null, df);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080079 } else {
80 // TODO(dborowitz): Handle non-commit or missing HEAD?
Stefan Zagerd2187432014-02-28 02:23:02 -080081 data = Maps.newHashMapWithExpectedSize(7);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080082 }
83 } else {
Stefan Zagerd2187432014-02-28 02:23:02 -080084 data = Maps.newHashMapWithExpectedSize(7);
Dave Borowitz209d0aa2012-12-28 14:28:53 -080085 }
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 }
Stefan Zagerd2187432014-02-28 02:23:02 -0800105 GitilesConfig.putVariant(getAccess(req).getConfig(), "logEntry", "logEntryVariant", data);
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800106 return data;
Dave Borowitz9de65952012-08-13 16:09:45 -0700107 }
Dave Borowitz14ce8282012-12-20 14:08:25 -0800108
Dave Borowitz209d0aa2012-12-28 14:28:53 -0800109 private static <T> List<T> trim(List<T> list) {
110 return list.size() > REF_LIMIT ? list.subList(0, REF_LIMIT) : list;
Dave Borowitz14ce8282012-12-20 14:08:25 -0800111 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700112}