blob: ec375bad639c559a9eb07c62c81934be9821b763 [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 Borowitza03760a2014-01-29 16:17:28 -080031import org.eclipse.jgit.util.GitDateFormatter;
32import org.eclipse.jgit.util.GitDateFormatter.Format;
Dave Borowitz9de65952012-08-13 16:09:45 -070033
34import java.io.IOException;
Dave Borowitz9de65952012-08-13 16:09:45 -070035import java.util.List;
36import java.util.Map;
37
38import javax.servlet.http.HttpServletRequest;
39import javax.servlet.http.HttpServletResponse;
40
41/** Serves the index page for a repository, if accessed directly by a browser. */
42public class RepositoryIndexServlet extends BaseServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080043 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070044
Dave Borowitz209d0aa2012-12-28 14:28:53 -080045 static final int REF_LIMIT = 10;
46 private static final int LOG_LIMIT = 20;
47
Dave Borowitz14ce8282012-12-20 14:08:25 -080048 private final TimeCache timeCache;
49
Dave Borowitz8d6d6872014-03-16 15:18:14 -070050 public RepositoryIndexServlet(GitilesAccess.Factory accessFactory, Renderer renderer,
Dave Borowitz14ce8282012-12-20 14:08:25 -080051 TimeCache timeCache) {
Dave Borowitz8d6d6872014-03-16 15:18:14 -070052 super(renderer, 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 Borowitz8d6d6872014-03-16 15:18:14 -070065 RepositoryDescription desc = getAccess(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 Borowitza03760a2014-01-29 16:17:28 -080077 GitDateFormatter df = new GitDateFormatter(Format.DEFAULT);
78 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}