diff --git a/SPEC.md b/SPEC.md index f431ca2..40f0de8 100644 --- a/SPEC.md +++ b/SPEC.md @@ -193,10 +193,10 @@ credentials: ## Credential Commits -Commits of type `credential` contain one or more credentials for some set of -changes, and the change hash to which those credentials apply. The commit -message head is not spec'd, but should be a human-readable description of "who -is crediting what, and how". +Commits of type `credential` contain one or more credentials for some hash +(presumably a change hash, but in the future there may be other types). The +commit message head is not spec'd, but should be a human-readable description of +"who is crediting what, and how". Example credential commit message: @@ -204,7 +204,7 @@ Example credential commit message: some_user_id pgp sig of commits AAA..BBB with key CCC --- -change_hash: XXX +credentialed_hash: XXX credentials: - type: pgp_signature account_id: some_user_id diff --git a/commit.go b/commit.go index 3450f8c..775166e 100644 --- a/commit.go +++ b/commit.go @@ -39,7 +39,8 @@ type CommitInterface interface { // 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 { - Change *CommitChange `type:"change,default"` + Change *CommitChange `type:"change,default"` + Credential *CommitCredential `type:"credential"` // Credentials represent all created Credentials for this commit, and can be // set on all Commit objects regardless of other fields being set. diff --git a/commit_signature.go b/commit_signature.go new file mode 100644 index 0000000..3d79b07 --- /dev/null +++ b/commit_signature.go @@ -0,0 +1,46 @@ +package dehub + +import ( + "dehub/yamlutil" + "encoding/base64" + "fmt" + + "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"` +} + +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 +} + +// MessageHead implements the method for the CommitInterface interface. +func (cc CommitCredential) MessageHead() (string, error) { + hash64 := base64.StdEncoding.EncodeToString(cc.CredentialedHash) + if len(hash64) > 6 { + hash64 = hash64[:6] + "..." + } + return fmt.Sprintf("Credential of hash %s", hash64), 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 +}