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

Commit 6c087f2

Browse files
committed
Enhance commit functionality in cli.py and update tests 🛠️
This commit introduces the ability to specify files for the commit command in cli.py. Now, users can choose to commit changes from specific files, providing more flexibility. The changes also include updates to the corresponding tests to reflect this new functionality. Additionally, minor adjustments have been made to improve code readability and maintainability. For instance, the 'sys.exit()' calls have been replaced with 'return' or 'sys.exit(0)' for better clarity. This should make the commit process more intuitive and user-friendly. Happy coding! 😊
1 parent c2505d6 commit 6c087f2

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

aicodebot/cli.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,23 @@ def alignment(response_token_size, verbose):
8282
@click.option("-t", "--response-token-size", type=int, default=250)
8383
@click.option("-y", "--yes", is_flag=True, default=False, help="Don't ask for confirmation before committing.")
8484
@click.option("--skip-pre-commit", is_flag=True, help="Skip running pre-commit (otherwise run it if it is found).")
85-
def commit(verbose, response_token_size, yes, skip_pre_commit):
85+
@click.argument("files", nargs=-1, type=click.Path(exists=True))
86+
def commit(verbose, response_token_size, yes, skip_pre_commit, files):
8687
"""Generate a commit message based on your changes."""
8788
setup_config()
8889

89-
# Get the changes from git
90+
# If files are specified, only consider those files
91+
if files:
92+
staged_files = [f for f in Coder.git_staged_files() if f in files]
93+
unstaged_files = [f for f in Coder.git_unstaged_files() if f in files]
94+
else:
95+
# Otherwise use git
96+
staged_files = Coder.git_staged_files()
97+
unstaged_files = Coder.git_unstaged_files()
9098

91-
staged_files = Coder.git_staged_files()
92-
unstaged_files = Coder.git_unstaged_files()
9399
if not staged_files:
94100
# If no files are staged, they probably want to commit all changed files, confirm.
95-
if not yes:
101+
if not files and not yes:
96102
click.confirm(
97103
"Since there are no git staged files, all of the modified files will be committed:\n\t"
98104
+ "\n\t".join(unstaged_files)
@@ -119,8 +125,8 @@ def commit(verbose, response_token_size, yes, skip_pre_commit):
119125
return
120126

121127
if not staged_files:
122-
# Add all files to the index (git add -A)
123-
exec_and_get_output(["git", "add", "-A"])
128+
# Stage the files that we are committing
129+
exec_and_get_output(["git", "add"] + list(files))
124130

125131
# Load the prompt
126132
prompt = get_prompt("commit")
@@ -272,7 +278,7 @@ def debug(command, verbose):
272278
# If it succeeded, exit
273279
if process.returncode == 0:
274280
console.print("✅ The command completed successfully.")
275-
sys.exit(0)
281+
return
276282

277283
# If the command failed, send its output to ChatGPT for analysis
278284
error_output = process.stderr
@@ -339,14 +345,14 @@ def fun_fact(verbose, response_token_size):
339345
@click.option("-v", "--verbose", count=True)
340346
@click.option("--output-format", default="text", type=click.Choice(["text", "json"], case_sensitive=False))
341347
@click.option("-t", "--response-token-size", type=int, default=DEFAULT_MAX_TOKENS * 2)
342-
def review(commit, verbose, output_format, response_token_size):
348+
def review(commit, verbose, output_format, response_token_size, files):
343349
"""Do a code review, with [un]staged changes, or a specified commit."""
344350
setup_config()
345351

346352
diff_context = Coder.git_diff_context(commit)
347353
if not diff_context:
348354
console.print("No changes detected for review. 🤷")
349-
sys.exit(0)
355+
return
350356

351357
# Load the prompt
352358
prompt = get_prompt("review", structured_output=output_format == "json")
@@ -466,7 +472,7 @@ def setup_config():
466472
if not existing_config:
467473
console.print("Welcome to AI Code Bot! 🤖. Let's set up your config file.\n", style=bot_style)
468474
configure.callback(openai_api_key=os.getenv("OPENAI_API_KEY"), verbose=0)
469-
sys.exit()
475+
sys.exit(0)
470476
else:
471477
return existing_config
472478

tests/test_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ def test_commit(cli_runner, temp_git_repo):
1919
with cli_runner.isolated_filesystem():
2020
os.chdir(temp_git_repo.working_dir) # change to the temporary repo directory
2121

22-
response_token_size = 200 # smaller than the default so tests go a little faster
22+
response_token_size = 200 # smaller than the default so tests go a little faster
2323
# Scenario 1: Only unstaged changes
2424
create_and_write_file("test1.txt", "This is a test file.")
2525
repo = Repo(temp_git_repo.working_dir)
2626
repo.git.add("test1.txt") # stage the new file
27-
result = cli_runner.invoke(cli, ["commit", "-y", "-t", response_token_size])
27+
result = cli_runner.invoke(cli, ["commit", "-y", "-t", response_token_size, "test1.txt"])
2828
assert result.exit_code == 0
2929
assert "✅ 1 file(s) committed" in result.output
3030

@@ -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])
36+
result = cli_runner.invoke(cli, ["commit", "-y", "-t", response_token_size, "test2.txt"])
3737
assert result.exit_code == 0
3838
assert "✅ 1 file(s) committed" in result.output
3939

0 commit comments

Comments
 (0)