-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathprepare-commit-msg.sh
executable file
·61 lines (50 loc) · 2.02 KB
/
prepare-commit-msg.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
# The first argument is the path to the commit message file.
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
# Skip the hook if this is a rebase or merge commit
if [ "$COMMIT_SOURCE" = "rebase" ] || [ -d "$(git rev-parse --git-dir)/rebase-merge" ] || [ -d "$(git rev-parse --git-dir)/rebase-apply" ]; then
exit 0
fi
COMMIT_MSG=$(cat $COMMIT_MSG_FILE)
RESULT=$COMMIT_MSG
# Get the directory of the script, resolving symlinks
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
EXTRA_ARGS=""
# Check if commit message ends with *
if [[ $COMMIT_MSG == *\* ]]; then
echo "The commit message ends with '*' Rating will be added."
COMMIT_MSG="${COMMIT_MSG%\*}"
EXTRA_ARGS="-a -r"
fi
# Check if commit message ends with ~~~ / ~~ / ~
if [[ $COMMIT_MSG == *~~~ ]]; then
echo "The commit message ends with '~~~' It will be replaced by AI. Emoji will be added."
COMMIT_MSG="${COMMIT_MSG%~~~}"
RESULT=$(git diff --cached | "$SCRIPT_DIR/gpt.sh" -d -g -e $EXTRA_ARGS -m "$COMMIT_MSG")
elif [[ $COMMIT_MSG == *~~ ]]; then
echo "The commit message ends with '~~' It will be replaced by AI. Emoji will not be added."
COMMIT_MSG="${COMMIT_MSG%~~}"
RESULT=$(git diff --cached | "$SCRIPT_DIR/gpt.sh" -d -g $EXTRA_ARGS -m "$COMMIT_MSG")
elif [[ $COMMIT_MSG == *~ ]]; then
echo "The commit message ends with '~'. Only Emoji will be added by AI."
COMMIT_MSG="${COMMIT_MSG%\~}"
RESULT=$(git diff --cached | "$SCRIPT_DIR/gpt.sh" -d -e $EXTRA_ARGS -m "$COMMIT_MSG")
else
if [ -z "$EXTRA_ARGS" ]; then
echo "The commit message does not end with '~', '~~', or '~~~'. Nothing to do."
else
echo "The commit message does not end with '*' Only rating will be added."
RESULT=$(git diff --cached | "$SCRIPT_DIR/gpt.sh" -d $EXTRA_ARGS -m "$COMMIT_MSG")
fi
fi
# Check if the previous command was successful
if [ $? -ne 0 ]; then
echo "Error: Failed to run gpt.sh"
echo "$RESULT"
exit 1
fi
echo "$RESULT"
# Overwrite the commit message file with the result
echo -e "${RESULT}" > $COMMIT_MSG_FILE
exit 0