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