| 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 | |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 17 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT; |
| 19 | |
| 20 | import com.google.common.annotations.VisibleForTesting; |
| Dave Borowitz | 7326989 | 2013-11-13 14:23:50 -0800 | [diff] [blame] | 21 | import com.google.common.base.Strings; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
| 23 | import com.google.common.collect.Maps; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 24 | import com.google.gitiles.PathServlet.FileType; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 25 | import com.google.gitiles.doc.MarkdownConfig; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 26 | import java.io.IOException; |
| 27 | import java.util.List; |
| 28 | import java.util.Map; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | import org.eclipse.jgit.errors.MissingObjectException; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 30 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 31 | import org.eclipse.jgit.lib.ObjectId; |
| Dave Borowitz | 7c0a833 | 2014-05-01 11:07:04 -0700 | [diff] [blame] | 32 | import org.eclipse.jgit.lib.ObjectReader; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 33 | import org.eclipse.jgit.revwalk.RevTree; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 34 | import org.eclipse.jgit.treewalk.TreeWalk; |
| 35 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 36 | /** Soy data converter for git trees. */ |
| 37 | public class TreeSoyData { |
| 38 | /** |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 39 | * Number of characters to display for a symlink target. Targets longer than this are abbreviated |
| 40 | * for display in a tree listing. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 41 | */ |
| 42 | private static final int MAX_SYMLINK_TARGET_LENGTH = 72; |
| 43 | |
| 44 | /** |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 45 | * Maximum number of bytes to load from a blob that claims to be a symlink. If the blob is larger |
| 46 | * than this byte limit it will be displayed as a binary file instead of as a symlink. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 47 | */ |
| 48 | static final int MAX_SYMLINK_SIZE = 16 << 10; |
| 49 | |
| 50 | static String resolveTargetUrl(GitilesView view, String target) { |
| Dave Borowitz | cfc1c53 | 2015-02-18 13:41:19 -0800 | [diff] [blame] | 51 | String resolved = PathUtil.simplifyPathUpToRoot(target, view.getPathPart()); |
| Dave Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 52 | if (resolved == null) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 53 | return null; |
| 54 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 55 | return GitilesView.path().copyFrom(view).setPathPart(resolved).toUrl(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @VisibleForTesting |
| 59 | static String getTargetDisplayName(String target) { |
| 60 | if (target.length() <= MAX_SYMLINK_TARGET_LENGTH) { |
| 61 | return target; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 62 | } |
| David Pursehouse | b3b630f | 2016-06-15 21:51:18 +0900 | [diff] [blame] | 63 | int lastSlash = target.lastIndexOf('/'); |
| 64 | // TODO(dborowitz): Doesn't abbreviate a long last path component. |
| 65 | return lastSlash >= 0 ? "..." + target.substring(lastSlash) : target; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| Dave Borowitz | 7c0a833 | 2014-05-01 11:07:04 -0700 | [diff] [blame] | 68 | private final ObjectReader reader; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 69 | private final GitilesView view; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 70 | private final Config cfg; |
| 71 | private final RevTree rootTree; |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 72 | private final String requestUri; |
| Dave Borowitz | c782ebe | 2013-11-11 11:43:29 -0800 | [diff] [blame] | 73 | private ArchiveFormat archiveFormat; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 74 | |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 75 | public TreeSoyData( |
| 76 | ObjectReader reader, GitilesView view, Config cfg, RevTree rootTree, String requestUri) { |
| Dave Borowitz | 7c0a833 | 2014-05-01 11:07:04 -0700 | [diff] [blame] | 77 | this.reader = reader; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 78 | this.view = view; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 79 | this.cfg = cfg; |
| 80 | this.rootTree = rootTree; |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 81 | this.requestUri = requestUri; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| Dave Borowitz | c782ebe | 2013-11-11 11:43:29 -0800 | [diff] [blame] | 84 | public TreeSoyData setArchiveFormat(ArchiveFormat archiveFormat) { |
| 85 | this.archiveFormat = archiveFormat; |
| 86 | return this; |
| 87 | } |
| 88 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 89 | public Map<String, Object> toSoyData(ObjectId treeId, TreeWalk tw) |
| 90 | throws MissingObjectException, IOException { |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 91 | ReadmeHelper readme = |
| 92 | new ReadmeHelper(reader, view, MarkdownConfig.get(cfg), rootTree, requestUri); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 93 | List<Object> entries = Lists.newArrayList(); |
| 94 | GitilesView.Builder urlBuilder = GitilesView.path().copyFrom(view); |
| 95 | while (tw.next()) { |
| 96 | FileType type = FileType.forEntry(tw); |
| 97 | String name = tw.getNameString(); |
| 98 | |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 99 | GitilesView.Type viewType = view.getType(); |
| 100 | if (viewType == GitilesView.Type.PATH) { |
| 101 | urlBuilder.setPathPart(view.getPathPart() + "/" + name); |
| 102 | } else if (viewType == GitilesView.Type.REVISION) { |
| 103 | // Got here from a tag pointing at a tree. |
| 104 | urlBuilder.setPathPart(name); |
| 105 | } else { |
| 106 | throw new IllegalStateException( |
| 107 | String.format("Cannot render TreeSoyData from %s view", viewType)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | String url = urlBuilder.toUrl(); |
| 111 | if (type == FileType.TREE) { |
| 112 | name += "/"; |
| 113 | url += "/"; |
| 114 | } |
| 115 | Map<String, String> entry = Maps.newHashMapWithExpectedSize(4); |
| 116 | entry.put("type", type.toString()); |
| 117 | entry.put("name", name); |
| 118 | entry.put("url", url); |
| 119 | if (type == FileType.SYMLINK) { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 120 | String target = new String(reader.open(tw.getObjectId(0)).getCachedBytes(), UTF_8); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 121 | entry.put("targetName", getTargetDisplayName(target)); |
| 122 | String targetUrl = resolveTargetUrl(view, target); |
| 123 | if (targetUrl != null) { |
| 124 | entry.put("targetUrl", targetUrl); |
| 125 | } |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 126 | } else { |
| 127 | readme.considerEntry(tw); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 128 | } |
| 129 | entries.add(entry); |
| 130 | } |
| 131 | |
| 132 | Map<String, Object> data = Maps.newHashMapWithExpectedSize(3); |
| 133 | data.put("sha", treeId.name()); |
| 134 | data.put("entries", entries); |
| 135 | |
| 136 | if (view.getType() == GitilesView.Type.PATH |
| 137 | && view.getRevision().getPeeledType() == OBJ_COMMIT) { |
| 138 | data.put("logUrl", GitilesView.log().copyFrom(view).toUrl()); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 139 | data.put( |
| 140 | "archiveUrl", |
| 141 | GitilesView.archive() |
| 142 | .copyFrom(view) |
| 143 | .setPathPart(Strings.emptyToNull(view.getPathPart())) |
| 144 | .setExtension(archiveFormat.getDefaultSuffix()) |
| 145 | .toUrl()); |
| Dave Borowitz | c782ebe | 2013-11-11 11:43:29 -0800 | [diff] [blame] | 146 | data.put("archiveType", archiveFormat.getShortName()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 149 | if (readme.isPresent()) { |
| 150 | data.put("readmePath", readme.getPath()); |
| 151 | data.put("readmeHtml", readme.render()); |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 154 | return data; |
| 155 | } |
| 156 | |
| 157 | public Map<String, Object> toSoyData(ObjectId treeId) throws MissingObjectException, IOException { |
| Dave Borowitz | 7c0a833 | 2014-05-01 11:07:04 -0700 | [diff] [blame] | 158 | TreeWalk tw = new TreeWalk(reader); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 159 | tw.addTree(treeId); |
| 160 | tw.setRecursive(false); |
| 161 | return toSoyData(treeId, tw); |
| 162 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 163 | } |