package dehub import ( "dehub/fs" "dehub/yamlutil" "errors" "strings" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/object" ) // CommitChange describes the structure of a change commit message. type CommitChange struct { Message string `yaml:"message"` ChangeHash yamlutil.Blob `yaml:"change_hash"` } var _ CommitInterface = CommitChange{} // NewCommitChange constructs a Commit populated with a CommitChange // encompassing the currently staged file changes. The Credentials of the // returned Commit will _not_ be filled in. func (r *Repo) NewCommitChange(msg string) (Commit, error) { _, headTree, err := r.head() if errors.Is(err, plumbing.ErrReferenceNotFound) { headTree = &object.Tree{} } else if err != nil { return Commit{}, err } _, stagedTree, err := fs.FromStagedChangesTree(r.GitRepo) if err != nil { return Commit{}, err } cc := CommitChange{Message: msg} if cc.ChangeHash, err = cc.Hash(headTree, stagedTree); err != nil { return Commit{}, err } return Commit{ Change: &cc, }, nil } // MessageHead implements the method for the CommitInterface interface. func (cc CommitChange) MessageHead() (string, error) { i := strings.Index(cc.Message, "\n") if i > 0 { return cc.Message[:i], nil } return cc.Message, nil } // Hash implements the method for the CommitInterface interface. func (cc CommitChange) Hash(parent, this *object.Tree) ([]byte, error) { return genChangeHash(nil, cc.Message, parent, this), nil } // GetHash implements the method for the CommitInterface interface. func (cc CommitChange) GetHash() []byte { return cc.ChangeHash }