返回 DeepSeek-Reasonix
gitstatus.go
根目录 / internal / cli / gitstatus.go
1 package cli
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "path/filepath"
8 "strconv"
9 "strings"
10 "time"
11
12 tea "charm.land/bubbletea/v2"
13 "github.com/charmbracelet/x/ansi"
14
15 "reasonix/internal/gitcmd"
16 )
17
18 const gitStatusTimeout = 700 * time.Millisecond
19
20 type gitStatus struct {
21 Repo string
22 Branch string
23 Detached bool
24 Added int
25 Removed int
26 Untracked int
27 }
28
29 func fetchGitStatus() tea.Cmd {
30 return func() tea.Msg {
31 ctx, cancel := context.WithTimeout(context.Background(), gitStatusTimeout)
32 defer cancel()
33 status, err := loadGitStatus(ctx, "")
34 if err != nil {
35 return gitStatusMsg{}
36 }
37 return gitStatusMsg{status: status}
38 }
39 }
40
41 func loadGitStatus(ctx context.Context, cwd string) (gitStatus, error) {
42 return loadGitStatusWithRunner(ctx, cwd, runGit)
43 }
44
45 func loadGitStatusWithRunner(ctx context.Context, cwd string, run func(context.Context, string, ...string) (string, error)) (gitStatus, error) {
46 root, err := run(ctx, cwd, "rev-parse", "--show-toplevel")
47 if err != nil {
48 return gitStatus{}, err
49 }
50 root = strings.TrimSpace(root)
51 if root == "" {
52 return gitStatus{}, errors.New("empty git root")
53 }
54
55 status := gitStatus{Repo: filepath.Base(root)}
56 if branch, err := run(ctx, root, "symbolic-ref", "--quiet", "--short", "HEAD"); err == nil && strings.TrimSpace(branch) != "" {
57 status.Branch = strings.TrimSpace(branch)
58 } else if sha, err := run(ctx, root, "rev-parse", "--short", "HEAD"); err == nil && strings.TrimSpace(sha) != "" {
59 status.Branch = strings.TrimSpace(sha)
60 status.Detached = true
61 } else if ref, err := run(ctx, root, "symbolic-ref", "--short", "HEAD"); err == nil && strings.TrimSpace(ref) != "" {
62 status.Branch = strings.TrimSpace(ref)
63 }
64 if status.Branch == "" {
65 status.Branch = "HEAD"
66 status.Detached = true
67 }
68
69 if out, err := run(ctx, root, "diff", "--numstat", "HEAD", "--"); err == nil {
70 status.Added, status.Removed = parseGitNumstat(out)
71 }
72 if out, err := run(ctx, root, "status", "--porcelain=v1", "--untracked-files=normal"); err == nil {
73 status.Untracked = countUntracked(out)
74 }
75 if err := ctx.Err(); err != nil {
76 return gitStatus{}, err
77 }
78 return status, nil
79 }
80
81 func runGit(ctx context.Context, cwd string, args ...string) (string, error) {
82 // cwd goes through gitcmd's dir parameter, not cmd.Dir, so the gitcmd
83 // baseline can resolve the repository's own config relative to it (the
84 // filter-driver neutralization reads <cwd>/.git/config).
85 cmd := gitcmd.Command(ctx, cwd, args...)
86 out, err := cmd.Output()
87 if err != nil {
88 return "", err
89 }
90 return string(out), nil
91 }
92
93 func parseGitNumstat(out string) (added int, removed int) {
94 for line := range strings.SplitSeq(strings.TrimSpace(out), "\n") {
95 if line == "" {
96 continue
97 }
98 fields := strings.Fields(line)
99 if len(fields) < 2 {
100 continue
101 }
102 if fields[0] != "-" {
103 if n, err := strconv.Atoi(fields[0]); err == nil {
104 added += n
105 }
106 }
107 if fields[1] != "-" {
108 if n, err := strconv.Atoi(fields[1]); err == nil {
109 removed += n
110 }
111 }
112 }
113 return added, removed
114 }
115
116 func countUntracked(out string) int {
117 n := 0
118 for line := range strings.SplitSeq(strings.TrimRight(out, "\n"), "\n") {
119 if strings.HasPrefix(line, "?? ") {
120 n++
121 }
122 }
123 return n
124 }
125
126 func (m chatTUI) gitTag() string {
127 if strings.TrimSpace(m.gitStatus.Repo) == "" || strings.TrimSpace(m.gitStatus.Branch) == "" {
128 return ""
129 }
130 return m.gitStatus.render(themeFg(m.statusModeColor(), m.gitStatus.Repo), m.gitStatus.Branch)
131 }
132
133 var (
134 statusAutoColor = cliColor{"#f59e0b", 214}
135 statusPlanColor = cliColor{"#2563eb", 27}
136 statusYoloColor = cliColor{"#e5484d", 167}
137 statusShellColor = cliColor{"#16a34a", 71}
138 modeTagLight = cliColor{"#ffffff", 231}
139 modeTagDark = cliColor{"#111827", 234}
140 )
141
142 func (m chatTUI) statusModeColor() cliColor {
143 switch {
144 case m.ctrl != nil && m.ctrl.AutoApproveTools():
145 return statusYoloColor
146 case m.planMode:
147 return statusPlanColor
148 default:
149 return statusAutoColor
150 }
151 }
152
153 func (s gitStatus) Render() string {
154 return s.RenderRepo(accent(s.Repo))
155 }
156
157 func (s gitStatus) RenderRepo(repo string) string {
158 if strings.TrimSpace(s.Repo) == "" || strings.TrimSpace(s.Branch) == "" {
159 return ""
160 }
161 return s.render(repo, s.Branch)
162 }
163
164 func (s gitStatus) RenderWithin(maxWidth int, repoColor cliColor) string {
165 if strings.TrimSpace(s.Repo) == "" || strings.TrimSpace(s.Branch) == "" {
166 return ""
167 }
168 repo, branch := s.compactIdentity(maxWidth)
169 out := s.render(themeFg(repoColor, repo), branch)
170 if maxWidth > 0 && visibleWidth(out) > maxWidth {
171 return ansi.Truncate(out, maxWidth, "…")
172 }
173 return out
174 }
175
176 func (s gitStatus) compactIdentity(maxWidth int) (repo, branch string) {
177 repo = strings.TrimSpace(s.Repo)
178 branch = strings.TrimSpace(s.Branch)
179 if maxWidth <= 0 {
180 return repo, branch
181 }
182 dirtyWidth := visibleWidth(s.dirtyPlain())
183 nameBudget := maxWidth - dirtyWidth - visibleWidth("@")
184 if nameBudget <= 2 {
185 return compactEnd(repo, max(1, nameBudget)), ""
186 }
187 repoWidth := visibleWidth(repo)
188 branchWidth := visibleWidth(branch)
189 if repoWidth+branchWidth <= nameBudget {
190 return repo, branch
191 }
192
193 minRepo := min(repoWidth, 8)
194 if repoBudget := nameBudget - branchWidth; repoBudget >= minRepo {
195 return compactMiddle(repo, repoBudget), branch
196 }
197
198 repoBudget := min(repoWidth, max(4, min(10, nameBudget/3)))
199 if nameBudget-repoBudget < 8 {
200 repoBudget = max(1, nameBudget-8)
201 }
202 branchBudget := max(1, nameBudget-repoBudget)
203 return compactMiddle(repo, repoBudget), compactMiddle(branch, branchBudget)
204 }
205
206 func (s gitStatus) dirtyPlain() string {
207 var parts []string
208 if s.Added > 0 || s.Removed > 0 {
209 parts = append(parts, fmt.Sprintf("+%d", s.Added), fmt.Sprintf("-%d", s.Removed))
210 }
211 if s.Untracked > 0 {
212 parts = append(parts, fmt.Sprintf("?%d", s.Untracked))
213 }
214 if len(parts) == 0 {
215 return ""
216 }
217 return " " + strings.Join(parts, " ")
218 }
219
220 func (s gitStatus) render(repo, branch string) string {
221 var b strings.Builder
222 b.WriteString(repo)
223 b.WriteString(dim("@"))
224 if s.Detached {
225 b.WriteString(yellow(branch))
226 } else {
227 // A branch name is identity, not a success condition. Keep semantic green
228 // for additions and use the theme's readable neutral value colour here.
229 b.WriteString(footerValue(branch))
230 }
231
232 var parts []string
233 if s.Added > 0 || s.Removed > 0 {
234 parts = append(parts, green(fmt.Sprintf("+%d", s.Added)), red(fmt.Sprintf("-%d", s.Removed)))
235 }
236 if s.Untracked > 0 {
237 parts = append(parts, yellow(fmt.Sprintf("?%d", s.Untracked)))
238 }
239 if len(parts) > 0 {
240 b.WriteString(" ")
241 b.WriteString(strings.Join(parts, " "))
242 }
243 return b.String()
244 }
245
245 lines GO