blob: f1366ad7a31eb61332e215f174bf6bfff760bf85 [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;
25
26import org.eclipse.jgit.errors.MissingObjectException;
Shawn Pearce73e34532015-02-12 16:27:54 -080027import org.eclipse.jgit.lib.Config;
Dave Borowitz9de65952012-08-13 16:09:45 -070028import org.eclipse.jgit.lib.ObjectId;
Dave Borowitz7c0a8332014-05-01 11:07:04 -070029import org.eclipse.jgit.lib.ObjectReader;
Shawn Pearce73e34532015-02-12 16:27:54 -080030import org.eclipse.jgit.revwalk.RevTree;
Dave Borowitz9de65952012-08-13 16:09:45 -070031import org.eclipse.jgit.treewalk.TreeWalk;
32
33import java.io.IOException;
34import java.util.List;
35import java.util.Map;
Dave Borowitz9de65952012-08-13 16:09:45 -070036
37/** Soy data converter for git trees. */
38public class TreeSoyData {
39 /**
40 * Number of characters to display for a symlink target. Targets longer than
41 * this are abbreviated for display in a tree listing.
42 */
43 private static final int MAX_SYMLINK_TARGET_LENGTH = 72;
44
45 /**
46 * Maximum number of bytes to load from a blob that claims to be a symlink. If
47 * the blob is larger than this byte limit it will be displayed as a binary
48 * file instead of as a symlink.
49 */
50 static final int MAX_SYMLINK_SIZE = 16 << 10;
51
52 static String resolveTargetUrl(GitilesView view, String target) {
Dave Borowitzcfc1c532015-02-18 13:41:19 -080053 String resolved = PathUtil.simplifyPathUpToRoot(target, view.getPathPart());
Dave Borowitzbcd753d2013-02-08 11:10:19 -080054 if (resolved == null) {
Dave Borowitz9de65952012-08-13 16:09:45 -070055 return null;
56 }
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020057 return GitilesView.path().copyFrom(view).setPathPart(resolved).toUrl();
Dave Borowitz9de65952012-08-13 16:09:45 -070058 }
59
60 @VisibleForTesting
61 static String getTargetDisplayName(String target) {
62 if (target.length() <= MAX_SYMLINK_TARGET_LENGTH) {
63 return target;
64 } else {
65 int lastSlash = target.lastIndexOf('/');
66 // TODO(dborowitz): Doesn't abbreviate a long last path component.
67 return lastSlash >= 0 ? "..." + target.substring(lastSlash) : target;
68 }
69 }
70
Dave Borowitz7c0a8332014-05-01 11:07:04 -070071 private final ObjectReader reader;
Dave Borowitz9de65952012-08-13 16:09:45 -070072 private final GitilesView view;
Shawn Pearce73e34532015-02-12 16:27:54 -080073 private final Config cfg;
74 private final RevTree rootTree;
Dave Borowitzc782ebe2013-11-11 11:43:29 -080075 private ArchiveFormat archiveFormat;
Dave Borowitz9de65952012-08-13 16:09:45 -070076
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020077 public TreeSoyData(ObjectReader reader, GitilesView view, Config cfg, RevTree rootTree) {
Dave Borowitz7c0a8332014-05-01 11:07:04 -070078 this.reader = reader;
Dave Borowitz9de65952012-08-13 16:09:45 -070079 this.view = view;
Shawn Pearce73e34532015-02-12 16:27:54 -080080 this.cfg = cfg;
81 this.rootTree = rootTree;
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 Pearce45e83752015-02-20 17:59:05 -080091 ReadmeHelper readme = new ReadmeHelper(reader, view, cfg, rootTree);
Dave Borowitz9de65952012-08-13 16:09:45 -070092 List<Object> entries = Lists.newArrayList();
93 GitilesView.Builder urlBuilder = GitilesView.path().copyFrom(view);
94 while (tw.next()) {
95 FileType type = FileType.forEntry(tw);
96 String name = tw.getNameString();
97
98 switch (view.getType()) {
99 case PATH:
Dave Borowitzdd3c3d92013-03-11 16:38:41 -0700100 urlBuilder.setPathPart(view.getPathPart() + "/" + name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700101 break;
102 case REVISION:
103 // Got here from a tag pointing at a tree.
Dave Borowitzdd3c3d92013-03-11 16:38:41 -0700104 urlBuilder.setPathPart(name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700105 break;
106 default:
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200107 throw new IllegalStateException(
108 String.format("Cannot render TreeSoyData from %s view", view.getType()));
Dave Borowitz9de65952012-08-13 16:09:45 -0700109 }
110
111 String url = urlBuilder.toUrl();
112 if (type == FileType.TREE) {
113 name += "/";
114 url += "/";
115 }
116 Map<String, String> entry = Maps.newHashMapWithExpectedSize(4);
117 entry.put("type", type.toString());
118 entry.put("name", name);
119 entry.put("url", url);
120 if (type == FileType.SYMLINK) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200121 String target = new String(reader.open(tw.getObjectId(0)).getCachedBytes(), UTF_8);
Dave Borowitz9de65952012-08-13 16:09:45 -0700122 entry.put("targetName", getTargetDisplayName(target));
123 String targetUrl = resolveTargetUrl(view, target);
124 if (targetUrl != null) {
125 entry.put("targetUrl", targetUrl);
126 }
Shawn Pearce45e83752015-02-20 17:59:05 -0800127 } else {
128 readme.considerEntry(tw);
Dave Borowitz9de65952012-08-13 16:09:45 -0700129 }
130 entries.add(entry);
131 }
132
133 Map<String, Object> data = Maps.newHashMapWithExpectedSize(3);
134 data.put("sha", treeId.name());
135 data.put("entries", entries);
136
137 if (view.getType() == GitilesView.Type.PATH
138 && view.getRevision().getPeeledType() == OBJ_COMMIT) {
139 data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200140 data.put(
141 "archiveUrl",
142 GitilesView.archive()
143 .copyFrom(view)
144 .setPathPart(Strings.emptyToNull(view.getPathPart()))
145 .setExtension(archiveFormat.getDefaultSuffix())
146 .toUrl());
Dave Borowitzc782ebe2013-11-11 11:43:29 -0800147 data.put("archiveType", archiveFormat.getShortName());
Dave Borowitz9de65952012-08-13 16:09:45 -0700148 }
149
Shawn Pearce45e83752015-02-20 17:59:05 -0800150 if (readme.isPresent()) {
151 data.put("readmePath", readme.getPath());
152 data.put("readmeHtml", readme.render());
Shawn Pearce73e34532015-02-12 16:27:54 -0800153 }
154
Dave Borowitz9de65952012-08-13 16:09:45 -0700155 return data;
156 }
157
158 public Map<String, Object> toSoyData(ObjectId treeId) throws MissingObjectException, IOException {
Dave Borowitz7c0a8332014-05-01 11:07:04 -0700159 TreeWalk tw = new TreeWalk(reader);
Dave Borowitz9de65952012-08-13 16:09:45 -0700160 tw.addTree(treeId);
161 tw.setRecursive(false);
162 return toSoyData(treeId, tw);
163 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700164}