refactor: change commit length limit and change exit codes

This commit is contained in:
Slug-Boi
2025-06-16 17:43:02 +02:00
parent eeb93fb411
commit 9e85fdd1b9
+17 -18
View File
@@ -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
}