From dee0ee79f1ef8c6da9fe8b33834eacca07ce2cec Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Tue, 27 May 2025 15:06:53 +0200 Subject: [PATCH] feat: add amend command --- src/cmd/amend.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/cmd/amend.go diff --git a/src/cmd/amend.go b/src/cmd/amend.go new file mode 100644 index 0000000..1e64e83 --- /dev/null +++ b/src/cmd/amend.go @@ -0,0 +1,75 @@ +/* +Copyright © 2025 NAME HERE +*/ +package cmd + +import ( + "os" + "strings" + + "github.com/Slug-Boi/cocommit/src/cmd/tui" + "github.com/Slug-Boi/cocommit/src/cmd/utils" + "github.com/spf13/cobra" +) + +// amendCmd represents the amend command +var amendCmd = &cobra.Command{ + Use: "amend", + Short: "Amend a commit message", + Long: `Ammend an existing commit message and add co-authors to it. + If ran without any arguments, it will open the TUI to select co-authors. + If ran with arguments, it will add the co-authors to the commit message.`, + Run: func(cmd *cobra.Command, args []string) { + // check if the print flag is set + pflag, _ := cmd.Flags().GetBool("print-output") + tflag, _ := cmd.Flags().GetBool("test_print") + git_flags, _ := cmd.Flags().GetString("git-flags") + edit, _ := cmd.Flags().GetBool("edit") + hash, _ := cmd.Flags().GetString("hash") + + + if hash != "" { + println("Hash based commit amendment is not yet implemented please use rebase option manually in git and then use this command to add co-authors.") + hash = "" + return + } + + if edit { + + } + + var authors string + if len(args) == 0 { + // open the TUI to select co-authors + list_authors := tui.Entry() + if list_authors == nil { + println("No authors selected, exiting.") + os.Exit(1) + } + authors = utils.Commit("", list_authors) + } else { + authors = utils.Commit("", args) + } + + git_flags_split := []string{} + if git_flags != "" { + git_flags_split = strings.Split(git_flags, " ") + } + + err := utils.GitCommitAppender(authors, hash, git_flags_split, tflag, pflag) + if err != nil { + println("Error amending commit:", err.Error()) + os.Exit(1) + } + + }, +} + +func init() { + rootCmd.AddCommand(amendCmd) + amendCmd.Flags().StringP("git-flags", "g", "", "Git flags to add to the commit command") + amendCmd.Flags().BoolP("print-output", "p", false, "Print the commit message to stdout") + amendCmd.Flags().BoolP("test_print", "t", false, "Print the commit message to stdout without amending") + amendCmd.Flags().BoolP("edit", "e", false, "Edit the commit message in the editor") + amendCmd.Flags().StringP("hash", "s", "", "Hash of the commit to amend") +}