/
fileview.go
378 lines (339 loc) · 9.56 KB
/
fileview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package server
import (
"bytes"
"fmt"
"net/url"
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/livegrep/livegrep/server/config"
)
// Mapping from known file extensions to filetype hinting.
var filenameToLangMap map[string]string = map[string]string{
"BUILD": "python",
"BUILD.bazel": "python",
"WORKSPACE": "python",
}
var extToLangMap map[string]string = map[string]string{
".adoc": "AsciiDoc",
".asc": "AsciiDoc",
".asciidoc": "AsciiDoc",
".AppleScript": "applescript",
".bzl": "python",
".c": "c",
".coffee": "coffeescript",
".cpp": "cpp",
".css": "css",
".ex": "elixir",
".exs": "elixir",
".erl": "erlang",
".go": "go",
".h": "cpp",
".hs": "haskell",
".html": "markup",
".java": "java",
".js": "javascript",
".json": "json",
".jsx": "jsx",
".m": "objectivec",
".markdown": "markdown",
".md": "markdown",
".mdown": "markdown",
".mkdn": "markdown",
".mediawiki": "markdown",
".nix": "nix",
".php": "php",
".pl": "perl",
".proto": "go",
".py": "python",
".pyst": "python",
".rb": "ruby",
".rdoc": "markdown",
".rs": "rust",
".scala": "scala",
".scpt": "applescript",
".scss": "scss",
".sh": "bash",
".sky": "python",
".sql": "sql",
".swift": "swift",
".textile": "markdown",
".ts": "typescript",
".tsx": "tsx",
".wiki": "markdown",
".xml": "markup",
".yaml": "yaml",
".yml": "yaml",
}
var fileFirstLineToLangMap map[*regexp.Regexp]string = map[*regexp.Regexp]string{
regexp.MustCompile(`^#!.*\bpython[23]?\b`): "python",
regexp.MustCompile(`^#!.*\bbash\b`): "bash",
regexp.MustCompile(`^#!.*\bsh\b`): "bash",
regexp.MustCompile(`^#!.*\bruby\b`): "ruby",
regexp.MustCompile(`^#!.*\bperl\b`): "perl",
}
// Grabbed from the extensions GitHub supports here - https://github.com/github/markup
var supportedReadmeExtensions = []string{
"markdown", "mdown", "mkdn", "md", "textile", "rdoc", "org", "creole", "mediawiki", "wiki",
"rst", "asciidoc", "adoc", "asc", "pod",
}
var supportedReadmeRegex = buildReadmeRegex(supportedReadmeExtensions)
type breadCrumbEntry struct {
Name string
Path string
}
type directoryListEntry struct {
Name string
Path string
IsDir bool
SymlinkTarget string
}
type fileViewerContext struct {
PathSegments []breadCrumbEntry
Repo config.RepoConfig
Commit string
DirContent *directoryContent
FileContent *sourceFileContent
ExternalDomain string
Permalink string
Headlink string
}
type sourceFileContent struct {
Content string
LineCount int
Language string
}
type directoryContent struct {
Entries []directoryListEntry
ReadmeContent *sourceFileContent
}
type DirListingSort []directoryListEntry
func (s DirListingSort) Len() int {
return len(s)
}
func (s DirListingSort) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s DirListingSort) Less(i, j int) bool {
if s[i].IsDir != s[j].IsDir {
return s[i].IsDir
}
return s[i].Name < s[j].Name
}
func gitCommitHash(ref string, repoPath string) (string, error) {
out, err := exec.Command(
"git", "-C", repoPath, "rev-parse", ref,
).Output()
if err != nil {
return "", err
}
return string(out), nil
}
func gitObjectType(obj string, repoPath string) (string, error) {
out, err := exec.Command("git", "-C", repoPath, "cat-file", "-t", obj).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
func gitCatBlob(obj string, repoPath string) (string, error) {
out, err := exec.Command("git", "-C", repoPath, "cat-file", "blob", obj).Output()
if err != nil {
return "", err
}
return string(out), nil
}
type gitTreeEntry struct {
Mode string
ObjectType string
ObjectId string
ObjectName string
}
func gitParseTreeEntry(line string) gitTreeEntry {
dataAndPath := strings.SplitN(line, "\t", 2)
dataFields := strings.Split(dataAndPath[0], " ")
return gitTreeEntry{
Mode: dataFields[0],
ObjectType: dataFields[1],
ObjectId: dataFields[2],
ObjectName: dataAndPath[1],
}
}
func gitListDir(obj string, repoPath string) ([]gitTreeEntry, error) {
out, err := exec.Command("git", "-C", repoPath, "cat-file", "-p", obj).Output()
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
lines = lines[:len(lines)-1]
result := make([]gitTreeEntry, len(lines))
for i, line := range lines {
result[i] = gitParseTreeEntry(line)
}
return result, nil
}
func viewUrl(repo string, path string) string {
return "/view/" + repo + "/" + path
}
func getFileUrl(repo string, pathFromRoot string, name string, isDir bool) string {
fileUrl := viewUrl(repo, filepath.Join(pathFromRoot, path.Clean(name)))
if isDir {
fileUrl += "/"
}
return fileUrl
}
func buildReadmeRegex(supportedReadmeExtensions []string) *regexp.Regexp {
// Sort in descending order of length so most specific match is selected by regex engine
sort.Slice(supportedReadmeExtensions, func(i, j int) bool {
return len(supportedReadmeExtensions[i]) >= len(supportedReadmeExtensions[j])
})
// Build regex of form "README.(ext1|ext2)" README case insensitive
var buf bytes.Buffer
for i, ext := range supportedReadmeExtensions {
buf.WriteString(regexp.QuoteMeta(ext))
if i < len(supportedReadmeExtensions)-1 {
buf.WriteString("|")
}
}
repoRegexAlt := buf.String()
repoFileRegex := regexp.MustCompile(fmt.Sprintf("((?i)readme)\\.(%s)", repoRegexAlt))
return repoFileRegex
}
func buildDirectoryListEntry(treeEntry gitTreeEntry, pathFromRoot string, repo config.RepoConfig) directoryListEntry {
var fileUrl string
var symlinkTarget string
if treeEntry.Mode == "120000" {
resolvedPath, err := gitCatBlob(treeEntry.ObjectId, repo.Path)
if err == nil {
symlinkTarget = resolvedPath
}
} else {
fileUrl = getFileUrl(repo.Name, pathFromRoot, treeEntry.ObjectName, treeEntry.ObjectType == "tree")
}
return directoryListEntry{
Name: treeEntry.ObjectName,
Path: fileUrl,
IsDir: treeEntry.ObjectType == "tree",
SymlinkTarget: symlinkTarget,
}
}
func languageFromFirstLine(line string) string {
for regex, lang := range fileFirstLineToLangMap {
if regex.MatchString(line) {
return lang
}
}
return ""
}
func buildFileData(relativePath string, repo config.RepoConfig, commit string) (*fileViewerContext, error) {
commitHash := commit
out, err := gitCommitHash(commit, repo.Path)
if err == nil {
commitHash = out[:strings.Index(out, "\n")]
}
cleanPath := path.Clean(relativePath)
if cleanPath == "." {
cleanPath = ""
}
obj := commitHash + ":" + cleanPath
pathSplits := strings.Split(cleanPath, "/")
var fileContent *sourceFileContent
var dirContent *directoryContent
objectType, err := gitObjectType(obj, repo.Path)
if err != nil {
return nil, err
}
if objectType == "tree" {
treeEntries, err := gitListDir(obj, repo.Path)
if err != nil {
return nil, err
}
dirEntries := make([]directoryListEntry, len(treeEntries))
var readmePath, readmeLang string
for i, treeEntry := range treeEntries {
dirEntries[i] = buildDirectoryListEntry(treeEntry, cleanPath, repo)
// Git supports case sensitive files, so README.md & readme.md in the same tree is possible
// so in this case we just grab the first matching file
if readmePath != "" {
continue
}
parts := supportedReadmeRegex.FindStringSubmatch(dirEntries[i].Name)
if len(parts) != 3 {
continue
}
readmePath = obj + parts[0]
readmeLang = parts[2]
}
var readmeContent *sourceFileContent
if readmePath != "" {
if content, err := gitCatBlob(readmePath, repo.Path); err == nil {
readmeContent = &sourceFileContent{
Content: content,
LineCount: strings.Count(content, "\n"),
Language: extToLangMap["."+readmeLang],
}
}
}
sort.Sort(DirListingSort(dirEntries))
dirContent = &directoryContent{
Entries: dirEntries,
ReadmeContent: readmeContent,
}
} else if objectType == "blob" {
content, err := gitCatBlob(obj, repo.Path)
if err != nil {
return nil, err
}
language := filenameToLangMap[filepath.Base(cleanPath)]
if language == "" {
language = extToLangMap[filepath.Ext(cleanPath)]
}
if language == "" {
firstLine, _, found := strings.Cut(string(content), "\n")
if found {
language = languageFromFirstLine(firstLine)
}
}
fileContent = &sourceFileContent{
Content: content,
LineCount: strings.Count(string(content), "\n"),
Language: language,
}
}
segments := make([]breadCrumbEntry, len(pathSplits))
for i, name := range pathSplits {
parentPath := path.Clean(strings.Join(pathSplits[0:i], "/"))
segments[i] = breadCrumbEntry{
Name: name,
Path: getFileUrl(repo.Name, parentPath, name, true),
}
}
externalDomain := "external viewer"
if url, err := url.Parse(repo.Metadata["url_pattern"]); err == nil {
externalDomain = url.Hostname()
}
permalink := ""
headlink := ""
if !strings.HasPrefix(commitHash, commit) {
permalink = "?commit=" + commitHash[:16]
} else {
if dirContent != nil {
headlink = "."
} else {
headlink = segments[len(segments)-1].Name
}
}
return &fileViewerContext{
PathSegments: segments,
Repo: repo,
Commit: commit,
DirContent: dirContent,
FileContent: fileContent,
ExternalDomain: externalDomain,
Permalink: permalink,
Headlink: headlink,
}, nil
}