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
| package main
import (
"embed"
"encoding/base64"
"fmt"
"html/template"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/filemode"
"github.com/go-git/go-git/v5/plumbing/object"
)
//go:embed icons/*.png
var iconsFS embed.FS
//go:embed templates/*.tmpl
var templatesFS embed.FS
var icons map[string]string
var templates *template.Template
const DOMAIN = "https://git.mcksp.com"
type Config struct {
outputPath string
filesPath string
repoName string
description string
lastModified string
}
type Entry struct {
Name string
Path string
}
type IndexData struct {
RepoName string
Description string
Domain string
Directories []Entry
Files []Entry
ReadmeContent string
HasReadme bool
LastModified string
Icons map[string]string
PathPrefix string
}
type FileData struct {
IsBinary bool
ShowLines bool
LineNumbers []int
Content string
}
func basename(path string) string {
return filepath.Base(path)
}
func countLines(s string) int {
lines := 0
for _, c := range s {
if c == '\n' {
lines++
}
}
return lines
}
func joinPaths(path1, path2 string) string {
if path1 == "" {
return path2
}
if strings.HasSuffix(path1, "/") {
return path1 + path2
}
return path1 + "/" + path2
}
func htmlEncode(content string) string {
var sb strings.Builder
for _, c := range content {
switch c {
case '<':
sb.WriteString("<")
case '>':
sb.WriteString(">")
case '\'':
sb.WriteString("'")
case '&':
sb.WriteString("&")
case '"':
sb.WriteString(""")
default:
sb.WriteRune(c)
}
}
return sb.String()
}
func processFile(repo *git.Repository, file *object.File, root, name string, conf *Config) {
content, err := file.Contents()
if err != nil {
fmt.Printf("cannot get file contents: %v\n", err)
return
}
path := fmt.Sprintf("%s/%s/%s.html", conf.filesPath, root, name)
f, err := os.Create(path)
if err != nil {
fmt.Printf("couldnt open file %s: %v\n", path, err)
return
}
defer f.Close()
data := FileData{
IsBinary: isBinary(content),
ShowLines: true,
}
if !data.IsBinary {
lineCount := countLines(content)
data.LineNumbers = make([]int, lineCount)
for i := 0; i < lineCount; i++ {
data.LineNumbers[i] = i + 1
}
data.Content = content
}
err = templates.ExecuteTemplate(f, "file.tmpl", data)
if err != nil {
fmt.Printf("template execution error: %v\n", err)
}
}
func isBinary(s string) bool {
for i := 0; i < len(s) && i < 8000; i++ {
if s[i] == 0 {
return true
}
}
return false
}
func processTree(repo *git.Repository, tree *object.Tree, treePath string, conf *Config, isRoot bool) {
dirPath := joinPaths(conf.filesPath, treePath)
os.MkdirAll(dirPath, 0755)
var indexPath string
if isRoot {
indexPath = fmt.Sprintf("%s/index.html", conf.outputPath)
} else {
indexPath = fmt.Sprintf("%s.html", joinPaths(conf.filesPath, treePath))
}
f, err := os.Create(indexPath)
if err != nil {
fmt.Printf("couldnt create index: %v\n", err)
return
}
defer f.Close()
pathPrefix := ""
if isRoot {
pathPrefix = "files/"
}
data := IndexData{
RepoName: conf.repoName,
Description: conf.description,
Domain: DOMAIN,
Directories: []Entry{},
Files: []Entry{},
LastModified: conf.lastModified,
Icons: icons,
PathPrefix: pathPrefix,
}
// Collect directories first
for _, entry := range tree.Entries {
if entry.Mode == filemode.Dir {
childName := entry.Name
childPath := joinPaths(treePath, childName)
data.Directories = append(data.Directories, Entry{
Name: childName,
Path: childPath,
})
childTree, err := repo.TreeObject(entry.Hash)
if err != nil {
fmt.Printf("couldnt find tree for %s: %v\n", childPath, err)
continue
}
processTree(repo, childTree, childPath, conf, false)
}
}
// Collect files second
for _, entry := range tree.Entries {
if entry.Mode == filemode.Regular || entry.Mode == filemode.Executable {
childName := entry.Name
childPath := joinPaths(treePath, childName)
data.Files = append(data.Files, Entry{
Name: childName,
Path: childPath,
})
file, err := tree.File(childName)
if err != nil {
fmt.Printf("couldnt get file %s: %v\n", childName, err)
continue
}
processFile(repo, file, treePath, childName, conf)
}
}
// Add README if this is root
if isRoot {
readme, err := tree.File("README")
if err == nil {
content, err := readme.Contents()
if err == nil {
data.ReadmeContent = content
data.HasReadme = true
}
}
}
err = templates.ExecuteTemplate(f, "index.tmpl", data)
if err != nil {
fmt.Printf("template execution error: %v\n", err)
}
}
func removeExt(repoName string) string {
if idx := strings.LastIndex(repoName, "."); idx != -1 {
return repoName[:idx]
}
return repoName
}
func loadIcons() {
icons = make(map[string]string)
iconFiles := []string{"dir.png", "text.png", "image.png", "blob.png", "gituwa.png"}
for _, iconFile := range iconFiles {
data, err := iconsFS.ReadFile("icons/" + iconFile)
if err != nil {
fmt.Printf("warning: could not load icon %s: %v\n", iconFile, err)
continue
}
encoded := base64.StdEncoding.EncodeToString(data)
icons[iconFile] = "data:image/png;base64," + encoded
}
}
func loadTemplates() {
var err error
funcMap := template.FuncMap{
"iconSrc": func(iconName string, icons map[string]string) template.HTMLAttr {
return template.HTMLAttr(fmt.Sprintf("src='%s'", icons[iconName]))
},
"htmlEncode": func(s string) template.HTML {
return template.HTML(htmlEncode(s))
},
}
templates, err = template.New("").Funcs(funcMap).ParseFS(templatesFS, "templates/*.tmpl")
if err != nil {
fmt.Printf("error loading templates: %v\n", err)
os.Exit(1)
}
}
func main() {
loadIcons()
loadTemplates()
args := os.Args
if len(args) < 3 {
fmt.Println("usage: gituwa <repo_path> <output_path> <description>")
return
}
repoPath := args[1]
outputPath := args[2]
filesPath := joinPaths(outputPath, "files")
description := ""
if len(args) > 3 {
description = strings.Join(args[3:], " ")
}
repo, err := git.PlainOpen(repoPath)
if err != nil {
fmt.Printf("cannot open repository: %v\n", err)
return
}
repoName := removeExt(basename(repoPath))
head, err := repo.Head()
if err != nil {
fmt.Printf("cannot get HEAD: %v\n", err)
return
}
commit, err := repo.CommitObject(head.Hash())
if err != nil {
fmt.Printf("cannot get commit: %v\n", err)
return
}
tree, err := commit.Tree()
if err != nil {
fmt.Printf("cannot get tree: %v\n", err)
return
}
// Format the commit timestamp
lastModified := commit.Author.When.Format("2006-01-02 15:04:05")
conf := &Config{
outputPath: outputPath,
filesPath: filesPath,
repoName: repoName,
description: description,
lastModified: lastModified,
}
os.MkdirAll(conf.outputPath, 0755)
os.MkdirAll(conf.filesPath, 0755)
processTree(repo, tree, "", conf, true)
}
|