aboutsummaryrefslogtreecommitdiff
path: root/http_handle_repo_tree.go
blob: a1a3ca4d9d536ef55c9235765b971d4dc0d9396b (plain) (blame)
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
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileContributor: Runxi Yu <https://runxiyu.org>

package main

import (
	"bytes"
	"html/template"
	"net/http"
	"path"
	"strings"

	"github.com/alecthomas/chroma/v2"
	chromaHTML "github.com/alecthomas/chroma/v2/formatters/html"
	chromaLexers "github.com/alecthomas/chroma/v2/lexers"
	chromaStyles "github.com/alecthomas/chroma/v2/styles"
	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/plumbing/object"
)

func httpHandleRepoTree(w http.ResponseWriter, r *http.Request, params map[string]any) {
	var rawPathSpec, pathSpec string
	var repo *git.Repository
	var refHash plumbing.Hash
	var commitObject *object.Commit
	var tree *object.Tree
	var err error

	rawPathSpec = params["rest"].(string)
	repo, pathSpec = params["repo"].(*git.Repository), strings.TrimSuffix(rawPathSpec, "/")
	params["path_spec"] = pathSpec

	if refHash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil {
		http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError)
		return
	}
	if commitObject, err = repo.CommitObject(refHash); err != nil {
		http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError)
		return
	}
	if tree, err = commitObject.Tree(); err != nil {
		http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError)
		return
	}

	var target *object.Tree
	if pathSpec == "" {
		target = tree
	} else {
		if target, err = tree.Tree(pathSpec); err != nil {
			var file *object.File
			var fileContent string
			var lexer chroma.Lexer
			var iterator chroma.Iterator
			var style *chroma.Style
			var formatter *chromaHTML.Formatter
			var formattedHTML template.HTML

			if file, err = tree.File(pathSpec); err != nil {
				http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError)
				return
			}
			if len(rawPathSpec) != 0 && rawPathSpec[len(rawPathSpec)-1] == '/' {
				http.Redirect(w, r, "../"+pathSpec, http.StatusSeeOther)
				return
			}
			if fileContent, err = file.Contents(); err != nil {
				http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError)
				return
			}
			lexer = chromaLexers.Match(pathSpec)
			if lexer == nil {
				lexer = chromaLexers.Fallback
			}
			if iterator, err = lexer.Tokenise(nil, fileContent); err != nil {
				http.Error(w, "Error tokenizing code: "+err.Error(), http.StatusInternalServerError)
				return
			}
			var formattedHTMLStr bytes.Buffer
			style = chromaStyles.Get("autumn")
			formatter = chromaHTML.New(chromaHTML.WithClasses(true), chromaHTML.TabWidth(8))
			if err = formatter.Format(&formattedHTMLStr, style, iterator); err != nil {
				http.Error(w, "Error formatting code: "+err.Error(), http.StatusInternalServerError)
				return
			}
			formattedHTML = template.HTML(formattedHTMLStr.Bytes()) //#nosec G203
			params["file_contents"] = formattedHTML

			renderTemplate(w, "repo_tree_file", params)
			return
		}
	}

	if len(rawPathSpec) != 0 && rawPathSpec[len(rawPathSpec)-1] != '/' {
		http.Redirect(w, r, path.Base(pathSpec)+"/", http.StatusSeeOther)
		return
	}

	params["readme_filename"], params["readme"] = renderReadmeAtTree(target)
	params["files"] = makeDisplayTree(target)

	renderTemplate(w, "repo_tree_dir", params)
}