| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [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 | import static com.google.common.base.Preconditions.checkState; |
| Dave Borowitz | d6184a6 | 2013-01-10 11:53:54 -0800 | [diff] [blame] | 19 | import static org.eclipse.jgit.diff.DiffEntry.ChangeType.COPY; |
| 20 | import static org.eclipse.jgit.diff.DiffEntry.ChangeType.DELETE; |
| 21 | import static org.eclipse.jgit.diff.DiffEntry.ChangeType.RENAME; |
| 22 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
| 24 | import com.google.common.collect.ImmutableMap; |
| 25 | import com.google.common.collect.ImmutableSet; |
| 26 | import com.google.common.collect.Lists; |
| 27 | import com.google.common.collect.Maps; |
| 28 | import com.google.template.soy.data.restricted.NullData; |
| 29 | |
| 30 | import org.eclipse.jgit.diff.DiffEntry; |
| 31 | import org.eclipse.jgit.diff.DiffEntry.ChangeType; |
| 32 | import org.eclipse.jgit.diff.DiffFormatter; |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 33 | import org.eclipse.jgit.http.server.ServletUtils; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 34 | import org.eclipse.jgit.lib.AnyObjectId; |
| 35 | import org.eclipse.jgit.lib.Constants; |
| 36 | import org.eclipse.jgit.lib.ObjectId; |
| 37 | import org.eclipse.jgit.lib.ObjectReader; |
| 38 | import org.eclipse.jgit.lib.PersonIdent; |
| 39 | import org.eclipse.jgit.lib.Ref; |
| 40 | import org.eclipse.jgit.lib.Repository; |
| 41 | import org.eclipse.jgit.revwalk.RevCommit; |
| 42 | import org.eclipse.jgit.revwalk.RevWalk; |
| 43 | import org.eclipse.jgit.treewalk.AbstractTreeIterator; |
| 44 | import org.eclipse.jgit.treewalk.CanonicalTreeParser; |
| 45 | import org.eclipse.jgit.treewalk.EmptyTreeIterator; |
| 46 | import org.eclipse.jgit.util.GitDateFormatter; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 47 | import org.eclipse.jgit.util.RelativeDateFormatter; |
| 48 | import org.eclipse.jgit.util.io.NullOutputStream; |
| 49 | |
| 50 | import java.io.IOException; |
| 51 | import java.util.Collections; |
| 52 | import java.util.Comparator; |
| 53 | import java.util.List; |
| 54 | import java.util.Map; |
| 55 | import java.util.Set; |
| 56 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 57 | import javax.servlet.http.HttpServletRequest; |
| 58 | |
| 59 | /** Soy data converter for git commits. */ |
| 60 | public class CommitSoyData { |
| 61 | /** Valid sets of keys to include in Soy data for commits. */ |
| 62 | public static enum KeySet { |
| Dave Borowitz | c222cce | 2013-06-19 10:47:06 -0700 | [diff] [blame] | 63 | DETAIL("author", "committer", "sha", "tree", "treeUrl", "parents", "message", "logUrl", |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 64 | "archiveUrl", "archiveType"), |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 65 | DETAIL_DIFF_TREE(DETAIL, "diffTree"), |
| 66 | SHORTLOG("abbrevSha", "url", "shortMessage", "author", "branches", "tags"), |
| 67 | DEFAULT(DETAIL); |
| 68 | |
| 69 | private final Set<String> keys; |
| 70 | |
| 71 | private KeySet(String... keys) { |
| 72 | this.keys = ImmutableSet.copyOf(keys); |
| 73 | } |
| 74 | |
| 75 | private KeySet(KeySet other, String... keys) { |
| 76 | this.keys = ImmutableSet.<String> builder().addAll(other.keys).add(keys).build(); |
| 77 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 78 | |
| 79 | private boolean contains(String key) { |
| 80 | return keys.contains(key); |
| 81 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 84 | private Linkifier linkifier; |
| 85 | private Repository repo; |
| 86 | private RevWalk walk; |
| 87 | private GitilesView view; |
| 88 | private Map<AnyObjectId, Set<Ref>> refsById; |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 89 | private ArchiveFormat archiveFormat; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 90 | |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 91 | public CommitSoyData setLinkifier(Linkifier linkifier) { |
| 92 | this.linkifier = checkNotNull(linkifier, "linkifier"); |
| 93 | return this; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 96 | public CommitSoyData setRevWalk(RevWalk walk) { |
| 97 | this.walk = checkNotNull(walk, "walk"); |
| 98 | return this; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 101 | public CommitSoyData setArchiveFormat(ArchiveFormat archiveFormat) { |
| 102 | this.archiveFormat = checkNotNull(archiveFormat, "archiveFormat"); |
| 103 | return this; |
| 104 | } |
| 105 | |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 106 | public Map<String, Object> toSoyData(HttpServletRequest req, RevCommit commit, KeySet ks, |
| 107 | GitDateFormatter df) throws IOException { |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 108 | checkKeys(ks); |
| Dave Borowitz | 3809d4f | 2013-08-13 10:21:53 -0700 | [diff] [blame] | 109 | checkNotNull(req, "request"); |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 110 | repo = ServletUtils.getRepository(req); |
| 111 | view = ViewFilter.getView(req); |
| 112 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 113 | Map<String, Object> data = Maps.newHashMapWithExpectedSize(KeySet.DEFAULT.keys.size()); |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 114 | if (ks.contains("author")) { |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 115 | data.put("author", toSoyData(commit.getAuthorIdent(), df)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 116 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 117 | if (ks.contains("committer")) { |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 118 | data.put("committer", toSoyData(commit.getCommitterIdent(), df)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 119 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 120 | if (ks.contains("sha")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 121 | data.put("sha", ObjectId.toString(commit)); |
| 122 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 123 | if (ks.contains("abbrevSha")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 124 | ObjectReader reader = repo.getObjectDatabase().newReader(); |
| 125 | try { |
| 126 | data.put("abbrevSha", reader.abbreviate(commit).name()); |
| 127 | } finally { |
| 128 | reader.release(); |
| 129 | } |
| 130 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 131 | if (ks.contains("url")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 132 | data.put("url", GitilesView.revision() |
| 133 | .copyFrom(view) |
| 134 | .setRevision(commit) |
| 135 | .toUrl()); |
| 136 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 137 | if (ks.contains("logUrl")) { |
| Dave Borowitz | c222cce | 2013-06-19 10:47:06 -0700 | [diff] [blame] | 138 | data.put("logUrl", urlFromView(view, commit, GitilesView.log())); |
| 139 | } |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 140 | if (ks.contains("archiveUrl")) { |
| 141 | data.put("archiveUrl", urlFromView(view, commit, |
| 142 | GitilesView.archive().setExtension(archiveFormat.getDefaultSuffix()))); |
| 143 | } |
| 144 | if (ks.contains("archiveType")) { |
| 145 | data.put("archiveType", archiveFormat.getShortName()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 146 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 147 | if (ks.contains("tree")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 148 | data.put("tree", ObjectId.toString(commit.getTree())); |
| 149 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 150 | if (ks.contains("treeUrl")) { |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 151 | data.put("treeUrl", GitilesView.path().copyFrom(view).setPathPart("/").toUrl()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 152 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 153 | if (ks.contains("parents")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 154 | data.put("parents", toSoyData(view, commit.getParents())); |
| 155 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 156 | if (ks.contains("shortMessage")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 157 | data.put("shortMessage", commit.getShortMessage()); |
| 158 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 159 | if (ks.contains("branches")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 160 | data.put("branches", getRefsById(commit, Constants.R_HEADS)); |
| 161 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 162 | if (ks.contains("tags")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 163 | data.put("tags", getRefsById(commit, Constants.R_TAGS)); |
| 164 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 165 | if (ks.contains("message")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 166 | if (linkifier != null) { |
| 167 | data.put("message", linkifier.linkify(req, commit.getFullMessage())); |
| 168 | } else { |
| 169 | data.put("message", commit.getFullMessage()); |
| 170 | } |
| 171 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 172 | if (ks.contains("diffTree")) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 173 | data.put("diffTree", computeDiffTree(commit)); |
| 174 | } |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 175 | checkState(ks.keys.size() == data.size(), "bad commit data keys: %s != %s", ks.keys, |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 176 | data.keySet()); |
| 177 | return ImmutableMap.copyOf(data); |
| 178 | } |
| 179 | |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 180 | public Map<String, Object> toSoyData(HttpServletRequest req, RevCommit commit, |
| 181 | GitDateFormatter df) throws IOException { |
| 182 | return toSoyData(req, commit, KeySet.DEFAULT, df); |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | private void checkKeys(KeySet ks) { |
| 186 | checkState(!ks.contains("diffTree") || walk != null, "RevWalk required for diffTree"); |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 187 | if (ks.contains("archiveUrl") || ks.contains("archiveType")) { |
| 188 | checkState(archiveFormat != null, "archive format required"); |
| 189 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // TODO(dborowitz): Extract this. |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 193 | static Map<String, String> toSoyData(PersonIdent ident, GitDateFormatter df) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 194 | return ImmutableMap.of( |
| 195 | "name", ident.getName(), |
| 196 | "email", ident.getEmailAddress(), |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 197 | "time", df.formatDate(ident), |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 198 | // TODO(dborowitz): Switch from relative to absolute at some threshold. |
| 199 | "relativeTime", RelativeDateFormatter.format(ident.getWhen())); |
| 200 | } |
| 201 | |
| 202 | private List<Map<String, String>> toSoyData(GitilesView view, RevCommit[] parents) { |
| 203 | List<Map<String, String>> result = Lists.newArrayListWithCapacity(parents.length); |
| 204 | int i = 1; |
| 205 | // TODO(dborowitz): Render something slightly different when we're actively |
| 206 | // viewing a diff against one of the parents. |
| 207 | for (RevCommit parent : parents) { |
| 208 | String name = parent.name(); |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 209 | GitilesView.Builder diff = GitilesView.diff().copyFrom(view).setPathPart(""); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 210 | String parentName; |
| 211 | if (parents.length == 1) { |
| 212 | parentName = view.getRevision().getName() + "^"; |
| 213 | } else { |
| 214 | parentName = view.getRevision().getName() + "^" + (i++); |
| 215 | } |
| 216 | result.add(ImmutableMap.of( |
| 217 | "sha", name, |
| 218 | "url", GitilesView.revision() |
| 219 | .copyFrom(view) |
| 220 | .setRevision(parentName, parent) |
| 221 | .toUrl(), |
| 222 | "diffUrl", diff.setOldRevision(parentName, parent).toUrl())); |
| 223 | } |
| 224 | return result; |
| 225 | } |
| 226 | |
| 227 | private AbstractTreeIterator getTreeIterator(RevWalk walk, RevCommit commit) throws IOException { |
| 228 | CanonicalTreeParser p = new CanonicalTreeParser(); |
| 229 | p.reset(walk.getObjectReader(), walk.parseTree(walk.parseCommit(commit).getTree())); |
| 230 | return p; |
| 231 | } |
| 232 | |
| 233 | private Object computeDiffTree(RevCommit commit) throws IOException { |
| 234 | AbstractTreeIterator oldTree; |
| 235 | GitilesView.Builder diffUrl = GitilesView.diff().copyFrom(view) |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 236 | .setPathPart(""); |
| Dave Borowitz | d6184a6 | 2013-01-10 11:53:54 -0800 | [diff] [blame] | 237 | Revision oldRevision; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 238 | switch (commit.getParentCount()) { |
| 239 | case 0: |
| 240 | oldTree = new EmptyTreeIterator(); |
| Dave Borowitz | d6184a6 | 2013-01-10 11:53:54 -0800 | [diff] [blame] | 241 | oldRevision = Revision.NULL; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 242 | break; |
| 243 | case 1: |
| 244 | oldTree = getTreeIterator(walk, commit.getParent(0)); |
| Dave Borowitz | d6184a6 | 2013-01-10 11:53:54 -0800 | [diff] [blame] | 245 | oldRevision = Revision.peeled(view.getRevision().getName() + "^", commit.getParent(0)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 246 | break; |
| 247 | default: |
| 248 | // TODO(dborowitz): handle merges |
| 249 | return NullData.INSTANCE; |
| 250 | } |
| Dave Borowitz | d6184a6 | 2013-01-10 11:53:54 -0800 | [diff] [blame] | 251 | diffUrl.setOldRevision(oldRevision); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 252 | AbstractTreeIterator newTree = getTreeIterator(walk, commit); |
| 253 | |
| 254 | DiffFormatter diff = new DiffFormatter(NullOutputStream.INSTANCE); |
| 255 | try { |
| 256 | diff.setRepository(repo); |
| 257 | diff.setDetectRenames(true); |
| 258 | |
| 259 | List<Object> result = Lists.newArrayList(); |
| 260 | for (DiffEntry e : diff.scan(oldTree, newTree)) { |
| 261 | Map<String, Object> entry = Maps.newHashMapWithExpectedSize(5); |
| Dave Borowitz | d6184a6 | 2013-01-10 11:53:54 -0800 | [diff] [blame] | 262 | ChangeType type = e.getChangeType(); |
| 263 | if (type != DELETE) { |
| 264 | entry.put("path", e.getNewPath()); |
| 265 | entry.put("url", GitilesView.path() |
| 266 | .copyFrom(view) |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 267 | .setPathPart(e.getNewPath()) |
| Dave Borowitz | d6184a6 | 2013-01-10 11:53:54 -0800 | [diff] [blame] | 268 | .toUrl()); |
| 269 | } else { |
| 270 | entry.put("path", e.getOldPath()); |
| 271 | entry.put("url", GitilesView.path() |
| 272 | .copyFrom(view) |
| 273 | .setRevision(oldRevision) |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 274 | .setPathPart(e.getOldPath()) |
| Dave Borowitz | d6184a6 | 2013-01-10 11:53:54 -0800 | [diff] [blame] | 275 | .toUrl()); |
| 276 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 277 | entry.put("diffUrl", diffUrl.setAnchor("F" + result.size()).toUrl()); |
| 278 | entry.put("changeType", e.getChangeType().toString()); |
| Dave Borowitz | d6184a6 | 2013-01-10 11:53:54 -0800 | [diff] [blame] | 279 | if (type == COPY || type == RENAME) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 280 | entry.put("oldPath", e.getOldPath()); |
| 281 | } |
| 282 | result.add(entry); |
| 283 | } |
| 284 | return result; |
| 285 | } finally { |
| 286 | diff.release(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private static final Comparator<Map<String, String>> NAME_COMPARATOR = |
| 291 | new Comparator<Map<String, String>>() { |
| 292 | @Override |
| 293 | public int compare(Map<String, String> o1, Map<String, String> o2) { |
| 294 | return o1.get("name").compareTo(o2.get("name")); |
| 295 | } |
| 296 | }; |
| 297 | |
| 298 | private List<Map<String, String>> getRefsById(ObjectId id, String prefix) { |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 299 | if (refsById == null) { |
| 300 | refsById = repo.getAllRefsByPeeledObjectId(); |
| 301 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 302 | Set<Ref> refs = refsById.get(id); |
| 303 | if (refs == null) { |
| 304 | return ImmutableList.of(); |
| 305 | } |
| 306 | List<Map<String, String>> result = Lists.newArrayListWithCapacity(refs.size()); |
| 307 | for (Ref ref : refs) { |
| 308 | if (ref.getName().startsWith(prefix)) { |
| 309 | result.add(ImmutableMap.of( |
| 310 | "name", ref.getName().substring(prefix.length()), |
| 311 | "url", GitilesView.revision() |
| 312 | .copyFrom(view) |
| 313 | .setRevision(Revision.unpeeled(ref.getName(), ref.getObjectId())) |
| 314 | .toUrl())); |
| 315 | } |
| 316 | } |
| 317 | Collections.sort(result, NAME_COMPARATOR); |
| 318 | return result; |
| 319 | } |
| Dave Borowitz | c222cce | 2013-06-19 10:47:06 -0700 | [diff] [blame] | 320 | |
| 321 | private String urlFromView(GitilesView view, RevCommit commit, GitilesView.Builder builder) { |
| 322 | Revision rev = view.getRevision(); |
| 323 | return builder.copyFrom(view) |
| 324 | .setRevision(rev.getId().equals(commit) ? rev.getName() : commit.name(), commit) |
| 325 | .setPathPart(null) |
| 326 | .toUrl(); |
| 327 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 328 | } |