| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 1 | // Copyright (C) 2014 The Android Open Source Project |
| 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 | 3b4baab | 2016-10-04 09:44:10 -0400 | [diff] [blame] | 19 | import static java.util.Comparator.comparing; |
| 20 | import static java.util.stream.Collectors.toList; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 21 | |
| Dave Borowitz | 3b4baab | 2016-10-04 09:44:10 -0400 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Iterables; |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Sets; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 26 | import java.io.IOException; |
| 27 | import java.util.Arrays; |
| 28 | import java.util.List; |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | import javax.annotation.Nullable; |
| 32 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 33 | import org.eclipse.jgit.diff.DiffEntry; |
| 34 | import org.eclipse.jgit.diff.DiffFormatter; |
| 35 | import org.eclipse.jgit.http.server.ServletUtils; |
| 36 | import org.eclipse.jgit.lib.AbbreviatedObjectId; |
| 37 | import org.eclipse.jgit.lib.AnyObjectId; |
| 38 | import org.eclipse.jgit.lib.Constants; |
| 39 | import org.eclipse.jgit.lib.ObjectId; |
| 40 | import org.eclipse.jgit.lib.ObjectReader; |
| 41 | import org.eclipse.jgit.lib.PersonIdent; |
| 42 | import org.eclipse.jgit.lib.Ref; |
| 43 | import org.eclipse.jgit.lib.Repository; |
| Pontus Jaensson | c144de9 | 2021-10-29 11:13:23 +0200 | [diff] [blame] | 44 | import org.eclipse.jgit.notes.NoteMap; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 45 | import org.eclipse.jgit.revwalk.RevCommit; |
| 46 | import org.eclipse.jgit.revwalk.RevWalk; |
| 47 | import org.eclipse.jgit.treewalk.AbstractTreeIterator; |
| 48 | import org.eclipse.jgit.treewalk.CanonicalTreeParser; |
| 49 | import org.eclipse.jgit.treewalk.EmptyTreeIterator; |
| 50 | import org.eclipse.jgit.util.io.NullOutputStream; |
| 51 | |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 52 | /** Format-independent data about a single commit. */ |
| 53 | class CommitData { |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 54 | enum Field { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 55 | ABBREV_SHA, |
| 56 | ARCHIVE_TYPE, |
| 57 | ARCHIVE_URL, |
| 58 | AUTHOR, |
| 59 | BRANCHES, |
| 60 | COMMITTER, |
| 61 | DIFF_TREE, |
| 62 | LOG_URL, |
| 63 | MESSAGE, |
| Pontus Jaensson | c144de9 | 2021-10-29 11:13:23 +0200 | [diff] [blame] | 64 | NOTES, |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 65 | PARENTS, |
| Dave Borowitz | f03fcea | 2014-04-21 17:20:33 -0700 | [diff] [blame] | 66 | PARENT_BLAME_URL, |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 67 | SHA, |
| 68 | SHORT_MESSAGE, |
| 69 | TAGS, |
| 70 | TREE, |
| 71 | TREE_URL, |
| 72 | URL; |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 73 | |
| 74 | static ImmutableSet<Field> setOf(Iterable<Field> base, Field... fields) { |
| 75 | return Sets.immutableEnumSet(Iterables.concat(base, Arrays.asList(fields))); |
| 76 | } |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static class DiffList { |
| Dave Borowitz | 2298cef | 2014-09-23 10:29:46 -0700 | [diff] [blame] | 80 | Revision revision; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 81 | Revision oldRevision; |
| 82 | List<DiffEntry> entries; |
| 83 | } |
| 84 | |
| 85 | static class Builder { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 86 | private ArchiveFormat archiveFormat; |
| 87 | private Map<AnyObjectId, Set<Ref>> refsById; |
| Pontus Jaensson | c144de9 | 2021-10-29 11:13:23 +0200 | [diff] [blame] | 88 | private static final int MAX_NOTE_SIZE = 524288; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 89 | |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 90 | Builder setArchiveFormat(@Nullable ArchiveFormat archiveFormat) { |
| 91 | this.archiveFormat = archiveFormat; |
| 92 | return this; |
| 93 | } |
| 94 | |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 95 | CommitData build(HttpServletRequest req, RevWalk walk, RevCommit c, Set<Field> fs) |
| 96 | throws IOException { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 97 | checkFields(fs); |
| 98 | checkNotNull(req, "request"); |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 99 | checkNotNull(walk, "walk"); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 100 | Repository repo = ServletUtils.getRepository(req); |
| 101 | GitilesView view = ViewFilter.getView(req); |
| 102 | |
| 103 | CommitData result = new CommitData(); |
| 104 | |
| 105 | if (fs.contains(Field.AUTHOR)) { |
| Jonathan Nieder | 4b6753d | 2019-03-07 14:25:13 -0800 | [diff] [blame] | 106 | walk.parseBody(c); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 107 | result.author = c.getAuthorIdent(); |
| 108 | } |
| 109 | if (fs.contains(Field.COMMITTER)) { |
| Jonathan Nieder | 4b6753d | 2019-03-07 14:25:13 -0800 | [diff] [blame] | 110 | walk.parseBody(c); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 111 | result.committer = c.getCommitterIdent(); |
| 112 | } |
| 113 | if (fs.contains(Field.SHA)) { |
| 114 | result.sha = c.copy(); |
| 115 | } |
| 116 | if (fs.contains(Field.ABBREV_SHA)) { |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 117 | try (ObjectReader reader = repo.getObjectDatabase().newReader()) { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 118 | result.abbrev = reader.abbreviate(c); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | if (fs.contains(Field.URL)) { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 122 | result.url = GitilesView.revision().copyFrom(view).setRevision(c).toUrl(); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 123 | } |
| 124 | if (fs.contains(Field.LOG_URL)) { |
| 125 | result.logUrl = urlFromView(view, c, GitilesView.log()); |
| 126 | } |
| 127 | if (fs.contains(Field.ARCHIVE_URL)) { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 128 | result.archiveUrl = |
| 129 | urlFromView( |
| 130 | view, c, GitilesView.archive().setExtension(archiveFormat.getDefaultSuffix())); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 131 | } |
| 132 | if (fs.contains(Field.ARCHIVE_TYPE)) { |
| 133 | result.archiveType = archiveFormat; |
| 134 | } |
| 135 | if (fs.contains(Field.TREE)) { |
| 136 | result.tree = c.getTree().copy(); |
| 137 | } |
| 138 | if (fs.contains(Field.TREE_URL)) { |
| Dave Borowitz | 359ad6d | 2014-04-21 16:07:59 -0700 | [diff] [blame] | 139 | // Tree always implies the root tree. |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 140 | result.treeUrl = GitilesView.path().copyFrom(view).setPathPart("/").toUrl(); |
| 141 | } |
| 142 | if (fs.contains(Field.PARENTS)) { |
| 143 | result.parents = Arrays.asList(c.getParents()); |
| 144 | } |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 145 | if (fs.contains(Field.BRANCHES)) { |
| 146 | result.branches = getRefsById(repo, c, Constants.R_HEADS); |
| 147 | } |
| 148 | if (fs.contains(Field.TAGS)) { |
| 149 | result.tags = getRefsById(repo, c, Constants.R_TAGS); |
| 150 | } |
| Pontus Jaensson | c144de9 | 2021-10-29 11:13:23 +0200 | [diff] [blame] | 151 | if (fs.contains(Field.NOTES)) { |
| 152 | Ref notesRef = repo.getRefDatabase().exactRef(Constants.R_NOTES_COMMITS); |
| 153 | if (notesRef != null) { |
| 154 | try { |
| 155 | byte[] data = |
| 156 | NoteMap.read(walk.getObjectReader(), walk.parseCommit(notesRef.getObjectId())) |
| 157 | .getCachedBytes(c, MAX_NOTE_SIZE); |
| 158 | result.notes = new String(data, "utf-8"); |
| 159 | } catch (Exception e) { |
| 160 | result.notes = ""; |
| 161 | } |
| 162 | } |
| 163 | } |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 164 | if (fs.contains(Field.MESSAGE)) { |
| Jonathan Nieder | 4b6753d | 2019-03-07 14:25:13 -0800 | [diff] [blame] | 165 | walk.parseBody(c); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 166 | result.message = c.getFullMessage(); |
| 167 | } |
| Shawn Pearce | 680ab12 | 2015-05-11 23:15:06 -0700 | [diff] [blame] | 168 | if (fs.contains(Field.SHORT_MESSAGE)) { |
| Jonathan Nieder | 4b6753d | 2019-03-07 14:25:13 -0800 | [diff] [blame] | 169 | walk.parseBody(c); |
| Shawn Pearce | 680ab12 | 2015-05-11 23:15:06 -0700 | [diff] [blame] | 170 | String msg = c.getShortMessage(); |
| 171 | if (msg.length() > 80) { |
| 172 | String ft = result.message; |
| 173 | if (ft == null) { |
| 174 | ft = c.getFullMessage(); |
| 175 | } |
| 176 | int lf = ft.indexOf('\n'); |
| 177 | if (lf > 0) { |
| 178 | msg = ft.substring(0, lf); |
| 179 | } |
| 180 | } |
| 181 | result.shortMessage = msg; |
| 182 | } |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 183 | if (fs.contains(Field.DIFF_TREE)) { |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 184 | result.diffEntries = computeDiffEntries(repo, view, walk, c); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 185 | } |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 186 | return result; |
| 187 | } |
| 188 | |
| 189 | private void checkFields(Set<Field> fs) { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 190 | if (fs.contains(Field.ARCHIVE_URL) || fs.contains(Field.ARCHIVE_TYPE)) { |
| 191 | checkState(archiveFormat != null, "archive format required"); |
| 192 | } |
| 193 | } |
| 194 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 195 | private static String urlFromView( |
| 196 | GitilesView view, RevCommit commit, GitilesView.Builder builder) { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 197 | Revision rev = view.getRevision(); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 198 | return builder |
| 199 | .copyFrom(view) |
| Dave Borowitz | 359ad6d | 2014-04-21 16:07:59 -0700 | [diff] [blame] | 200 | .setOldRevision(Revision.NULL) |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 201 | .setRevision(rev.getId().equals(commit) ? rev.getName() : commit.name(), commit) |
| Dave Borowitz | 359ad6d | 2014-04-21 16:07:59 -0700 | [diff] [blame] | 202 | .setPathPart(view.getPathPart()) |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 203 | .toUrl(); |
| 204 | } |
| 205 | |
| Jonathan Nieder | c383b26 | 2022-03-16 17:14:32 -0700 | [diff] [blame] | 206 | private List<Ref> getRefsById(Repository repo, ObjectId id, String prefix) throws IOException { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 207 | if (refsById == null) { |
| 208 | refsById = repo.getAllRefsByPeeledObjectId(); |
| 209 | } |
| Dave Borowitz | 3b4baab | 2016-10-04 09:44:10 -0400 | [diff] [blame] | 210 | Set<Ref> refs = refsById.get(id); |
| 211 | if (refs == null) { |
| 212 | return ImmutableList.of(); |
| 213 | } |
| 214 | return refs.stream() |
| David Pursehouse | 14293fe | 2016-10-04 15:55:22 +0900 | [diff] [blame] | 215 | .filter(r -> r.getName().startsWith(prefix)) |
| Dave Borowitz | 3b4baab | 2016-10-04 09:44:10 -0400 | [diff] [blame] | 216 | .sorted(comparing(Ref::getName)) |
| 217 | .collect(toList()); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 220 | private AbstractTreeIterator getTreeIterator(RevWalk walk, RevCommit commit) |
| 221 | throws IOException { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 222 | CanonicalTreeParser p = new CanonicalTreeParser(); |
| 223 | p.reset(walk.getObjectReader(), walk.parseTree(walk.parseCommit(commit).getTree())); |
| 224 | return p; |
| 225 | } |
| 226 | |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 227 | private DiffList computeDiffEntries( |
| 228 | Repository repo, GitilesView view, RevWalk walk, RevCommit commit) throws IOException { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 229 | DiffList result = new DiffList(); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 230 | result.revision = |
| 231 | view.getRevision().matches(commit) |
| 232 | ? view.getRevision() |
| 233 | : Revision.peeled(commit.name(), commit); |
| Dave Borowitz | 2298cef | 2014-09-23 10:29:46 -0700 | [diff] [blame] | 234 | |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 235 | AbstractTreeIterator oldTree; |
| 236 | switch (commit.getParentCount()) { |
| 237 | case 0: |
| 238 | result.oldRevision = Revision.NULL; |
| 239 | oldTree = new EmptyTreeIterator(); |
| 240 | break; |
| 241 | case 1: |
| Dave Borowitz | e360d5c | 2015-09-16 16:53:30 -0400 | [diff] [blame] | 242 | result.oldRevision = |
| 243 | Revision.peeled(result.revision.getName() + "^", commit.getParent(0)); |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 244 | oldTree = getTreeIterator(walk, commit.getParent(0)); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 245 | break; |
| 246 | default: |
| 247 | // TODO(dborowitz): handle merges |
| 248 | return result; |
| 249 | } |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 250 | AbstractTreeIterator newTree = getTreeIterator(walk, commit); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 251 | |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 252 | try (DiffFormatter diff = new DiffFormatter(NullOutputStream.INSTANCE)) { |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 253 | diff.setRepository(repo); |
| 254 | diff.setDetectRenames(true); |
| 255 | result.entries = diff.scan(oldTree, newTree); |
| 256 | return result; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | ObjectId sha; |
| 262 | PersonIdent author; |
| 263 | PersonIdent committer; |
| 264 | AbbreviatedObjectId abbrev; |
| 265 | ObjectId tree; |
| 266 | List<RevCommit> parents; |
| 267 | String shortMessage; |
| 268 | String message; |
| Pontus Jaensson | c144de9 | 2021-10-29 11:13:23 +0200 | [diff] [blame] | 269 | String notes; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 270 | |
| 271 | List<Ref> branches; |
| 272 | List<Ref> tags; |
| 273 | DiffList diffEntries; |
| 274 | |
| 275 | String url; |
| 276 | String logUrl; |
| 277 | String treeUrl; |
| 278 | String archiveUrl; |
| 279 | ArchiveFormat archiveType; |
| 280 | } |