| 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 | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 25 | import com.google.gitiles.doc.GitilesMarkdown; |
| 26 | import com.google.gitiles.doc.ImageLoader; |
| 27 | import com.google.gitiles.doc.MarkdownToHtml; |
| 28 | import com.google.template.soy.data.SanitizedContent; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 30 | import org.eclipse.jgit.errors.LargeObjectException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 31 | import org.eclipse.jgit.errors.MissingObjectException; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 32 | import org.eclipse.jgit.lib.Config; |
| 33 | import org.eclipse.jgit.lib.Constants; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 34 | import org.eclipse.jgit.lib.ObjectId; |
| Dave Borowitz | 7c0a833 | 2014-05-01 11:07:04 -0700 | [diff] [blame] | 35 | import org.eclipse.jgit.lib.ObjectReader; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 36 | import org.eclipse.jgit.revwalk.RevTree; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 37 | import org.eclipse.jgit.treewalk.TreeWalk; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 38 | import org.eclipse.jgit.util.RawParseUtils; |
| 39 | import org.pegdown.ast.RootNode; |
| 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 42 | |
| 43 | import java.io.IOException; |
| 44 | import java.util.List; |
| 45 | import java.util.Map; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | |
| 47 | /** Soy data converter for git trees. */ |
| 48 | public class TreeSoyData { |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 49 | private static final Logger log = LoggerFactory.getLogger(TreeSoyData.class); |
| 50 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Number of characters to display for a symlink target. Targets longer than |
| 53 | * this are abbreviated for display in a tree listing. |
| 54 | */ |
| 55 | private static final int MAX_SYMLINK_TARGET_LENGTH = 72; |
| 56 | |
| 57 | /** |
| 58 | * Maximum number of bytes to load from a blob that claims to be a symlink. If |
| 59 | * the blob is larger than this byte limit it will be displayed as a binary |
| 60 | * file instead of as a symlink. |
| 61 | */ |
| 62 | static final int MAX_SYMLINK_SIZE = 16 << 10; |
| 63 | |
| 64 | static String resolveTargetUrl(GitilesView view, String target) { |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 65 | String resolved = Paths.simplifyPathUpToRoot(target, view.getPathPart()); |
| Dave Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 66 | if (resolved == null) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 67 | return null; |
| 68 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 69 | return GitilesView.path() |
| 70 | .copyFrom(view) |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 71 | .setPathPart(resolved) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 72 | .toUrl(); |
| 73 | } |
| 74 | |
| 75 | @VisibleForTesting |
| 76 | static String getTargetDisplayName(String target) { |
| 77 | if (target.length() <= MAX_SYMLINK_TARGET_LENGTH) { |
| 78 | return target; |
| 79 | } else { |
| 80 | int lastSlash = target.lastIndexOf('/'); |
| 81 | // TODO(dborowitz): Doesn't abbreviate a long last path component. |
| 82 | return lastSlash >= 0 ? "..." + target.substring(lastSlash) : target; |
| 83 | } |
| 84 | } |
| 85 | |
| Dave Borowitz | 7c0a833 | 2014-05-01 11:07:04 -0700 | [diff] [blame] | 86 | private final ObjectReader reader; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 87 | private final GitilesView view; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 88 | private final Config cfg; |
| 89 | private final RevTree rootTree; |
| Dave Borowitz | c782ebe | 2013-11-11 11:43:29 -0800 | [diff] [blame] | 90 | private ArchiveFormat archiveFormat; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 91 | |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 92 | public TreeSoyData(ObjectReader reader, GitilesView view, Config cfg, |
| 93 | RevTree rootTree) { |
| Dave Borowitz | 7c0a833 | 2014-05-01 11:07:04 -0700 | [diff] [blame] | 94 | this.reader = reader; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 95 | this.view = view; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 96 | this.cfg = cfg; |
| 97 | this.rootTree = rootTree; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| Dave Borowitz | c782ebe | 2013-11-11 11:43:29 -0800 | [diff] [blame] | 100 | public TreeSoyData setArchiveFormat(ArchiveFormat archiveFormat) { |
| 101 | this.archiveFormat = archiveFormat; |
| 102 | return this; |
| 103 | } |
| 104 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 105 | public Map<String, Object> toSoyData(ObjectId treeId, TreeWalk tw) throws MissingObjectException, |
| 106 | IOException { |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 107 | String readmePath = null; |
| 108 | ObjectId readmeId = null; |
| 109 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 110 | List<Object> entries = Lists.newArrayList(); |
| 111 | GitilesView.Builder urlBuilder = GitilesView.path().copyFrom(view); |
| 112 | while (tw.next()) { |
| 113 | FileType type = FileType.forEntry(tw); |
| 114 | String name = tw.getNameString(); |
| 115 | |
| 116 | switch (view.getType()) { |
| 117 | case PATH: |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 118 | urlBuilder.setPathPart(view.getPathPart() + "/" + name); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 119 | break; |
| 120 | case REVISION: |
| 121 | // Got here from a tag pointing at a tree. |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 122 | urlBuilder.setPathPart(name); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 123 | break; |
| 124 | default: |
| 125 | throw new IllegalStateException(String.format( |
| 126 | "Cannot render TreeSoyData from %s view", view.getType())); |
| 127 | } |
| 128 | |
| 129 | String url = urlBuilder.toUrl(); |
| 130 | if (type == FileType.TREE) { |
| 131 | name += "/"; |
| 132 | url += "/"; |
| 133 | } |
| 134 | Map<String, String> entry = Maps.newHashMapWithExpectedSize(4); |
| 135 | entry.put("type", type.toString()); |
| 136 | entry.put("name", name); |
| 137 | entry.put("url", url); |
| 138 | if (type == FileType.SYMLINK) { |
| 139 | String target = new String( |
| Dave Borowitz | 7c0a833 | 2014-05-01 11:07:04 -0700 | [diff] [blame] | 140 | reader.open(tw.getObjectId(0)).getCachedBytes(), |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 141 | UTF_8); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 142 | entry.put("targetName", getTargetDisplayName(target)); |
| 143 | String targetUrl = resolveTargetUrl(view, target); |
| 144 | if (targetUrl != null) { |
| 145 | entry.put("targetUrl", targetUrl); |
| 146 | } |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 147 | } else if (isReadmeFile(name) && type == FileType.REGULAR_FILE) { |
| 148 | readmePath = tw.getPathString(); |
| 149 | readmeId = tw.getObjectId(0); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 150 | } |
| 151 | entries.add(entry); |
| 152 | } |
| 153 | |
| 154 | Map<String, Object> data = Maps.newHashMapWithExpectedSize(3); |
| 155 | data.put("sha", treeId.name()); |
| 156 | data.put("entries", entries); |
| 157 | |
| 158 | if (view.getType() == GitilesView.Type.PATH |
| 159 | && view.getRevision().getPeeledType() == OBJ_COMMIT) { |
| 160 | data.put("logUrl", GitilesView.log().copyFrom(view).toUrl()); |
| Dave Borowitz | c782ebe | 2013-11-11 11:43:29 -0800 | [diff] [blame] | 161 | data.put("archiveUrl", GitilesView.archive() |
| 162 | .copyFrom(view) |
| Dave Borowitz | 7326989 | 2013-11-13 14:23:50 -0800 | [diff] [blame] | 163 | .setPathPart(Strings.emptyToNull(view.getPathPart())) |
| Dave Borowitz | c782ebe | 2013-11-11 11:43:29 -0800 | [diff] [blame] | 164 | .setExtension(archiveFormat.getDefaultSuffix()) |
| 165 | .toUrl()); |
| 166 | data.put("archiveType", archiveFormat.getShortName()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 169 | if (readmeId != null && cfg.getBoolean("markdown", "render", true)) { |
| 170 | data.put("readmePath", readmePath); |
| 171 | data.put("readmeHtml", render(readmePath, readmeId)); |
| 172 | } |
| 173 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 174 | return data; |
| 175 | } |
| 176 | |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 177 | /** True if the file is the default markdown file to render in tree view. */ |
| 178 | private static boolean isReadmeFile(String name) { |
| 179 | return name.equalsIgnoreCase("README.md"); |
| 180 | } |
| 181 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 182 | public Map<String, Object> toSoyData(ObjectId treeId) throws MissingObjectException, IOException { |
| Dave Borowitz | 7c0a833 | 2014-05-01 11:07:04 -0700 | [diff] [blame] | 183 | TreeWalk tw = new TreeWalk(reader); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 184 | tw.addTree(treeId); |
| 185 | tw.setRecursive(false); |
| 186 | return toSoyData(treeId, tw); |
| 187 | } |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame^] | 188 | |
| 189 | private SanitizedContent render(String path, ObjectId id) { |
| 190 | try { |
| 191 | int inputLimit = cfg.getInt("markdown", "inputLimit", 5 << 20); |
| 192 | byte[] raw = reader.open(id, Constants.OBJ_BLOB).getCachedBytes(inputLimit); |
| 193 | String md = RawParseUtils.decode(raw); |
| 194 | RootNode root = GitilesMarkdown.parseFile(view, path, md); |
| 195 | if (root == null) { |
| 196 | return null; |
| 197 | } |
| 198 | |
| 199 | int imageLimit = cfg.getInt("markdown", "imageLimit", 256 << 10); |
| 200 | ImageLoader img = null; |
| 201 | if (imageLimit > 0) { |
| 202 | img = new ImageLoader(reader, view, rootTree, path, imageLimit); |
| 203 | } |
| 204 | |
| 205 | return new MarkdownToHtml(view, cfg) |
| 206 | .setImageLoader(img) |
| 207 | .toSoyHtml(root); |
| 208 | } catch (LargeObjectException | IOException e) { |
| 209 | log.error(String.format("error rendering %s/%s/%s", |
| 210 | view.getRepositoryName(), view.getPathPart(), path), e); |
| 211 | return null; |
| 212 | } |
| 213 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 214 | } |