Skip to content

Commit 1c20cd1

Browse files
authored
setup python black linter (#65)
* ✨refactor code formatting in multiple files, update dev requirements * ✨refactor argument parsing for commands and enhance code formatting
1 parent 7759730 commit 1c20cd1

25 files changed

+656
-413
lines changed

ai_commit_msg/cli/config_handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ai_commit_msg.utils.logger import Logger
55
from ai_commit_msg.services.onboarding_service import onboarding_flow
66

7+
78
def handle_config_setup():
89
onboarding_flow()
910
return
@@ -25,7 +26,7 @@ def config_handler(args):
2526
has_updated = True
2627

2728
if args.reset:
28-
#reset the db the entire db
29+
# reset the db the entire db
2930
LocalDbService().reset_db()
3031
Logger().log("Configuration has been reset")
3132
has_updated = True

ai_commit_msg/cli/gen_ai_commit_message_handler.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
from ai_commit_msg.utils.error import AIModelHandlerError
66
from ai_commit_msg.utils.logger import Logger
77

8+
89
def gen_ai_commit_message_handler():
910
logger = Logger()
1011

1112
PipService().display_outdated_version_message()
1213

13-
if(len(GitService.get_staged_files()) == 0):
14-
print("🚨 No files are staged for commit. Run `git add` to stage some of your changes")
15-
return
14+
if len(GitService.get_staged_files()) == 0:
15+
print(
16+
"🚨 No files are staged for commit. Run `git add` to stage some of your changes"
17+
)
18+
return
1619

1720
staged_diff = GitService.get_staged_diff()
1821

@@ -34,24 +37,28 @@ def gen_ai_commit_message_handler():
3437

3538
should_push_changes = input(command_string)
3639

37-
if should_push_changes == 'n':
40+
if should_push_changes == "n":
3841
logger.log("👋 Goodbye!")
3942
return
40-
elif should_push_changes != 'y':
43+
elif should_push_changes != "y":
4144
logger.log("🚨 Invalid input. Exiting.")
4245
return
4346

44-
execute_cli_command(['git', 'commit', '-m', ai_gen_commit_msg], output=True)
47+
execute_cli_command(["git", "commit", "-m", ai_gen_commit_msg], output=True)
4548
current_branch = GitService.get_current_branch()
4649
has_upstream = GitService.has_upstream_branch(current_branch)
4750

4851
if has_upstream:
49-
execute_cli_command(['git', 'push'], output=True)
52+
execute_cli_command(["git", "push"], output=True)
5053
return
5154

52-
set_upstream = input(f"No upstream branch found for '{current_branch}'. This will run: 'git push --set-upstream origin {current_branch}'. Set upstream? (y/n): ")
53-
if set_upstream.lower() == 'y':
54-
execute_cli_command(['git', 'push', '--set-upstream', 'origin', current_branch], output=True)
55+
set_upstream = input(
56+
f"No upstream branch found for '{current_branch}'. This will run: 'git push --set-upstream origin {current_branch}'. Set upstream? (y/n): "
57+
)
58+
if set_upstream.lower() == "y":
59+
execute_cli_command(
60+
["git", "push", "--set-upstream", "origin", current_branch], output=True
61+
)
5562
print(f"🔄 Upstream branch set for '{current_branch}'")
5663
else:
5764
print("Skipping push. You can set upstream manually")

ai_commit_msg/cli/hook_handler.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def get_bash_script():
1212
version = PipService.get_version()
1313
id = uuid.uuid4()
1414

15-
PREPARE_COMMIT_MSG_BASH_SCRIPT=f"""#!/usr/bin/env bash
15+
PREPARE_COMMIT_MSG_BASH_SCRIPT = f"""#!/usr/bin/env bash
1616
# This file was generated by git-ai-commit: https://github.com/the-cafe/git-ai-commit
1717
# version: {version}
1818
# hash: {id}
@@ -30,26 +30,29 @@ def get_bash_script():
3030
"""
3131
return PREPARE_COMMIT_MSG_BASH_SCRIPT
3232

33+
3334
def handle_setup_hook(hook_directory_path: str):
3435
existing_hook_content = ""
3536
if os.path.exists(hook_directory_path):
36-
with open(hook_directory_path, 'r') as file:
37-
existing_hook_content = file.read()
37+
with open(hook_directory_path, "r") as file:
38+
existing_hook_content = file.read()
3839

3940
if existing_hook_content == get_bash_script():
4041
Logger().log("prepare-commit-msg hook already exists")
4142
return
4243

4344
if existing_hook_content:
44-
override_content = input(f"prepare-commit-msg hook already exists in {hook_directory_path}\n\nWould you like to overwrite it? (y/n): ")
45+
override_content = input(
46+
f"prepare-commit-msg hook already exists in {hook_directory_path}\n\nWould you like to overwrite it? (y/n): "
47+
)
4548

46-
if override_content.lower() == 'n':
47-
return
48-
elif override_content.lower() != 'y':
49-
Logger().log("Invalid input. Exiting.")
49+
if override_content.lower() == "n":
50+
return
51+
elif override_content.lower() != "y":
52+
Logger().log("Invalid input. Exiting.")
5053

5154
## create the prepare-commit-msg file and write the hook code
52-
with open(hook_directory_path, 'w') as file:
55+
with open(hook_directory_path, "w") as file:
5356
file.write(get_bash_script())
5457

5558
# make the file executable
@@ -74,7 +77,9 @@ def setup_husky_git_hook():
7477
Logger().log("[Setup] Husky prepare-commit-msg hook")
7578

7679
if not HuskyService.repo_has_husky_framework():
77-
Logger().log("Husky framework not found in the repository. Please install husky - https://typicode.github.io/husky/")
80+
Logger().log(
81+
"Husky framework not found in the repository. Please install husky - https://typicode.github.io/husky/"
82+
)
7883
return
7984

8085
file_path = HuskyService.get_husky_prepare_commit_msg_hook_path()
@@ -95,6 +100,7 @@ def setup_git_hook():
95100

96101
return
97102

103+
98104
def hook_handler(args):
99105
if args.setup:
100106
setup_git_hook()

ai_commit_msg/cli/summary_handler.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,47 @@
33
from ai_commit_msg.utils.logger import Logger
44
from ai_commit_msg.utils.utils import execute_cli_command
55

6+
67
def print_summary(summary):
7-
Logger().log(f"""Here is a summary of your changes:
8+
Logger().log(
9+
f"""Here is a summary of your changes:
810
911
{summary}
1012
1113
to use this summary run: `git commit -m "{summary}"`
12-
""")
14+
"""
15+
)
16+
1317

1418
def summaryFromDiffFile(diff_file_path):
15-
with open(diff_file_path, 'r') as file:
16-
diff = file.read()
17-
ai_commit_msg = generate_commit_message(diff)
18-
print_summary(ai_commit_msg)
19+
with open(diff_file_path, "r") as file:
20+
diff = file.read()
21+
ai_commit_msg = generate_commit_message(diff)
22+
print_summary(ai_commit_msg)
23+
1924

2025
def summary_handler(args):
2126

22-
if args.diff:
23-
summaryFromDiffFile(args.diff)
24-
return
27+
if args.diff:
28+
summaryFromDiffFile(args.diff)
29+
return
2530

26-
if args.unstaged:
27-
Logger().log("Fetching your unstaged changes...\n")
28-
unstaged_changes_diff = execute_cli_command(['git', 'diff'])
29-
ai_commit_msg = generate_commit_message(unstaged_changes_diff.stdout)
30-
print_summary(ai_commit_msg)
31-
return
31+
if args.unstaged:
32+
Logger().log("Fetching your unstaged changes...\n")
33+
unstaged_changes_diff = execute_cli_command(["git", "diff"])
34+
ai_commit_msg = generate_commit_message(unstaged_changes_diff.stdout)
35+
print_summary(ai_commit_msg)
36+
return
3237

33-
Logger().log("Fetching your staged changes...\n")
38+
Logger().log("Fetching your staged changes...\n")
3439

35-
if(len(GitService.get_staged_files()) == 0):
36-
Logger().log("🚨 No files are staged for commit. Run `git add` to stage some of your changes")
37-
return
40+
if len(GitService.get_staged_files()) == 0:
41+
Logger().log(
42+
"🚨 No files are staged for commit. Run `git add` to stage some of your changes"
43+
)
44+
return
3845

39-
staged_changes_diff = execute_cli_command(['git', 'diff', '--staged'])
40-
ai_commit_msg = generate_commit_message(staged_changes_diff.stdout)
46+
staged_changes_diff = execute_cli_command(["git", "diff", "--staged"])
47+
ai_commit_msg = generate_commit_message(staged_changes_diff.stdout)
4148

42-
print_summary(ai_commit_msg)
49+
print_summary(ai_commit_msg)

ai_commit_msg/core/gen_commit_msg.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,29 @@
77
from ai_commit_msg.utils.logger import Logger
88
from ai_commit_msg.utils.models import ANTHROPIC_MODEL_LIST, OPEN_AI_MODEL_LIST
99

10+
1011
def generate_commit_message(diff: str = None) -> str:
1112

12-
if diff is None:
13-
raise ValueError("Diff is required to generate a commit message")
13+
if diff is None:
14+
raise ValueError("Diff is required to generate a commit message")
1415

15-
select_model = ConfigService.get_model()
16+
select_model = ConfigService.get_model()
1617

17-
prompt = get_prompt(diff)
18+
prompt = get_prompt(diff)
1819

19-
# TODO - create a factory with a shared interface for calling the LLM models, this will make it easier to add new models
20-
ai_gen_commit_msg = None
21-
if str(select_model) in OPEN_AI_MODEL_LIST :
22-
ai_gen_commit_msg = OpenAiService().chat_with_openai(prompt)
23-
elif(select_model.startswith("ollama")):
24-
ai_gen_commit_msg = OLlamaService().chat_completion(prompt)
25-
elif(select_model in ANTHROPIC_MODEL_LIST):
26-
ai_gen_commit_msg = AnthropicService().chat_completion(prompt)
20+
# TODO - create a factory with a shared interface for calling the LLM models, this will make it easier to add new models
21+
ai_gen_commit_msg = None
22+
if str(select_model) in OPEN_AI_MODEL_LIST:
23+
ai_gen_commit_msg = OpenAiService().chat_with_openai(prompt)
24+
elif select_model.startswith("ollama"):
25+
ai_gen_commit_msg = OLlamaService().chat_completion(prompt)
26+
elif select_model in ANTHROPIC_MODEL_LIST:
27+
ai_gen_commit_msg = AnthropicService().chat_completion(prompt)
2728

28-
if ai_gen_commit_msg is None:
29-
Logger().log("Unsupported model: " + select_model)
30-
return ""
29+
if ai_gen_commit_msg is None:
30+
Logger().log("Unsupported model: " + select_model)
31+
return ""
3132

32-
prefix = ConfigService().prefix
33+
prefix = ConfigService().prefix
3334

34-
return prefix + ai_gen_commit_msg
35+
return prefix + ai_gen_commit_msg

ai_commit_msg/core/prompt.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from ai_commit_msg.services.config_service import ConfigService
22

3+
34
def get_prompt(diff):
4-
max_length = ConfigService().max_length
5+
max_length = ConfigService().max_length
56

6-
COMMIT_MSG_SYSTEM_MESSAGE = f'''
7+
COMMIT_MSG_SYSTEM_MESSAGE = f"""
78
Your a software engineer and you are reviewing a set of code changes.
89
You will be provided with a set of code changes in diff format.
910
@@ -13,9 +14,9 @@ def get_prompt(diff):
1314
- You don't need to add any punctuation or capitalization.
1415
- Instead of and, use a comma to save characters.
1516
- Your response cannot more than {max_length} characters.
16-
'''
17+
"""
1718

18-
return [
19-
{"role": "system", "content": COMMIT_MSG_SYSTEM_MESSAGE},
20-
{"role": "user", "content": diff},
21-
]
19+
return [
20+
{"role": "system", "content": COMMIT_MSG_SYSTEM_MESSAGE},
21+
{"role": "user", "content": diff},
22+
]

0 commit comments

Comments
 (0)