git-commit-msg-ai 1.7.0__py3-none-any.whl → 1.8.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- git_commit_msg_ai/ai_client.py +8 -10
- git_commit_msg_ai/cli.py +24 -16
- git_commit_msg_ai/editor.py +16 -12
- git_commit_msg_ai/git_ops.py +6 -17
- {git_commit_msg_ai-1.7.0.dist-info → git_commit_msg_ai-1.8.0.dist-info}/METADATA +2 -2
- git_commit_msg_ai-1.8.0.dist-info/RECORD +11 -0
- git_commit_msg_ai-1.7.0.dist-info/RECORD +0 -11
- {git_commit_msg_ai-1.7.0.dist-info → git_commit_msg_ai-1.8.0.dist-info}/WHEEL +0 -0
- {git_commit_msg_ai-1.7.0.dist-info → git_commit_msg_ai-1.8.0.dist-info}/entry_points.txt +0 -0
- {git_commit_msg_ai-1.7.0.dist-info → git_commit_msg_ai-1.8.0.dist-info}/top_level.txt +0 -0
git_commit_msg_ai/ai_client.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
import textwrap
|
|
3
|
-
from typing import Final
|
|
3
|
+
from typing import Final
|
|
4
4
|
|
|
5
5
|
import anthropic
|
|
6
6
|
|
|
@@ -24,7 +24,7 @@ def generate_commit_message(diff: str) -> str:
|
|
|
24
24
|
try:
|
|
25
25
|
anthropic_client = anthropic.Anthropic()
|
|
26
26
|
|
|
27
|
-
logger.debug("Calling Anthropic API: model
|
|
27
|
+
logger.debug(f"Calling Anthropic API: model={MODEL} max_tokens={MAX_TOKENS}")
|
|
28
28
|
anthropic_api_response = anthropic_client.messages.create(
|
|
29
29
|
model=MODEL,
|
|
30
30
|
max_tokens=MAX_TOKENS,
|
|
@@ -32,21 +32,19 @@ def generate_commit_message(diff: str) -> str:
|
|
|
32
32
|
messages=[{"role": "user", "content": diff}],
|
|
33
33
|
)
|
|
34
34
|
except anthropic.AuthenticationError:
|
|
35
|
-
logger.error("Anthropic authentication error")
|
|
36
35
|
raise AIError("Anthropic API key is missing or invalid. Set the ANTHROPIC_API_KEY environment variable.")
|
|
37
36
|
except anthropic.RateLimitError:
|
|
38
|
-
logger.error("Anthropic rate limit error")
|
|
39
37
|
raise AIError("Anthropic API rate limit reached. Wait a moment and try again.")
|
|
40
38
|
except anthropic.APIConnectionError:
|
|
41
|
-
logger.error("Anthropic API connection error")
|
|
42
39
|
raise AIError("Could not reach the Anthropic API. Check your network connection.")
|
|
43
|
-
except anthropic.APIStatusError as
|
|
44
|
-
|
|
45
|
-
raise AIError(f"Anthropic API returned an error: {error.status_code}.")
|
|
40
|
+
except anthropic.APIStatusError as api_status_error:
|
|
41
|
+
raise AIError(f"Anthropic API returned an error: {api_status_error.status_code}.")
|
|
46
42
|
|
|
47
43
|
anthropic_api_response_message = anthropic_api_response.content[0]
|
|
48
|
-
|
|
44
|
+
if not isinstance(anthropic_api_response_message, anthropic.types.TextBlock):
|
|
45
|
+
raise AIError("Unexpected response format from Anthropic API.")
|
|
46
|
+
commit_message = anthropic_api_response_message.text.strip()
|
|
49
47
|
|
|
50
|
-
logger.info("Commit message generated:
|
|
48
|
+
logger.info(f"Commit message generated: {len(commit_message)} chars")
|
|
51
49
|
|
|
52
50
|
return commit_message
|
git_commit_msg_ai/cli.py
CHANGED
|
@@ -6,20 +6,28 @@ from typing import Final
|
|
|
6
6
|
from git_commit_msg_ai import ai_client, editor, git_ops
|
|
7
7
|
from git_commit_msg_ai.exceptions import GitCommitAIError
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
LOG_LEVEL_ENVIRONMENT_VARIABLE: Final[str] = "GIT_COMMIT_AI_LOG_LEVEL"
|
|
10
|
+
DEFAULT_LOG_LEVEL: Final[str] = "WARNING"
|
|
11
|
+
LOG_FORMAT: Final[str] = "%(levelname)s:%(name)s:%(message)s"
|
|
12
|
+
SELECTION_PROMPT: Final[str] = "[a]ccept / [e]dit / [r]eject: "
|
|
13
|
+
SELECTION_ACCEPT: Final[str] = "a"
|
|
14
|
+
SELECTION_EDIT: Final[str] = "e"
|
|
15
|
+
SELECTION_REJECT: Final[str] = "r"
|
|
16
|
+
REJECTED_MESSAGE: Final[str] = "User rejected the generated commit message. No commit made."
|
|
17
|
+
INVALID_SELECTION_MESSAGE: Final[str] = "Invalid selection."
|
|
18
|
+
ABORTED_MESSAGE: Final[str] = "Aborted."
|
|
10
19
|
|
|
11
20
|
|
|
12
21
|
def main() -> None:
|
|
13
|
-
log_level_name = os.environ.get(
|
|
22
|
+
log_level_name = os.environ.get(LOG_LEVEL_ENVIRONMENT_VARIABLE, DEFAULT_LOG_LEVEL).upper()
|
|
14
23
|
numeric_log_level = getattr(logging, log_level_name, None)
|
|
24
|
+
valid_log_level = isinstance(numeric_log_level, int)
|
|
25
|
+
effective_log_level = numeric_log_level if valid_log_level else logging.WARNING
|
|
15
26
|
|
|
16
|
-
if not
|
|
17
|
-
|
|
27
|
+
if not valid_log_level:
|
|
28
|
+
logging.warning(f"Invalid {LOG_LEVEL_ENVIRONMENT_VARIABLE} value {log_level_name!r}, defaulting to {DEFAULT_LOG_LEVEL}")
|
|
18
29
|
|
|
19
|
-
logging.basicConfig(level=
|
|
20
|
-
|
|
21
|
-
if not isinstance(getattr(logging, log_level_name, None), int):
|
|
22
|
-
logging.warning("Invalid %s value %r, defaulting to WARNING", LOG_LEVEL_ENV_VAR, log_level_name)
|
|
30
|
+
logging.basicConfig(level=effective_log_level, format=LOG_FORMAT, stream=sys.stderr)
|
|
23
31
|
|
|
24
32
|
try:
|
|
25
33
|
diff = git_ops.get_staged_diff()
|
|
@@ -27,23 +35,23 @@ def main() -> None:
|
|
|
27
35
|
print(commit_message)
|
|
28
36
|
|
|
29
37
|
print()
|
|
30
|
-
user_selection = input(
|
|
38
|
+
user_selection = input(SELECTION_PROMPT).strip().lower()
|
|
31
39
|
|
|
32
|
-
if user_selection ==
|
|
40
|
+
if user_selection == SELECTION_ACCEPT:
|
|
33
41
|
git_ops.commit(commit_message)
|
|
34
|
-
elif user_selection ==
|
|
42
|
+
elif user_selection == SELECTION_EDIT:
|
|
35
43
|
updated_commit_message = editor.open_in_editor(commit_message)
|
|
36
44
|
git_ops.commit(updated_commit_message)
|
|
37
|
-
elif user_selection ==
|
|
38
|
-
print(
|
|
45
|
+
elif user_selection == SELECTION_REJECT:
|
|
46
|
+
print(REJECTED_MESSAGE)
|
|
39
47
|
else:
|
|
40
|
-
print(
|
|
48
|
+
print(INVALID_SELECTION_MESSAGE)
|
|
41
49
|
sys.exit(1)
|
|
42
50
|
except GitCommitAIError as error:
|
|
43
|
-
|
|
51
|
+
logging.error(str(error))
|
|
44
52
|
sys.exit(1)
|
|
45
53
|
except (KeyboardInterrupt, EOFError):
|
|
46
|
-
print(
|
|
54
|
+
print(ABORTED_MESSAGE)
|
|
47
55
|
sys.exit(1)
|
|
48
56
|
|
|
49
57
|
|
git_commit_msg_ai/editor.py
CHANGED
|
@@ -3,44 +3,49 @@ import os
|
|
|
3
3
|
import platform
|
|
4
4
|
import subprocess
|
|
5
5
|
import tempfile
|
|
6
|
+
from typing import Final
|
|
6
7
|
|
|
7
8
|
from git_commit_msg_ai.exceptions import EditorError
|
|
8
9
|
|
|
9
10
|
logger = logging.getLogger(__name__)
|
|
10
11
|
|
|
12
|
+
_WINDOWS_PLATFORM: Final[str] = "Windows"
|
|
13
|
+
WINDOWS_EDITOR: Final[str] = "notepad"
|
|
14
|
+
FALLBACK_EDITOR: Final[str] = "vi"
|
|
15
|
+
_EDITOR_ENVIRONMENT_VARIABLE: Final[str] = "EDITOR"
|
|
16
|
+
_TEMP_FILE_SUFFIX: Final[str] = ".txt"
|
|
17
|
+
_TEMP_FILE_MODE: Final[str] = "w"
|
|
18
|
+
|
|
11
19
|
|
|
12
20
|
def get_default_editor() -> str:
|
|
13
21
|
current_platform = platform.system()
|
|
14
|
-
is_windows = current_platform ==
|
|
22
|
+
is_windows = current_platform == _WINDOWS_PLATFORM
|
|
15
23
|
|
|
16
|
-
return
|
|
24
|
+
return WINDOWS_EDITOR if is_windows else FALLBACK_EDITOR
|
|
17
25
|
|
|
18
26
|
|
|
19
27
|
def open_in_editor(initial_text: str) -> str:
|
|
20
28
|
try:
|
|
21
|
-
with tempfile.NamedTemporaryFile(suffix=
|
|
29
|
+
with tempfile.NamedTemporaryFile(suffix=_TEMP_FILE_SUFFIX, mode=_TEMP_FILE_MODE, delete=False) as temp_file:
|
|
22
30
|
temp_file.write(initial_text)
|
|
23
31
|
temp_file_path = temp_file.name
|
|
24
32
|
except OSError:
|
|
25
|
-
logger.error("Could not create temporary file")
|
|
26
33
|
raise EditorError("Could not create a temporary file.")
|
|
27
34
|
|
|
28
|
-
logger.debug("Temporary file created:
|
|
35
|
+
logger.debug(f"Temporary file created: {temp_file_path}")
|
|
29
36
|
|
|
30
37
|
platform_default_editor = get_default_editor()
|
|
31
|
-
editor_command = os.environ.get(
|
|
38
|
+
editor_command = os.environ.get(_EDITOR_ENVIRONMENT_VARIABLE, platform_default_editor)
|
|
32
39
|
|
|
33
|
-
logger.debug("Selected editor:
|
|
40
|
+
logger.debug(f"Selected editor: {editor_command}")
|
|
34
41
|
|
|
35
42
|
try:
|
|
36
43
|
try:
|
|
37
|
-
logger.debug("Opening editor:
|
|
44
|
+
logger.debug(f"Opening editor: {editor_command} {temp_file_path}")
|
|
38
45
|
subprocess.run([editor_command, temp_file_path], check=True)
|
|
39
46
|
except FileNotFoundError:
|
|
40
|
-
logger.error("Editor not found: %s", editor_command)
|
|
41
47
|
raise EditorError(f'Editor "{editor_command}" was not found. Set the EDITOR environment variable to a valid editor.')
|
|
42
48
|
except subprocess.CalledProcessError:
|
|
43
|
-
logger.error("Editor exited with error: %s", editor_command)
|
|
44
49
|
raise EditorError(f'Editor "{editor_command}" exited with an error.')
|
|
45
50
|
|
|
46
51
|
logger.debug("Editor closed, reading edited content")
|
|
@@ -49,11 +54,10 @@ def open_in_editor(initial_text: str) -> str:
|
|
|
49
54
|
with open(temp_file_path) as temp_file:
|
|
50
55
|
edited_text = temp_file.read().strip()
|
|
51
56
|
except OSError:
|
|
52
|
-
logger.error("Could not read temporary file: %s", temp_file_path)
|
|
53
57
|
raise EditorError("Could not read the edited commit message from the temporary file.")
|
|
54
58
|
finally:
|
|
55
59
|
os.unlink(temp_file_path)
|
|
56
60
|
|
|
57
|
-
logger.debug("Edited text read:
|
|
61
|
+
logger.debug(f"Edited text read: {len(edited_text)} chars")
|
|
58
62
|
|
|
59
63
|
return edited_text
|
git_commit_msg_ai/git_ops.py
CHANGED
|
@@ -5,34 +5,25 @@ from typing import Final
|
|
|
5
5
|
from git_commit_msg_ai.exceptions import GitError
|
|
6
6
|
|
|
7
7
|
GIT_COMMAND: Final[str] = "git"
|
|
8
|
-
DIFF_SUBCOMMAND: Final[str] = "diff"
|
|
9
|
-
CACHED_FLAG: Final[str] = "--cached"
|
|
10
|
-
COMMIT_SUBCOMMAND: Final[str] = "commit"
|
|
11
|
-
MESSAGE_FLAG: Final[str] = "-m"
|
|
12
|
-
UTF8_ENCODING: Final[str] = "utf-8"
|
|
13
8
|
|
|
14
9
|
logger = logging.getLogger(__name__)
|
|
15
10
|
|
|
16
11
|
|
|
17
12
|
def get_staged_diff() -> str:
|
|
18
13
|
try:
|
|
19
|
-
logger.debug("Running:
|
|
20
|
-
raw_diff_bytes = subprocess.check_output([GIT_COMMAND,
|
|
21
|
-
staged_diff = raw_diff_bytes.decode(
|
|
14
|
+
logger.debug(f"Running: {GIT_COMMAND} diff --cached")
|
|
15
|
+
raw_diff_bytes = subprocess.check_output([GIT_COMMAND, "diff", "--cached"])
|
|
16
|
+
staged_diff = raw_diff_bytes.decode("utf-8")
|
|
22
17
|
except FileNotFoundError:
|
|
23
|
-
logger.error("git not found on PATH")
|
|
24
18
|
raise GitError("git is not installed or not on PATH.")
|
|
25
19
|
except subprocess.CalledProcessError:
|
|
26
|
-
logger.error("git diff --cached exited non-zero")
|
|
27
20
|
raise GitError("Failed to get staged diff. Are you inside a git repository?")
|
|
28
21
|
except UnicodeDecodeError:
|
|
29
|
-
logger.error("Staged diff could not be decoded as UTF-8")
|
|
30
22
|
raise GitError("Staged diff contains bytes that could not be decoded as UTF-8.")
|
|
31
23
|
|
|
32
|
-
logger.debug("Staged diff received:
|
|
24
|
+
logger.debug(f"Staged diff received: {len(staged_diff)} chars")
|
|
33
25
|
|
|
34
26
|
if not staged_diff:
|
|
35
|
-
logger.warning("No staged changes found")
|
|
36
27
|
raise GitError("No staged changes found. Stage files with git add before running.")
|
|
37
28
|
|
|
38
29
|
return staged_diff
|
|
@@ -40,13 +31,11 @@ def get_staged_diff() -> str:
|
|
|
40
31
|
|
|
41
32
|
def commit(message: str) -> None:
|
|
42
33
|
try:
|
|
43
|
-
logger.debug("Running:
|
|
44
|
-
subprocess.run([GIT_COMMAND,
|
|
34
|
+
logger.debug(f"Running: {GIT_COMMAND} commit -m <message>")
|
|
35
|
+
subprocess.run([GIT_COMMAND, "commit", "-m", message], check=True)
|
|
45
36
|
except FileNotFoundError:
|
|
46
|
-
logger.error("git not found on PATH")
|
|
47
37
|
raise GitError("git is not installed or not on PATH.")
|
|
48
38
|
except subprocess.CalledProcessError:
|
|
49
|
-
logger.error("git commit exited non-zero")
|
|
50
39
|
raise GitError("git commit failed.")
|
|
51
40
|
|
|
52
41
|
logger.info("Commit created successfully")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: git-commit-msg-ai
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.8.0
|
|
4
4
|
Summary: AI-powered git commit message generator following Conventional Commits
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Requires-Python: >=3.10
|
|
@@ -156,6 +156,6 @@ git-commit-msg-ai
|
|
|
156
156
|
| Level | What you see |
|
|
157
157
|
|---|---|
|
|
158
158
|
| `DEBUG` | git commands run, API model/token params, temp file paths, char counts |
|
|
159
|
-
| `INFO` | commit message generated, commit created |
|
|
159
|
+
| `INFO` | commit message generated (with char count), commit created |
|
|
160
160
|
| `WARNING` | no staged changes found |
|
|
161
161
|
| `ERROR` | git not found, API failures, editor errors |
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
git_commit_msg_ai/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
git_commit_msg_ai/ai_client.py,sha256=DktS01lI62kaIRW4SccdelpwFn3VSJ_YRjlO5_IQE6I,2238
|
|
3
|
+
git_commit_msg_ai/cli.py,sha256=Z8uIRW1rTK4YDYdj8pIxgLC1Q0agfX5VFTIqI2geHoo,2189
|
|
4
|
+
git_commit_msg_ai/editor.py,sha256=wW7TJ4zVvvN7ThLtrJthAf9FjdmIDTbBS7V5pIW490s,2143
|
|
5
|
+
git_commit_msg_ai/exceptions.py,sha256=7Hwluf3zHMjs4lpGktWS-Lwgo8y_4Xbb1WqzPQHkkUA,352
|
|
6
|
+
git_commit_msg_ai/git_ops.py,sha256=qZae4w3IEf3Lmf2T5oWItkgM0PrEYHCWr3TgBJLeecY,1387
|
|
7
|
+
git_commit_msg_ai-1.8.0.dist-info/METADATA,sha256=UuCgjMWD8xVBSCk4F6FQ6nmIkmjwYN5BQ2jELeHM_kI,4226
|
|
8
|
+
git_commit_msg_ai-1.8.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
9
|
+
git_commit_msg_ai-1.8.0.dist-info/entry_points.txt,sha256=KTu6wUhl0p3nf27k8L4vpSH_hpeRQpwzMPSmKv7K5Cs,65
|
|
10
|
+
git_commit_msg_ai-1.8.0.dist-info/top_level.txt,sha256=XYQC2BXvrcREGKEG7sm9nbwO7ifqcUSVgU7SW8BTURs,18
|
|
11
|
+
git_commit_msg_ai-1.8.0.dist-info/RECORD,,
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
git_commit_msg_ai/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
git_commit_msg_ai/ai_client.py,sha256=ET7wWfgiIPzXsuGi1bV8bV8UWOThPXn5JzRgcnh3yNI,2340
|
|
3
|
-
git_commit_msg_ai/cli.py,sha256=IBU_BhBbd5azbTI6Rso92LtabLHlVUsO3ZUYA2Zr9wE,1686
|
|
4
|
-
git_commit_msg_ai/editor.py,sha256=PycIvOuvHIAx8fSl6_L8AkzAWOS5PRNGTHb6yE11KN0,2087
|
|
5
|
-
git_commit_msg_ai/exceptions.py,sha256=7Hwluf3zHMjs4lpGktWS-Lwgo8y_4Xbb1WqzPQHkkUA,352
|
|
6
|
-
git_commit_msg_ai/git_ops.py,sha256=jJJ_8E_tOIl-SQ5Nf51XawO767P-5VMVXQ7gNNMLDyM,1974
|
|
7
|
-
git_commit_msg_ai-1.7.0.dist-info/METADATA,sha256=4TloB0yvccJL4nQtoPUOCMrGY7026LcTpyF7RJ4Cnpo,4208
|
|
8
|
-
git_commit_msg_ai-1.7.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
9
|
-
git_commit_msg_ai-1.7.0.dist-info/entry_points.txt,sha256=KTu6wUhl0p3nf27k8L4vpSH_hpeRQpwzMPSmKv7K5Cs,65
|
|
10
|
-
git_commit_msg_ai-1.7.0.dist-info/top_level.txt,sha256=XYQC2BXvrcREGKEG7sm9nbwO7ifqcUSVgU7SW8BTURs,18
|
|
11
|
-
git_commit_msg_ai-1.7.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|