| 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 | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 21 | import com.google.common.collect.Maps; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 22 | |
| 23 | import org.eclipse.jgit.http.server.ServletUtils; |
| 24 | import org.eclipse.jgit.lib.Constants; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 25 | import org.eclipse.jgit.lib.ObjectId; |
| 26 | import org.eclipse.jgit.lib.Repository; |
| 27 | import org.eclipse.jgit.revwalk.RevCommit; |
| 28 | import org.eclipse.jgit.revwalk.RevObject; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 29 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 30 | |
| 31 | import java.io.IOException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | |
| 35 | import javax.servlet.http.HttpServletRequest; |
| 36 | import javax.servlet.http.HttpServletResponse; |
| 37 | |
| 38 | /** Serves the index page for a repository, if accessed directly by a browser. */ |
| 39 | public class RepositoryIndexServlet extends BaseServlet { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 40 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 41 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 42 | static final int REF_LIMIT = 10; |
| 43 | private static final int LOG_LIMIT = 20; |
| 44 | |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 45 | private final GitilesAccess.Factory accessFactory; |
| 46 | private final TimeCache timeCache; |
| 47 | |
| 48 | public RepositoryIndexServlet(Renderer renderer, GitilesAccess.Factory accessFactory, |
| 49 | TimeCache timeCache) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 50 | super(renderer); |
| 51 | this.accessFactory = checkNotNull(accessFactory, "accessFactory"); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 52 | this.timeCache = checkNotNull(timeCache, "timeCache"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 53 | } |
| 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 Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 62 | GitilesView view = ViewFilter.getView(req); |
| 63 | Repository repo = ServletUtils.getRepository(req); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 64 | RepositoryDescription desc = accessFactory.forRequest(req).getRepositoryDescription(); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 65 | RevWalk walk = new RevWalk(repo); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 66 | List<Map<String, String>> tags; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 67 | Map<String, Object> data; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 68 | try { |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 69 | 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 Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 84 | } finally { |
| 85 | walk.release(); |
| 86 | } |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 87 | List<Map<String, String>> branches = RefServlet.getBranches(req, REF_LIMIT); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 88 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 89 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 95 | } |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 96 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 101 | } |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 102 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 103 | private static <T> List<T> trim(List<T> list) { |
| 104 | return list.size() > REF_LIMIT ? list.subList(0, REF_LIMIT) : list; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 105 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 106 | } |