Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Commit 2a7ad34

Browse files
🐛 bugfix(hook): Fix bug in commit message hook for LOG (#6)
## 💌 Description <!-- Add a more detailed description of the changes if needed. --> ## 🩹 Related issue <!-- If your PR refers to a related issue, link it here. --> Fixes: # ## 🏗️ Type of change <!-- Mark with an `x` all the checkboxes that apply (like `[x]`) --> - [ ] 📚 Examples / docs / tutorials / dependencies update - [ ] 🐛 Bug fix (non-breaking change which fixes an issue) - [ ] 🥂 Improvement (non-breaking change which improves an existing feature) - [ ] 🚀 New feature (non-breaking change which adds functionality) - [ ] 💥 Breaking change (fix or feature that would cause existing functionality to change) - [ ] 🔐 Security fix - [ ] ⬆️ Dependencies update ## ✅ Checklist <!-- Mark with an `x` all the checkboxes that apply (like `[x]`) --> - [ ] I've read the [`CODE_OF_CONDUCT.md`](https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks/blob/main/CODE_OF_CONDUCT.md) document. - [ ] I've read the [`CONTRIBUTING.md`](https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks/blob/main/CONTRIBUTING.md) guide. - [ ] I've written tests for all new methods and classes that I created. - [ ] I've written the docstring in Google format for all the methods and classes that I used.
1 parent d661bf7 commit 2a7ad34

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ repos:
3131
- "500"
3232
- "--emoji"
3333
- "--description"
34+
- "--log-level"
35+
- "debug"
3436
stages: [prepare-commit-msg]

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Example:
152152
```yaml
153153
repos:
154154
- repo: https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks
155-
rev: v0.1.1
155+
rev: v0.1.2
156156
hooks:
157157
- id: chatgpt-commit-message
158158
```
@@ -174,7 +174,7 @@ repos:
174174
- or include it in a `requirements.txt` file in your project:
175175

176176
```text
177-
chatgpt-pre-commit-hooks~=0.1.1
177+
chatgpt-pre-commit-hooks~=0.1.2
178178
```
179179

180180
and run:
@@ -204,25 +204,25 @@ repos:
204204

205205
1. Add to your `.pre-commit-config.yaml`
206206

207-
```yml
207+
```yaml
208208
repos:
209209
- repo: local
210210
hooks:
211-
- id: ... # follow 🎣 Hooks section to see available hooks IDs
212-
name: Run chatgpt-pre-commit-hooks (<name>)
213-
entry: chatgpt-pre-commit-hooks.<id> # follow 🎣 Hooks section to see available hooks IDs
211+
- id: <id> # follow 🎣 Hooks section to see available hooks IDs
212+
name: Run <name>
213+
entry: <id> # follow 🎣 Hooks section to see available hooks IDs
214214
language: system
215215
```
216216

217217
Example:
218218

219-
```yml
219+
```yaml
220220
repos:
221-
- repo: local
221+
- repo: local
222222
hooks:
223-
- id: chatgpt-commit-message
224-
name: Run chatgpt-pre-commit-hooks (chatgpt-commit-message)
225-
entry: chatgpt-pre-commit-hooks.chatgpt-commit-message
223+
- id: chatgpt-commit-message
224+
name: Run ChatGPT commit-message
225+
entry: chatgpt-commit-message
226226
language: system
227227
```
228228

chatgpt_pre_commit_hooks/chatgpt_commit_message.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import tiktoken
1717
from git.repo import Repo
1818

19-
logger = logging.getLogger(__name__)
19+
LOG = logging.getLogger(__name__)
2020

2121

2222
def get_args() -> argparse.Namespace:
@@ -66,13 +66,13 @@ def get_git_diff(max_char_count: int, git_repo_path: str) -> str:
6666
diff = repo.git.diff(staged=True)
6767
if len(diff) > max_char_count:
6868
diff = repo.git.diff(staged=True, stat=True)
69-
logger.debug(f"GIT_DIFF: {diff}")
69+
LOG.debug(f"GIT_DIFF: {diff}")
7070
return diff
7171

7272

7373
def get_user_commit_message(commit_msg_file_path: str, prepare_commit_message_source: Optional[str]) -> Optional[str]:
7474
"""Get user commit message (if specified)."""
75-
logger.debug(f"PREPARE_COMMIT_MESSAGE_SOURCE: {prepare_commit_message_source}")
75+
LOG.debug(f"PREPARE_COMMIT_MESSAGE_SOURCE: {prepare_commit_message_source}")
7676
user_commit_message = None
7777
if prepare_commit_message_source == "message" or prepare_commit_message_source is None:
7878
commit_msg_file = Path(commit_msg_file_path)
@@ -83,7 +83,7 @@ def get_user_commit_message(commit_msg_file_path: str, prepare_commit_message_so
8383
if lines != []:
8484
user_commit_message = "".join(lines).strip()
8585

86-
logger.debug(f"USER_COMMIT_MESSAGE: {user_commit_message}")
86+
LOG.debug(f"USER_COMMIT_MESSAGE: {user_commit_message}")
8787

8888
if user_commit_message is not None:
8989
skip_keywords = ["#no-ai", "#no-openai", "#no-chatgpt", "#no-gpt", "#skip-ai", "#skip-openai", "#skip-chatgpt", "#skip-gpt"]
@@ -131,8 +131,8 @@ def get_openai_chat_prompt_messages(user_commit_message: Optional[str], git_diff
131131
role_system_prompt = " ".join(role_system)
132132
role_user_prompt = " ".join(role_user)
133133

134-
logger.debug(f"ROLE_SYSTEM_PROMPT: {role_system_prompt}")
135-
logger.debug(f"ROLE_USER_PROMPT: {role_user_prompt}")
134+
LOG.debug(f"ROLE_SYSTEM_PROMPT: {role_system_prompt}")
135+
LOG.debug(f"ROLE_USER_PROMPT: {role_user_prompt}")
136136

137137
return [
138138
{"role": "system", "content": role_system_prompt},
@@ -142,7 +142,7 @@ def get_openai_chat_prompt_messages(user_commit_message: Optional[str], git_diff
142142

143143
def get_openai_chat_response(messages: List[Dict[str, str]], args: argparse.Namespace) -> str:
144144
"""Get OpenAI Chat Response."""
145-
if logger.isEnabledFor(logging.DEBUG):
145+
if LOG.isEnabledFor(logging.DEBUG):
146146
_num_tokens_from_messages(messages, str(args.openai_model))
147147
openai.debug = True
148148

@@ -161,7 +161,7 @@ def get_openai_chat_response(messages: List[Dict[str, str]], args: argparse.Name
161161
temperature=0,
162162
top_p=0.1,
163163
)
164-
logger.debug(f"OPENAI_CHAT_RESPONSE: {response}")
164+
LOG.debug(f"OPENAI_CHAT_RESPONSE: {response}")
165165

166166
return response["choices"][0]["message"]["content"]
167167

@@ -197,7 +197,7 @@ def _num_tokens_from_messages(messages: List[Dict[str, str]], model: str) -> int
197197
if key == "name":
198198
num_tokens += tokens_per_name
199199
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
200-
logger.debug(f"NUM_TOKENS: {num_tokens}")
200+
LOG.debug(f"NUM_TOKENS: {num_tokens}")
201201
return num_tokens
202202

203203

@@ -211,8 +211,21 @@ def set_commit_message(commit_msg_file_path: str, commit_msg: str) -> None:
211211
commit_msg_file_wrapper.close()
212212

213213

214-
def main(args: argparse.Namespace) -> int:
214+
def main() -> int:
215215
"""Main function of module."""
216+
global LOG # noqa: PLW0603
217+
218+
args = get_args()
219+
LOG = logging.getLogger(__name__)
220+
LOG.setLevel(args.log_level.upper())
221+
222+
if LOG.isEnabledFor(logging.DEBUG):
223+
fh = logging.FileHandler(filename="debug.log", mode="w")
224+
LOG.addHandler(fh)
225+
226+
LOG.debug(f"SYS_ARGV: {sys.argv}")
227+
LOG.debug(f"ARGS: {args}")
228+
216229
try:
217230
user_commit_message = get_user_commit_message(args.commit_msg_filename, args.prepare_commit_message_source)
218231
git_diff = get_git_diff(args.max_char_count, ".")
@@ -226,15 +239,4 @@ def main(args: argparse.Namespace) -> int:
226239

227240

228241
if __name__ == "__main__":
229-
args = get_args()
230-
logger = logging.getLogger(__name__)
231-
logger.setLevel(args.log_level.upper())
232-
233-
if logger.isEnabledFor(logging.DEBUG):
234-
fh = logging.FileHandler(filename="debug.log", mode="w")
235-
logger.addHandler(fh)
236-
237-
logger.debug(f"SYS_ARGV: {sys.argv}")
238-
logger.debug(f"ARGS: {args}")
239-
240-
sys.exit(main(args))
242+
sys.exit(main())

docs/chatgpt_commit_message.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Add to your `.pre-commit-config.yaml`
2525
```yaml
2626
repos:
2727
- repo: https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks
28-
rev: v0.1.1 # Use the ref you want to point at, see ⚠️ NOTE below!
28+
rev: v0.1.2 # Use the ref you want to point at, see ⚠️ NOTE below!
2929
hooks:
3030
- id: chatgpt-commit-message
3131
```
@@ -49,7 +49,7 @@ Example:
4949
```yaml
5050
repos:
5151
- repo: https://github.com/DariuszPorowski/chatgpt-pre-commit-hooks
52-
rev: v0.1.1 # Use the ref you want to point at, see ⚠️ NOTE below!
52+
rev: v0.1.2 # Use the ref you want to point at, see ⚠️ NOTE below!
5353
hooks:
5454
- id: chatgpt-commit-message
5555
args:

0 commit comments

Comments
 (0)