| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | |
| 19 | import com.google.common.annotations.VisibleForTesting; |
| 20 | import com.google.common.base.Strings; |
| Dave Borowitz | 5db0eed | 2012-12-28 15:08:39 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 23 | |
| 24 | import org.eclipse.jgit.http.server.ServletUtils; |
| 25 | import org.eclipse.jgit.lib.Constants; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 26 | import org.eclipse.jgit.lib.ObjectId; |
| 27 | import org.eclipse.jgit.lib.Repository; |
| 28 | import org.eclipse.jgit.revwalk.RevCommit; |
| 29 | import org.eclipse.jgit.revwalk.RevObject; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 30 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 31 | |
| 32 | import java.io.IOException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | |
| 36 | import javax.servlet.http.HttpServletRequest; |
| 37 | import javax.servlet.http.HttpServletResponse; |
| 38 | |
| 39 | /** Serves the index page for a repository, if accessed directly by a browser. */ |
| 40 | public class RepositoryIndexServlet extends BaseServlet { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 41 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 42 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 43 | static final int REF_LIMIT = 10; |
| 44 | private static final int LOG_LIMIT = 20; |
| 45 | |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 46 | private final GitilesAccess.Factory accessFactory; |
| 47 | private final TimeCache timeCache; |
| 48 | |
| 49 | public RepositoryIndexServlet(Renderer renderer, GitilesAccess.Factory accessFactory, |
| 50 | TimeCache timeCache) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 51 | super(renderer); |
| 52 | this.accessFactory = checkNotNull(accessFactory, "accessFactory"); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 53 | this.timeCache = checkNotNull(timeCache, "timeCache"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 58 | renderHtml(req, res, "gitiles.repositoryIndex", buildData(req)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @VisibleForTesting |
| 62 | Map<String, ?> buildData(HttpServletRequest req) throws IOException { |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 63 | GitilesView view = ViewFilter.getView(req); |
| 64 | Repository repo = ServletUtils.getRepository(req); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 65 | RepositoryDescription desc = accessFactory.forRequest(req).getRepositoryDescription(); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 66 | RevWalk walk = new RevWalk(repo); |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 67 | List<Map<String, Object>> tags; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 68 | Map<String, Object> data; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 69 | try { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 70 | tags = RefServlet.getTagsSoyData(req, timeCache, walk, REF_LIMIT); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 71 | ObjectId headId = repo.resolve(Constants.HEAD); |
| 72 | if (headId != null) { |
| Dave Borowitz | 5db0eed | 2012-12-28 15:08:39 -0800 | [diff] [blame] | 73 | RevObject head = walk.parseAny(headId); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 74 | if (head.getType() == Constants.OBJ_COMMIT) { |
| 75 | walk.reset(); |
| 76 | walk.markStart((RevCommit) head); |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 77 | data = |
| 78 | new LogSoyData(req, view).toSoyData(walk, LOG_LIMIT, "HEAD", null); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 79 | } 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 Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 86 | } finally { |
| 87 | walk.release(); |
| 88 | } |
| Dave Borowitz | 5db0eed | 2012-12-28 15:08:39 -0800 | [diff] [blame] | 89 | if (!data.containsKey("entries")) { |
| 90 | data.put("entries", ImmutableList.of()); |
| 91 | } |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 92 | List<Map<String, Object>> branches = RefServlet.getBranchesSoyData(req, REF_LIMIT); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 93 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 94 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 100 | } |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 101 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 106 | } |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 107 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 108 | private static <T> List<T> trim(List<T> list) { |
| 109 | return list.size() > REF_LIMIT ? list.subList(0, REF_LIMIT) : list; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 110 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 111 | } |