···
10
+
"github.com/go-git/go-git/v5/plumbing"
11
+
"github.com/go-git/go-git/v5/plumbing/object"
12
+
"tangled.sh/tangled.sh/core/types"
15
+
func (g *GitRepo) Branches() ([]types.Branch, error) {
24
+
"committerdate:unix",
30
+
var outFormat strings.Builder
31
+
outFormat.WriteString("--format=")
32
+
for i, f := range fields {
34
+
outFormat.WriteString(fieldSeparator)
36
+
outFormat.WriteString(fmt.Sprintf("%%(%s)", f))
38
+
outFormat.WriteString("")
39
+
outFormat.WriteString(recordSeparator)
41
+
output, err := g.forEachRef(outFormat.String(), "refs/heads")
43
+
return nil, fmt.Errorf("failed to get branches: %w", err)
46
+
records := strings.Split(strings.TrimSpace(string(output)), recordSeparator)
47
+
if len(records) == 1 && records[0] == "" {
51
+
branches := make([]types.Branch, 0, len(records))
53
+
// ignore errors here
54
+
defaultBranch, _ := g.FindMainBranch()
56
+
for _, line := range records {
57
+
parts := strings.SplitN(strings.TrimSpace(line), fieldSeparator, len(fields))
62
+
branchName := parts[0]
63
+
commitHash := plumbing.NewHash(parts[1])
64
+
authorName := parts[2]
65
+
authorEmail := strings.TrimSuffix(strings.TrimPrefix(parts[3], "<"), ">")
66
+
authorDate := parts[4]
67
+
committerName := parts[5]
68
+
committerEmail := strings.TrimSuffix(strings.TrimPrefix(parts[6], "<"), ">")
69
+
committerDate := parts[7]
70
+
treeHash := plumbing.NewHash(parts[8])
71
+
parentHash := plumbing.NewHash(parts[9])
72
+
message := parts[10]
74
+
// parse creation time
75
+
var authoredAt, committedAt time.Time
76
+
if unix, err := strconv.ParseInt(authorDate, 10, 64); err == nil {
77
+
authoredAt = time.Unix(unix, 0)
79
+
if unix, err := strconv.ParseInt(committerDate, 10, 64); err == nil {
80
+
committedAt = time.Unix(unix, 0)
83
+
branch := types.Branch{
84
+
IsDefault: branchName == defaultBranch,
85
+
Reference: types.Reference{
87
+
Hash: commitHash.String(),
89
+
Commit: &object.Commit{
91
+
Author: object.Signature{
96
+
Committer: object.Signature{
97
+
Name: committerName,
98
+
Email: committerEmail,
101
+
TreeHash: treeHash,
102
+
ParentHashes: []plumbing.Hash{parentHash},
107
+
branches = append(branches, branch)
110
+
slices.Reverse(branches)
111
+
return branches, nil