diff --git a/cmd/commit/main.go b/cmd/commit/main.go index d02c511..efcc67b 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) } @@ -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) +}