blob: 985edfa544b18fbefc19ccc2946a0c07648c9015 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// 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
15package com.google.gitiles;
16
David Pletcherd7bdaf32014-08-27 14:50:32 -070017import static java.nio.charset.StandardCharsets.UTF_8;
Dave Borowitz9de65952012-08-13 16:09:45 -070018import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
19
20import com.google.common.annotations.VisibleForTesting;
Dave Borowitz73269892013-11-13 14:23:50 -080021import com.google.common.base.Strings;
Dave Borowitz9de65952012-08-13 16:09:45 -070022import com.google.common.collect.Lists;
23import com.google.common.collect.Maps;
Dave Borowitz9de65952012-08-13 16:09:45 -070024import com.google.gitiles.PathServlet.FileType;
Shawn Pearce47fd6562016-05-28 14:15:15 -070025import com.google.gitiles.doc.MarkdownConfig;
Dave Borowitz3b744b12016-08-19 16:11:10 -040026import java.io.IOException;
27import java.util.List;
28import java.util.Map;
Dave Borowitz9de65952012-08-13 16:09:45 -070029import org.eclipse.jgit.errors.MissingObjectException;
Shawn Pearce73e34532015-02-12 16:27:54 -080030import org.eclipse.jgit.lib.Config;
Dave Borowitz9de65952012-08-13 16:09:45 -070031import org.eclipse.jgit.lib.ObjectId;
Dave Borowitz7c0a8332014-05-01 11:07:04 -070032import org.eclipse.jgit.lib.ObjectReader;
Shawn Pearce73e34532015-02-12 16:27:54 -080033import org.eclipse.jgit.revwalk.RevTree;
Dave Borowitz9de65952012-08-13 16:09:45 -070034import org.eclipse.jgit.treewalk.TreeWalk;
35
Dave Borowitz9de65952012-08-13 16:09:45 -070036/** Soy data converter for git trees. */
37public class TreeSoyData {
38 /**
Dave Borowitz40255d52016-08-19 16:16:22 -040039 * Number of characters to display for a symlink target. Targets longer than this are abbreviated
40 * for display in a tree listing.
Dave Borowitz9de65952012-08-13 16:09:45 -070041 */
42 private static final int MAX_SYMLINK_TARGET_LENGTH = 72;
43
Pontus Jaensson4c5c8d22021-11-10 12:46:16 +010044 private static final Map<String, Integer> TYPE_WEIGHT =
45 Map.of(
46 "TREE", 0,
47 "GITLINK", 1,
48 "SYMLINK", 2,
49 "REGULAR_FILE", 3,
50 "EXECUTABLE_FILE", 3);
51
Dave Borowitz9de65952012-08-13 16:09:45 -070052 /**
Dave Borowitz40255d52016-08-19 16:16:22 -040053 * Maximum number of bytes to load from a blob that claims to be a symlink. If the blob is larger
54 * than this byte limit it will be displayed as a binary file instead of as a symlink.
Dave Borowitz9de65952012-08-13 16:09:45 -070055 */
56 static final int MAX_SYMLINK_SIZE = 16 << 10;
57
58 static String resolveTargetUrl(GitilesView view, String target) {
Dave Borowitzcfc1c532015-02-18 13:41:19 -080059 String resolved = PathUtil.simplifyPathUpToRoot(target, view.getPathPart());
Dave Borowitzbcd753d2013-02-08 11:10:19 -080060 if (resolved == null) {
Dave Borowitz9de65952012-08-13 16:09:45 -070061 return null;
62 }
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020063 return GitilesView.path().copyFrom(view).setPathPart(resolved).toUrl();
Dave Borowitz9de65952012-08-13 16:09:45 -070064 }
65
66 @VisibleForTesting
67 static String getTargetDisplayName(String target) {
68 if (target.length() <= MAX_SYMLINK_TARGET_LENGTH) {
69 return target;
Dave Borowitz9de65952012-08-13 16:09:45 -070070 }
David Pursehouseb3b630f2016-06-15 21:51:18 +090071 int lastSlash = target.lastIndexOf('/');
72 // TODO(dborowitz): Doesn't abbreviate a long last path component.
73 return lastSlash >= 0 ? "..." + target.substring(lastSlash) : target;
Dave Borowitz9de65952012-08-13 16:09:45 -070074 }
75
Pontus Jaenssonf08cb4a2021-11-11 14:06:57 +010076 static String stripEndingSolidus(String p) {
77 return p.endsWith("/") ? p.substring(0, p.length() - 1) : p;
78 }
79
80 static int sortByTypeAlpha(Map<String, String> m1, Map<String, String> m2) {
81 int weightDiff = TYPE_WEIGHT.get(m1.get("type")).compareTo(TYPE_WEIGHT.get(m2.get("type")));
82 if (weightDiff == 0) {
83 String s1 = m1.get("name");
84 String s2 = m2.get("name");
85 if (m1.get("type").equals("TREE")) {
86 s1 = stripEndingSolidus(s1);
87 s2 = stripEndingSolidus(s2);
88 }
89 return s1.compareToIgnoreCase(s2);
90 }
91 return weightDiff;
Pontus Jaensson4c5c8d22021-11-10 12:46:16 +010092 }
93
Dave Borowitz7c0a8332014-05-01 11:07:04 -070094 private final ObjectReader reader;
Dave Borowitz9de65952012-08-13 16:09:45 -070095 private final GitilesView view;
Shawn Pearce73e34532015-02-12 16:27:54 -080096 private final Config cfg;
97 private final RevTree rootTree;
Shawn Pearcec68ad0b2016-05-28 16:52:47 -070098 private final String requestUri;
Dave Borowitzc782ebe2013-11-11 11:43:29 -080099 private ArchiveFormat archiveFormat;
Dave Borowitz9de65952012-08-13 16:09:45 -0700100
Shawn Pearcec68ad0b2016-05-28 16:52:47 -0700101 public TreeSoyData(
102 ObjectReader reader, GitilesView view, Config cfg, RevTree rootTree, String requestUri) {
Dave Borowitz7c0a8332014-05-01 11:07:04 -0700103 this.reader = reader;
Dave Borowitz9de65952012-08-13 16:09:45 -0700104 this.view = view;
Shawn Pearce73e34532015-02-12 16:27:54 -0800105 this.cfg = cfg;
106 this.rootTree = rootTree;
Shawn Pearcec68ad0b2016-05-28 16:52:47 -0700107 this.requestUri = requestUri;
Dave Borowitz9de65952012-08-13 16:09:45 -0700108 }
109
Dave Borowitzc782ebe2013-11-11 11:43:29 -0800110 public TreeSoyData setArchiveFormat(ArchiveFormat archiveFormat) {
111 this.archiveFormat = archiveFormat;
112 return this;
113 }
114
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200115 public Map<String, Object> toSoyData(ObjectId treeId, TreeWalk tw)
116 throws MissingObjectException, IOException {
Shawn Pearcec68ad0b2016-05-28 16:52:47 -0700117 ReadmeHelper readme =
118 new ReadmeHelper(reader, view, MarkdownConfig.get(cfg), rootTree, requestUri);
Pontus Jaensson4c5c8d22021-11-10 12:46:16 +0100119 List<Map<String, String>> entries = Lists.newArrayList();
Dave Borowitz9de65952012-08-13 16:09:45 -0700120 GitilesView.Builder urlBuilder = GitilesView.path().copyFrom(view);
121 while (tw.next()) {
122 FileType type = FileType.forEntry(tw);
123 String name = tw.getNameString();
124
David Pursehousecb91aaf2016-06-15 22:05:24 +0900125 GitilesView.Type viewType = view.getType();
126 if (viewType == GitilesView.Type.PATH) {
127 urlBuilder.setPathPart(view.getPathPart() + "/" + name);
128 } else if (viewType == GitilesView.Type.REVISION) {
129 // Got here from a tag pointing at a tree.
130 urlBuilder.setPathPart(name);
131 } else {
132 throw new IllegalStateException(
133 String.format("Cannot render TreeSoyData from %s view", viewType));
Dave Borowitz9de65952012-08-13 16:09:45 -0700134 }
135
136 String url = urlBuilder.toUrl();
137 if (type == FileType.TREE) {
138 name += "/";
139 url += "/";
140 }
141 Map<String, String> entry = Maps.newHashMapWithExpectedSize(4);
142 entry.put("type", type.toString());
143 entry.put("name", name);
144 entry.put("url", url);
145 if (type == FileType.SYMLINK) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200146 String target = new String(reader.open(tw.getObjectId(0)).getCachedBytes(), UTF_8);
Dave Borowitz9de65952012-08-13 16:09:45 -0700147 entry.put("targetName", getTargetDisplayName(target));
148 String targetUrl = resolveTargetUrl(view, target);
149 if (targetUrl != null) {
150 entry.put("targetUrl", targetUrl);
151 }
Shawn Pearce45e83752015-02-20 17:59:05 -0800152 } else {
153 readme.considerEntry(tw);
Dave Borowitz9de65952012-08-13 16:09:45 -0700154 }
155 entries.add(entry);
156 }
157
Pontus Jaenssonf08cb4a2021-11-11 14:06:57 +0100158 entries.sort(TreeSoyData::sortByTypeAlpha);
Pontus Jaensson4c5c8d22021-11-10 12:46:16 +0100159
Dave Borowitz9de65952012-08-13 16:09:45 -0700160 Map<String, Object> data = Maps.newHashMapWithExpectedSize(3);
161 data.put("sha", treeId.name());
162 data.put("entries", entries);
163
164 if (view.getType() == GitilesView.Type.PATH
165 && view.getRevision().getPeeledType() == OBJ_COMMIT) {
166 data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200167 data.put(
168 "archiveUrl",
169 GitilesView.archive()
170 .copyFrom(view)
171 .setPathPart(Strings.emptyToNull(view.getPathPart()))
172 .setExtension(archiveFormat.getDefaultSuffix())
173 .toUrl());
Dave Borowitzc782ebe2013-11-11 11:43:29 -0800174 data.put("archiveType", archiveFormat.getShortName());
Dave Borowitz9de65952012-08-13 16:09:45 -0700175 }
176
Shawn Pearce45e83752015-02-20 17:59:05 -0800177 if (readme.isPresent()) {
178 data.put("readmePath", readme.getPath());
179 data.put("readmeHtml", readme.render());
Shawn Pearce73e34532015-02-12 16:27:54 -0800180 }
181
Dave Borowitz9de65952012-08-13 16:09:45 -0700182 return data;
183 }
184
185 public Map<String, Object> toSoyData(ObjectId treeId) throws MissingObjectException, IOException {
Dave Borowitz7c0a8332014-05-01 11:07:04 -0700186 TreeWalk tw = new TreeWalk(reader);
Dave Borowitz9de65952012-08-13 16:09:45 -0700187 tw.addTree(treeId);
188 tw.setRecursive(false);
189 return toSoyData(treeId, tw);
190 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700191}