From 9e85fdd1b992fd24129453f613c3c23359a97b27 Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Mon, 16 Jun 2025 17:43:02 +0200 Subject: [PATCH] refactor: change commit length limit and change exit codes --- src/cmd/utils/editor.go | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/cmd/utils/editor.go b/src/cmd/utils/editor.go index c27ec58..676a3ce 100644 --- a/src/cmd/utils/editor.go +++ b/src/cmd/utils/editor.go @@ -7,7 +7,6 @@ import ( "strings" ) - func HandleEditor() (string, error) { editor := ConfigVar.Settings.Editor if editor == "built-in" { @@ -25,7 +24,6 @@ func HandleEditor() (string, error) { return "", fmt.Errorf("editor %s not found in PATH", editor) } - output, err := LaunchEditor(editor, "") if err != nil { return "", fmt.Errorf("failed to launch editor %s: %v", editor, err) @@ -33,7 +31,7 @@ func HandleEditor() (string, error) { return output, nil } -func LaunchEditor(editor string, filepath string) (string,error) { +func LaunchEditor(editor string, filepath string) (string, error) { // Create a temp file or use an existing file var tempFile *os.File var err error @@ -55,12 +53,12 @@ func LaunchEditor(editor string, filepath string) (string,error) { if filepath == "" { tempFile, err = os.CreateTemp("", "cocommit_editor_*.txt") - } else { - tempFile, err = os.OpenFile(filepath, os.O_RDWR, 0666) - } - if err != nil { - return "", fmt.Errorf("Could not create or open tempfile: %s", err.Error()) - } + } else { + tempFile, err = os.OpenFile(filepath, os.O_RDWR, 0666) + } + if err != nil { + return "", fmt.Errorf("Could not create or open tempfile: %s", err.Error()) + } cmd := exec.Command(editor, tempFile.Name()) cmd.Stdin = os.Stdin @@ -71,7 +69,6 @@ func LaunchEditor(editor string, filepath string) (string,error) { return "", fmt.Errorf("error running editor command: %v", err) } - data, err := os.ReadFile(tempFile.Name()) if err != nil { return "", fmt.Errorf("error reading temp file: %v", err) @@ -79,7 +76,8 @@ func LaunchEditor(editor string, filepath string) (string,error) { message := string(data) if message == "" { - return "", fmt.Errorf("commit message is empty") + fmt.Printf("Error: Commit message is empty. Please provide a commit message.\n") + os.Exit(0) } // Clean up the temp file @@ -93,15 +91,16 @@ func LaunchEditor(editor string, filepath string) (string,error) { message = strings.TrimSuffix(message, "\r") } if strings.TrimSpace(message) == "" { - return "", fmt.Errorf("commit message is empty after trimming") + fmt.Printf("Error: Commit message is empty. Please provide a commit message.\n") + os.Exit(0) } // If the message is too long, truncate it - if len(message) > 72 { - fmt.Printf("Warning: Commit message is too long (%d characters). It will be truncated to 72 characters.\n", len(message)) - //TODO: Maybe add the rest to the description of the message? - //description := message[72:] - message = message[:72] - } + // if len(message) > 72 { + // fmt.Printf("Warning: Commit message is too long (%d characters). It will be truncated to 72 characters.\n", len(message)) + // //TODO: Maybe add the rest to the description of the message? + // //description := message[72:] + // message = message[:72] + // } return message, nil -} \ No newline at end of file +}