syw/commit.go

46 lines
987 B
Go

package syw
import (
"strings"
"time"
)
// Commit represents a git commit.
type Commit struct {
Repo *Repository
Hash string
Parents []string
CommitterName string
CommitterEmail string
CommitDate time.Time
AuthorName string
AuthorEmail string
AuthorDate time.Time
Message string
}
// ParentHash returns a ref name usable to reach the commit's parent.
func (c *Commit) ParentHash() string {
return c.Hash + "^"
}
// ShortMessage returns the first line of the commit message.
func (c *Commit) ShortMessage() string {
short, _, _ := strings.Cut(c.Message, "\n")
return short
}
// RestOfMessage returns all but the first line of the commit message.
//
// It will trim any newline prefixes however, so be aware that
// c.ShortMessage + "\n" + c.RestOfMessage may not produce the original
// commit message. For that use c.Message.
func (c *Commit) RestOfMessage() string {
_, rest, _ := strings.Cut(c.Message, "\n")
return strings.TrimPrefix(rest, "\n")
}