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