dehub/commit_credential.go
mediocregopher 84399603cf refactor how file changes and hashes are handled, and add tests for hashes
---
type: change
message: |-
  refactor how file changes and hashes are handled, and add tests for hashes

  This involved a few different changes that all worked together:

  * Making fileChanged public as ChangedFile, and reorganizing how it's
    created/used a bit.

  * Renaming the CommitInterface methods to be clearer, and passing in a
    `[]ChangedFile` into `ExpectedHash` (previously just `Hash`) rather than
    trees.

  * Adding tests for the gen*Hash functions.
change_hash: AGCdt2k6FARuf1CBao7zxiuDVMOAdeL5vL8Whnr9KjMu
credentials:
- type: pgp_signature
  pub_key_id: 95C46FA6A41148AC
  body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl6bVGIACgkQlcRvpqQRSKwkmhAAqAuUXKIpr1caE7yuM09udiW51G0TtSe0tLItoZ6ROnSe6lsISZ3hkmOWjSyfeeZFggGIAlK3Uipb42pbSO3Usf0fqDWdNekPLRtTd6qpB65Ye9kDgC3Nz6Z0EhFSMDc6sQzNcisWqAAry6CyEy4ucAm3aK1frSTH/6i/x9DQZXY7WVLM5NewegTr/fatf57Fh6kky7o7yiALeOmQR/oAh7x4AADJgmJMs2aBLsO958GwrNOlQI/RhC83TfoXM7YcTkFv+xz2LucIyBZYYxpM327UTmUk2vuOf3p2OFbiISfIZxFeI3KhpUH05TVXyzohQTnrwyWJDKFGKO8iqxaWHRxdH0INUKsAcBTA8dzTGoRyFF5CUKYX0ZnorlT5NV2HeObmFaDpUHNQKHCcQAAAFoSOGVNNqfGS1IqUNwwZkln+Nzr7YrxmCJ98fDopF0C9ZWhNzNF3+oQbOCIOj3+1kqxsQrcqgK3dwefCL46u244qLDGyqF9s8Aqp4rtrVT+4V2hTB7psdR/EDwu5xZDtXEPEktY+6z0RyYwqxUKBkSklOd+dgmqMYw7YrrNKCW433HjHUEf8Qk/x8CjRANkNYMgOAQqND0pegaAHRJJJbD4xMBrMBO8QlAX5+/ocFfoyEXTKlUuNJXVOh/2TDtOWXhmbVhOBsx7TOhTjUhtAGwE=
  account: mediocregopher
2020-04-18 13:26:32 -06:00

82 lines
2.4 KiB
Go

package dehub
import (
"encoding/base64"
"errors"
"fmt"
"strings"
"dehub.dev/src/dehub.git/yamlutil"
)
// CommitCredential describes the structure of a credential commit message.
type CommitCredential struct {
CredentialedHash yamlutil.Blob `yaml:"credentialed_hash"`
// CommitHashes represents the commits which this credential is accrediting.
// It is only present for informational purposes, as commits don't not have
// any bearing on the CredentialedHash itself.
CommitHashes []string `yaml:"commits,omitempty"`
}
var _ CommitInterface = CommitCredential{}
// NewCommitCredential constructs and returns a Commit populated with a
// CommitCredential encompassing the given hash. The Credentials of the returned
// Commit will _not_ be filled in.
func (r *Repo) NewCommitCredential(hash []byte) (Commit, error) {
return Commit{
Credential: &CommitCredential{
CredentialedHash: hash,
},
}, nil
}
// NewCommitCredentialFromChanges constructs and returns a Commit populated with
// a CommitCredential for all changes in the given range of GitCommits. The
// message of the last change commit in the range is used when generating the
// hash.
func (r *Repo) NewCommitCredentialFromChanges(commits []GitCommit) (Commit, error) {
info, err := r.changeRangeInfo(commits)
if err != nil {
return Commit{}, err
}
commitCred, err := r.NewCommitCredential(info.changeHash)
if err != nil {
return Commit{}, err
}
for _, commit := range info.changeCommits {
commitCred.Credential.CommitHashes = append(
commitCred.Credential.CommitHashes,
commit.GitCommit.Hash.String(),
)
}
return commitCred, nil
}
// MessageHead implements the method for the CommitInterface interface.
func (cc CommitCredential) MessageHead(common CommitCommon) (string, error) {
hash64 := base64.StdEncoding.EncodeToString(cc.CredentialedHash)
if len(hash64) > 6 {
hash64 = hash64[:6] + "..."
}
credIDs := strings.Join(common.credIDs(), ", ")
return fmt.Sprintf("Credential of hash %s by %s", hash64, credIDs), nil
}
// ExpectedHash implements the method for the CommitInterface.
func (cc CommitCredential) ExpectedHash(changes []ChangedFile) ([]byte, error) {
if len(changes) > 0 {
return nil, errors.New("CommitCredential cannot have any changed files")
}
return cc.CredentialedHash, nil
}
// StoredHash implements the method for the CommitInterface.
func (cc CommitCredential) StoredHash() []byte {
return cc.CredentialedHash
}