blob: 43fddfbe0a6b7917d64198237c9bed602bce737c [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 Borowitz9de65952012-08-13 16:09:45 -070026
27import org.eclipse.jgit.errors.MissingObjectException;
Shawn Pearce73e34532015-02-12 16:27:54 -080028import org.eclipse.jgit.lib.Config;
Dave Borowitz9de65952012-08-13 16:09:45 -070029import org.eclipse.jgit.lib.ObjectId;
Dave Borowitz7c0a8332014-05-01 11:07:04 -070030import org.eclipse.jgit.lib.ObjectReader;
Shawn Pearce73e34532015-02-12 16:27:54 -080031import org.eclipse.jgit.revwalk.RevTree;
Dave Borowitz9de65952012-08-13 16:09:45 -070032import org.eclipse.jgit.treewalk.TreeWalk;
33
34import java.io.IOException;
35import java.util.List;
36import java.util.Map;
Dave Borowitz9de65952012-08-13 16:09:45 -070037
38/** Soy data converter for git trees. */
39public 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 Borowitzcfc1c532015-02-18 13:41:19 -080054 String resolved = PathUtil.simplifyPathUpToRoot(target, view.getPathPart());
Dave Borowitzbcd753d2013-02-08 11:10:19 -080055 if (resolved == null) {
Dave Borowitz9de65952012-08-13 16:09:45 -070056 return null;
57 }
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020058 return GitilesView.path().copyFrom(view).setPathPart(resolved).toUrl();
Dave Borowitz9de65952012-08-13 16:09:45 -070059 }
60
61 @VisibleForTesting
62 static String getTargetDisplayName(String target) {
63 if (target.length() <= MAX_SYMLINK_TARGET_LENGTH) {
64 return target;
65 } else {
66 int lastSlash = target.lastIndexOf('/');
67 // TODO(dborowitz): Doesn't abbreviate a long last path component.
68 return lastSlash >= 0 ? "..." + target.substring(lastSlash) : target;
69 }
70 }
71
Dave Borowitz7c0a8332014-05-01 11:07:04 -070072 private final ObjectReader reader;
Dave Borowitz9de65952012-08-13 16:09:45 -070073 private final GitilesView view;
Shawn Pearce73e34532015-02-12 16:27:54 -080074 private final Config cfg;
75 private final RevTree rootTree;
Shawn Pearcec68ad0b2016-05-28 16:52:47 -070076 private final String requestUri;
Dave Borowitzc782ebe2013-11-11 11:43:29 -080077 private ArchiveFormat archiveFormat;
Dave Borowitz9de65952012-08-13 16:09:45 -070078
Shawn Pearcec68ad0b2016-05-28 16:52:47 -070079 public TreeSoyData(
80 ObjectReader reader, GitilesView view, Config cfg, RevTree rootTree, String requestUri) {
Dave Borowitz7c0a8332014-05-01 11:07:04 -070081 this.reader = reader;
Dave Borowitz9de65952012-08-13 16:09:45 -070082 this.view = view;
Shawn Pearce73e34532015-02-12 16:27:54 -080083 this.cfg = cfg;
84 this.rootTree = rootTree;
Shawn Pearcec68ad0b2016-05-28 16:52:47 -070085 this.requestUri = requestUri;
Dave Borowitz9de65952012-08-13 16:09:45 -070086 }
87
Dave Borowitzc782ebe2013-11-11 11:43:29 -080088 public TreeSoyData setArchiveFormat(ArchiveFormat archiveFormat) {
89 this.archiveFormat = archiveFormat;
90 return this;
91 }
92
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020093 public Map<String, Object> toSoyData(ObjectId treeId, TreeWalk tw)
94 throws MissingObjectException, IOException {
Shawn Pearcec68ad0b2016-05-28 16:52:47 -070095 ReadmeHelper readme =
96 new ReadmeHelper(reader, view, MarkdownConfig.get(cfg), rootTree, requestUri);
Dave Borowitz9de65952012-08-13 16:09:45 -070097 List<Object> entries = Lists.newArrayList();
98 GitilesView.Builder urlBuilder = GitilesView.path().copyFrom(view);
99 while (tw.next()) {
100 FileType type = FileType.forEntry(tw);
101 String name = tw.getNameString();
102
103 switch (view.getType()) {
104 case PATH:
Dave Borowitzdd3c3d92013-03-11 16:38:41 -0700105 urlBuilder.setPathPart(view.getPathPart() + "/" + name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700106 break;
107 case REVISION:
108 // Got here from a tag pointing at a tree.
Dave Borowitzdd3c3d92013-03-11 16:38:41 -0700109 urlBuilder.setPathPart(name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700110 break;
111 default:
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200112 throw new IllegalStateException(
113 String.format("Cannot render TreeSoyData from %s view", view.getType()));
Dave Borowitz9de65952012-08-13 16:09:45 -0700114 }
115
116 String url = urlBuilder.toUrl();
117 if (type == FileType.TREE) {
118 name += "/";
119 url += "/";
120 }
121 Map<String, String> entry = Maps.newHashMapWithExpectedSize(4);
122 entry.put("type", type.toString());
123 entry.put("name", name);
124 entry.put("url", url);
125 if (type == FileType.SYMLINK) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200126 String target = new String(reader.open(tw.getObjectId(0)).getCachedBytes(), UTF_8);
Dave Borowitz9de65952012-08-13 16:09:45 -0700127 entry.put("targetName", getTargetDisplayName(target));
128 String targetUrl = resolveTargetUrl(view, target);
129 if (targetUrl != null) {
130 entry.put("targetUrl", targetUrl);
131 }
Shawn Pearce45e83752015-02-20 17:59:05 -0800132 } else {
133 readme.considerEntry(tw);
Dave Borowitz9de65952012-08-13 16:09:45 -0700134 }
135 entries.add(entry);
136 }
137
138 Map<String, Object> data = Maps.newHashMapWithExpectedSize(3);
139 data.put("sha", treeId.name());
140 data.put("entries", entries);
141
142 if (view.getType() == GitilesView.Type.PATH
143 && view.getRevision().getPeeledType() == OBJ_COMMIT) {
144 data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200145 data.put(
146 "archiveUrl",
147 GitilesView.archive()
148 .copyFrom(view)
149 .setPathPart(Strings.emptyToNull(view.getPathPart()))
150 .setExtension(archiveFormat.getDefaultSuffix())
151 .toUrl());
Dave Borowitzc782ebe2013-11-11 11:43:29 -0800152 data.put("archiveType", archiveFormat.getShortName());
Dave Borowitz9de65952012-08-13 16:09:45 -0700153 }
154
Shawn Pearce45e83752015-02-20 17:59:05 -0800155 if (readme.isPresent()) {
156 data.put("readmePath", readme.getPath());
157 data.put("readmeHtml", readme.render());
Shawn Pearce73e34532015-02-12 16:27:54 -0800158 }
159
Dave Borowitz9de65952012-08-13 16:09:45 -0700160 return data;
161 }
162
163 public Map<String, Object> toSoyData(ObjectId treeId) throws MissingObjectException, IOException {
Dave Borowitz7c0a8332014-05-01 11:07:04 -0700164 TreeWalk tw = new TreeWalk(reader);
Dave Borowitz9de65952012-08-13 16:09:45 -0700165 tw.addTree(treeId);
166 tw.setRecursive(false);
167 return toSoyData(treeId, tw);
168 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700169}