From 94ae2cb1533a8ff3ba412ebf6baa5d7072971a21 Mon Sep 17 00:00:00 2001 From: Artem Yelizarov <52959979+artem-y@users.noreply.github.com> Date: Thu, 18 Apr 2024 22:59:39 +0300 Subject: [PATCH 1/2] Prevent commit when message is empty --- cmd/commit/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/commit/main.go b/cmd/commit/main.go index d02c511..e9f2506 100644 --- a/cmd/commit/main.go +++ b/cmd/commit/main.go @@ -47,7 +47,7 @@ func main() { // Reads commit message from command line arguments func getCommitMessage() string { args := os.Args[1:] - if len(args) < 1 { + if len(args) < 1 || args[0] == "" { fmt.Fprintln(os.Stderr, helpers.Red("Commit message cannot be empty")) os.Exit(1) } From b0b95a24c12a65343ff01f78e38365c15530832b Mon Sep 17 00:00:00 2001 From: Artem Yelizarov <52959979+artem-y@users.noreply.github.com> Date: Thu, 18 Apr 2024 23:31:26 +0300 Subject: [PATCH 2/2] Prevent commit when no staged changes --- cmd/commit/main.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cmd/commit/main.go b/cmd/commit/main.go index e9f2506..efcc67b 100644 --- a/cmd/commit/main.go +++ b/cmd/commit/main.go @@ -115,6 +115,9 @@ func makeCommitOptions(usr user.User) git.CommitOptions { // Commits changes with provided message func commitChanges(repo *git.Repository, commitMessage string) { worktree := openWorktree(repo) + + checkStagedChanges(worktree) + usr := user.GetUser(*repo) commitOptions := makeCommitOptions(usr) @@ -124,3 +127,21 @@ func commitChanges(repo *git.Repository, commitMessage string) { os.Exit(1) } } + +// Checks if there are any staged changes to commit +func checkStagedChanges(worktree *git.Worktree) { + fileStatuses, err := worktree.Status() + if err != nil { + fmt.Fprintf(os.Stderr, helpers.Red("Failed to read the status of the worktree: %v\n"), err) + os.Exit(1) + } + + for _, status := range fileStatuses { + if status.Staging != git.Unmodified { + return + } + } + + fmt.Fprintln(os.Stderr, helpers.Red("No staged changes to commit")) + os.Exit(1) +}