-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit.go
More file actions
80 lines (71 loc) · 1.41 KB
/
git.go
File metadata and controls
80 lines (71 loc) · 1.41 KB
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
package main
import (
"io"
"os"
"path"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
var (
StateDir = "state"
GitRepo = "https://git.kernel.org/pub/scm/infra/transparency-logs/gitolite/git/1.git"
GitDir = path.Join(StateDir, path.Base(GitRepo))
)
type TlogRepo struct {
Git *git.Repository
}
func GetRepo() (*TlogRepo, error) {
if _, err := os.Stat(GitDir); err == nil {
repo, err := git.PlainOpen(GitDir)
if err != nil {
return nil, err
}
return &TlogRepo{repo}, nil
}
repo, err := git.PlainClone(GitDir, false, &git.CloneOptions{
URL: GitRepo,
Progress: os.Stdout,
})
if err != nil {
return nil, err
}
return &TlogRepo{repo}, nil
}
func (tr *TlogRepo) Pull() error {
w, err := tr.Git.Worktree()
if err != nil {
return err
}
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
if err != nil {
return err
}
return nil
}
func (tr *TlogRepo) LogSince(since *time.Time, fn func(c *object.Commit, r io.Reader) error) error {
cIter, err := tr.Git.Log(&git.LogOptions{Since: since})
if err != nil {
return err
}
err = cIter.ForEach(func(c *object.Commit) error {
f, err := c.File("m")
if err != nil {
return err
}
blob, err := f.Contents()
if err != nil {
return err
}
r := strings.NewReader(blob)
if err := fn(c, r); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}