| 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 | |
| David Pursehouse | 55a49f4 | 2018-07-03 14:10:11 +0900 | [diff] [blame] | 17 | import static com.google.common.base.Preconditions.checkArgument; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| David Pursehouse | 55a49f4 | 2018-07-03 14:10:11 +0900 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 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; |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 27 | import com.google.gitiles.GitilesRequestFailureException.FailureReason; |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 28 | import com.google.gson.reflect.TypeToken; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 29 | import java.io.IOException; |
| 30 | import java.io.Writer; |
| 31 | import java.util.Collection; |
| 32 | import java.util.LinkedHashMap; |
| 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | import javax.annotation.Nullable; |
| 36 | import javax.servlet.http.HttpServletRequest; |
| 37 | import javax.servlet.http.HttpServletResponse; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 38 | import org.eclipse.jgit.http.server.ServletUtils; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 39 | import org.eclipse.jgit.lib.AnyObjectId; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 40 | import org.eclipse.jgit.lib.Constants; |
| 41 | import org.eclipse.jgit.lib.Ref; |
| 42 | import org.eclipse.jgit.lib.RefComparator; |
| 43 | import org.eclipse.jgit.lib.RefDatabase; |
| 44 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 45 | import org.eclipse.jgit.transport.RefAdvertiser; |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 46 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 47 | /** Serves an HTML page with all the refs in a repository. */ |
| 48 | public class RefServlet extends BaseServlet { |
| 49 | private static final long serialVersionUID = 1L; |
| 50 | |
| 51 | private final TimeCache timeCache; |
| 52 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 53 | protected RefServlet( |
| 54 | GitilesAccess.Factory accessFactory, Renderer renderer, TimeCache timeCache) { |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 55 | super(renderer, accessFactory); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 56 | this.timeCache = checkNotNull(timeCache, "timeCache"); |
| 57 | } |
| 58 | |
| 59 | @Override |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 60 | protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 61 | if (!ViewFilter.getView(req).getPathPart().isEmpty()) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 62 | throw new GitilesRequestFailureException(FailureReason.INCORECT_PARAMETER); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 63 | } |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 64 | List<Map<String, Object>> tags; |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 65 | try (RevWalk walk = new RevWalk(ServletUtils.getRepository(req))) { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 66 | tags = getTagsSoyData(req, timeCache, walk, 0); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 67 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 68 | renderHtml( |
| 69 | req, |
| 70 | res, |
| Sven Selberg | d99004c | 2022-01-31 10:24:08 +0100 | [diff] [blame] | 71 | "com.google.gitiles.templates.RefList.refsDetail", |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 72 | ImmutableMap.of("branches", getBranchesSoyData(req, 0), "tags", tags)); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 75 | @Override |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 76 | protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 77 | GitilesView view = ViewFilter.getView(req); |
| David Pursehouse | 55a49f4 | 2018-07-03 14:10:11 +0900 | [diff] [blame] | 78 | RefsResult refs = 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); |
| David Pursehouse | 55a49f4 | 2018-07-03 14:10:11 +0900 | [diff] [blame] | 81 | adv.send(refs.refs); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 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); |
| David Pursehouse | 55a49f4 | 2018-07-03 14:10:11 +0900 | [diff] [blame] | 88 | RefsResult refs = getRefs(ServletUtils.getRepository(req).getRefDatabase(), view.getPathPart()); |
| Dave Borowitz | 96a6f47 | 2014-11-04 16:38:20 -0800 | [diff] [blame] | 89 | Map<String, RefJsonData> jsonRefs = new LinkedHashMap<>(); |
| David Pursehouse | 55a49f4 | 2018-07-03 14:10:11 +0900 | [diff] [blame] | 90 | int prefixLen = refs.prefix.length(); |
| 91 | for (Ref ref : refs.refs) { |
| 92 | jsonRefs.put(ref.getName().substring(prefixLen), new RefJsonData(ref)); |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 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 { |
| David Pursehouse | 55a49f4 | 2018-07-03 14:10:11 +0900 | [diff] [blame] | 164 | checkArgument(prefix.endsWith("/"), "ref hierarchy prefix should end with /: %s", prefix); |
| 165 | Collection<Ref> refs = refdb.getRefsByPrefix(prefix); |
| 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()); |
| Ivan Frade | 92b8472 | 2019-02-12 14:03:10 -0800 | [diff] [blame] | 171 | Map<String, Object> value = Maps.newHashMapWithExpectedSize(3); |
| 172 | value.put( |
| 173 | "url", |
| 174 | GitilesView.revision() |
| 175 | .copyFrom(view) |
| 176 | .setRevision(Revision.unpeeled(ref.getName(), ref.getObjectId())) |
| 177 | .toUrl()); |
| 178 | value.put("name", name); |
| 179 | if (headLeaf != null) { |
| 180 | value.put("isHead", headLeaf.equals(ref)); |
| Dave Borowitz | e5fead0 | 2013-01-07 13:12:59 -0800 | [diff] [blame] | 181 | } |
| Ivan Frade | 92b8472 | 2019-02-12 14:03:10 -0800 | [diff] [blame] | 182 | result.add(value); |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 183 | } |
| Ivan Frade | 92b8472 | 2019-02-12 14:03:10 -0800 | [diff] [blame] | 184 | |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 185 | return result; |
| 186 | } |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 187 | |
| Dave Borowitz | ba9c118 | 2013-03-13 14:16:43 -0700 | [diff] [blame] | 188 | static String sanitizeRefForText(String refName) { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 189 | return refName.replace("&", "&").replace("<", "<").replace(">", ">"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| David Pursehouse | 55a49f4 | 2018-07-03 14:10:11 +0900 | [diff] [blame] | 192 | private static class RefsResult { |
| 193 | String prefix; |
| 194 | List<Ref> refs; |
| 195 | |
| 196 | RefsResult(String prefix, List<Ref> refs) { |
| 197 | this.prefix = prefix; |
| 198 | this.refs = refs; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private static RefsResult getRefs(RefDatabase refdb, String path) throws IOException { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 203 | path = GitilesView.maybeTrimLeadingAndTrailingSlash(path); |
| 204 | if (path.isEmpty()) { |
| David Pursehouse | 55a49f4 | 2018-07-03 14:10:11 +0900 | [diff] [blame] | 205 | return new RefsResult(path, refdb.getRefs()); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 206 | } |
| 207 | path = Constants.R_REFS + path; |
| Jonathan Nieder | cbe013f | 2016-05-26 15:28:12 -0700 | [diff] [blame] | 208 | Ref singleRef = refdb.exactRef(path); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 209 | if (singleRef != null) { |
| Alice Kober-Sotzek | 53794aa | 2018-11-07 14:29:47 +0100 | [diff] [blame] | 210 | return new RefsResult("", ImmutableList.of(singleRef)); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 211 | } |
| David Pursehouse | 55a49f4 | 2018-07-03 14:10:11 +0900 | [diff] [blame] | 212 | path = path + '/'; |
| 213 | return new RefsResult(path, refdb.getRefsByPrefix(path)); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | private static class TextRefAdvertiser extends RefAdvertiser { |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 217 | private final Writer writer; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 218 | |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 219 | private TextRefAdvertiser(Writer writer) { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 220 | this.writer = writer; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void advertiseId(AnyObjectId id, String refName) throws IOException { |
| 225 | super.advertiseId(id, sanitizeRefForText(refName)); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | protected void writeOne(CharSequence line) throws IOException { |
| Dave Borowitz | 673d198 | 2014-05-02 12:30:49 -0700 | [diff] [blame] | 230 | writer.append(line); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void end() throws IOException { |
| 235 | writer.close(); |
| 236 | } |
| 237 | } |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 238 | |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 239 | static class RefJsonData { |
| 240 | RefJsonData(Ref ref) { |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 241 | value = ref.getObjectId().getName(); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 242 | if (ref.getPeeledObjectId() != null) { |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 243 | peeled = ref.getPeeledObjectId().getName(); |
| 244 | } |
| 245 | if (ref.isSymbolic()) { |
| 246 | target = ref.getTarget().getName(); |
| 247 | } |
| 248 | } |
| 249 | |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 250 | String value; |
| 251 | String peeled; |
| 252 | String target; |
| Kevin Graney | 77dbea9 | 2014-07-08 14:07:40 -0400 | [diff] [blame] | 253 | } |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 254 | } |