|
10 | 10 | import argparse
|
11 | 11 | import logging
|
12 | 12 | import re
|
| 13 | +import subprocess |
13 | 14 | import sys
|
14 | 15 | from logging.handlers import RotatingFileHandler
|
15 | 16 |
|
@@ -170,20 +171,25 @@ def add_icon_to_commit_message(commit_type: str, existing_commit_msg: str) -> st
|
170 | 171 | return existing_commit_msg
|
171 | 172 |
|
172 | 173 |
|
173 |
| -def write_commit_message(file_path: str, commit_msg: str) -> None: |
| 174 | +def amend_commit(new_commit_msg: str) -> None: |
174 | 175 | """
|
175 |
| - Writes the updated commit message back to the commit message file. |
| 176 | + Amends the current commit with the new commit message. |
176 | 177 |
|
177 | 178 | Args:
|
178 |
| - file_path (str): Path to the commit message file. |
179 |
| - commit_msg (str): The updated commit message. |
| 179 | + new_commit_msg (str): The new commit message. |
| 180 | +
|
| 181 | + Raises: |
| 182 | + subprocess.CalledProcessError: If git amend fails. |
180 | 183 | """
|
181 | 184 | try:
|
182 |
| - with open(file_path, "w", encoding="utf-8") as file: |
183 |
| - file.write(commit_msg + "\n") |
184 |
| - logger.debug(f"Commit message written to file: {file_path}") |
185 |
| - except Exception as e: |
186 |
| - logger.error(f"Error writing to commit message file: {e}") |
| 185 | + # Amend the commit with the new commit message |
| 186 | + subprocess.run(["git", "commit", "--amend", "-m", new_commit_msg], check=True) |
| 187 | + logger.info("Successfully amended the commit with the new commit message.") |
| 188 | + logger.info( |
| 189 | + "Please perform a push using 'git push' to update the remote repository. Avoid use --force" |
| 190 | + ) |
| 191 | + except subprocess.CalledProcessError as e: |
| 192 | + logger.error(f"Failed to amend the commit: {e}") |
187 | 193 | sys.exit(1)
|
188 | 194 |
|
189 | 195 |
|
@@ -253,7 +259,7 @@ def main() -> None:
|
253 | 259 | updated_commit_msg = add_icon_to_commit_message(commit_type, commit_msg)
|
254 | 260 |
|
255 | 261 | # Write the updated commit message back to the file
|
256 |
| - write_commit_message(commit_msg_file, updated_commit_msg) |
| 262 | + amend_commit(updated_commit_msg) |
257 | 263 |
|
258 | 264 | # Inform the user and abort the commit to allow them to review the amended message
|
259 | 265 | logger.info(
|
|
0 commit comments