diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2018-08-17 11:17:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-17 11:17:46 +0200 |
commit | 7b6c1266556f59ac436fada3fa6106d4a84f9b56 (patch) | |
tree | f2f9226dc9436848c4b4855f65d91bd89dc987da /worktree_commit.go | |
parent | ba0f659cbf9982846de731cca426ce2498601130 (diff) | |
parent | 39954f2d9310204cc586715def7f075296a21d4c (diff) | |
download | go-git-7b6c1266556f59ac436fada3fa6106d4a84f9b56.tar.gz |
Merge pull request #920 from vancluever/f-add-commit-signkeyv4.6.0
git: Add ability to PGP sign commits
Diffstat (limited to 'worktree_commit.go')
-rw-r--r-- | worktree_commit.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/worktree_commit.go b/worktree_commit.go index 5fa63ab..b83bf0c 100644 --- a/worktree_commit.go +++ b/worktree_commit.go @@ -1,9 +1,11 @@ package git import ( + "bytes" "path" "strings" + "golang.org/x/crypto/openpgp" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/filemode" "gopkg.in/src-d/go-git.v4/plumbing/format/index" @@ -92,6 +94,14 @@ func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumb ParentHashes: opts.Parents, } + if opts.SignKey != nil { + sig, err := w.buildCommitSignature(commit, opts.SignKey) + if err != nil { + return plumbing.ZeroHash, err + } + commit.PGPSignature = sig + } + obj := w.r.Storer.NewEncodedObject() if err := commit.Encode(obj); err != nil { return plumbing.ZeroHash, err @@ -99,6 +109,22 @@ func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumb return w.r.Storer.SetEncodedObject(obj) } +func (w *Worktree) buildCommitSignature(commit *object.Commit, signKey *openpgp.Entity) (string, error) { + encoded := &plumbing.MemoryObject{} + if err := commit.Encode(encoded); err != nil { + return "", err + } + r, err := encoded.Reader() + if err != nil { + return "", err + } + var b bytes.Buffer + if err := openpgp.ArmoredDetachSign(&b, signKey, r, nil); err != nil { + return "", err + } + return b.String(), nil +} + // buildTreeHelper converts a given index.Index file into multiple git objects // reading the blobs from the given filesystem and creating the trees from the // index structure. The created objects are pushed to a given Storer. |