This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitstorage.go
486 lines (410 loc) · 10.3 KB
/
gitstorage.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright 2014 Daniel Kertesz <daniel@spatof.org>
// All rights reserved. This program comes with ABSOLUTELY NO WARRANTY.
// See the file LICENSE for details.
package spock
import (
"errors"
"fmt"
"github.com/libgit2/git2go"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
type GitStorage struct {
WorkDir string
r *git.Repository
}
// Create a new git repository, initializing it.
func CreateGitStorage(path string) (*GitStorage, error) {
repo, err := git.InitRepository(path, false)
if err != nil {
return nil, err
}
gitstorage := &GitStorage{WorkDir: path, r: repo}
return gitstorage, nil
}
// Open an existing git repository, optionally creating a new one if the
// specified directory is not found and 'create' is true.
func OpenGitStorage(path string, create bool) (*GitStorage, error) {
if _, err := os.Stat(filepath.Join(path, ".git")); err != nil {
if create {
return CreateGitStorage(path)
} else {
return nil, err
}
}
repo, err := git.OpenRepository(path)
if err != nil {
return nil, err
}
gitstorage := &GitStorage{WorkDir: path, r: repo}
return gitstorage, nil
}
func (gs *GitStorage) MakeAbsPath(path string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Join(gs.WorkDir, path)
}
func (gs *GitStorage) JoinPath(relpath string) (string, error) {
res := filepath.Join(gs.WorkDir, relpath)
if !strings.HasPrefix(res, gs.WorkDir) {
return "", errors.New("Requested file outside repository directory")
}
return res, nil
}
// Returns the last (root) commit and tree objects.
func (gs *GitStorage) currentState() (commit *git.Commit, tree *git.Tree, err error) {
var head *git.Reference
head, err = gs.r.Head()
if err != nil {
return
}
commit, err = gs.r.LookupCommit(head.Target())
if err != nil {
return
}
tree, err = commit.Tree()
return
}
// Returns true if the git repository has a "root commit" (i.e. the so called
// initial commit).
func (gs *GitStorage) hasRootCommit() bool {
refname := "refs/heads/master"
_, err := gs.r.LookupReference(refname)
if err != nil {
return false
}
return true
}
func (gs *GitStorage) saveIndex(idx *git.Index, signature *CommitSignature, message string) (*git.Oid, error) {
sig := &git.Signature{
Name: signature.Name,
Email: signature.Email,
When: signature.When,
}
treeId, err := idx.WriteTree()
if err != nil {
return nil, err
}
// http://stackoverflow.com/questions/16056759/untracked-dirs-on-commit-with-pygit2
// We need to also call Write() to avoid leaving "untracked files".
if err = idx.Write(); err != nil {
return nil, err
}
tree, err := gs.r.LookupTree(treeId)
if err != nil {
return nil, err
}
var commitId *git.Oid
if gs.hasRootCommit() {
var currentTip *git.Commit
currentTip, _, err = gs.currentState()
if err != nil {
return nil, err
}
commitId, err = gs.r.CreateCommit("HEAD", sig, sig, message, tree, currentTip)
} else {
commitId, err = gs.r.CreateCommit("HEAD", sig, sig, message, tree)
}
return commitId, err
}
func (gs *GitStorage) CommitFile(path string, signature *CommitSignature, message string) (revId RevID, err error) {
idx, err := gs.r.Index()
if err != nil {
return
}
// XXX should we "RemoveByPath()" on error condition ?
if err = idx.AddByPath(path); err != nil {
return
}
commitId, err := gs.saveIndex(idx, signature, message)
if err != nil {
return
}
revId = RevID(commitId.String())
return
}
func (gs *GitStorage) RenamePage(origPath, destPath string, signature *CommitSignature, message string) (revId RevID, err error) {
idx, err := gs.r.Index()
if err != nil {
return
}
// 1. rename file
// 2. add renamed file to index
// 3. remove old file from index (from the docs we see "it may exists")
// 4. commit
if err = os.Rename(gs.MakeAbsPath(origPath), gs.MakeAbsPath(destPath)); err != nil {
return
}
if err = idx.AddByPath(destPath); err != nil {
return
}
if err = idx.RemoveByPath(origPath); err != nil {
return
}
commitId, err := gs.saveIndex(idx, signature, message)
if err != nil {
return
}
revId = RevID(commitId.String())
return
}
func (gs *GitStorage) DeletePage(path string, signature *CommitSignature, message string) (revId RevID, err error) {
idx, err := gs.r.Index()
if err != nil {
return
}
if err = os.Remove(gs.MakeAbsPath(path)); err != nil {
return
}
if err = idx.RemoveByPath(path); err != nil {
return
}
commitId, err := gs.saveIndex(idx, signature, message)
if err != nil {
return
}
revId = RevID(commitId.String())
return
}
func extractCommitLog(commit *git.Commit) *CommitLog {
author := commit.Author()
return &CommitLog{
Message: commit.Message(),
Name: author.Name,
Email: author.Email,
When: author.When,
Id: commit.Id().String(),
}
}
func (gs *GitStorage) LogsForPage(path string) (result []CommitLog, err error) {
var oidList []git.Oid
var commitMap = make(map[git.Oid]*git.Commit)
walker, err := gs.r.Walk()
if err != nil {
return
}
if err = walker.PushHead(); err != nil {
return
}
err = walker.Iterate(func(commit *git.Commit) bool {
tree, err := commit.Tree()
if err != nil {
log.Println("LogsForPage: cannot get tree of walked commit:", err)
return false
}
entry, err := tree.EntryByPath(path)
if err != nil {
// the requested file does not exists in this tree, stop the walk
return false
}
_, found := commitMap[*entry.Id]
if !found {
oidList = append(oidList, *entry.Id)
}
commitMap[*entry.Id] = commit
return true
})
if err != nil {
return
}
for _, oid := range oidList {
commit := commitMap[oid]
cl := extractCommitLog(commit)
result = append(result, *cl)
}
return
}
// LookupPage is used to fetch pages from the wiki storage. The "relpath"
// argument refers to a page path relative to the root of the wiki; for
// example "notes/Linux" and "/notes/Linux" both refers to the "Linux"
// page found inside the "/notes" wiki directory. Remember that wiki
// pages are qualified by their "wiki filename", that is the page filename
// without the extension.
//
// Paths are checked for directory traversal attacks and an error will be
// returned if the "relpath" argument refers to a file outside the wiki
// repository.
func (gs *GitStorage) LookupPage(relpath string) (*Page, bool, error) {
abspath, err := gs.JoinPath(relpath)
if err != nil {
return nil, false, err
}
for i := range PAGE_EXTENSIONS {
filename := abspath + "." + PAGE_EXTENSIONS[i]
relfilename := relpath + "." + PAGE_EXTENSIONS[i]
if _, err := os.Stat(filename); err == nil {
page, err := LoadPage(filename, relfilename)
if err != nil {
return nil, true, err
}
return page, true, nil
}
}
filename := relpath + "." + DefaultExtension
page := NewPage(filename)
return page, false, nil
}
type OidSet struct {
set map[*git.Oid]bool
}
func NewOidSet() *OidSet {
return &OidSet{set: make(map[*git.Oid]bool)}
}
func (o *OidSet) Add(oid *git.Oid) bool {
_, found := o.set[oid]
o.set[oid] = true
return !found
}
func (gs *GitStorage) GetLastCommit(path string) (*CommitLog, error) {
commit, tree, err := gs.currentState()
if err != nil {
return nil, err
}
blob, err := tree.EntryByPath(path)
if err != nil {
return nil, err
}
visited := NewOidSet()
var queue []*git.Commit
var cc *git.Commit
visited.Add(blob.Id)
stop := false
queue = append(queue, commit)
for {
if len(queue) == 0 {
break
}
cc = queue[0]
queue = queue[1:]
var i uint
for i = 0; i < cc.ParentCount(); i++ {
parent := cc.Parent(i)
if parent == nil {
log.Fatal("parent = nil")
}
ptree, err := parent.Tree()
if err != nil {
return nil, err
}
pblob, err := ptree.EntryByPath(path)
if err != nil {
continue
}
if !blob.Id.Equal(pblob.Id) {
stop = true
} else {
if rv := visited.Add(parent.TreeId()); rv {
queue = append(queue, parent)
}
}
}
if stop {
break
}
}
return extractCommitLog(cc), nil
}
func (gs *GitStorage) SavePage(page *Page, sig *CommitSignature, message string) error {
fullpath := filepath.Join(gs.WorkDir, page.Path)
if err := MkMissingDirs(fullpath); err != nil {
return err
}
if err := ioutil.WriteFile(fullpath, page.RawBytes, 0644); err != nil {
return err
}
_, err := gs.CommitFile(page.Path, sig, message)
return err
}
func (gs *GitStorage) ListPages() ([]string, error) {
var result []string
// Return early if we are on a new repository (i.e. one without a "root"
// commit).
if !gs.hasRootCommit() {
return result, nil
}
exts := make(map[string]bool)
for _, ext := range PAGE_EXTENSIONS {
exts["."+ext] = true
}
_, tree, err := gs.currentState()
if err != nil {
return result, err
}
err = tree.Walk(func(root string, t *git.TreeEntry) int {
switch git.Filemode(t.Filemode) {
case git.FilemodeBlob, git.FilemodeBlobExecutable:
pageext := filepath.Ext(t.Name)
if len(pageext) > 0 {
if _, ok := exts[pageext]; ok {
result = append(result, ShortenPageName(root+t.Name))
}
}
}
// to avoid going into sibdirectories return 1
return 0
})
return result, err
}
// Return the git.Tree of the specified SHA id.
func (gs *GitStorage) treeFromId(id string) (*git.Tree, error) {
oid, err := git.NewOid(id)
if err != nil {
return nil, err
}
commit, err := gs.r.LookupCommit(oid)
if err != nil {
return nil, err
}
return commit.Tree()
}
func (gs *GitStorage) DiffPage(page *Page, revA, revB string) ([]string, error) {
// commit A
oldTree, err := gs.treeFromId(revA)
if err != nil {
return nil, err
}
// commit B
newTree, err := gs.treeFromId(revB)
if err != nil {
return nil, err
}
// run git diff
diffopts, err := git.DefaultDiffOptions()
if err != nil {
return nil, err
}
diff, err := gs.r.DiffTreeToTree(newTree, oldTree, &diffopts)
if err != nil {
return nil, err
}
// we can't know in advance how many deltas are useful to us inside this diff.
result := make([]string, 0)
dlen, err := diff.NumDeltas()
if err != nil {
return nil, err
}
for i := 0; i < dlen; i++ {
delta, err := diff.GetDelta(i)
if err != nil {
return nil, err
}
// skip patches for other files
if delta.OldFile.Path != page.Path {
continue
}
patch, err := diff.Patch(i)
if err != nil {
return nil, err
}
if patchStr, err := patch.String(); err == nil {
result = append(result, patchStr)
} else {
fmt.Print(err)
}
}
return result, nil
}