summaryrefslogtreecommitdiffstats
path: root/branch.go
blob: fe7a7af017c953bc0c068a75b27673db89a6e35b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main

import (
	"context"
	"fmt"
	"sort"
	"strings"
	"time"

	"github.com/charmbracelet/lipgloss"
	"github.com/muesli/reflow/truncate"
	"github.com/shurcooL/githubv4"
)

var branchesQuery struct {
	Repository struct {
		Refs struct {
			Nodes []struct {
				Name   githubv4.String
				Target struct {
					Commit QLCommit `graphql:"... on Commit"`
				}
			}
		} `graphql:"refs(first: 100, refPrefix: \"refs/heads/\")"`
	} `graphql:"repository(owner: $owner, name: $name)"`
}

type Branch struct {
	Name       string
	LastCommit Commit
}

func branches(owner string, name string) ([]Branch, error) {
	variables := map[string]interface{}{
		"owner": githubv4.String(owner),
		"name":  githubv4.String(name),
	}

	if err := queryWithRetry(context.Background(), &branchesQuery, variables); err != nil {
		return nil, err
	}

	var branches []Branch
	for _, node := range branchesQuery.Repository.Refs.Nodes {
		branches = append(branches, Branch{
			Name:       string(node.Name),
			LastCommit: CommitFromQL(node.Target.Commit),
		})
	}

	return branches, nil
}

func printBranch(branch Branch, maxWidth int) {
	genericStyle := lipgloss.NewStyle().
		Foreground(lipgloss.Color(theme.colorGray))
	numberStyle := lipgloss.NewStyle().
		Foreground(lipgloss.Color(theme.colorBlue)).Width(maxWidth)
	timeStyle := lipgloss.NewStyle().
		Foreground(lipgloss.Color(theme.colorGreen)).Width(8).Align(lipgloss.Right)
	titleStyle := lipgloss.NewStyle().
		Foreground(lipgloss.Color(theme.colorDarkGray)).Width(80 - maxWidth)

	var s string
	s += numberStyle.Render(branch.Name)
	s += genericStyle.Render(" ")
	s += titleStyle.Render(truncate.String(branch.LastCommit.MessageHeadline, uint(80-maxWidth)))
	s += genericStyle.Render(" ")
	s += timeStyle.Render(ago(branch.LastCommit.CommittedAt))
	s += genericStyle.Render(" ")
	s += numberStyle.Render(branch.LastCommit.Author)

	fmt.Println(s)
}

func printBranches(branches []Branch) {
	headerStyle := lipgloss.NewStyle().
		PaddingTop(1).
		Foreground(lipgloss.Color(theme.colorMagenta))

	sort.Slice(branches, func(i, j int) bool {
		if branches[i].LastCommit.CommittedAt.Equal(branches[j].LastCommit.CommittedAt) {
			return strings.Compare(branches[i].Name, branches[j].Name) < 0
		}
		return branches[i].LastCommit.CommittedAt.After(branches[j].LastCommit.CommittedAt)
	})

	// filter list
	var b []Branch
	for _, v := range branches {
		if v.LastCommit.CommittedAt.Before(time.Now().Add(-24 * time.Duration(*maxBranchAge) * time.Hour)) {
			continue
		}
		b = append(b, v)
	}
	branches = b

	// trimmed := false
	if *maxBranches > 0 && len(branches) > *maxBranches {
		branches = branches[:*maxBranches]
		// trimmed = true
	}

	fmt.Println(headerStyle.Render(fmt.Sprintf("🌳 %d active branches", len(branches))))

	// detect max width of branch name
	var maxWidth int
	for _, v := range branches {
		if len(v.Name) > maxWidth {
			maxWidth = len(v.Name)
		}
	}

	for _, v := range branches {
		printBranch(v, maxWidth)
	}
	// if trimmed {
	// 	fmt.Println("...")
	// }
}