From a61407bf564ebebaa9f8b562a19e42c6ed65d339 Mon Sep 17 00:00:00 2001 From: zeeland Date: Thu, 4 Jul 2024 18:05:13 +0800 Subject: [PATCH] feat: Enhance generate_commit_message to generate multiple commit options --- gcop/__main__.py | 26 +++++++++++++++----------- pyproject.toml | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/gcop/__main__.py b/gcop/__main__.py index 39ef21d..b962bb3 100644 --- a/gcop/__main__.py +++ b/gcop/__main__.py @@ -2,7 +2,7 @@ import subprocess from enum import Enum from pathlib import Path -from typing import Literal +from typing import List, Literal import click import pne @@ -17,7 +17,6 @@ load_dotenv() - app = typer.Typer( name="gcop", help="gcop is your local git command copilot", @@ -36,9 +35,9 @@ class Color(str, Enum): class LLMResponse(BaseModel): - content: str = Field( + content: List[str] = Field( ..., - description="git commit message, eg: feat: Add type annotation to generate_commit_message function", # noqa + description="three of alternative git commit messages, eg: feat: Add type annotation to generate_commit_message function", # noqa ) @@ -60,7 +59,7 @@ def get_git_diff(diff_type: Literal["--staged", "--cached"]) -> str: raise ValueError(f"Error getting git diff: {e}") -def generate_commit_message(diff: str) -> str: +def generate_commit_message(diff: str) -> List[str]: """Generate a git commit message based on the given diff. Args: @@ -147,14 +146,19 @@ def commit_command(): console.print(f"[yellow]Staged: {diff}[/]") - commit_message: str = generate_commit_message(diff) + commit_messages: List[str] = generate_commit_message(diff) - response = questionary.confirm( - f"Ready to run command: git commit -m '{commit_message}', continue?" + response = questionary.select( + "Select a commit message to commit", choices=[*commit_messages, "retry"] ).ask() + + if response == "retry": + commit_command() + return + if response: - console.print(f"[green]Command: git commit -m '{commit_message}'[/]") - subprocess.run(["git", "commit", "-m", f"{commit_message}"]) + console.print(f"[green]Command: git commit -m '{response}'[/]") + subprocess.run(["git", "commit", "-m", f"{response}"]) else: console.print("[red]Canceled[/]") @@ -170,7 +174,7 @@ def help_command(): console.print("[bold] git ghelp Add command into git config") console.print("[bold] git gconfig Open the config file in the default editor") console.print( - "[bold] git gcommit Generate a git commit message based on the staged changes and commit the changes" # noqa + "[bold] git gcommit Generate a git commit message based on the staged changes and commit the changes" # noqa ) diff --git a/pyproject.toml b/pyproject.toml index 382541e..02a1211 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ requires = ["poetry-core"] [tool.poetry] name = "gcop" -version = "1.0.1" +version = "1.1.0" description = "gcop is your git AI copilot." readme = "README.md" authors = ["gcop "]