-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
61 lines (49 loc) · 1.31 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
package main
import (
"fmt"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pkg/errors"
)
const (
authorCloak = `[email protected]`
committerName = `Twitch-Bot %s`
committerEmail = `[email protected]`
)
type gitHelper struct {
repoDir string
}
func newGitHelper(repoDir string) *gitHelper {
return &gitHelper{
repoDir: repoDir,
}
}
func (g gitHelper) CommitChange(filename, authorName, authorEmail, summary string) error {
repo, err := git.PlainOpen(g.repoDir)
if err != nil {
return errors.Wrap(err, "opening git repo")
}
wt, err := repo.Worktree()
if err != nil {
return errors.Wrap(err, "getting worktree")
}
if _, err = wt.Add(filename); err != nil {
return errors.Wrap(err, "adding file to index")
}
if authorEmail == "" {
authorEmail = authorCloak
}
_, err = wt.Commit(summary, &git.CommitOptions{
Author: g.getSignature(authorName, authorEmail),
Committer: g.getSignature(fmt.Sprintf(committerName, version), committerEmail),
})
return errors.Wrap(err, "issuing commit")
}
func (g gitHelper) HasRepo() bool {
_, err := git.PlainOpen(g.repoDir)
return err == nil
}
func (gitHelper) getSignature(name, mail string) *object.Signature {
return &object.Signature{Name: name, Email: mail, When: time.Now()}
}