blob: 87fc5c96e2af0139c49f1ae43c1d2ff4f62b7168 [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
Dave Borowitzec6a9cc2014-04-25 15:51:23 -040017import static com.google.common.base.Preconditions.checkState;
Dave Borowitz9de65952012-08-13 16:09:45 -070018import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
19
Dave Borowitzec6a9cc2014-04-25 15:51:23 -040020import com.google.common.base.Strings;
Dave Borowitzab6f7d22014-04-29 11:39:20 -070021import com.google.common.collect.ImmutableList;
Dave Borowitz9de65952012-08-13 16:09:45 -070022import com.google.common.collect.Maps;
Dave Borowitzec6a9cc2014-04-25 15:51:23 -040023import com.google.template.soy.data.SoyListData;
24import com.google.template.soy.data.SoyMapData;
Dave Borowitz9de65952012-08-13 16:09:45 -070025
26import org.eclipse.jgit.diff.RawText;
27import org.eclipse.jgit.errors.LargeObjectException;
28import org.eclipse.jgit.errors.MissingObjectException;
29import org.eclipse.jgit.lib.Constants;
30import org.eclipse.jgit.lib.ObjectId;
31import org.eclipse.jgit.lib.ObjectLoader;
Dave Borowitz7c0a8332014-05-01 11:07:04 -070032import org.eclipse.jgit.lib.ObjectReader;
Dave Borowitz9de65952012-08-13 16:09:45 -070033import org.eclipse.jgit.util.RawParseUtils;
Dave Borowitzab6f7d22014-04-29 11:39:20 -070034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
Dave Borowitz9de65952012-08-13 16:09:45 -070036
37import java.io.IOException;
Dave Borowitzec6a9cc2014-04-25 15:51:23 -040038import java.util.List;
Dave Borowitz9de65952012-08-13 16:09:45 -070039import java.util.Map;
40
Shawn Pearcec10cc992015-02-09 00:22:33 -080041import prettify.parser.Prettify;
42import syntaxhighlight.ParseResult;
43
Dave Borowitz9de65952012-08-13 16:09:45 -070044/** Soy data converter for git blobs. */
45public class BlobSoyData {
Dave Borowitzab6f7d22014-04-29 11:39:20 -070046 private static final Logger log = LoggerFactory.getLogger(BlobSoyData.class);
47
Dave Borowitz9de65952012-08-13 16:09:45 -070048 /**
49 * Maximum number of bytes to load from a supposed text file for display.
50 * Files larger than this will be displayed as binary files, even if the
51 * contents was text. For example really big XML files may be above this limit
52 * and will get displayed as binary.
53 */
54 private static final int MAX_FILE_SIZE = 10 << 20;
55
56 private final GitilesView view;
Dave Borowitz7c0a8332014-05-01 11:07:04 -070057 private final ObjectReader reader;
Dave Borowitz9de65952012-08-13 16:09:45 -070058
Dave Borowitz7c0a8332014-05-01 11:07:04 -070059 public BlobSoyData(ObjectReader reader, GitilesView view) {
60 this.reader = reader;
Dave Borowitz9de65952012-08-13 16:09:45 -070061 this.view = view;
Dave Borowitz9de65952012-08-13 16:09:45 -070062 }
63
64 public Map<String, Object> toSoyData(ObjectId blobId)
65 throws MissingObjectException, IOException {
66 return toSoyData(null, blobId);
67 }
68
69 public Map<String, Object> toSoyData(String path, ObjectId blobId)
70 throws MissingObjectException, IOException {
71 Map<String, Object> data = Maps.newHashMapWithExpectedSize(4);
72 data.put("sha", ObjectId.toString(blobId));
73
Dave Borowitz7c0a8332014-05-01 11:07:04 -070074 ObjectLoader loader = reader.open(blobId, Constants.OBJ_BLOB);
Dave Borowitz9de65952012-08-13 16:09:45 -070075 String content;
76 try {
77 byte[] raw = loader.getCachedBytes(MAX_FILE_SIZE);
78 content = !RawText.isBinary(raw) ? RawParseUtils.decode(raw) : null;
79 } catch (LargeObjectException.OutOfMemory e) {
80 throw e;
81 } catch (LargeObjectException e) {
82 content = null;
83 }
84
Dave Borowitz9de65952012-08-13 16:09:45 -070085 if (content != null) {
Dave Borowitzec6a9cc2014-04-25 15:51:23 -040086 data.put("lines", prettify(path, content));
Shawn Pearcefdd98f62015-02-12 10:04:07 -080087 if (path != null && path.endsWith(".md")) {
88 data.put("docUrl", GitilesView.doc().copyFrom(view).toUrl());
89 }
Dave Borowitz96a6f472014-11-04 16:38:20 -080090 } else {
Dave Borowitzec6a9cc2014-04-25 15:51:23 -040091 data.put("lines", null);
Dave Borowitz9de65952012-08-13 16:09:45 -070092 data.put("size", Long.toString(loader.getSize()));
93 }
94 if (path != null && view.getRevision().getPeeledType() == OBJ_COMMIT) {
Dave Borowitz7745e7f2014-04-27 19:47:47 -060095 data.put("fileUrl", GitilesView.path().copyFrom(view).toUrl());
Dave Borowitz9de65952012-08-13 16:09:45 -070096 data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
Dave Borowitz6ec0c872014-01-29 13:59:37 -080097 data.put("blameUrl", GitilesView.blame().copyFrom(view).toUrl());
Dave Borowitz9de65952012-08-13 16:09:45 -070098 }
99 return data;
100 }
101
Dave Borowitzab6f7d22014-04-29 11:39:20 -0700102 private SoyListData prettify(String path, String content) {
103 List<ParseResult> results = parse(path, content);
Dave Borowitzec6a9cc2014-04-25 15:51:23 -0400104 SoyListData lines = new SoyListData();
105 SoyListData line = new SoyListData();
106 lines.add(line);
107
108 int last = 0;
109 for (ParseResult r : results) {
110 checkState(r.getOffset() >= last,
111 "out-of-order ParseResult, expected %s >= %s", r.getOffset(), last);
112 line = writeResult(lines, null, content, last, r.getOffset());
113 last = r.getOffset() + r.getLength();
114 line = writeResult(lines, r.getStyleKeysString(), content, r.getOffset(), last);
115 }
116 if (last < content.length()) {
117 writeResult(lines, null, content, last, content.length());
118 }
119 return lines;
120 }
121
Dave Borowitzab6f7d22014-04-29 11:39:20 -0700122 private List<ParseResult> parse(String path, String content) {
Shawn Pearcec10cc992015-02-09 00:22:33 -0800123 String lang = extension(path, content);
Dave Borowitzab6f7d22014-04-29 11:39:20 -0700124 try {
Shawn Pearcec10cc992015-02-09 00:22:33 -0800125 return ThreadSafePrettifyParser.INSTANCE.parse(lang, content);
Dave Borowitzab6f7d22014-04-29 11:39:20 -0700126 } catch (StackOverflowError e) {
127 // TODO(dborowitz): Aaagh. Make prettify use RE2. Or replace it something
128 // else. Or something.
129 log.warn("StackOverflowError prettifying " + view.toUrl());
130 return ImmutableList.of(
131 new ParseResult(0, content.length(), ImmutableList.of(Prettify.PR_PLAIN)));
132 }
133 }
134
Dave Borowitzec6a9cc2014-04-25 15:51:23 -0400135 private static SoyListData writeResult(SoyListData lines, String classes,
136 String s, int start, int end) {
137 SoyListData line = lines.getListData(lines.length() - 1);
138 while (true) {
139 int nl = nextLineBreak(s, start, end);
140 if (nl < 0) {
141 break;
142 }
143 addSpan(line, classes, s, start, nl);
144
145 start = nl + (isCrNl(s, nl) ? 2 : 1);
146 if (start == s.length()) {
147 return null;
148 }
149 line = new SoyListData();
150 lines.add(line);
151 }
152 addSpan(line, classes, s, start, end);
153 return line;
154 }
155
156 private static void addSpan(SoyListData line, String classes, String s, int start, int end) {
157 if (end - start > 0) {
158 if (Strings.isNullOrEmpty(classes)) {
159 classes = Prettify.PR_PLAIN;
160 }
161 line.add(new SoyMapData("classes", classes, "text", s.substring(start, end)));
162 }
163 }
164
165 private static boolean isCrNl(String s, int n) {
166 return s.charAt(n) == '\r' && n != s.length() - 1 && s.charAt(n + 1) == '\n';
167 }
168
169 private static int nextLineBreak(String s, int start, int end) {
170 for (int i = start; i < end; i++) {
171 if (s.charAt(i) == '\n' || s.charAt(i) == '\r') {
172 return i;
173 }
174 }
175 return -1;
176 }
177
178 private static String extension(String path, String content) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700179 if (content.startsWith("#!/bin/sh") || content.startsWith("#!/bin/bash")) {
180 return "sh";
181 } else if (content.startsWith("#!/usr/bin/perl")) {
Dave Borowitzec6a9cc2014-04-25 15:51:23 -0400182 return "pl";
Dave Borowitz9de65952012-08-13 16:09:45 -0700183 } else if (content.startsWith("#!/usr/bin/python")) {
184 return "py";
185 } else if (path == null) {
186 return null;
187 }
188
189 int slash = path.lastIndexOf('/');
190 int dot = path.lastIndexOf('.');
Dave Borowitzec6a9cc2014-04-25 15:51:23 -0400191 String ext = ((0 < dot) && (slash < dot)) ? path.substring(dot + 1) : null;
192 if ("txt".equalsIgnoreCase(ext)) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700193 return null;
Dave Borowitzec6a9cc2014-04-25 15:51:23 -0400194 } else if ("mk".equalsIgnoreCase(ext)) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700195 return "sh";
196 } else if ("Makefile".equalsIgnoreCase(path)
197 || ((0 < slash) && "Makefile".equalsIgnoreCase(path.substring(slash + 1)))) {
198 return "sh";
199 } else {
Dave Borowitzec6a9cc2014-04-25 15:51:23 -0400200 return ext;
Dave Borowitz9de65952012-08-13 16:09:45 -0700201 }
202 }
203}