Skip to content

Commit

Permalink
refactor: enhance commit command with path filtering and prefix handling
Browse files Browse the repository at this point in the history
- Adds path filtering to the commit command, allowing users to specify which files to include in the diff
- Introduces default exclusion patterns to ignore lock files and other non-essential files
- Ensures that generated commit messages always start with a specified prefix
  • Loading branch information
liblaf committed Nov 29, 2024
1 parent de62d93 commit 301512f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/llm_cli/cmd/commit/_app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import asyncio
from typing import Annotated

import typer

app = typer.Typer(name="commit")


@app.command()
def main() -> None:
def main(
path: Annotated[list[str] | None, typer.Argument()] = None,
*,
default_exclude: Annotated[bool, typer.Option()] = True,
verify: Annotated[bool, typer.Option()] = True,
) -> None:
from ._main import main

asyncio.run(main())
path: list[str] = path or []
if default_exclude:
path += [":!*-lock.*", ":!*.lock*", ":!*.cspell.*"]
asyncio.run(main(path, verify=verify))
5 changes: 3 additions & 2 deletions src/llm_cli/cmd/commit/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import llm_cli.utils as lu


async def main(*, verify: bool = True) -> None:
async def main(path: list[str], *, verify: bool = True) -> None:
await lu.run("git", "status")
prompt_template = string.Template(lu.get_prompt("commit"))
repo = git.Repo(search_parent_directories=True)
diff: str = repo.git.diff("--cached", "--no-ext-diff")
diff: str = repo.git.diff("--cached", "--no-ext-diff", *path)
files: str = repo.git.ls_files()
prompt: str = prompt_template.substitute({"GIT_DIFF": diff, "GIT_FILES": files})
resp: litellm.ModelResponse = await lc.output(prompt, prefix="<Answer>")
Expand Down
2 changes: 2 additions & 0 deletions src/llm_cli/interactive/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ async def output(
response = litellm.stream_chunk_builder(chunks) # pyright: ignore [reportAssignmentType]
choices: litellm.Choices = response.choices[0] # pyright: ignore [reportAssignmentType]
content: str = choices.message.content or ""
if prefix and not content.startswith(prefix):
content = prefix + content
if sanitize:
content = sanitize(content)
live.update(
Expand Down

0 comments on commit 301512f

Please sign in to comment.