| 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; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 18 | import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 19 | |
| 20 | import com.google.common.base.Function; |
| 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.Lists; |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Maps; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 24 | import com.google.common.collect.Ordering; |
| 25 | import com.google.common.util.concurrent.UncheckedExecutionException; |
| 26 | |
| 27 | import org.eclipse.jgit.http.server.ServletUtils; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 28 | import org.eclipse.jgit.lib.AnyObjectId; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 29 | import org.eclipse.jgit.lib.Constants; |
| 30 | import org.eclipse.jgit.lib.Ref; |
| 31 | import org.eclipse.jgit.lib.RefComparator; |
| 32 | import org.eclipse.jgit.lib.RefDatabase; |
| 33 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 34 | import org.eclipse.jgit.transport.RefAdvertiser; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 35 | |
| 36 | import java.io.IOException; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 37 | import java.io.PrintWriter; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 38 | import java.util.Collection; |
| 39 | import java.util.List; |
| 40 | import java.util.Map; |
| 41 | |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 42 | import javax.annotation.Nullable; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 43 | import javax.servlet.http.HttpServletRequest; |
| 44 | import javax.servlet.http.HttpServletResponse; |
| 45 | |
| 46 | /** Serves an HTML page with all the refs in a repository. */ |
| 47 | public class RefServlet extends BaseServlet { |
| 48 | private static final long serialVersionUID = 1L; |
| 49 | |
| 50 | private final TimeCache timeCache; |
| 51 | |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 52 | protected RefServlet(GitilesAccess.Factory accessFactory, Renderer renderer, |
| 53 | TimeCache timeCache) { |
| 54 | super(renderer, accessFactory); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 55 | this.timeCache = checkNotNull(timeCache, "timeCache"); |
| 56 | } |
| 57 | |
| 58 | @Override |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 59 | protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 60 | throws IOException { |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 61 | if (!ViewFilter.getView(req).getPathPart().isEmpty()) { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 62 | res.setStatus(SC_NOT_FOUND); |
| 63 | return; |
| 64 | } |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 65 | RevWalk walk = new RevWalk(ServletUtils.getRepository(req)); |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 66 | List<Map<String, Object>> tags; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 67 | try { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 68 | tags = getTagsSoyData(req, timeCache, walk, 0); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 69 | } finally { |
| 70 | walk.release(); |
| 71 | } |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 72 | renderHtml(req, res, "gitiles.refsDetail", |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 73 | ImmutableMap.of("branches", getBranchesSoyData(req, 0), "tags", tags)); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 76 | @Override |
| 77 | protected void doGetText(HttpServletRequest req, HttpServletResponse res) |
| 78 | throws IOException { |
| 79 | GitilesView view = ViewFilter.getView(req); |
| 80 | Map<String, Ref> refs = getRefs(ServletUtils.getRepository(req).getRefDatabase(), |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 81 | view.getPathPart()); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 82 | TextRefAdvertiser adv = new TextRefAdvertiser(startRenderText(req, res)); |
| 83 | adv.setDerefTags(true); |
| 84 | adv.send(refs); |
| 85 | adv.end(); |
| 86 | } |
| 87 | |
| 88 | static List<Map<String, Object>> getBranchesSoyData(HttpServletRequest req, int limit) |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 89 | throws IOException { |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 90 | RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase(); |
| 91 | Ref head = refdb.getRef(Constants.HEAD); |
| 92 | Ref headLeaf = head != null && head.isSymbolic() ? head.getLeaf() : null; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 93 | return getRefsSoyData( |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 94 | refdb, |
| 95 | ViewFilter.getView(req), |
| 96 | Constants.R_HEADS, |
| 97 | branchComparator(headLeaf), |
| 98 | headLeaf, |
| 99 | limit); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 102 | private static Ordering<Ref> branchComparator(Ref headLeaf) { |
| 103 | if (headLeaf == null) { |
| 104 | return Ordering.from(RefComparator.INSTANCE); |
| 105 | } |
| 106 | final String headLeafName = headLeaf.getName(); |
| 107 | return new Ordering<Ref>() { |
| 108 | @Override |
| 109 | public int compare(@Nullable Ref left, @Nullable Ref right) { |
| 110 | int l = isHead(left) ? 1 : 0; |
| 111 | int r = isHead(right) ? 1 : 0; |
| Dave Borowitz | 5d11e2d | 2013-01-08 10:03:58 -0800 | [diff] [blame] | 112 | return r - l; |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private final boolean isHead(Ref ref) { |
| 116 | return ref != null && ref.getName().equals(headLeafName); |
| 117 | } |
| 118 | }.compound(RefComparator.INSTANCE); |
| 119 | } |
| 120 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 121 | static List<Map<String, Object>> getTagsSoyData(HttpServletRequest req, |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 122 | TimeCache timeCache, RevWalk walk, int limit) throws IOException { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 123 | return getRefsSoyData( |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 124 | ServletUtils.getRepository(req).getRefDatabase(), |
| 125 | ViewFilter.getView(req), |
| 126 | Constants.R_TAGS, |
| 127 | tagComparator(timeCache, walk), |
| 128 | null, |
| 129 | limit); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private static Ordering<Ref> tagComparator(final TimeCache timeCache, final RevWalk walk) { |
| 133 | return Ordering.natural().onResultOf(new Function<Ref, Long>() { |
| 134 | @Override |
| 135 | public Long apply(Ref ref) { |
| 136 | try { |
| 137 | return timeCache.getTime(walk, ref.getObjectId()); |
| 138 | } catch (IOException e) { |
| 139 | throw new UncheckedExecutionException(e); |
| 140 | } |
| 141 | } |
| 142 | }).reverse().compound(RefComparator.INSTANCE); |
| 143 | } |
| 144 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 145 | private static List<Map<String, Object>> getRefsSoyData( |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 146 | RefDatabase refdb, |
| 147 | GitilesView view, |
| 148 | String prefix, |
| 149 | Ordering<Ref> ordering, |
| 150 | @Nullable Ref headLeaf, |
| 151 | int limit) throws IOException { |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 152 | Collection<Ref> refs = refdb.getRefs(prefix).values(); |
| 153 | refs = ordering.leastOf(refs, limit > 0 ? limit + 1 : refs.size()); |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 154 | List<Map<String, Object>> result = Lists.newArrayListWithCapacity(refs.size()); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 155 | |
| 156 | for (Ref ref : refs) { |
| 157 | String name = ref.getName().substring(prefix.length()); |
| 158 | boolean needPrefix = !ref.getName().equals(refdb.getRef(name).getName()); |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 159 | Map<String, Object> value = Maps.newHashMapWithExpectedSize(3); |
| 160 | value.put("url", GitilesView.revision().copyFrom(view).setRevision( |
| 161 | Revision.unpeeled(needPrefix ? ref.getName() : name, ref.getObjectId())).toUrl()); |
| 162 | value.put("name", name); |
| 163 | if (headLeaf != null) { |
| 164 | value.put("isHead", headLeaf.equals(ref)); |
| 165 | } |
| 166 | result.add(value); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 167 | } |
| 168 | return result; |
| 169 | } |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 170 | |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 171 | static String sanitizeRefForText(String refName) { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 172 | return refName.replace("&", "&") |
| 173 | .replace("<", "<") |
| 174 | .replace(">", ">"); |
| 175 | } |
| 176 | |
| 177 | private static Map<String, Ref> getRefs(RefDatabase refdb, String path) throws IOException { |
| 178 | path = GitilesView.maybeTrimLeadingAndTrailingSlash(path); |
| 179 | if (path.isEmpty()) { |
| 180 | return refdb.getRefs(RefDatabase.ALL); |
| 181 | } |
| 182 | path = Constants.R_REFS + path; |
| 183 | Ref singleRef = refdb.getRef(path); |
| 184 | if (singleRef != null) { |
| 185 | return ImmutableMap.of(singleRef.getName(), singleRef); |
| 186 | } |
| 187 | return refdb.getRefs(path + '/'); |
| 188 | } |
| 189 | |
| 190 | private static class TextRefAdvertiser extends RefAdvertiser { |
| 191 | private final PrintWriter writer; |
| 192 | |
| 193 | private TextRefAdvertiser(PrintWriter writer) { |
| 194 | this.writer = writer; |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void advertiseId(AnyObjectId id, String refName) throws IOException { |
| 199 | super.advertiseId(id, sanitizeRefForText(refName)); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | protected void writeOne(CharSequence line) throws IOException { |
| 204 | writer.print(line); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void end() throws IOException { |
| 209 | writer.close(); |
| 210 | } |
| 211 | } |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 212 | } |