| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [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.base.Function; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import com.google.common.collect.Lists; |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Ordering; |
| 24 | import com.google.common.util.concurrent.UncheckedExecutionException; |
| 25 | |
| 26 | import org.eclipse.jgit.http.server.ServletUtils; |
| 27 | import org.eclipse.jgit.lib.Constants; |
| 28 | import org.eclipse.jgit.lib.Ref; |
| 29 | import org.eclipse.jgit.lib.RefComparator; |
| 30 | import org.eclipse.jgit.lib.RefDatabase; |
| 31 | import org.eclipse.jgit.revwalk.RevWalk; |
| 32 | |
| 33 | import java.io.IOException; |
| 34 | import java.util.Collection; |
| 35 | import java.util.List; |
| 36 | import java.util.Map; |
| 37 | |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 38 | import javax.annotation.Nullable; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 39 | import javax.servlet.http.HttpServletRequest; |
| 40 | import javax.servlet.http.HttpServletResponse; |
| 41 | |
| 42 | /** Serves an HTML page with all the refs in a repository. */ |
| 43 | public class RefServlet extends BaseServlet { |
| 44 | private static final long serialVersionUID = 1L; |
| 45 | |
| 46 | private final TimeCache timeCache; |
| 47 | |
| 48 | protected RefServlet(Renderer renderer, TimeCache timeCache) { |
| 49 | super(renderer); |
| 50 | this.timeCache = checkNotNull(timeCache, "timeCache"); |
| 51 | } |
| 52 | |
| 53 | @Override |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 54 | protected void doGet(HttpServletRequest req, HttpServletResponse res) |
| 55 | throws IOException { |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 56 | RevWalk walk = new RevWalk(ServletUtils.getRepository(req)); |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 57 | List<Map<String, Object>> tags; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 58 | try { |
| 59 | tags = getTags(req, timeCache, walk, 0); |
| 60 | } finally { |
| 61 | walk.release(); |
| 62 | } |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 63 | render(req, res, "gitiles.refsDetail", |
| 64 | ImmutableMap.of("branches", getBranches(req, 0), "tags", tags)); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 67 | static List<Map<String, Object>> getBranches(HttpServletRequest req, int limit) |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 68 | throws IOException { |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 69 | RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase(); |
| 70 | Ref head = refdb.getRef(Constants.HEAD); |
| 71 | Ref headLeaf = head != null && head.isSymbolic() ? head.getLeaf() : null; |
| 72 | return getRefs( |
| 73 | refdb, |
| 74 | ViewFilter.getView(req), |
| 75 | Constants.R_HEADS, |
| 76 | branchComparator(headLeaf), |
| 77 | headLeaf, |
| 78 | limit); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 81 | private static Ordering<Ref> branchComparator(Ref headLeaf) { |
| 82 | if (headLeaf == null) { |
| 83 | return Ordering.from(RefComparator.INSTANCE); |
| 84 | } |
| 85 | final String headLeafName = headLeaf.getName(); |
| 86 | return new Ordering<Ref>() { |
| 87 | @Override |
| 88 | public int compare(@Nullable Ref left, @Nullable Ref right) { |
| 89 | int l = isHead(left) ? 1 : 0; |
| 90 | int r = isHead(right) ? 1 : 0; |
| Dave Borowitz | 5d11e2d | 2013-01-08 10:03:58 -0800 | [diff] [blame^] | 91 | return r - l; |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | private final boolean isHead(Ref ref) { |
| 95 | return ref != null && ref.getName().equals(headLeafName); |
| 96 | } |
| 97 | }.compound(RefComparator.INSTANCE); |
| 98 | } |
| 99 | |
| 100 | static List<Map<String, Object>> getTags(HttpServletRequest req, |
| 101 | TimeCache timeCache, RevWalk walk, int limit) throws IOException { |
| 102 | return getRefs( |
| 103 | ServletUtils.getRepository(req).getRefDatabase(), |
| 104 | ViewFilter.getView(req), |
| 105 | Constants.R_TAGS, |
| 106 | tagComparator(timeCache, walk), |
| 107 | null, |
| 108 | limit); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | private static Ordering<Ref> tagComparator(final TimeCache timeCache, final RevWalk walk) { |
| 112 | return Ordering.natural().onResultOf(new Function<Ref, Long>() { |
| 113 | @Override |
| 114 | public Long apply(Ref ref) { |
| 115 | try { |
| 116 | return timeCache.getTime(walk, ref.getObjectId()); |
| 117 | } catch (IOException e) { |
| 118 | throw new UncheckedExecutionException(e); |
| 119 | } |
| 120 | } |
| 121 | }).reverse().compound(RefComparator.INSTANCE); |
| 122 | } |
| 123 | |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 124 | private static List<Map<String, Object>> getRefs( |
| 125 | RefDatabase refdb, |
| 126 | GitilesView view, |
| 127 | String prefix, |
| 128 | Ordering<Ref> ordering, |
| 129 | @Nullable Ref headLeaf, |
| 130 | int limit) throws IOException { |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 131 | Collection<Ref> refs = refdb.getRefs(prefix).values(); |
| 132 | refs = ordering.leastOf(refs, limit > 0 ? limit + 1 : refs.size()); |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 133 | List<Map<String, Object>> result = Lists.newArrayListWithCapacity(refs.size()); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 134 | |
| 135 | for (Ref ref : refs) { |
| 136 | String name = ref.getName().substring(prefix.length()); |
| 137 | boolean needPrefix = !ref.getName().equals(refdb.getRef(name).getName()); |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 138 | Map<String, Object> value = Maps.newHashMapWithExpectedSize(3); |
| 139 | value.put("url", GitilesView.revision().copyFrom(view).setRevision( |
| 140 | Revision.unpeeled(needPrefix ? ref.getName() : name, ref.getObjectId())).toUrl()); |
| 141 | value.put("name", name); |
| 142 | if (headLeaf != null) { |
| 143 | value.put("isHead", headLeaf.equals(ref)); |
| 144 | } |
| 145 | result.add(value); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 146 | } |
| 147 | return result; |
| 148 | } |
| 149 | } |