Skip to content

Commit 0f11360

Browse files
committed
šŸ› fix(core): fix commit icons and commit regex validation [patch candidate]
1 parent 1f5f3d3 commit 0f11360

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

ā€Žcommit_msg_version_bump/main.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def get_latest_commit_message() -> str:
120120
stdout=subprocess.PIPE,
121121
stderr=subprocess.PIPE,
122122
text=True,
123+
encoding="utf-8",
123124
).stdout.strip()
124125
logger.debug(f"Latest commit message: {message}")
125126
return message
@@ -201,7 +202,11 @@ def bump_version(part: str) -> None:
201202
subprocess.CalledProcessError: If bump2version fails.
202203
"""
203204
try:
204-
subprocess.run(["bump2version", part], check=True)
205+
subprocess.run(
206+
["bump2version", part],
207+
check=True,
208+
encoding="utf-8",
209+
)
205210
logger.info(f"Successfully bumped the {part} version.")
206211
except subprocess.CalledProcessError as error:
207212
logger.error(f"Failed to bump the {part} version: {error}")
@@ -216,7 +221,11 @@ def stage_changes(pyproject_path: str = "pyproject.toml") -> None:
216221
pyproject_path (str): Path to the file to stage.
217222
"""
218223
try:
219-
subprocess.run(["git", "add", pyproject_path], check=True)
224+
subprocess.run(
225+
["git", "add", pyproject_path],
226+
check=True,
227+
encoding="utf-8",
228+
)
220229
logger.debug(f"Staged {pyproject_path} for commit.")
221230
except subprocess.CalledProcessError as e:
222231
logger.error(f"Failed to stage {pyproject_path}: {e}")
@@ -235,10 +244,14 @@ def amend_commit(new_commit_msg: str) -> None:
235244
"""
236245
try:
237246
# Amend the commit with the new commit message
238-
subprocess.run(["git", "commit", "--amend", "-m", new_commit_msg], check=True)
247+
subprocess.run(
248+
["git", "commit", "--amend", "-m", new_commit_msg],
249+
check=True,
250+
encoding="utf-8",
251+
)
239252
logger.info("Successfully amended the commit with the new version bump.")
240253
logger.info(
241-
"Please perform a push using 'git push' to update the remote repository. Avoid use --force"
254+
"Please perform a push using 'git push' to update the remote repository. Avoid using --force"
242255
)
243256
except subprocess.CalledProcessError as e:
244257
logger.error(f"Failed to amend the commit: {e}")
@@ -283,7 +296,7 @@ def main() -> None:
283296
amend_commit(updated_commit_msg)
284297

285298
logger.info(
286-
"Aborting the current push. Please perform a push using 'git push'. Avoid use --force"
299+
"Aborting the current push. Please perform a push using 'git push'. Avoid using --force"
287300
)
288301
sys.exit(1)
289302
else:

ā€Žcontrol_commit/main.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -240,35 +240,29 @@ def main() -> None:
240240
if not validate_commit_message(commit_msg):
241241
logger.error("Commit message validation failed. Aborting commit.")
242242
sys.exit(1)
243+
logger.debug("Commit message does not contain square brackets. Proceeding to add icon.")
243244

244-
# Check if the commit message contains square brackets
245-
if not has_square_brackets(commit_msg):
246-
logger.debug("Commit message does not contain square brackets. Proceeding to add icon.")
247-
248-
# Determine the type of commit to get the appropriate icon
249-
type_match = COMMIT_TYPE_REGEX.match(commit_msg)
250-
if type_match:
251-
commit_type = type_match.group("type")
252-
logger.debug(f"Detected commit type: {commit_type}")
253-
else:
254-
commit_type = "chore" # Default to 'chore' if no type is found
255-
logger.debug("No commit type detected. Defaulting to 'chore'.")
256-
sys.exit(1)
257-
258-
# Add the icon to the existing commit message
259-
updated_commit_msg = add_icon_to_commit_message(commit_type, commit_msg)
260-
261-
# Write the updated commit message back to the file
262-
amend_commit(updated_commit_msg)
263-
264-
# Inform the user and abort the commit to allow them to review the amended message
265-
logger.info(
266-
"Commit message has been updated with an icon. Please review and finalize the commit."
267-
)
268-
sys.exit(1)
245+
# Determine the type of commit to get the appropriate icon
246+
type_match = COMMIT_TYPE_REGEX.match(commit_msg)
247+
if type_match:
248+
commit_type = type_match.group("type")
249+
logger.debug(f"Detected commit type: {commit_type}")
269250
else:
270-
logger.debug("Commit message contains square brackets. No icon added.")
271-
sys.exit(0) # Valid commit message without needing an icon; proceed
251+
commit_type = "chore" # Default to 'chore' if no type is found
252+
logger.debug("No commit type detected. Defaulting to 'chore'.")
253+
sys.exit(1)
254+
255+
# Add the icon to the existing commit message
256+
updated_commit_msg = add_icon_to_commit_message(commit_type, commit_msg)
257+
258+
# Write the updated commit message back to the file
259+
amend_commit(updated_commit_msg)
260+
261+
# Inform the user and abort the commit to allow them to review the amended message
262+
logger.info(
263+
"Commit message has been updated with an icon. Please review and finalize the commit."
264+
)
265+
sys.exit(1)
272266

273267

274268
if __name__ == "__main__":

0 commit comments

Comments
Ā (0)