From 5c7575a889e1926badb620f604d6c952a767383c Mon Sep 17 00:00:00 2001 From: daveberrys <84567826+daveberrys@users.noreply.github.com> Date: Sun, 19 Jul 2026 03:06:58 +0800 Subject: [PATCH] improve commiting and pushing --- src/cmd/commit.go | 57 ++++++++++++++++++++++++++++++++++++++++++++--- src/cmd/push.go | 23 ++++++++++++++++--- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/cmd/commit.go b/src/cmd/commit.go index e276e7f..51f3249 100644 --- a/src/cmd/commit.go +++ b/src/cmd/commit.go @@ -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 }, } diff --git a/src/cmd/push.go b/src/cmd/push.go index 2342f38..3ac2c02 100644 --- a/src/cmd/push.go +++ b/src/cmd/push.go @@ -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]}) }, }