-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlog.go
More file actions
154 lines (130 loc) · 3.44 KB
/
log.go
File metadata and controls
154 lines (130 loc) · 3.44 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// cmd/log.go
package cmd
import (
"fmt"
"os"
"strconv"
"github.com/boneskull/gh-stack/internal/config"
"github.com/boneskull/gh-stack/internal/git"
"github.com/boneskull/gh-stack/internal/github"
"github.com/boneskull/gh-stack/internal/style"
"github.com/boneskull/gh-stack/internal/tree"
"github.com/cli/go-gh/v2/pkg/tableprinter"
"github.com/cli/go-gh/v2/pkg/term"
"github.com/spf13/cobra"
)
var logCmd = &cobra.Command{
Use: "log",
Short: "Display the branch tree",
Long: `Display the stack tree structure with branch names and PR numbers.`,
RunE: runLog,
}
var (
logAllFlag bool
logPorcelainFlag bool
)
func init() {
logCmd.Flags().BoolVar(&logAllFlag, "all", false, "show all branches")
logCmd.Flags().BoolVar(&logPorcelainFlag, "porcelain", false, "machine-readable output")
rootCmd.AddCommand(logCmd)
}
func runLog(cmd *cobra.Command, args []string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
g := git.New(cwd)
cfg, err := config.New(g)
if err != nil {
return err
}
root, err := tree.Build(cfg)
if err != nil {
return err
}
currentBranch, _ := g.CurrentBranch() //nolint:errcheck // empty string is fine for display
// Try to get GitHub client for PR URLs (optional - may fail if not in a GitHub repo)
gh, _ := github.NewClient() //nolint:errcheck // nil is fine, URLs won't be shown
s := style.New()
if logPorcelainFlag {
printPorcelain(root, currentBranch, gh, s)
} else {
opts := tree.FormatOptions{
CurrentBranch: currentBranch,
Style: s,
}
if gh != nil {
opts.PRURLFunc = gh.PRURL
}
fmt.Print(tree.FormatTree(root, opts))
}
return nil
}
// printPorcelain outputs stack information in table format.
// In TTY mode, outputs nicely formatted columns.
// In non-TTY mode (piped/scripted), outputs tab-separated values.
//
// Columns:
// - BRANCH: branch name (* prefix for current branch in TTY mode)
// - PARENT: parent branch name (empty for trunk)
// - PR: associated PR number (empty if none)
// - URL: full PR URL (empty if no PR or GitHub client unavailable)
func printPorcelain(node *tree.Node, current string, gh *github.Client, s *style.Style) {
t := term.FromEnv()
isTTY := t.IsTerminalOutput()
var width int
if isTTY {
w, _, err := t.Size()
if err != nil || w <= 0 {
width = 80 // reasonable default width for TTY when detection fails
} else {
width = w
}
} else {
// In non-TTY mode, tableprinter outputs TSV; use a large width to avoid truncation.
width = 4096
}
tp := tableprinter.New(os.Stdout, isTTY, width)
// Add headers in TTY mode
if isTTY {
tp.AddHeader([]string{"BRANCH", "PARENT", "PR", "URL"})
}
// Collect all nodes in tree order
var addNode func(*tree.Node)
addNode = func(n *tree.Node) {
branchName := n.Name
if isTTY && n.Name == current {
branchName = s.Bold("* " + n.Name)
} else if isTTY {
branchName = s.Branch(n.Name)
}
parent := ""
if n.Parent != nil {
if isTTY {
parent = s.Branch(n.Parent.Name)
} else {
parent = n.Parent.Name
}
}
prNum := ""
if n.PR > 0 {
prNum = strconv.Itoa(n.PR)
}
prURL := ""
if n.PR > 0 && gh != nil {
prURL = gh.PRURL(n.PR)
}
tp.AddField(branchName)
tp.AddField(parent)
tp.AddField(prNum)
tp.AddField(prURL)
tp.EndRow()
for _, child := range n.Children {
addNode(child)
}
}
addNode(node)
if err := tp.Render(); err != nil {
fmt.Fprintf(os.Stderr, "%s failed to render table: %v\n", s.WarningIcon(), err)
}
}