Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion cmd/commit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}