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
|
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
package main
import (
"errors"
"fmt"
"html/template"
"io"
"net"
"net/http"
"strings"
"git.sr.ht/~sircmpwn/go-bare"
)
// httpHandleRepoTree provides a friendly, syntax-highlighted view of
// individual files, and provides directory views that link to these files.
//
// TODO: Do not highlight files that are too large.
func httpHandleRepoTree(writer http.ResponseWriter, request *http.Request, params map[string]any) {
repoName := params["repo_name"].(string)
groupPath := params["group_path"].([]string)
rawPathSpec := params["rest"].(string)
pathSpec := strings.TrimSuffix(rawPathSpec, "/")
params["path_spec"] = pathSpec
_, repoPath, _, _, _, _, _ := getRepoInfo(request.Context(), groupPath, repoName, "")
conn, err := net.Dial("unix", config.Git.Socket)
if err != nil {
errorPage500(writer, params, "git2d connection failed: "+err.Error())
return
}
defer conn.Close()
brWriter := bare.NewWriter(conn)
brReader := bare.NewReader(conn)
if err := brWriter.WriteData([]byte(repoPath)); err != nil {
errorPage500(writer, params, "sending repo path failed: "+err.Error())
return
}
if err := brWriter.WriteUint(2); err != nil {
errorPage500(writer, params, "sending command failed: "+err.Error())
return
}
if err := brWriter.WriteData([]byte(pathSpec)); err != nil {
errorPage500(writer, params, "sending path failed: "+err.Error())
return
}
status, err := brReader.ReadUint()
if err != nil {
errorPage500(writer, params, "reading status failed: "+err.Error())
return
}
switch status {
case 0:
kind, err := brReader.ReadUint()
if err != nil {
errorPage500(writer, params, "reading object kind failed: "+err.Error())
return
}
switch kind {
case 1:
// Tree
count, err := brReader.ReadUint()
if err != nil {
errorPage500(writer, params, "reading entry count failed: "+err.Error())
return
}
files := make([]displayTreeEntry, 0, count)
for i := uint64(0); i < count; i++ {
typeCode, err := brReader.ReadUint()
if err != nil {
errorPage500(writer, params, "error reading entry type: "+err.Error())
return
}
mode, err := brReader.ReadUint()
if err != nil {
errorPage500(writer, params, "error reading entry mode: "+err.Error())
return
}
size, err := brReader.ReadUint()
if err != nil {
errorPage500(writer, params, "error reading entry size: "+err.Error())
return
}
name, err := brReader.ReadData()
if err != nil {
errorPage500(writer, params, "error reading entry name: "+err.Error())
return
}
files = append(files, displayTreeEntry{
Name: string(name),
Mode: fmt.Sprintf("%06o", mode),
Size: int64(size),
IsFile: typeCode == 2,
IsSubtree: typeCode == 1,
})
}
params["files"] = files
params["readme_filename"] = "README.md"
params["readme"] = template.HTML("<p>README rendering here is WIP again</p>") // TODO
renderTemplate(writer, "repo_tree_dir", params)
case 2:
// Blob
content, err := brReader.ReadData()
if err != nil && !errors.Is(err, io.EOF) {
errorPage500(writer, params, "error reading file content: "+err.Error())
return
}
rendered := renderHighlightedFile(pathSpec, string(content))
params["file_contents"] = rendered
renderTemplate(writer, "repo_tree_file", params)
default:
errorPage500(writer, params, fmt.Sprintf("unknown kind: %d", kind))
return
}
case 3:
errorPage500(writer, params, fmt.Sprintf("path not found: %s", pathSpec))
return
default:
errorPage500(writer, params, fmt.Sprintf("unknown status code: %d", status))
}
}
|