mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 12:45:47 +00:00
refactor: change commit length limit and change exit codes
This commit is contained in:
+17
-18
@@ -7,7 +7,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func HandleEditor() (string, error) {
|
func HandleEditor() (string, error) {
|
||||||
editor := ConfigVar.Settings.Editor
|
editor := ConfigVar.Settings.Editor
|
||||||
if editor == "built-in" {
|
if editor == "built-in" {
|
||||||
@@ -25,7 +24,6 @@ func HandleEditor() (string, error) {
|
|||||||
return "", fmt.Errorf("editor %s not found in PATH", editor)
|
return "", fmt.Errorf("editor %s not found in PATH", editor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
output, err := LaunchEditor(editor, "")
|
output, err := LaunchEditor(editor, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to launch editor %s: %v", editor, err)
|
return "", fmt.Errorf("failed to launch editor %s: %v", editor, err)
|
||||||
@@ -33,7 +31,7 @@ func HandleEditor() (string, error) {
|
|||||||
return output, nil
|
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
|
// Create a temp file or use an existing file
|
||||||
var tempFile *os.File
|
var tempFile *os.File
|
||||||
var err error
|
var err error
|
||||||
@@ -55,12 +53,12 @@ func LaunchEditor(editor string, filepath string) (string,error) {
|
|||||||
|
|
||||||
if filepath == "" {
|
if filepath == "" {
|
||||||
tempFile, err = os.CreateTemp("", "cocommit_editor_*.txt")
|
tempFile, err = os.CreateTemp("", "cocommit_editor_*.txt")
|
||||||
} else {
|
} else {
|
||||||
tempFile, err = os.OpenFile(filepath, os.O_RDWR, 0666)
|
tempFile, err = os.OpenFile(filepath, os.O_RDWR, 0666)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Could not create or open tempfile: %s", err.Error())
|
return "", fmt.Errorf("Could not create or open tempfile: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(editor, tempFile.Name())
|
cmd := exec.Command(editor, tempFile.Name())
|
||||||
cmd.Stdin = os.Stdin
|
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)
|
return "", fmt.Errorf("error running editor command: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
data, err := os.ReadFile(tempFile.Name())
|
data, err := os.ReadFile(tempFile.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("error reading temp file: %v", err)
|
return "", fmt.Errorf("error reading temp file: %v", err)
|
||||||
@@ -79,7 +76,8 @@ func LaunchEditor(editor string, filepath string) (string,error) {
|
|||||||
|
|
||||||
message := string(data)
|
message := string(data)
|
||||||
if message == "" {
|
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
|
// Clean up the temp file
|
||||||
@@ -93,15 +91,16 @@ func LaunchEditor(editor string, filepath string) (string,error) {
|
|||||||
message = strings.TrimSuffix(message, "\r")
|
message = strings.TrimSuffix(message, "\r")
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(message) == "" {
|
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 the message is too long, truncate it
|
||||||
if len(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))
|
// 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?
|
// //TODO: Maybe add the rest to the description of the message?
|
||||||
//description := message[72:]
|
// //description := message[72:]
|
||||||
message = message[:72]
|
// message = message[:72]
|
||||||
}
|
// }
|
||||||
|
|
||||||
return message, nil
|
return message, nil
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user