mirror of
https://github.com/daveberrys/guh.git
synced 2026-11-29 00:26:46 +08:00
implement per file commit
This commit is contained in:
1 parent
789190955c
commit
b0dc539715
1 file changed
+38
-5
+38
-5
@@ -1,6 +1,9 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/daveberrys/guh/src/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -10,12 +13,42 @@ func init() {
|
||||
}
|
||||
|
||||
var commitCmd = &cobra.Command{
|
||||
Use: "commit",
|
||||
Short: "Add current changes and commit",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "commit [files] [message] [description]",
|
||||
Short: "Add specified files and commit",
|
||||
Args: cobra.RangeArgs(0, 3),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
utils.RunGitSequence([]string{"add", "."})
|
||||
utils.RunGitSequence([]string{"commit", "-m", args[0]})
|
||||
if len(args) == 0 {
|
||||
return utils.RunGit("status", "--short")
|
||||
}
|
||||
|
||||
if len(args) < 2 {
|
||||
return fmt.Errorf("requires at least 2 arguments: files (JSON array or \".\") and commit message")
|
||||
}
|
||||
|
||||
var addArgs []string
|
||||
if args[0] == "." {
|
||||
addArgs = []string{"add", "."}
|
||||
} else {
|
||||
var files []string
|
||||
if err := json.Unmarshal([]byte(args[0]), &files); err != nil {
|
||||
return fmt.Errorf("first argument must be \".\" or a JSON array of file paths: %w", err)
|
||||
}
|
||||
addArgs = append([]string{"add"}, files...)
|
||||
}
|
||||
|
||||
fmt.Println("Edited files:")
|
||||
if err := utils.RunGit("status", "--short"); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
utils.RunGitSequence(addArgs)
|
||||
|
||||
commitArgs := []string{"commit", "-m", args[1]}
|
||||
if len(args) == 3 {
|
||||
commitArgs = append(commitArgs, "-m", args[2])
|
||||
}
|
||||
utils.RunGitSequence(commitArgs)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
Reference in new issue
Block a user