This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
git.go
248 lines (214 loc) · 5.68 KB
/
git.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
package main
import (
pb "github.com/OUCC/SyaroNote/syaro/gitservice"
"github.com/libgit2/git2go"
"fmt"
"time"
)
const (
ENTRY_LIMIT = 100
)
func getRepo() *git.Repository {
repo, err := git.OpenRepository(repoRoot)
if err != nil {
log.Panic(err)
}
return repo
}
func commitChange(repo *git.Repository, manip func(idx *git.Index) error,
sig *git.Signature, message string) (*git.Commit, error) {
// staging and get index tree
idx, _ := repo.Index()
defer idx.Free()
if err := manip(idx); err != nil {
return nil, err
}
treeid, _ := idx.WriteTree()
tree, _ := repo.LookupTree(treeid)
defer tree.Free()
// get latest commit of current branch
parent := getLastCommit(repo)
if parent != nil {
defer parent.Free()
log.Debug("parent commit: %s", parent.Message())
} else {
log.Debug("parent not found (initial commit)")
}
// commit
var oid *git.Oid
var err error
if parent != nil {
oid, err = repo.CreateCommit("HEAD", sig, sig, message, tree, parent)
} else {
oid, err = repo.CreateCommit("HEAD", sig, sig, message, tree)
}
if err != nil {
log.Error("CreateCommit: %s", err)
return nil, err
}
// save index
idx.Write()
commit, _ := repo.LookupCommit(oid)
return commit, nil
}
// getLastCommit returns latest commit of current branch
func getLastCommit(repo *git.Repository) *git.Commit {
ref, err := repo.LookupReference("HEAD")
if err != nil {
log.Debug("HEAD not found: %s", err)
return nil
}
defer ref.Free()
ref, err = ref.Resolve()
if err != nil {
log.Debug("Couldn't resolve HEAD: %s", err)
return nil
}
var parent *git.Commit
if ref != nil {
parent, err = repo.LookupCommit(ref.Target())
if err != nil {
log.Debug("Failed to lookup HEAD: %s", err)
return nil
}
return parent
} else {
return nil
}
}
func getDefaultSignature(repo *git.Repository) *git.Signature {
config, _ := repo.Config()
defer config.Free()
name, err := config.LookupString("user.name")
if err != nil {
log.Error("failed to look up user.name: %s", err)
return nil
}
email, err := config.LookupString("user.email")
if err != nil {
log.Error("failed to look up user.email: %s", err)
return nil
}
return &git.Signature{
Name: name,
Email: email,
When: time.Now(),
}
}
func logCommit(c *git.Commit) {
log.Notice("Git commit created")
log.Info("Message: %s", c.Message())
log.Info("Author: %s <%s>", c.Author().Name, c.Author().Email)
log.Info("Committer: %s <%s>", c.Committer().Name, c.Committer().Email)
}
func getChanges(repo *git.Repository, wpath string) ([]*pb.Change, error) {
if wpath == "/" {
return getAllChanges(repo)
}
// setup revision walker
walk, _ := repo.Walk()
walk.Sorting(git.SortTopological | git.SortTime)
walk.PushHead()
// find out current oid of the wpath
head := getLastCommit(repo)
tree, _ := head.Tree()
entry, err := tree.EntryByPath(wpath[1:])
if err != nil {
return nil, fmt.Errorf("%s not found in HEAD", wpath)
}
oid := entry.Id
name := wpath[1:]
previous := head
changes := make([]*pb.Change, 0)
// revision walking func
fun := func(c *git.Commit) bool {
tree, _ := c.Tree()
found := false
// tree walking
tree.Walk(func(dir string, entry *git.TreeEntry) int {
if dir+entry.Name == name && entry.Id.Equal(oid) {
// found (not changed)
found = true
return -1 // end tree walking
} else if dir+entry.Name == name && !entry.Id.Equal(oid) { // found (by name)
// entry is found but its contents is differ
log.Debug("%s is updated in %s", name, previous.Id().String()[:7])
changes = append(changes, &pb.Change{
Op: pb.Change_OpUpdate,
Name: previous.Author().Name,
Email: previous.Author().Email,
Msg: previous.Message(),
})
oid = entry.Id.Copy()
found = true
return -1 // end tree walking
} else if entry.Id.Equal(oid) { // found (by oid)
// found a contents but its name is differ
log.Debug("%s is renamed to %s in %s", entry.Name, name, previous.Id().String()[:7])
changes = append(changes, &pb.Change{
Op: pb.Change_OpRename,
Name: previous.Author().Name,
Email: previous.Author().Email,
Msg: previous.Message(),
})
name = entry.Name
found = true
return -1 // end tree walking
}
return 0 // continue tree walking
})
if !found {
// contents not found
log.Debug("%s is added in %s", name, previous.Id().String()[:7])
changes = append(changes, &pb.Change{
Op: pb.Change_OpAdd,
Name: previous.Author().Name,
Email: previous.Author().Email,
Msg: previous.Message(),
})
return false // end rev walking
}
previous = c
if len(changes) == ENTRY_LIMIT { // limit number of entry
return false // end rev walking
}
return true // continue rev walking
}
if err := walk.Iterate(fun); err != nil {
return nil, fmt.Errorf("Error while rev walking: %s", err)
}
if l := len(changes); l < ENTRY_LIMIT && (l == 0 || changes[l-1].Op != pb.Change_OpAdd) {
// file is added in last commit
changes = append(changes, &pb.Change{
Op: pb.Change_OpAdd,
Name: previous.Author().Name,
Email: previous.Author().Email,
Msg: previous.Message(),
})
}
return changes, nil
}
func getAllChanges(repo *git.Repository) ([]*pb.Change, error) {
// setup revision walker
walk, _ := repo.Walk()
walk.Sorting(git.SortTopological | git.SortTime)
walk.PushHead()
changes := make([]*pb.Change, 0)
// walking func
fun := func(c *git.Commit) bool {
changes = append(changes, &pb.Change{
Op: pb.Change_OpNone,
Name: c.Author().Name,
Email: c.Author().Email,
Msg: c.Message(),
})
if len(changes) == ENTRY_LIMIT {
return false
}
return true
}
if err := walk.Iterate(fun); err != nil {
return nil, err
}
return changes, nil
}