feat(*): 自动添加文件到GIT缓冲区

main
熊二 2 years ago
parent 19618fd5a0
commit b5c8cd7098
  1. 28
      main.go

@ -205,6 +205,7 @@ type Message struct {
Scope string Scope string
Subject string Subject string
Files []string Files []string
News []string
} }
func (m *Message) String() string { func (m *Message) String() string {
@ -247,13 +248,6 @@ func (m *Message) Valid() bool {
return len(m.Type) > 0 && len(m.Subject) > 0 && len(m.Files) > 0 return len(m.Type) > 0 && len(m.Subject) > 0 && len(m.Files) > 0
} }
type file struct {
Name string
Add bool
Del bool
New bool
}
func readFiles(path string, f func(string)) error { func readFiles(path string, f func(string)) error {
entries, err := os.ReadDir(path) entries, err := os.ReadDir(path)
if err != nil { if err != nil {
@ -312,6 +306,7 @@ func (m *Message) AskFiles() error {
} }
opts = append(opts, s) opts = append(opts, s)
infos[s] = "新增" infos[s] = "新增"
m.News = append(m.News, s)
}) })
if err != nil { if err != nil {
return err return err
@ -336,6 +331,7 @@ func (m *Message) AskFiles() error {
infos[name] = "更新但未合并" infos[name] = "更新但未合并"
case '?': case '?':
infos[name] = "新增" infos[name] = "新增"
m.News = append(m.News, name)
} }
} }
} }
@ -504,7 +500,21 @@ func (m *Message) Confirm() (bool, error) {
return true, nil return true, nil
} }
func (m *Message) AddNews() error {
args := append([]string{"add"}, m.News...)
cmd := exec.Command("git", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (m *Message) Commit() error { func (m *Message) Commit() error {
if len(m.News) > 0 {
err := m.AddNews()
if err != nil {
return err
}
}
args := []string{"commit", "-o"} args := []string{"commit", "-o"}
for _, f := range m.Files { for _, f := range m.Files {
args = append(args, "./"+f) args = append(args, "./"+f)
@ -517,10 +527,6 @@ func (m *Message) Commit() error {
} }
func main() { func main() {
//readFiles(".", func(s string) {
// fmt.Println(s)
//})
var msg Message var msg Message
ok, err := msg.Confirm() ok, err := msg.Confirm()
if err == nil && ok { if err == nil && ok {

Loading…
Cancel
Save