Repository navigation
Expand file tree
/
Copy pathlist.go
More file actions
249 lines (218 loc) · 5.43 KB
/
Copy pathlist.go
File metadata and controls
249 lines (218 loc) · 5.43 KB
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
package main
import (
"context"
"fmt"
"html/template"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"github.com/antonmedv/gitmal/pkg/git"
"github.com/antonmedv/gitmal/pkg/links"
"github.com/antonmedv/gitmal/pkg/progress_bar"
"github.com/antonmedv/gitmal/pkg/templates"
)
func generateLists(files []git.Blob, params Params) error {
// Build directory indexes
type dirInfo struct {
subdirs map[string]struct{}
files []git.Blob
}
dirs := map[string]*dirInfo{}
ensureDir := func(p string) *dirInfo {
if di, ok := dirs[p]; ok {
return di
}
di := &dirInfo{subdirs: map[string]struct{}{}, files: []git.Blob{}}
dirs[p] = di
return di
}
dirsSet := links.BuildDirSet(files)
filesSet := links.BuildFileSet(files)
for _, b := range files {
// Normalize to forward slash paths for URL construction
p := b.Path
parts := strings.Split(p, "/")
// walk directories
cur := ""
for i := 0; i < len(parts)-1; i++ {
child := parts[i]
ensureDir(cur).subdirs[child] = struct{}{}
if cur == "" {
cur = child
} else {
cur = cur + "/" + child
}
ensureDir(cur) // ensure it exists
}
ensureDir(cur).files = append(ensureDir(cur).files, b)
}
// Prepare jobs slice to have stable iteration order (optional)
type job struct {
dirPath string
di *dirInfo
}
jobsSlice := make([]job, 0, len(dirs))
for dp, di := range dirs {
jobsSlice = append(jobsSlice, job{dirPath: dp, di: di})
}
// Sort by dirPath for determinism
sort.Slice(jobsSlice, func(i, j int) bool { return jobsSlice[i].dirPath < jobsSlice[j].dirPath })
// Worker pool similar to generateBlobs
workers := runtime.NumCPU()
if workers < 1 {
workers = 1
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
jobCh := make(chan job)
errCh := make(chan error, 1)
var wg sync.WaitGroup
p := progress_bar.NewProgressBar("lists for "+params.Ref.String(), len(jobsSlice))
check := func(err error) bool {
if err != nil {
select {
case errCh <- err:
cancel()
default:
}
return true
}
return false
}
workerFn := func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case jb, ok := <-jobCh:
if !ok {
return
}
func() {
dirPath := jb.dirPath
di := jb.di
outDir := filepath.Join(params.OutputDir, "blob", params.Ref.DirName())
if dirPath != "" {
// convert forward slash path into OS path
outDir = filepath.Join(outDir, filepath.FromSlash(dirPath))
}
if err := os.MkdirAll(outDir, 0o755); check(err) {
return
}
// Build entries
dirNames := make([]string, 0, len(di.subdirs))
for name := range di.subdirs {
dirNames = append(dirNames, name)
}
// Sort for stable output
sort.Strings(dirNames)
sort.Slice(di.files, func(i, j int) bool {
return di.files[i].FileName < di.files[j].FileName
})
subdirEntries := make([]templates.ListEntry, 0, len(dirNames))
for _, name := range dirNames {
subdirEntries = append(subdirEntries, templates.ListEntry{
Name: name + "/",
Href: name + "/index.html",
IsDir: true,
})
}
fileEntries := make([]templates.ListEntry, 0, len(di.files))
for _, b := range di.files {
fileEntries = append(fileEntries, templates.ListEntry{
Name: b.FileName + "",
Href: b.FileName + ".html",
Mode: b.Mode,
Size: humanizeSize(b.Size),
})
}
// Title and current path label
title := fmt.Sprintf("%s/%s at %s", params.Name, dirPath, params.Ref)
if dirPath == "" {
title = fmt.Sprintf("%s at %s", params.Name, params.Ref)
}
f, err := os.Create(filepath.Join(outDir, "index.html"))
if check(err) {
return
}
defer func() {
_ = f.Close()
}()
// parent link is not shown for root
parent := "../index.html"
if dirPath == "" {
parent = ""
}
depth := 0
if dirPath != "" {
depth = len(strings.Split(dirPath, "/"))
}
rootHref := strings.Repeat("../", depth+2)
readmeHTML := readme(di.files, dirsSet, filesSet, params, rootHref)
var CSSMarkdown template.CSS
if readmeHTML != "" {
CSSMarkdown = cssMarkdown(params.Dark)
}
err = templates.ListTemplate.ExecuteTemplate(f, "layout.gohtml", templates.ListParams{
LayoutParams: templates.LayoutParams{
Title: title,
Name: params.Name,
Dark: params.Dark,
CSSMarkdown: CSSMarkdown,
RootHref: rootHref,
CurrentRefDir: params.Ref.DirName(),
Selected: "code",
},
HeaderParams: templates.HeaderParams{
Ref: params.Ref,
Breadcrumbs: breadcrumbs(params.Name, dirPath, false),
},
Ref: params.Ref,
ParentHref: parent,
Dirs: subdirEntries,
Files: fileEntries,
Readme: readmeHTML,
})
if check(err) {
return
}
}()
p.Inc()
}
}
}
// Start workers
wg.Add(workers)
for i := 0; i < workers; i++ {
go workerFn()
}
// Feed jobs
go func() {
defer close(jobCh)
for _, jb := range jobsSlice {
select {
case <-ctx.Done():
return
case jobCh <- jb:
}
}
}()
// Wait for workers or first error
doneCh := make(chan struct{})
go func() {
wg.Wait()
close(doneCh)
}()
var runErr error
select {
case runErr = <-errCh:
<-doneCh
case <-doneCh:
}
p.Done()
return runErr
}