Files
MyGolandRelease/internal/app/git.go
T
zerorigin a80c0f886a MyGoland 首次提交:Go 轻量 IDE
- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲)
- 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表)
- 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记
- Git:状态着色、选择性暂存提交、pull/push、历史并排对比
- 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复
- 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈
- 设计文档与验证记录见 docs/plans/
2026-11-04 16:37:05 +08:00

285 lines
6.9 KiB
Go

package app
import (
"fmt"
"path/filepath"
"sort"
"strings"
"github.com/egoist/mygo/ui"
"mygoland/internal/git"
)
// gitState 是界面用的 git 快照。
type gitState struct {
info git.Info
// stats 是各文件相对 HEAD 的行数增减,键是仓库相对路径(正斜杠)。
stats map[string][2]int
// at 是上次刷新的时间,避免连续保存时并发跑 git。
busy bool
}
// refreshGit 后台刷新一次仓库状态。
func (a *App) refreshGit() {
if a.root == "" || a.gst.busy {
return
}
root := a.root
a.gst.busy = true
go func() {
info := git.Load(root)
// 顺手取一份行数增减,状态栏直接查表,不再每帧起进程。
stats := git.Numstat(root)
a.win.Update(func() {
if a.root == root {
a.gst.info = info
a.gst.stats = stats
}
a.gst.busy = false
a.win.Update(func() {})
})
}()
}
// gitColor 返回状态码对应的颜色。
func (a *App) gitColor(code string) ui.Color {
t := a.th
switch code {
case "M":
return t.Warning
case "A", "R", "??":
return t.Success
case "D", "U":
return t.Danger
}
return t.Text
}
// gitBadge 是文件后面那个小标记的文字。
func gitBadge(code string) string {
switch code {
case "??":
return "U"
case "M", "A", "R", "D", "U":
return code
}
return ""
}
// OpenCommit 打开提交弹窗。
func (a *App) OpenCommit() {
if !a.gst.info.OK {
a.alert("Git", "当前工作区不是 git 仓库,或者没有安装 git")
return
}
if a.gst.info.Dirty == 0 {
a.alert("Git", "没有需要提交的改动")
return
}
// 文件清单按仓库相对路径排序;默认全选——大多数人多数时候
// 就是想全提交,个别不该带的取消勾选就行。
a.commitFiles = nil
a.commitSel = map[string]bool{}
for abs, code := range a.gst.info.Files {
rel, err := filepath.Rel(a.root, abs)
if err != nil {
continue
}
rel = filepath.ToSlash(rel)
a.commitFiles = append(a.commitFiles, rel)
a.commitSel[rel] = true
a.commitCode[rel] = code
}
sort.Strings(a.commitFiles)
a.commitOpen = true
a.commitMsg = ""
a.win.Update(func() {})
}
// commitChecked 统计勾选数。
func (a *App) commitChecked() int {
n := 0
for _, p := range a.commitFiles {
if a.commitSel[p] {
n++
}
}
return n
}
// commitDialog 画提交弹窗:文件清单可勾选,只提交勾选的。
func (a *App) commitDialog(c *ui.Context) {
if !a.commitOpen {
return
}
t := c.Theme()
ui.Modal(c, &a.commitOpen, func() {
ui.Column(c).Width(480).Gap(10).Children(func() {
ui.Text(c, "提交改动").FontSize(13).Bold().TextColor(t.Text)
ui.Row(c).Gap(8).AlignItems(ui.Center).Children(func() {
ui.Textf(c, "%s · 勾选 %d / %d", a.gst.info.Branch, a.commitChecked(), len(a.commitFiles)).
FontSize(10).TextColor(t.TextMuted).SingleLine()
ui.Box(c).Grow(1)
if ui.Button(c, "全选").Clicked() {
for _, p := range a.commitFiles {
a.commitSel[p] = true
}
}
if ui.Button(c, "全不选").Clicked() {
for _, p := range a.commitFiles {
a.commitSel[p] = false
}
}
})
// 文件清单:清单可能很长,限高滚动,别把弹窗顶出屏幕。
ui.Scroll(c).FillWidth().Height(220).Children(func() {
ui.Column(c).FillWidth().Gap(2).Children(func() {
for _, rel := range a.commitFiles {
p := rel
// map 索引取不了地址,用局部变量中转:控件点击后写回。
checked := a.commitSel[p]
ui.Row(c).FillWidth().Gap(6).AlignItems(ui.Center).Children(func() {
ui.Checkbox(c, &checked, "")
ui.Text(c, gitBadge(a.commitCode[p])).FontSize(10).
TextColor(a.gitColor(a.commitCode[p])).Width(16)
ui.Text(c, filepath.Base(rel)).FontSize(12).TextColor(t.Text).SingleLine()
ui.Box(c).Grow(1)
dir := filepath.ToSlash(filepath.Dir(rel))
if dir != "." {
ui.Text(c, dir).FontSize(10).TextColor(t.TextMuted).SingleLine()
}
})
a.commitSel[p] = checked
}
})
})
inp := ui.TextInput(c, &a.commitMsg).FillWidth().FontSize(13).AutoFocus()
if inp.Submitted() {
a.confirmCommit()
}
ui.Row(c).Gap(8).Justify(ui.End).Children(func() {
if ui.Button(c, "取消").Clicked() {
a.commitOpen = false
}
if ui.Button(c, "提交").Clicked() {
a.confirmCommit()
}
})
})
})
if !a.commitOpen && a.commitDlg {
a.commitDlg = false
a.focusEditorSoon()
}
if !a.commitDlg && a.commitOpen {
a.commitDlg = true
a.win.Update(func() {})
}
}
// confirmCommit 只暂存勾选的文件,再提交;结果写进输出面板。
func (a *App) confirmCommit() {
var paths []string
for _, p := range a.commitFiles {
if a.commitSel[p] {
paths = append(paths, p)
}
}
if len(paths) == 0 {
// 不关弹窗也不提交:空勾选多半是手滑,提醒比默默拒绝友好。
a.alert("Git", "没有勾选任何文件")
return
}
msg := a.commitMsg
if msg == "" {
msg = "更新"
}
root := a.root
a.commitOpen = false
a.win.Update(func() {})
go func() {
var out string
if err := git.StagePaths(root, paths); err != nil {
out = "git add 失败:" + err.Error()
} else if err := git.Commit(root, msg); err != nil {
out = "git commit 失败:" + err.Error()
} else {
out = fmt.Sprintf("已提交 %d 个文件:%s", len(paths), msg)
}
a.win.Update(func() {
kind := OutSuccess
if strings.HasPrefix(out, "git ") {
kind = OutError
}
a.out = append(a.out, OutLine{Text: out, Kind: kind})
a.panel = true
a.panelTab = 0
a.refreshGit()
a.win.Update(func() {})
})
}()
}
// gitPull 拉取当前分支(--ff-only,分叉时拒绝合并)。
func (a *App) gitPull() {
if !a.gst.info.OK {
a.alert("Git", "当前工作区不是 git 仓库,或者没有安装 git")
return
}
root := a.root
a.out = append(a.out, OutLine{Text: "git pull …", Kind: OutPlain})
a.panel = true
a.panelTab = 0
a.refresh()
go func() {
out, err := git.Pull(root)
a.gitOpDone("拉取", out, err)
}()
}
// gitPush 推送当前分支(无上游时自动 -u origin <分支>)。
func (a *App) gitPush() {
if !a.gst.info.OK {
a.alert("Git", "当前工作区不是 git 仓库,或者没有安装 git")
return
}
root := a.root
a.out = append(a.out, OutLine{Text: "git push …", Kind: OutPlain})
a.panel = true
a.panelTab = 0
a.refresh()
go func() {
out, err := git.Push(root)
a.gitOpDone("推送", out, err)
}()
}
// gitOpDone 把 pull/push 的结果写进输出面板并刷新仓库状态。
func (a *App) gitOpDone(op, out string, err error) {
a.win.Update(func() {
if err != nil {
text := op + "失败:" + err.Error()
if out != "" {
text += "\n" + out
}
a.out = append(a.out, OutLine{Text: text, Kind: OutError})
} else {
text := op + "完成"
if out != "" {
text += "\n" + out
}
a.out = append(a.out, OutLine{Text: text, Kind: OutSuccess})
}
a.panel = true
a.panelTab = 0
a.refreshGit()
a.win.Update(func() {})
})
}