improve commiting and pushing

This commit is contained in:
daveberrys committed 2026-07-19 03:06:58 +08:00
1 parent 99e2b8d92f
commit 5c7575a889
2 files changed
+74 -6

No files matched your search

+54 -3
View File
@@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"fmt"
"strings"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
@@ -13,7 +14,7 @@ func init() { rootCmd.AddCommand(commitCmd) }
var commitCmd = &cobra.Command{
Use: "commit [files] [message] [description]",
Short: "Add specified files and commit",
Args: cobra.RangeArgs(0, 3),
Args: cobra.RangeArgs(0, 5),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return utils.RunGitSequence(false, []string{"status", "--short"})
@@ -35,16 +36,66 @@ var commitCmd = &cobra.Command{
}
fmt.Println("Edited files:")
if err := utils.RunGitSequence(false, []string{"status", "--short"}); err != nil { return err }
if err := utils.RunGitSequence(false, []string{"status", "--short"}); err != nil {
return err
}
fmt.Println()
utils.RunGitSequence(false, addArgs)
commitArgs := []string{"commit", "-m", args[1]}
if len(args) == 3 {
afterPush, remoteName := false, "origin"
// case 1 -> guh commit [files] [message] [desc] push[:optional] [remote_name:optional(origin)]
// case 2 -> guh commit [files] [message] push[:optional] [remote_name:optional(origin)]
// case 3 -> guh commit [files] [message] [desc]
// case 4 -> guh commit [files] [message]
switch len(args) {
case 5:
// [files] [message] [desc] push [remote] (case 1)
commitArgs = append(commitArgs, "-m", args[2])
afterPush = true
if args[4] != "" {
remoteName = args[4]
}
case 4:
if args[2] == "push" {
// [files] [message] push [remote] (case 2)
afterPush = true
if args[3] != "" {
remoteName = args[3]
}
} else {
// [files] [message] [desc] push (case 1)
commitArgs = append(commitArgs, "-m", args[2])
afterPush = true
}
case 3:
if args[2] == "push" {
// [files] [message] push (case 2)
afterPush = true
} else {
// [files] [message] [desc] (case 3)
commitArgs = append(commitArgs, "-m", args[2])
}
}
utils.RunGitSequence(false, commitArgs)
if afterPush {
if remoteName == "all" {
remotes, err := utils.RunGit(true, "remote")
if err != nil {
return err
}
for _, r := range strings.Fields(remotes) {
if err := utils.RunGitSequence(false, []string{"push", r}); err != nil {
return err
}
}
} else {
utils.RunGitSequence(false, []string{"push", remoteName})
}
}
return nil
},
}
+20 -3
View File
@@ -1,6 +1,8 @@
package cmd
import (
"strings"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
@@ -8,10 +10,25 @@ import (
func init() { rootCmd.AddCommand(pushCmd) }
var pushCmd = &cobra.Command{
Use: "push",
Use: "push [remote]",
Short: "Add current changes and push",
Args: cobra.NoArgs,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return utils.RunGitSequence(false, []string{"push"})
if len(args) == 0 {
return utils.RunGitSequence(false, []string{"push"})
}
if args[0] == "all" {
remotes, err := utils.RunGit(true, "remote")
if err != nil {
return err
}
for _, r := range strings.Fields(remotes) {
if err := utils.RunGitSequence(false, []string{"push", r}); err != nil {
return err
}
}
return nil
}
return utils.RunGitSequence(false, []string{"push", args[0]})
},
}