-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
353 lines (292 loc) · 7.22 KB
/
model.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
package main
import (
"fmt"
"github.com/patrickmn/go-cache"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"io"
"io/ioutil"
"net/url"
"path"
"strings"
"time"
)
func (b *Bilbo) getPageHistoryFromCommit(fileName string, onlyLast bool, commit plumbing.Hash) (commits []*object.Commit, err error) {
// Determine cache key
cacheKey := fmt.Sprintf("getPageHistoryFromCommit-%s-%t-%s", fileName, onlyLast, commit)
// Try to retrieve page from cache
cachedCommits, found := b.cache.Get(cacheKey)
if found {
commits = cachedCommits.([]*object.Commit)
return
}
ci, err := b.repo.Log(&git.LogOptions{})
if err != nil {
return
}
var (
currentCommit *object.Commit
currentCommitFile *object.File
lastCommit *object.Commit
lastCommitFile *object.File
)
lastCommit, err = ci.Next()
if err != nil {
return
}
fromCommitSeen := lastCommit.Hash == commit
for {
currentCommit, err = ci.Next()
if err != nil {
if err == io.EOF {
err = nil
break
}
return
}
if !fromCommitSeen {
if currentCommit.Hash == commit {
fromCommitSeen = true
} else {
continue
}
}
lastCommitFile, err = lastCommit.File(fileName)
if err != nil && err != object.ErrFileNotFound {
break
}
currentCommitFile, err = currentCommit.File(fileName)
if err != nil && err != object.ErrFileNotFound {
break
}
err = nil
if (lastCommitFile == nil && currentCommitFile != nil) ||
(lastCommitFile != nil && currentCommitFile == nil) ||
(lastCommitFile != nil && currentCommitFile != nil && lastCommitFile.Blob.Hash != currentCommitFile.Blob.Hash) {
commits = append(commits, lastCommit)
if onlyLast {
b.cache.Set(cacheKey, commits, cache.DefaultExpiration)
return
}
}
lastCommit = currentCommit
}
b.cache.Set(cacheKey, commits, cache.DefaultExpiration)
return
}
func (b *Bilbo) getPageAtCommit(fileName string, withLastCommit bool, commit plumbing.Hash) (page *Page, err error) {
// Determine cache key
cacheKey := fmt.Sprintf("getPageAtCommit-%s-%t-%s", fileName, withLastCommit, commit)
// Try to retrieve page from cache
cachedPage, found := b.cache.Get(cacheKey)
if found {
page = cachedPage.(*Page)
return
}
obj, err := b.repo.CommitObject(commit)
if err != nil {
return
}
iter, err := obj.Files()
if err != nil {
return
}
defer iter.Close()
cleanFileName := normalizePageLink(fileName, false)
var (
file *object.File
fileLink string
)
for {
file, err = iter.Next()
if err != nil {
return
}
// match without file extension
fileLink = strings.TrimSuffix(file.Name, path.Ext(file.Name))
if fileLink == cleanFileName {
break
}
}
// Read page into byte slice
reader, err := file.Blob.Reader()
if err != nil {
return
}
data, err := ioutil.ReadAll(reader)
if err != nil {
return
}
// Get file history
var (
history []*object.Commit
lastCommit *object.Commit
)
if withLastCommit {
history, err = b.getPageHistoryFromCommit(file.Name, true, commit)
if err != nil {
return
}
lastCommit = history[0]
}
// Create page
page = &Page{
LastCommit: lastCommit,
Filepath: file.Name,
Linkpath: normalizePageLink(fileLink, true),
Source: data,
Title: path.Base(fileLink),
}
// Cache page
b.cache.Set(cacheKey, page, cache.DefaultExpiration)
return
}
func (b *Bilbo) getPagesAtCommit(folderPath string, commit plumbing.Hash) (folderStructure *FolderStructure, err error) {
folderStructure = &FolderStructure{}
// Determine cache key
cacheKey := fmt.Sprintf("getPagesAtCommit-%s-%s", folderPath, commit)
// Try to retrieve directories and pages from cache
cachedFolderStructure, found := b.cache.Get(cacheKey)
if found {
folderStructure = cachedFolderStructure.(*FolderStructure)
return
}
obj, err := b.repo.CommitObject(commit)
if err != nil {
return
}
tree, err := obj.Tree()
if err != nil {
return
}
cleanFolderPath := path.Clean(folderPath)
if cleanFolderPath != "." {
folderStructure.Folders = append(folderStructure.Folders, &Folder{
Title: "..",
Linkpath: "..",
})
tree, err = tree.Tree(cleanFolderPath)
if err != nil {
return
}
}
var (
linkpath *url.URL
page *Page
)
for _, entry := range tree.Entries {
if entry.Mode == filemode.Dir {
linkpath, err = b.mux.Get("pages#index").URL("folder", path.Join(cleanFolderPath, entry.Name))
if err != nil {
return
}
folderStructure.Folders = append(folderStructure.Folders, &Folder{
Title: entry.Name,
Linkpath: linkpath.String(),
})
} else {
page, err = b.getPageAtCommit(path.Join(cleanFolderPath, entry.Name), false, commit)
if err != nil {
return
}
folderStructure.Pages = append(folderStructure.Pages, page)
}
}
// Cache folder structure
b.cache.Set(cacheKey, folderStructure, cache.DefaultExpiration)
return
}
func (b *Bilbo) updatePage(fileName string, data string, message string) (err error) {
// Make sure the file has an extension
pageFilePath := ensureFileNameHasExtension(fileName)
// Make sure the file stays within our data dir
absPageFilePath, err := ensureSaveFilePath(pageFilePath, b.cfg.DataDir, true)
if err != nil {
return
}
// Write to file
// TODO configurable permission mode
err = ioutil.WriteFile(absPageFilePath, []byte(data), 0644)
if err != nil {
return
}
// Stage file
wt, err := b.repo.Worktree()
if err != nil {
return
}
_, err = wt.Add(pageFilePath)
if err != nil {
return
}
// Create commit
// TODO author from config
_, err = wt.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: "Anonymous",
Email: "[email protected]",
When: time.Now(),
},
})
return
}
func (b *Bilbo) movePage(currentFileName string, nextFileName string, message string) (err error) {
// Make sure both files are within our data dir
absCurrentFileName, err := ensureSaveFilePath(currentFileName, b.cfg.DataDir, false)
if err != nil {
return
}
absNextFileName, err := ensureSaveFilePath(nextFileName, b.cfg.DataDir, false)
if err != nil {
return
}
// Make sure the new file has an extension
absNextFileName = ensureFileNameHasExtension(absNextFileName)
// Just try to move the file
wt, err := b.repo.Worktree()
if err != nil {
return
}
_, err = wt.Move(strings.TrimPrefix(absCurrentFileName, "/"), strings.TrimPrefix(absNextFileName, "/"))
if err != nil {
return
}
// Create commit
// TODO author from config
_, err = wt.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: "Anonymous",
Email: "[email protected]",
When: time.Now(),
},
})
return
}
func (b *Bilbo) deletePage(fileName string, message string) (err error) {
// Make sure file is within our data dir
absFileName, err := ensureSaveFilePath(fileName, b.cfg.DataDir, false)
if err != nil {
return
}
// Just try to delete the file
wt, err := b.repo.Worktree()
if err != nil {
return
}
_, err = wt.Remove(strings.TrimPrefix(absFileName, "/"))
if err != nil {
return
}
// Create commit
// TODO author from config
_, err = wt.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: "Anonymous",
Email: "[email protected]",
When: time.Now(),
},
})
return
}