| 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; |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 25 | import com.google.common.primitives.Ints; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 26 | import com.google.common.util.concurrent.UncheckedExecutionException; |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 27 | import com.google.gson.reflect.TypeToken; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 28 | |
| 29 | import org.eclipse.jgit.http.server.ServletUtils; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 30 | import org.eclipse.jgit.lib.AnyObjectId; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 31 | import org.eclipse.jgit.lib.Constants; |
| 32 | import org.eclipse.jgit.lib.Ref; |
| 33 | import org.eclipse.jgit.lib.RefComparator; |
| 34 | import org.eclipse.jgit.lib.RefDatabase; |
| 35 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 36 | import org.eclipse.jgit.transport.RefAdvertiser; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 37 | |
| 38 | import java.io.IOException; |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 39 | import java.io.Writer; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 40 | import java.util.Collection; |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 41 | import java.util.LinkedHashMap; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 42 | import java.util.List; |
| 43 | import java.util.Map; |
| 44 | |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 45 | import javax.annotation.Nullable; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 46 | import javax.servlet.http.HttpServletRequest; |
| 47 | import javax.servlet.http.HttpServletResponse; |
| 48 | |
| 49 | /** Serves an HTML page with all the refs in a repository. */ |
| 50 | public class RefServlet extends BaseServlet { |
| 51 | private static final long serialVersionUID = 1L; |
| 52 | |
| 53 | private final TimeCache timeCache; |
| 54 | |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 55 | protected RefServlet(GitilesAccess.Factory accessFactory, Renderer renderer, |
| 56 | TimeCache timeCache) { |
| 57 | super(renderer, accessFactory); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 58 | this.timeCache = checkNotNull(timeCache, "timeCache"); |
| 59 | } |
| 60 | |
| 61 | @Override |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 62 | protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 63 | throws IOException { |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 64 | if (!ViewFilter.getView(req).getPathPart().isEmpty()) { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 65 | res.setStatus(SC_NOT_FOUND); |
| 66 | return; |
| 67 | } |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 68 | List<Map<String, Object>> tags; |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 69 | try (RevWalk walk = new RevWalk(ServletUtils.getRepository(req))) { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 70 | tags = getTagsSoyData(req, timeCache, walk, 0); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 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 | |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 88 | @Override |
| 89 | protected void doGetJson(HttpServletRequest req, HttpServletResponse res) |
| 90 | throws IOException { |
| 91 | GitilesView view = ViewFilter.getView(req); |
| 92 | Map<String, Ref> refs = getRefs(ServletUtils.getRepository(req).getRefDatabase(), |
| 93 | view.getPathPart()); |
| Dave Borowitz | 96a6f47 | 2014-11-04 16:38:20 -0800 | [diff] [blame] | 94 | Map<String, RefJsonData> jsonRefs = new LinkedHashMap<>(); |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 95 | for (Map.Entry<String, Ref> ref : refs.entrySet()) { |
| 96 | jsonRefs.put(ref.getKey(), new RefJsonData(ref.getValue())); |
| 97 | } |
| 98 | renderJson(req, res, jsonRefs, new TypeToken<Map<String, RefJsonData>>() {}.getType()); |
| 99 | } |
| 100 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 101 | static List<Map<String, Object>> getBranchesSoyData(HttpServletRequest req, int limit) |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 102 | throws IOException { |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 103 | RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase(); |
| 104 | Ref head = refdb.getRef(Constants.HEAD); |
| 105 | Ref headLeaf = head != null && head.isSymbolic() ? head.getLeaf() : null; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 106 | return getRefsSoyData( |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 107 | refdb, |
| 108 | ViewFilter.getView(req), |
| 109 | Constants.R_HEADS, |
| 110 | branchComparator(headLeaf), |
| 111 | headLeaf, |
| 112 | limit); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 115 | private static Ordering<Ref> branchComparator(Ref headLeaf) { |
| 116 | if (headLeaf == null) { |
| 117 | return Ordering.from(RefComparator.INSTANCE); |
| 118 | } |
| 119 | final String headLeafName = headLeaf.getName(); |
| 120 | return new Ordering<Ref>() { |
| 121 | @Override |
| 122 | public int compare(@Nullable Ref left, @Nullable Ref right) { |
| 123 | int l = isHead(left) ? 1 : 0; |
| 124 | int r = isHead(right) ? 1 : 0; |
| Dave Borowitz | 5d11e2d | 2013-01-08 10:03:58 -0800 | [diff] [blame] | 125 | return r - l; |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private final boolean isHead(Ref ref) { |
| 129 | return ref != null && ref.getName().equals(headLeafName); |
| 130 | } |
| 131 | }.compound(RefComparator.INSTANCE); |
| 132 | } |
| 133 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 134 | static List<Map<String, Object>> getTagsSoyData(HttpServletRequest req, |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 135 | TimeCache timeCache, RevWalk walk, int limit) throws IOException { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 136 | return getRefsSoyData( |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 137 | ServletUtils.getRepository(req).getRefDatabase(), |
| 138 | ViewFilter.getView(req), |
| 139 | Constants.R_TAGS, |
| 140 | tagComparator(timeCache, walk), |
| 141 | null, |
| 142 | limit); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private static Ordering<Ref> tagComparator(final TimeCache timeCache, final RevWalk walk) { |
| 146 | return Ordering.natural().onResultOf(new Function<Ref, Long>() { |
| 147 | @Override |
| 148 | public Long apply(Ref ref) { |
| 149 | try { |
| 150 | return timeCache.getTime(walk, ref.getObjectId()); |
| 151 | } catch (IOException e) { |
| 152 | throw new UncheckedExecutionException(e); |
| 153 | } |
| 154 | } |
| 155 | }).reverse().compound(RefComparator.INSTANCE); |
| 156 | } |
| 157 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 158 | private static List<Map<String, Object>> getRefsSoyData( |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 159 | RefDatabase refdb, |
| 160 | GitilesView view, |
| 161 | String prefix, |
| 162 | Ordering<Ref> ordering, |
| 163 | @Nullable Ref headLeaf, |
| 164 | int limit) throws IOException { |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 165 | Collection<Ref> refs = refdb.getRefs(prefix).values(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 166 | refs = ordering.leastOf(refs, limit > 0 ? Ints.saturatedCast(limit + 1L) : refs.size()); |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 167 | List<Map<String, Object>> result = Lists.newArrayListWithCapacity(refs.size()); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 168 | |
| 169 | for (Ref ref : refs) { |
| 170 | String name = ref.getName().substring(prefix.length()); |
| 171 | boolean needPrefix = !ref.getName().equals(refdb.getRef(name).getName()); |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 172 | Map<String, Object> value = Maps.newHashMapWithExpectedSize(3); |
| 173 | value.put("url", GitilesView.revision().copyFrom(view).setRevision( |
| 174 | Revision.unpeeled(needPrefix ? ref.getName() : name, ref.getObjectId())).toUrl()); |
| 175 | value.put("name", name); |
| 176 | if (headLeaf != null) { |
| 177 | value.put("isHead", headLeaf.equals(ref)); |
| 178 | } |
| 179 | result.add(value); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 180 | } |
| 181 | return result; |
| 182 | } |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 183 | |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 184 | static String sanitizeRefForText(String refName) { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 185 | return refName.replace("&", "&") |
| 186 | .replace("<", "<") |
| 187 | .replace(">", ">"); |
| 188 | } |
| 189 | |
| 190 | private static Map<String, Ref> getRefs(RefDatabase refdb, String path) throws IOException { |
| 191 | path = GitilesView.maybeTrimLeadingAndTrailingSlash(path); |
| 192 | if (path.isEmpty()) { |
| 193 | return refdb.getRefs(RefDatabase.ALL); |
| 194 | } |
| 195 | path = Constants.R_REFS + path; |
| 196 | Ref singleRef = refdb.getRef(path); |
| 197 | if (singleRef != null) { |
| 198 | return ImmutableMap.of(singleRef.getName(), singleRef); |
| 199 | } |
| 200 | return refdb.getRefs(path + '/'); |
| 201 | } |
| 202 | |
| 203 | private static class TextRefAdvertiser extends RefAdvertiser { |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 204 | private final Writer writer; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 205 | |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 206 | private TextRefAdvertiser(Writer writer) { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 207 | this.writer = writer; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void advertiseId(AnyObjectId id, String refName) throws IOException { |
| 212 | super.advertiseId(id, sanitizeRefForText(refName)); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | protected void writeOne(CharSequence line) throws IOException { |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 217 | writer.append(line); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void end() throws IOException { |
| 222 | writer.close(); |
| 223 | } |
| 224 | } |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 225 | |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 226 | static class RefJsonData { |
| 227 | RefJsonData(Ref ref) { |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 228 | value = ref.getObjectId().getName(); |
| 229 | if(ref.getPeeledObjectId() != null) { |
| 230 | peeled = ref.getPeeledObjectId().getName(); |
| 231 | } |
| 232 | if (ref.isSymbolic()) { |
| 233 | target = ref.getTarget().getName(); |
| 234 | } |
| 235 | } |
| 236 | |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 237 | String value; |
| 238 | String peeled; |
| 239 | String target; |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 240 | } |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 241 | } |