Files
simplegit/gitcmd/branch.go
T
2026-07-15 11:06:48 -04:00

57 lines
1.2 KiB
Go

package gitcmd
import (
"strings"
"time"
)
type Branch struct {
Name string `json:"name"`
SHA string `json:"sha"`
ShortSHA string `json:"short_sha"`
Message string `json:"message"`
Author string `json:"author"`
When time.Time `json:"when"`
IsDefault bool `json:"is_default"`
}
func (r *Repository) ListBranches() ([]Branch, error) {
out, _, err := NewCommand("for-each-ref").
Add("--format=%(refname:short)%1f%(objectname)%1f%(subject)%1f%(authorname)%1f%(authordate:iso8601)").
Add("refs/heads").
WithDir(r.Path).
RunStdString(r.ctx)
if err != nil {
return nil, err
}
defaultBranch, _ := r.DefaultBranch()
var branches []Branch
for _, line := range strings.Split(strings.TrimSpace(out), "\n") {
if line == "" {
continue
}
fields := strings.Split(line, "\x1f")
if len(fields) < 5 {
continue
}
when, _ := time.Parse("2006-01-02 15:04:05 -0700", fields[4])
sha := fields[1]
shortSHA := sha
if len(sha) >= 7 {
shortSHA = sha[:7]
}
branches = append(branches, Branch{
Name: fields[0],
SHA: sha,
ShortSHA: shortSHA,
Message: fields[2],
Author: fields[3],
When: when,
IsDefault: fields[0] == defaultBranch,
})
}
return branches, nil
}