Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Commit 92770bc

Browse files
committed
Enhance code review functionality in CLI and Coder 🛠️
This commit introduces the ability to specify files for the code review command in the CLI. If no files are specified, the command will consider all staged and unstaged changes. The `git_diff_context` method in the Coder class has also been updated to accommodate this change. In addition, the test cases have been updated to reflect these changes. This enhancement should provide more flexibility when performing code reviews. 🚀
1 parent 17d2319 commit 92770bc

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

aicodebot/cli.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,19 @@ def fun_fact(verbose, response_token_size):
352352
@click.option("-v", "--verbose", count=True)
353353
@click.option("--output-format", default="text", type=click.Choice(["text", "json"], case_sensitive=False))
354354
@click.option("-t", "--response-token-size", type=int, default=DEFAULT_MAX_TOKENS * 2)
355+
@click.argument("files", nargs=-1)
355356
def review(commit, verbose, output_format, response_token_size, files):
356357
"""Do a code review, with [un]staged changes, or a specified commit."""
357358
setup_config()
358359

359-
diff_context = Coder.git_diff_context(commit)
360+
# If files are specified, only consider those files
361+
# Otherwise, use git to get the list of files
362+
if not files:
363+
files = Coder.git_staged_files()
364+
if not files:
365+
files = Coder.git_unstaged_files()
366+
367+
diff_context = Coder.git_diff_context(commit, files)
360368
if not diff_context:
361369
console.print("No changes detected for review. 🤷")
362370
return
@@ -388,6 +396,7 @@ def review(commit, verbose, output_format, response_token_size, files):
388396

389397
else:
390398
# Stream live
399+
console.print("Examining the diff and generating the review for the following files:\n\t" + "\n\t".join(files))
391400
with Live(Markdown(""), auto_refresh=True) as live:
392401
llm.streaming = True
393402
llm.callbacks = [RichLiveCallbackHandler(live, bot_style)]

aicodebot/coder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def get_token_length(text, model="gpt-3.5-turbo"):
118118
return token_length
119119

120120
@staticmethod
121-
def git_diff_context(commit=None):
121+
def git_diff_context(commit=None, files=None):
122122
"""Get a text representation of the git diff for the current commit or staged files, including new files"""
123123
base_git_diff = ["git", "diff", "-U10"] # Tell diff to provide 10 lines of context
124124

@@ -136,10 +136,11 @@ def git_diff_context(commit=None):
136136
logger.debug(f"Getting diff for staged files: {staged_files}")
137137
diff_type = "--cached"
138138
else:
139-
logger.debug(f"Getting diff for unstaged files: {staged_files}")
140139
diff_type = "HEAD"
141140

142-
file_status = exec_and_get_output(["git", "diff", diff_type, "--name-status"]).splitlines()
141+
file_status = exec_and_get_output(
142+
["git", "diff", diff_type, "--name-status"] + list(files or [])
143+
).splitlines()
143144

144145
diffs = []
145146
for status in file_status:

tests/test_cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_commit(cli_runner, temp_git_repo):
3333
repo = Repo(temp_git_repo.working_dir)
3434
repo.git.add("test2.txt") # stage the new file
3535
create_and_write_file("test3.txt", "This is yet another test file.") # unstaged file
36-
result = cli_runner.invoke(cli, ["commit", "-y", "-t", response_token_size, "test2.txt"])
36+
result = cli_runner.invoke(cli, ["commit", "-y", "-t", response_token_size])
3737
assert result.exit_code == 0
3838
assert "✅ 1 file(s) committed" in result.output
3939

@@ -119,19 +119,19 @@ def test_review(cli_runner, temp_git_repo):
119119
repo.git.add("test.txt")
120120

121121
# Run the review command
122-
result = cli_runner.invoke(cli, ["review", "-t", "100"])
122+
result = cli_runner.invoke(cli, ["review", "-t", "100", "test.txt"])
123123

124124
# Check that the review command ran successfully
125125
assert result.exit_code == 0
126126
assert len(result.output) > 20
127127

128128
# Again with json output
129-
result = cli_runner.invoke(cli, ["review", "-t", "100", "--output-format", "json"])
129+
result = cli_runner.invoke(cli, ["review", "-t", "100", "--output-format", "json", "test.txt"])
130130

131131
assert result.exit_code == 0
132132
# Check if it's valid json
133133
parsed = json.loads(result.output)
134-
assert parsed["review_status"] in ["PASSED", "COMMENTS"]
134+
assert parsed["review_status"] in ["PASSED"]
135135

136136

137137
@pytest.mark.skipif(not os.getenv("OPENAI_API_KEY"), reason="Skipping live tests without an API key.")

0 commit comments

Comments
 (0)