package git import ( "errors" "farga/lib" errs "farga/lib/errors" "time" "github.com/go-git/go-git/v6" "github.com/go-git/go-git/v6/plumbing/object" ) type LogOptions struct { Repo *git.Repository Branch string FileName string } type GitCommitAuthor struct { Name string Email string } type Commit struct { Hash string ShortHash string Author GitCommitAuthor Message string Time time.Time } type Log []Commit // gets the log of a branch or file. Don't forget to pass in your repo! func GetLog(o LogOptions) (Log, error) { if o.Repo == nil { return nil, errs.ErrMissingProperty("Repo") } ref, err := ResolveHead(o.Repo, o.Branch) if err != nil { return nil, err } if ref == nil { return nil, ErrRepoHasNoHead } gitLog, err := o.Repo.Log(&git.LogOptions{ From: ref.Hash(), }) if err != nil { return nil, err } var log Log err = gitLog.ForEach(func(c *object.Commit) error { shortHash, err := ShortenHash(c.Hash.String()) if err != nil { return errors.New("Invalid hash length") } log = append(log, Commit{ Hash: c.Hash.String(), ShortHash: shortHash, Author: GitCommitAuthor{ Name: c.Author.Name, Email: c.Author.Email, }, Message: c.Message, Time: c.Author.When, }) return nil }) return log, err } // shortens a hash. returns the full string if it's asked to trim it at a length greater than it is. func ShortenHash(hash string) (string, error) { return lib.TruncateString(hash, lib.GitHashLength) }