Skip to content

Gracefully handle empty git repositories #92

@404-Page-Found

Description

@404-Page-Found

Task summary

When commit-echo is run in a freshly initialized git repository (no commits yet), checkGitRepo() passes (it only validates git rev-parse --git-dir), but the suggest flow eventually fails with a cryptic error. Add a check for zero commits early in the flow and show a clear, actionable message.

Why this is a good first issue

Single-file change that follows the existing error-handling pattern used in suggestCommand(). Teaches how git operations are wrapped, how error messages are surfaced to users, and the importance of validating preconditions before doing work.

Suggested files or areas

  • src/git/diff.ts — Add a hasCommits() function (e.g., git rev-list --count HEAD) or modify checkGitRepo() to also verify the repo has at least one commit
  • src/commands/suggest.ts — Call hasCommits() early in the suggest flow and show a friendly message if the repo has no commits

Acceptance criteria

  • Running commit-echo in a repo with zero commits shows a clear message like "This repository has no commits yet. commit-echo needs at least one commit to analyze your style."
  • The check happens before any LLM call (no wasted API call)
  • Repos with one or more commits work unchanged
  • npm run build passes with no errors

Extra context

  • checkGitRepo() in src/git/diff.ts runs git rev-parse --git-dir — add a helper that checks git rev-list --count HEAD (or git log --oneline -1) and returns a boolean
  • See the existing error handling in suggestCommand() for patterns (e.g., the loadOrPromptConfig catch block using outro(pc.red(...)))
  • The check should go right after checkGitRepo() succeeds, before loading config or calling getStagedDiff()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions