Skip to content

Commit

Permalink
Refactor commit message generation and editing
Browse files Browse the repository at this point in the history
- Introduces `handle_editing` function to facilitate commit message editing via a text editor
- Implements a mechanism to use the `$EDITOR` environment variable to determine the text editor
- Updates the interaction loop to re-assign the generated commit message to the `commit_message` variable
  • Loading branch information
yankeexe committed Feb 6, 2025
1 parent 9e6d0df commit b083246
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ai_commit/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ def generate_commit_message(staged_changes: str) -> str:


def handle_editing(commit_message: str):
"""Opens the provided commit message in a text editor for user modification.
Uses the editor specified by the environment variable `EDITOR`, defaulting to `vi`.
Creates a temporary file containing the commit message, opens it in the editor,
and reads the modified content from the file after the editor is closed.
Args:
commit_message: The initial commit message to be edited.
Returns:
The updated commit message after editing.
Raises:
SystemExit: If the specified editor is not found or if an error occurs during
the editing process.
"""
editor_not_found_msg = """
❌ {editor} not found in your system.
Please make sure you have exported $EDITOR in your terminal.
Expand Down Expand Up @@ -150,7 +166,7 @@ def interaction_loop(staged_changes: str):
match action:
case "r" | "regenerate":
subprocess.run(commands["clear_screen"], shell=True)
generate_commit_message(staged_changes)
commit_message = generate_commit_message(staged_changes)
case "y" | "yes":
print("committing...")
res = run_command(commands["commit"], [commit_message])
Expand Down

0 comments on commit b083246

Please sign in to comment.