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
|
package git2goutil
import (
"fmt"
git "github.com/libgit2/git2go/v33"
)
// CommitSubmitter is the helper struct to make signed Commits conveniently.
type CommitSubmitter struct {
Repo *git.Repository
SigningKeyPath string
}
// NewCommitSubmitter creates a new CommitSubmitter.
func NewCommitSubmitter(repo *git.Repository, signingKeyPath string) *CommitSubmitter {
return &CommitSubmitter{
Repo: repo,
SigningKeyPath: signingKeyPath,
}
}
// Commit commits a commit with or without OpenPGP signature depends on SigningKeyPath value.
func (cs *CommitSubmitter) Commit(
author, committer *git.Signature,
messageEncoding git.MessageEncoding,
message string,
tree *git.Tree,
parents ...*git.Commit,
) (*git.Oid, error) {
commitBytes, err := cs.Repo.CreateCommitBuffer(author, committer, messageEncoding, message, tree, parents...)
if err != nil {
return nil, err
}
signature, err := CreateCommitSignature(cs.SigningKeyPath, string(commitBytes))
if err != nil {
return nil, fmt.Errorf("create commit signature: %w", err)
}
commitID, err := cs.Repo.CreateCommitWithSignature(string(commitBytes), signature, "")
if err != nil {
return nil, err
}
return commitID, nil
}
|