2020-02-15 22:13:50 +00:00
|
|
|
package dehub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"dehub/accessctl"
|
|
|
|
"dehub/fs"
|
|
|
|
"dehub/sigcred"
|
2020-03-04 23:34:02 +00:00
|
|
|
"dehub/typeobj"
|
2020-02-15 22:13:50 +00:00
|
|
|
"encoding/base64"
|
2020-03-14 22:14:18 +00:00
|
|
|
"errors"
|
2020-02-15 22:13:50 +00:00
|
|
|
"fmt"
|
2020-03-21 18:24:38 +00:00
|
|
|
"reflect"
|
2020-03-21 17:22:15 +00:00
|
|
|
"sort"
|
2020-03-21 18:24:38 +00:00
|
|
|
"strings"
|
2020-02-15 22:13:50 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/src-d/go-git.v4"
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2020-03-04 23:34:02 +00:00
|
|
|
// CommitInterface describes the methods which must be implemented by the
|
|
|
|
// different commit types.
|
|
|
|
type CommitInterface interface {
|
|
|
|
// MessageHead returns the head of the commit message (i.e. the first line).
|
2020-03-21 17:22:15 +00:00
|
|
|
// The CommitCommon of the outer Commit is passed in for added context, if
|
|
|
|
// necessary.
|
|
|
|
MessageHead(CommitCommon) (string, error)
|
2020-03-04 23:34:02 +00:00
|
|
|
|
|
|
|
// Hash returns the raw hash which Signifiers can sign to accredit this
|
|
|
|
// commit. The tree objects given describe the filesystem state of the
|
|
|
|
// parent commit, and the filesystem state of this commit.
|
|
|
|
//
|
|
|
|
// This method should _not_ change any fields on the commit.
|
|
|
|
Hash(parent, this *object.Tree) ([]byte, error)
|
|
|
|
|
|
|
|
// GetHash returns the signable Hash embedded in the commit, which should
|
|
|
|
// hopefully correspond to the Commit's Credentials.
|
|
|
|
GetHash() []byte
|
|
|
|
}
|
|
|
|
|
2020-03-21 17:22:15 +00:00
|
|
|
// CommitCommon describes the fields common to all Commit objects.
|
|
|
|
type CommitCommon struct {
|
|
|
|
// Credentials represent all created Credentials for this commit, and can be
|
|
|
|
// set on all Commit objects regardless of other fields being set.
|
|
|
|
Credentials []sigcred.Credential `yaml:"credentials"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc CommitCommon) credAccountIDs() []string {
|
|
|
|
m := map[string]struct{}{}
|
|
|
|
for _, cred := range cc.Credentials {
|
|
|
|
m[cred.AccountID] = struct{}{}
|
|
|
|
}
|
|
|
|
s := make([]string, 0, len(m))
|
|
|
|
for accountID := range m {
|
|
|
|
s = append(s, accountID)
|
|
|
|
}
|
|
|
|
sort.Strings(s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-03-21 18:24:38 +00:00
|
|
|
func abbrevCommitMessage(msg string) string {
|
|
|
|
i := strings.Index(msg, "\n")
|
|
|
|
if i > 0 {
|
|
|
|
msg = msg[:i]
|
|
|
|
}
|
|
|
|
if len(msg) > 80 {
|
|
|
|
msg = msg[:80] + "..."
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2020-03-04 23:34:02 +00:00
|
|
|
// Commit represents a single Commit which is being added to a branch. Only one
|
|
|
|
// field should be set on a Commit, unless otherwise noted.
|
|
|
|
type Commit struct {
|
2020-03-05 23:43:40 +00:00
|
|
|
Change *CommitChange `type:"change,default"`
|
|
|
|
Credential *CommitCredential `type:"credential"`
|
2020-03-21 18:24:38 +00:00
|
|
|
Comment *CommitComment `type:"comment"`
|
2020-03-04 23:34:02 +00:00
|
|
|
|
2020-03-21 17:22:15 +00:00
|
|
|
Common CommitCommon `yaml:",inline"`
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 23:34:02 +00:00
|
|
|
// MarshalYAML implements the yaml.Marshaler interface.
|
|
|
|
func (c Commit) MarshalYAML() (interface{}, error) {
|
|
|
|
return typeobj.MarshalYAML(c)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 23:34:02 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *Commit) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
return typeobj.UnmarshalYAML(c, unmarshal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface returns the CommitInterface instance encapsulated by this Commit
|
|
|
|
// object.
|
|
|
|
func (c Commit) Interface() (CommitInterface, error) {
|
|
|
|
el, _, err := typeobj.Element(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
2020-03-04 23:34:02 +00:00
|
|
|
return el.(CommitInterface), nil
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 22:35:32 +00:00
|
|
|
// Type returns the Commit's type (as would be used in its YAML "type" field).
|
|
|
|
func (c Commit) Type() (string, error) {
|
|
|
|
_, typeStr, err := typeobj.Element(c)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return typeStr, nil
|
|
|
|
}
|
|
|
|
|
2020-02-15 22:13:50 +00:00
|
|
|
// MarshalText implements the encoding.TextMarshaler interface by returning the
|
2020-03-04 23:34:02 +00:00
|
|
|
// form the Commit object takes in the git commit message.
|
|
|
|
func (c Commit) MarshalText() ([]byte, error) {
|
|
|
|
commitInt, err := c.Interface()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not cast Commit %+v to interface : %w", c, err)
|
|
|
|
}
|
|
|
|
|
2020-03-21 17:22:15 +00:00
|
|
|
msgHead, err := commitInt.MessageHead(c.Common)
|
2020-03-04 23:34:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error constructing message head: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msgBodyB, err := yaml.Marshal(c)
|
2020-02-15 22:13:50 +00:00
|
|
|
if err != nil {
|
2020-03-04 23:34:02 +00:00
|
|
|
return nil, fmt.Errorf("error marshaling commit %+v as yaml: %w", c, err)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 23:34:02 +00:00
|
|
|
w := new(bytes.Buffer)
|
|
|
|
w.WriteString(msgHead)
|
|
|
|
w.WriteString("\n\n---\n")
|
|
|
|
w.Write(msgBodyB)
|
|
|
|
return w.Bytes(), nil
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText implements the encoding.TextUnmarshaler interface by decoding a
|
2020-03-04 23:34:02 +00:00
|
|
|
// Commit object which has been encoded into a git commit message.
|
|
|
|
func (c *Commit) UnmarshalText(msg []byte) error {
|
2020-02-15 22:13:50 +00:00
|
|
|
i := bytes.Index(msg, []byte("\n"))
|
|
|
|
if i < 0 {
|
2020-03-04 23:34:02 +00:00
|
|
|
return fmt.Errorf("commit message %q is malformed, it has no body", msg)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
2020-03-04 23:34:02 +00:00
|
|
|
msgBody := msg[i:]
|
2020-02-15 22:13:50 +00:00
|
|
|
|
2020-03-04 23:34:02 +00:00
|
|
|
if err := yaml.Unmarshal(msgBody, c); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal Commit message from yaml: %w", err)
|
2020-03-21 18:24:38 +00:00
|
|
|
|
|
|
|
} else if reflect.DeepEqual(*c, Commit{}) {
|
|
|
|
// a basic check, but worthwhile
|
|
|
|
return errors.New("commit message is malformed, could not unmarshal yaml object")
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
2020-03-21 18:24:38 +00:00
|
|
|
|
2020-03-04 23:34:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-02-15 22:13:50 +00:00
|
|
|
|
2020-03-04 23:34:02 +00:00
|
|
|
// AccreditCommit returns the given Commit with an appended Credential provided
|
2020-03-13 21:24:46 +00:00
|
|
|
// by the given SignifierInterface.
|
|
|
|
func (r *Repo) AccreditCommit(commit Commit, sigInt sigcred.SignifierInterface) (Commit, error) {
|
2020-03-04 23:34:02 +00:00
|
|
|
commitInt, err := commit.Interface()
|
|
|
|
if err != nil {
|
|
|
|
return commit, fmt.Errorf("could not cast commit %+v to interface: %w", commit, err)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
headFS, err := r.headFS()
|
2020-03-04 23:34:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return commit, fmt.Errorf("could not grab snapshot of HEAD fs: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cred, err := sigInt.Sign(headFS, commitInt.GetHash())
|
|
|
|
if err != nil {
|
2020-03-14 22:14:18 +00:00
|
|
|
return commit, fmt.Errorf("could not accredit change commit: %w", err)
|
2020-03-04 23:34:02 +00:00
|
|
|
}
|
2020-03-21 17:22:15 +00:00
|
|
|
commit.Common.Credentials = append(commit.Common.Credentials, cred)
|
2020-03-04 23:34:02 +00:00
|
|
|
return commit, nil
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 23:54:25 +00:00
|
|
|
// CommitBareParams are the parameters to the CommitBare method. All are
|
|
|
|
// required, unless otherwise noted.
|
|
|
|
type CommitBareParams struct {
|
|
|
|
Commit Commit
|
2020-03-23 22:49:01 +00:00
|
|
|
Author string
|
2020-03-22 23:54:25 +00:00
|
|
|
ParentHash plumbing.Hash // can be zero if the commit has no parents (Q_Q)
|
|
|
|
GitTree *object.Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitBare constructs a git commit object and and stores it, returning the
|
|
|
|
// resulting GitCommit. This method does not interact with HEAD at all.
|
|
|
|
func (r *Repo) CommitBare(params CommitBareParams) (GitCommit, error) {
|
|
|
|
msgB, err := params.Commit.MarshalText()
|
2020-02-16 17:28:59 +00:00
|
|
|
if err != nil {
|
2020-03-22 23:54:25 +00:00
|
|
|
return GitCommit{}, fmt.Errorf("encoding %T to message string: %w",
|
|
|
|
params.Commit, err)
|
2020-02-16 17:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 23:54:25 +00:00
|
|
|
author := object.Signature{
|
2020-03-23 22:49:01 +00:00
|
|
|
Name: params.Author,
|
2020-03-22 23:54:25 +00:00
|
|
|
When: time.Now(),
|
|
|
|
}
|
|
|
|
commit := &object.Commit{
|
|
|
|
Author: author,
|
|
|
|
Committer: author,
|
|
|
|
Message: string(msgB),
|
|
|
|
TreeHash: params.GitTree.Hash,
|
|
|
|
}
|
|
|
|
if params.ParentHash != plumbing.ZeroHash {
|
|
|
|
commit.ParentHashes = []plumbing.Hash{params.ParentHash}
|
|
|
|
}
|
|
|
|
|
|
|
|
commitObj := r.GitRepo.Storer.NewEncodedObject()
|
|
|
|
if err := commit.Encode(commitObj); err != nil {
|
|
|
|
return GitCommit{}, fmt.Errorf("encoding commit object: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
commitHash, err := r.GitRepo.Storer.SetEncodedObject(commitObj)
|
2020-02-16 17:28:59 +00:00
|
|
|
if err != nil {
|
2020-03-22 23:54:25 +00:00
|
|
|
return GitCommit{}, fmt.Errorf("setting encoded object: %w", err)
|
2020-02-16 17:28:59 +00:00
|
|
|
}
|
2020-03-22 23:54:25 +00:00
|
|
|
|
|
|
|
return r.GetGitCommit(commitHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit uses the given Commit to create a git commit object (with the
|
|
|
|
// specified accountID as the author) and commits it to the current HEAD,
|
|
|
|
// returning the full GitCommit.
|
|
|
|
func (r *Repo) Commit(commit Commit, accountID string) (GitCommit, error) {
|
|
|
|
headRef, err := r.TraverseReferenceChain(plumbing.HEAD, func(ref *plumbing.Reference) bool {
|
|
|
|
return ref.Type() == plumbing.HashReference
|
2020-02-16 17:28:59 +00:00
|
|
|
})
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
if err != nil {
|
2020-03-22 23:54:25 +00:00
|
|
|
return GitCommit{}, fmt.Errorf("resolving HEAD to a hash reference: %w", err)
|
|
|
|
}
|
|
|
|
headRefName := headRef.Name()
|
|
|
|
|
|
|
|
headHash, err := r.ReferenceToHash(headRefName)
|
|
|
|
if err != nil {
|
|
|
|
return GitCommit{}, fmt.Errorf("resolving ref %q (HEAD): %w", headRefName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO this is also used in the same way in NewCommitChange. It might make
|
|
|
|
// sense to refactor this logic out, it might not be needed in fs at all.
|
|
|
|
_, stagedTree, err := fs.FromStagedChangesTree(r.GitRepo)
|
|
|
|
if err != nil {
|
|
|
|
return GitCommit{}, fmt.Errorf("getting staged changes: %w", err)
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 23:54:25 +00:00
|
|
|
gitCommit, err := r.CommitBare(CommitBareParams{
|
|
|
|
Commit: commit,
|
2020-03-23 22:49:01 +00:00
|
|
|
Author: accountID,
|
2020-03-22 23:54:25 +00:00
|
|
|
ParentHash: headHash,
|
|
|
|
GitTree: stagedTree,
|
|
|
|
})
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
if err != nil {
|
2020-03-22 23:54:25 +00:00
|
|
|
return GitCommit{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// now set the branch to this new commit
|
|
|
|
newHeadRef := plumbing.NewHashReference(headRefName, gitCommit.GitCommit.Hash)
|
|
|
|
if err := r.GitRepo.Storer.SetReference(newHeadRef); err != nil {
|
|
|
|
return GitCommit{}, fmt.Errorf("setting reference %q to new commit hash %q: %w",
|
|
|
|
headRefName, gitCommit.GitCommit.Hash, err)
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
}
|
2020-03-22 23:54:25 +00:00
|
|
|
|
|
|
|
return gitCommit, nil
|
2020-02-16 17:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 00:37:19 +00:00
|
|
|
// HasStagedChanges returns true if there are file changes which have been
|
|
|
|
// staged (e.g. via "git add").
|
|
|
|
func (r *Repo) HasStagedChanges() (bool, error) {
|
|
|
|
w, err := r.GitRepo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error retrieving worktree: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := w.Status()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error retrieving worktree status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var any bool
|
|
|
|
for _, fileStatus := range status {
|
|
|
|
if fileStatus.Staging != git.Unmodified {
|
|
|
|
any = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return any, nil
|
|
|
|
}
|
|
|
|
|
2020-03-14 22:14:18 +00:00
|
|
|
type verificationCtx struct {
|
|
|
|
commit *object.Commit
|
|
|
|
commitTree, parentTree *object.Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
// non-gophers gonna hate on this method, but I say it's fine
|
|
|
|
func (r *Repo) verificationCtx(h plumbing.Hash) (vctx verificationCtx, err error) {
|
|
|
|
if vctx.commit, err = r.GitRepo.CommitObject(h); err != nil {
|
|
|
|
return vctx, fmt.Errorf("retrieving commit object: %w", err)
|
|
|
|
|
|
|
|
} else if vctx.commitTree, err = r.GitRepo.TreeObject(vctx.commit.TreeHash); err != nil {
|
|
|
|
return vctx, fmt.Errorf("retrieving commit tree object %q: %w",
|
|
|
|
vctx.commit.TreeHash, err)
|
|
|
|
|
|
|
|
} else if parent, err := vctx.commit.Parent(0); err != nil {
|
|
|
|
return vctx, fmt.Errorf("retrieving commit parent: %w", err)
|
|
|
|
|
|
|
|
} else if vctx.parentTree, err = r.GitRepo.TreeObject(parent.TreeHash); err != nil {
|
|
|
|
return vctx, fmt.Errorf("retrieving commit parent tree object %q: %w",
|
|
|
|
parent.Hash, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vctx, nil
|
|
|
|
}
|
|
|
|
|
2020-02-15 22:13:50 +00:00
|
|
|
func (r *Repo) assertAccessControls(
|
2020-03-18 22:35:32 +00:00
|
|
|
acl []accessctl.AccessControl,
|
2020-03-14 22:14:18 +00:00
|
|
|
commit Commit, vctx verificationCtx, branch plumbing.ReferenceName,
|
|
|
|
) (err error) {
|
|
|
|
filesChanged, err := calcDiff(vctx.parentTree, vctx.commitTree)
|
2020-02-15 22:13:50 +00:00
|
|
|
if err != nil {
|
2020-03-14 22:14:18 +00:00
|
|
|
return fmt.Errorf("calculating diff from tree %q to tree %q: %w",
|
|
|
|
vctx.parentTree.Hash, vctx.commitTree.Hash, err)
|
|
|
|
|
|
|
|
} else if len(filesChanged) > 0 && commit.Change == nil {
|
|
|
|
return errors.New("files changes but commit is not a change commit")
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pathsChanged := make([]string, len(filesChanged))
|
|
|
|
for i := range filesChanged {
|
|
|
|
pathsChanged[i] = filesChanged[i].path
|
|
|
|
}
|
|
|
|
|
2020-03-18 22:35:32 +00:00
|
|
|
commitType, err := commit.Type()
|
2020-02-15 22:13:50 +00:00
|
|
|
if err != nil {
|
2020-03-18 22:35:32 +00:00
|
|
|
return fmt.Errorf("determining type of commit %+v: %w", commit, err)
|
2020-03-14 22:14:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 22:35:32 +00:00
|
|
|
return accessctl.AssertCanCommit(acl, accessctl.CommitRequest{
|
|
|
|
Type: commitType,
|
|
|
|
Branch: branch.Short(),
|
2020-03-21 17:22:15 +00:00
|
|
|
Credentials: commit.Common.Credentials,
|
2020-03-18 22:35:32 +00:00
|
|
|
FilesChanged: pathsChanged,
|
|
|
|
})
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
// VerifyCommits verifies that the given commits, which are presumably on the
|
|
|
|
// given branch, are gucci.
|
|
|
|
func (r *Repo) VerifyCommits(branch plumbing.ReferenceName, gitCommits []GitCommit) error {
|
|
|
|
|
|
|
|
for i, gitCommit := range gitCommits {
|
|
|
|
// It's not a requirement that the given GitCommits are in ancestral
|
|
|
|
// order, but usually they are, so we can help verifyCommit not have to
|
|
|
|
// calculate the parentTree if the previous commit is the parent of this
|
|
|
|
// one.
|
|
|
|
var parentTree *object.Tree
|
|
|
|
if i > 0 && gitCommits[i-1].GitCommit.Hash == gitCommit.GitCommit.ParentHashes[0] {
|
|
|
|
parentTree = gitCommits[i-1].GitTree
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.verifyCommit(branch, gitCommit, parentTree); err != nil {
|
|
|
|
return fmt.Errorf("verifying commit %q: %w",
|
|
|
|
gitCommit.GitCommit.Hash, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:54:25 +00:00
|
|
|
// parentTree returns the tree of the parent commit of the given commit. If the
|
|
|
|
// given commit has no parents then a bare tree is returned.
|
|
|
|
func (r *Repo) parentTree(commitObj *object.Commit) (*object.Tree, error) {
|
|
|
|
switch commitObj.NumParents() {
|
|
|
|
case 0:
|
|
|
|
return new(object.Tree), nil
|
|
|
|
case 1:
|
|
|
|
if parentCommitObj, err := commitObj.Parent(0); err != nil {
|
|
|
|
return nil, fmt.Errorf("getting parent commit %q: %w",
|
|
|
|
commitObj.ParentHashes[0], err)
|
|
|
|
} else if parentTree, err := r.GitRepo.TreeObject(parentCommitObj.TreeHash); err != nil {
|
|
|
|
return nil, fmt.Errorf("getting parent tree object %q: %w",
|
|
|
|
parentCommitObj.TreeHash, err)
|
|
|
|
} else {
|
|
|
|
return parentTree, nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.New("commit has multiple parents")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
// if parentTree is nil then it will be inferred.
|
|
|
|
func (r *Repo) verifyCommit(branch plumbing.ReferenceName, gitCommit GitCommit, parentTree *object.Tree) error {
|
2020-03-22 23:54:25 +00:00
|
|
|
parentTree, err := r.parentTree(gitCommit.GitCommit)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("retrieving parent tree of commit: %w", err)
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vctx := verificationCtx{
|
|
|
|
commit: gitCommit.GitCommit,
|
|
|
|
commitTree: gitCommit.GitTree,
|
|
|
|
parentTree: parentTree,
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 22:14:18 +00:00
|
|
|
var sigFS fs.FS
|
2020-03-22 23:54:25 +00:00
|
|
|
if gitCommit.Root() {
|
2020-03-14 22:14:18 +00:00
|
|
|
sigFS = fs.FromTree(vctx.commitTree)
|
|
|
|
} else {
|
|
|
|
sigFS = fs.FromTree(vctx.parentTree)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := r.loadConfig(sigFS)
|
|
|
|
if err != nil {
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
return fmt.Errorf("loading config of parent %q: %w",
|
|
|
|
gitCommit.GitCommit.ParentHashes[0], err)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
err = r.assertAccessControls(cfg.AccessControls, gitCommit.Commit, vctx, branch)
|
2020-03-04 23:34:02 +00:00
|
|
|
if err != nil {
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
return fmt.Errorf("enforcing access controls: %w", err)
|
2020-03-04 23:34:02 +00:00
|
|
|
}
|
|
|
|
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
changeHash := gitCommit.Interface.GetHash()
|
|
|
|
expectedChangeHash, err := gitCommit.Interface.Hash(vctx.parentTree, vctx.commitTree)
|
2020-03-04 23:34:02 +00:00
|
|
|
if err != nil {
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
return fmt.Errorf("calculating expected change hash: %w", err)
|
2020-03-04 23:34:02 +00:00
|
|
|
} else if !bytes.Equal(changeHash, expectedChangeHash) {
|
2020-02-15 22:13:50 +00:00
|
|
|
return fmt.Errorf("malformed change_hash in commit body, is %s but should be %s",
|
|
|
|
base64.StdEncoding.EncodeToString(expectedChangeHash),
|
2020-03-04 23:34:02 +00:00
|
|
|
base64.StdEncoding.EncodeToString(changeHash))
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 17:22:15 +00:00
|
|
|
for _, cred := range gitCommit.Commit.Common.Credentials {
|
2020-02-15 22:13:50 +00:00
|
|
|
sig, err := r.signifierForCredential(sigFS, cred)
|
|
|
|
if err != nil {
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
return fmt.Errorf("finding signifier for credential %+v: %w", cred, err)
|
2020-02-15 22:13:50 +00:00
|
|
|
} else if err := sig.Verify(sigFS, expectedChangeHash, cred); err != nil {
|
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
---
type: change
message: |-
normalize how git commits are interacted with, including changing VerifyComit -> VerifyCommits
This commit attempts to normalize git commit interactions in order to reduce
the amount of manual `GitRepo.CommitObject`, `GitRepo.TreeObject`,
`Commit.UnmarshalText`, and `Commit.Interface` calls are done, by creating a
single structure (`GitCommit`) which holds the output of those calls, and is
only created by a single method (`GetGitCommit`), which is then used by a bunch
of other methods to expand its functionality, including implementing a range
request which can be used by verify and the pre-receive hook (though it's only
used by the hook, currently).
change_hash: AMae4PL6+jrxhn2KEGHejstcdT37Gw/jjkl/UuovHcgd
credentials:
- type: pgp_signature
pub_key_id: 95C46FA6A41148AC
body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl5uhvoACgkQlcRvpqQRSKzJrhAAqi2LEQVTyVktfsOBv/CZmefclLLqWTChVoeIZt2EAGDDGygmrx88hI0SEAviOzPMn0kiZFDeY5k7ICJMhJ9RVDU9WjH7fbOboMJW19rVhx6Ke/M2ERtrT0OFLRmFVJVDM0P8SEheQvR3HE/iiypBICVCtp+meHEq9mOJWZlZnoCqMaulAy/Nnq4N1VD0yPPlr16+yxMqedKHcgKbcH8K61ltNAjXDT+tCWwCq1huA5MVSuTm5EwqIeKPN6JKgwATv8Ku2GhYZWHSGUwecP1J3x2XTDPeChCQVDpC232Pxwk8z/D36F3J/XOfkdl0QYQ077xL1IJfYOnuuHir47CokDf3G0XCQnJ/+X4pZdtP387rc045o/2bhUi2U4eJ5HgS7Hvyi6EApT0Czv7SeJePTvdnRUYse8ZYuIwYXj5GWWxnbKQzLpyjcHdQc2a3B3RN84zXqqAOS6ObFrFPZQIfz2rfQojZN8kvcmUvYhJXSaT65XmqFjyJ4n6grrEnK/N+MfbnpzyF/yvlzxWPqGFQOQj9meosbTAdgZbmdwYqa5r1ee8DmlkzNJJxze96h503a733yciN8Ef4hGZNlRV6YFegkK/cCgKaA4NCEALKb1t0Uri5gnPldXk4HsPF+23GANbE7mjytY8ra3fhXG4VhaFt/WsLg3Bu7djQ0H74y+g=
account: mediocregopher
2020-03-15 19:50:24 +00:00
|
|
|
return fmt.Errorf("verifying credential %+v: %w", cred, err)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-23 23:15:44 +00:00
|
|
|
|
|
|
|
type changeRangeInfo struct {
|
2020-03-26 00:01:32 +00:00
|
|
|
changeCommits []GitCommit
|
2020-03-23 23:15:44 +00:00
|
|
|
authors map[string]struct{}
|
|
|
|
msg string
|
|
|
|
startTree, endTree *object.Tree
|
|
|
|
changeHash []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// changeRangeInfo returns various pieces of information about a range of
|
|
|
|
// commits' changes.
|
|
|
|
func (r *Repo) changeRangeInfo(commits []GitCommit) (changeRangeInfo, error) {
|
|
|
|
info := changeRangeInfo{
|
|
|
|
authors: map[string]struct{}{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, commit := range commits {
|
|
|
|
if _, ok := commit.Interface.(*CommitChange); ok {
|
2020-03-26 00:01:32 +00:00
|
|
|
info.changeCommits = append(info.changeCommits, commit)
|
2020-03-23 23:15:44 +00:00
|
|
|
for _, cred := range commit.Commit.Common.Credentials {
|
|
|
|
info.authors[cred.AccountID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 00:01:32 +00:00
|
|
|
if len(info.changeCommits) == 0 {
|
2020-03-23 23:15:44 +00:00
|
|
|
return changeRangeInfo{}, errors.New("no change commits found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// startTree has to be the tree of the parent of the first commit, which
|
|
|
|
// isn't included in commits. Determine it the hard way.
|
|
|
|
var err error
|
|
|
|
if info.startTree, err = r.parentTree(commits[0].GitCommit); err != nil {
|
|
|
|
return changeRangeInfo{}, fmt.Errorf("getting tree of parent of %q: %w",
|
|
|
|
commits[0].GitCommit.Hash, err)
|
|
|
|
}
|
|
|
|
|
2020-03-26 00:01:32 +00:00
|
|
|
lastChangeCommit := info.changeCommits[len(info.changeCommits)-1]
|
|
|
|
info.msg = lastChangeCommit.Commit.Change.Message
|
|
|
|
info.endTree = lastChangeCommit.GitTree
|
2020-03-23 23:15:44 +00:00
|
|
|
info.changeHash = genChangeHash(nil, info.msg, info.startTree, info.endTree)
|
|
|
|
return info, nil
|
|
|
|
}
|