-
Notifications
You must be signed in to change notification settings - Fork 2
/
git_read.go
60 lines (52 loc) · 1.15 KB
/
git_read.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/go-git/go-git/v5"
)
func (g *GitBackend) GetPage(title string) (*Page, error) {
g.mutex.RLock()
defer g.mutex.RUnlock()
filePath, gitPath, err := g.resolvePath(g.dir, fmt.Sprintf("%s.md", title))
if err != nil {
return nil, err
}
commitIter, err := g.repo.Log(&git.LogOptions{
PathFilter: func(s string) bool {
return s == gitPath
},
})
if err != nil {
return nil, err
}
commit, err := commitIter.Next()
if err != nil {
return nil, err
}
bytes, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
return &Page{
Content: bytes,
LastModified: &LogEntry{
ChangeId: commit.Hash.String(),
User: commit.Author.Name,
Time: commit.Author.When,
Message: commit.Message,
},
}, nil
}
func (g *GitBackend) GetFile(name string) (io.ReadCloser, error) {
filePath, _, err := g.resolvePath(g.dir, name)
if err != nil {
return nil, err
}
return os.Open(filePath)
}
func (g *GitBackend) GetConfig(name string) ([]byte, error) {
filePath := filepath.Join(g.dir, ".wiki", fmt.Sprintf("%s.json.enc", name))
return os.ReadFile(filePath)
}