blob: 1e4e4b7740a265612eba1ff03c0f489e3329d74a [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
44 /**
Dave Borowitz40255d52016-08-19 16:16:22 -040045 * Maximum number of bytes to load from a blob that claims to be a symlink. If the blob is larger
46 * than this byte limit it will be displayed as a binary file instead of as a symlink.
Dave Borowitz9de65952012-08-13 16:09:45 -070047 */
48 static final int MAX_SYMLINK_SIZE = 16 << 10;
49
50 static String resolveTargetUrl(GitilesView view, String target) {
Dave Borowitzcfc1c532015-02-18 13:41:19 -080051 String resolved = PathUtil.simplifyPathUpToRoot(target, view.getPathPart());
Dave Borowitzbcd753d2013-02-08 11:10:19 -080052 if (resolved == null) {
Dave Borowitz9de65952012-08-13 16:09:45 -070053 return null;
54 }
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020055 return GitilesView.path().copyFrom(view).setPathPart(resolved).toUrl();
Dave Borowitz9de65952012-08-13 16:09:45 -070056 }
57
58 @VisibleForTesting
59 static String getTargetDisplayName(String target) {
60 if (target.length() <= MAX_SYMLINK_TARGET_LENGTH) {
61 return target;
Dave Borowitz9de65952012-08-13 16:09:45 -070062 }
David Pursehouseb3b630f2016-06-15 21:51:18 +090063 int lastSlash = target.lastIndexOf('/');
64 // TODO(dborowitz): Doesn't abbreviate a long last path component.
65 return lastSlash >= 0 ? "..." + target.substring(lastSlash) : target;
Dave Borowitz9de65952012-08-13 16:09:45 -070066 }
67
Dave Borowitz7c0a8332014-05-01 11:07:04 -070068 private final ObjectReader reader;
Dave Borowitz9de65952012-08-13 16:09:45 -070069 private final GitilesView view;
Shawn Pearce73e34532015-02-12 16:27:54 -080070 private final Config cfg;
71 private final RevTree rootTree;
Shawn Pearcec68ad0b2016-05-28 16:52:47 -070072 private final String requestUri;
Dave Borowitzc782ebe2013-11-11 11:43:29 -080073 private ArchiveFormat archiveFormat;
Dave Borowitz9de65952012-08-13 16:09:45 -070074
Shawn Pearcec68ad0b2016-05-28 16:52:47 -070075 public TreeSoyData(
76 ObjectReader reader, GitilesView view, Config cfg, RevTree rootTree, String requestUri) {
Dave Borowitz7c0a8332014-05-01 11:07:04 -070077 this.reader = reader;
Dave Borowitz9de65952012-08-13 16:09:45 -070078 this.view = view;
Shawn Pearce73e34532015-02-12 16:27:54 -080079 this.cfg = cfg;
80 this.rootTree = rootTree;
Shawn Pearcec68ad0b2016-05-28 16:52:47 -070081 this.requestUri = requestUri;
Dave Borowitz9de65952012-08-13 16:09:45 -070082 }
83
Dave Borowitzc782ebe2013-11-11 11:43:29 -080084 public TreeSoyData setArchiveFormat(ArchiveFormat archiveFormat) {
85 this.archiveFormat = archiveFormat;
86 return this;
87 }
88
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020089 public Map<String, Object> toSoyData(ObjectId treeId, TreeWalk tw)
90 throws MissingObjectException, IOException {
Shawn Pearcec68ad0b2016-05-28 16:52:47 -070091 ReadmeHelper readme =
92 new ReadmeHelper(reader, view, MarkdownConfig.get(cfg), rootTree, requestUri);
Dave Borowitz9de65952012-08-13 16:09:45 -070093 List<Object> entries = Lists.newArrayList();
94 GitilesView.Builder urlBuilder = GitilesView.path().copyFrom(view);
95 while (tw.next()) {
96 FileType type = FileType.forEntry(tw);
97 String name = tw.getNameString();
98
David Pursehousecb91aaf2016-06-15 22:05:24 +090099 GitilesView.Type viewType = view.getType();
100 if (viewType == GitilesView.Type.PATH) {
101 urlBuilder.setPathPart(view.getPathPart() + "/" + name);
102 } else if (viewType == GitilesView.Type.REVISION) {
103 // Got here from a tag pointing at a tree.
104 urlBuilder.setPathPart(name);
105 } else {
106 throw new IllegalStateException(
107 String.format("Cannot render TreeSoyData from %s view", viewType));
Dave Borowitz9de65952012-08-13 16:09:45 -0700108 }
109
110 String url = urlBuilder.toUrl();
111 if (type == FileType.TREE) {
112 name += "/";
113 url += "/";
114 }
115 Map<String, String> entry = Maps.newHashMapWithExpectedSize(4);
116 entry.put("type", type.toString());
117 entry.put("name", name);
118 entry.put("url", url);
119 if (type == FileType.SYMLINK) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200120 String target = new String(reader.open(tw.getObjectId(0)).getCachedBytes(), UTF_8);
Dave Borowitz9de65952012-08-13 16:09:45 -0700121 entry.put("targetName", getTargetDisplayName(target));
122 String targetUrl = resolveTargetUrl(view, target);
123 if (targetUrl != null) {
124 entry.put("targetUrl", targetUrl);
125 }
Shawn Pearce45e83752015-02-20 17:59:05 -0800126 } else {
127 readme.considerEntry(tw);
Dave Borowitz9de65952012-08-13 16:09:45 -0700128 }
129 entries.add(entry);
130 }
131
132 Map<String, Object> data = Maps.newHashMapWithExpectedSize(3);
133 data.put("sha", treeId.name());
134 data.put("entries", entries);
135
136 if (view.getType() == GitilesView.Type.PATH
137 && view.getRevision().getPeeledType() == OBJ_COMMIT) {
138 data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200139 data.put(
140 "archiveUrl",
141 GitilesView.archive()
142 .copyFrom(view)
143 .setPathPart(Strings.emptyToNull(view.getPathPart()))
144 .setExtension(archiveFormat.getDefaultSuffix())
145 .toUrl());
Dave Borowitzc782ebe2013-11-11 11:43:29 -0800146 data.put("archiveType", archiveFormat.getShortName());
Dave Borowitz9de65952012-08-13 16:09:45 -0700147 }
148
Shawn Pearce45e83752015-02-20 17:59:05 -0800149 if (readme.isPresent()) {
150 data.put("readmePath", readme.getPath());
151 data.put("readmeHtml", readme.render());
Shawn Pearce73e34532015-02-12 16:27:54 -0800152 }
153
Dave Borowitz9de65952012-08-13 16:09:45 -0700154 return data;
155 }
156
157 public Map<String, Object> toSoyData(ObjectId treeId) throws MissingObjectException, IOException {
Dave Borowitz7c0a8332014-05-01 11:07:04 -0700158 TreeWalk tw = new TreeWalk(reader);
Dave Borowitz9de65952012-08-13 16:09:45 -0700159 tw.addTree(treeId);
160 tw.setRecursive(false);
161 return toSoyData(treeId, tw);
162 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700163}