-
Notifications
You must be signed in to change notification settings - Fork 2
/
git_write.go
158 lines (133 loc) · 3.58 KB
/
git_write.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
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
func (g *GitBackend) PutPage(title string, content []byte, user string, message string) error {
g.mutex.Lock()
defer g.mutex.Unlock()
filePath, gitPath, err := g.resolvePath(g.dir, fmt.Sprintf("%s.md", title))
if err != nil {
return err
}
return g.writeFile(filePath, gitPath, bytes.NewReader(content), user, message)
}
func (g *GitBackend) PutFile(name string, content io.ReadCloser, user string, message string) error {
g.mutex.Lock()
defer g.mutex.Unlock()
defer content.Close()
filePath, gitPath, err := g.resolvePath(g.dir, name)
if err != nil {
return err
}
return g.writeFile(filePath, gitPath, content, user, message)
}
func (g *GitBackend) PutConfig(name string, content []byte, user string, message string) error {
g.mutex.Lock()
defer g.mutex.Unlock()
filePath := filepath.Join(g.dir, ".wiki", fmt.Sprintf("%s.json.enc", name))
gitPath := filepath.Join(".wiki", fmt.Sprintf("%s.json.enc", name))
return g.writeFile(filePath, gitPath, bytes.NewReader(content), user, message)
}
func (g *GitBackend) writeFile(filePath, gitPath string, content io.Reader, user, message string) error {
if err := os.MkdirAll(filepath.Dir(filePath), os.FileMode(0755)); err != nil {
return err
}
f, err := os.Create(filePath)
if err != nil {
return err
}
if _, err := io.Copy(f, content); err != nil {
_ = f.Close()
return err
}
if err := f.Close(); err != nil {
return err
}
worktree, err := g.repo.Worktree()
if err != nil {
return err
}
if _, err := worktree.Add(gitPath); err != nil {
return err
}
_, err = worktree.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: user,
Email: user + "@wiki",
When: time.Now(),
},
})
return err
}
func (g *GitBackend) RenamePage(name string, newName string, message string, user string) error {
g.mutex.Lock()
defer g.mutex.Unlock()
_, gitPath, err := g.resolvePath(g.dir, fmt.Sprintf("%s.md", name))
if err != nil {
log.Printf("Unable to resolve old path: %s -> %s: %s", name, newName, err.Error())
return err
}
_, newGitPath, err := g.resolvePath(g.dir, fmt.Sprintf("%s.md", newName))
if err != nil {
log.Printf("Unable to resolve new path: %s -> %s: %s", name, newName, err.Error())
return err
}
worktree, err := g.repo.Worktree()
if err != nil {
log.Printf("Unable to get worktree: %s", err.Error())
return err
}
_, err = worktree.Move(gitPath, newGitPath)
if err != nil {
log.Printf("Unable to rename git: %s -> %s: %s", name, newName, err.Error())
return err
}
_, err = worktree.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: user,
Email: user + "@wiki",
When: time.Now(),
},
})
return nil
}
func (g *GitBackend) DeletePage(name string, message string, user string) error {
g.mutex.Lock()
defer g.mutex.Unlock()
return g.delete(fmt.Sprintf("%s.md", name), message, user)
}
func (g *GitBackend) DeleteFile(name string, message string, user string) error {
g.mutex.Lock()
defer g.mutex.Unlock()
return g.delete(name, message, user)
}
func (g *GitBackend) delete(name, message, user string) error {
_, gitPath, err := g.resolvePath(g.dir, name)
if err != nil {
return err
}
worktree, err := g.repo.Worktree()
if err != nil {
return err
}
_, err = worktree.Remove(gitPath)
if err != nil {
return err
}
_, err = worktree.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: user,
Email: user + "@wiki",
When: time.Now(),
},
})
return nil
}