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