Skip to content

Commit 70da054

Browse files
committed
refactor: streamline prepare-commit-msg hook 💡
- Simplify script by removing redundant checks - Add support for different commit message types - Improve error handling and script efficiency
1 parent 42b9a4e commit 70da054

File tree

1 file changed

+1
-40
lines changed

1 file changed

+1
-40
lines changed

hooks/prepare-commit-msg

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,9 @@
44
# It is triggered before the commit message editor is opened.
55
# Usage: Place this script in the `.git/hooks/` directory of your repository and make it executable.
66
COMMIT_MSG_FILE=$1
7-
<<<<<<< HEAD
8-
REPO_ROOT=$(git rev-parse --show-toplevel)
9-
10-
if [ -f $COMMIT_MSG_FILE ]; then
11-
# If the commit message file already exists, exit the script
12-
MESSAGE=$(cat $COMMIT_MSG_FILE)
13-
if [ -n "$MESSAGE" ]; then
14-
echo "Commit message already exists. Exiting."
15-
exit 0
16-
fi
17-
fi
18-
19-
=======
207
COMMIT_MSG_TYPE=$2
218
REPO_ROOT=$(git rev-parse --show-toplevel)
229

23-
>>>>>>> main
2410
# Check the repository root directory is valid
2511
# The `git rev-parse --show-toplevel` command returns the absolute path to the root of the repository
2612
# If the command fails, it means we are not in a Git repository
@@ -29,36 +15,12 @@ if [ ! -d "$REPO_ROOT" ]; then
2915
exit 1
3016
fi
3117

32-
<<<<<<< HEAD
33-
# Get only the diff of what has already been staged
34-
GIT_DIFF_OUTPUT=$(git diff --cached)
35-
36-
# Check if there are any staged changes to commit
37-
if [ -z "$GIT_DIFF_OUTPUT" ]; then
38-
echo "No staged changes detected. Aborting."
39-
exit 1
40-
fi
41-
42-
# Check if aigitcommit is installed
43-
if ! command -v aigitcommit &> /dev/null; then
44-
=======
4518
# Check if aigitcommit is installed
4619
if ! type aigitcommit >/dev/null 2>&1; then
47-
>>>>>>> main
4820
echo "Error: aigitcommit is not installed. Please install it first."
49-
exit 1
21+
exit 0
5022
fi
5123

52-
<<<<<<< HEAD
53-
# Execute aigitcommit to generate the commit message
54-
# Redirecting output to the commit message file
55-
# and suppressing any error messages
56-
# The --save option is used to save the generated commit message to the file
57-
aigitcommit $REPO_ROOT --save $COMMIT_MSG_FILE > /dev/null 2>&1
58-
if [ $? -ne 0 ]; then
59-
echo "Error: aigitcommit failed to generate commit message."
60-
exit 1
61-
=======
6224
# Only proceed if this is a regular commit (message type is empty or "message")
6325
if [ "$COMMIT_MSG_TYPE" = "message" ] || [ -z "$COMMIT_MSG_TYPE" ]; then
6426
# Get only the diff of what has already been staged
@@ -91,5 +53,4 @@ if [ "$COMMIT_MSG_TYPE" = "message" ] || [ -z "$COMMIT_MSG_TYPE" ]; then
9153
else
9254
# For other commit types (merge, template, etc.), exit without doing anything
9355
exit 0
94-
>>>>>>> main
9556
fi

0 commit comments

Comments
 (0)