|
10 | 10 | from rich.style import Style |
11 | 11 | import click, datetime, openai, os, random, subprocess, sys, tempfile, webbrowser |
12 | 12 |
|
13 | | -# Create a Console object |
14 | 13 | console = Console() |
15 | 14 | bot_style = Style(color="#30D5C8") |
16 | 15 | DEFAULT_MAX_TOKENS = 1024 |
@@ -212,6 +211,45 @@ def fun_fact(verbose): |
212 | 211 | console.print(response, style=bot_style) |
213 | 212 |
|
214 | 213 |
|
| 214 | +@cli.command |
| 215 | +@click.option("--commit", "-c", help="The commit hash to review.") |
| 216 | +@click.option("--verbose", "-v") |
| 217 | +def review(commit, verbose): |
| 218 | + """Use AI to do a code review, with [un]staged changes, or a specified commit.""" |
| 219 | + setup_environment() |
| 220 | + |
| 221 | + if commit: |
| 222 | + # If a commit hash is specified, get the diff for that commit |
| 223 | + diff = exec_and_get_output(["git", "show", commit]) |
| 224 | + else: |
| 225 | + # If no commit hash is specified, get the diff for changes, staged or not |
| 226 | + staged_files = exec_and_get_output(["git", "diff", "--name-only", "--cached"]) |
| 227 | + base_git_diff = ["git", "diff", "-U10"] # Tell diff to provide 10 lines of context |
| 228 | + if not staged_files: |
| 229 | + # Get the diff for all changes since the last commit |
| 230 | + diff = exec_and_get_output(base_git_diff + ["HEAD"]) |
| 231 | + else: |
| 232 | + # If some files are staged, get the diff for those files |
| 233 | + diff = exec_and_get_output(base_git_diff + ["--cached"]) |
| 234 | + |
| 235 | + if not diff: |
| 236 | + console.print("No changes to commit.") |
| 237 | + sys.exit(0) |
| 238 | + |
| 239 | + # Load the prompt |
| 240 | + prompt = load_prompt(Path(__file__).parent / "prompts" / "review.yaml") |
| 241 | + |
| 242 | + # Set up the language model |
| 243 | + llm = OpenAI(temperature=0.1, max_tokens=DEFAULT_MAX_TOKENS) |
| 244 | + |
| 245 | + # Set up the chain |
| 246 | + chain = LLMChain(llm=llm, prompt=prompt, verbose=verbose) |
| 247 | + |
| 248 | + with console.status("Reviewing", spinner="point"): |
| 249 | + response = chain.run(diff) |
| 250 | + console.print(response, style=bot_style) |
| 251 | + |
| 252 | + |
215 | 253 | @cli.command() |
216 | 254 | def version(): |
217 | 255 | """Print the version number.""" |
|
0 commit comments