dehub/commit_credential.go
mediocregopher 4d56716fe8 Give the project a proper root, dehub.dev/src/dehub.git
---
type: change
message: |-
  Give the project a proper root, dehub.dev/src/dehub.git

  "dehub" was being used as a placeholder up till this point, but now that the
  project has matured enough to have a home, and people will want to be installing
  the binary, it's time to give it a proper import root.
change_hash: ADTjLNnPARatUjx6HDo7m3O+0KUcWvcMJq1kNrj9++PU
credentials:
- type: pgp_signature
  pub_key_id: 95C46FA6A41148AC
  body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl6BECkACgkQlcRvpqQRSKyeJBAAocdHTs8AB2u9kZmMcJNf136Cq8KWjglvhuH49Nj9ILkeGOBF7+fIHBRSYxBQm4IIDWsjaQaRRhESJxKrkHUd7BG/z5iVsCfjYTyY7LPgv0oYJKEGbPOF2LDK2fuLLpRlA5v1fwamTdFYxs5j7yxW7weWiiKEgjMGjOiWQCWuqhw8rwOG6vjvYttQ5pekI7GvkdqWe+qODKmIF9xFFuhz7BMsFUAAwLz97W1wZkHA+O1BXGTe9hGwH88RY9IGJZftA6wuGes/SqwWJk+hpb89KV1yR0woMrwz7ITZZeNBboXO7KyxUXrfiLtxeRD4t15tJkdRY0FbIZvNCktviTqjPXOtoVh7c3m3p/LR5vcbgYDxaZKk51DYruovfaRI96Q9CYCK2kVb2PIJ4BJhlsUPzw8AWNVxXFECYZNEZly10hqNh4Xophr/x7PWOmoPoKL2PvXLtOHzk0r4Tw6JqUaC+7/08U/+lhlSKFKLdCj7Xru57qB7gxgd+P3g4rfqjIHqiB4tlViqR5RV7V8Z/goBMJg2uOJXIJGvwEZF9nj+QDgaofImMiQmcLrE6IBCYEELYz274w/6ugSCcdwVIC/n3VHuNY4zJDKN7Q/FgI9/GBS7UoNhTaHF2JmSKR9ErMKpm3PYPZr/VnsaXjfCBO5bVquvWLzfJ9aQ4t+tsRkuAvQ=
  account: mediocregopher
2020-03-29 15:16:36 -06:00

79 lines
2.3 KiB
Go

package dehub
import (
"dehub.dev/src/dehub.git/yamlutil"
"encoding/base64"
"fmt"
"strings"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// 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 Comit 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] + "..."
}
credAccounts := strings.Join(common.credAccountIDs(), ", ")
return fmt.Sprintf("Credential of hash %s by %s", hash64, credAccounts), nil
}
// Hash implements the method for the CommitInterface.
func (cc CommitCredential) Hash(_, _ *object.Tree) ([]byte, error) {
return cc.CredentialedHash, nil
}
// GetHash implements the method for the CommitInterface.
func (cc CommitCredential) GetHash() []byte {
return cc.CredentialedHash
}