Skip to content

Commit f7feea4

Browse files
committed
chore: ignore -m if used.
1 parent 5b40f7c commit f7feea4

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

commit

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,73 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
if [ -z "${1:-}" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
4+
# Function to display help message
5+
show_help() {
56
echo -e "Create a commit prefixed with the current branch name.\n"
67
echo "Usage:"
7-
echo -e " commit MESSAGE\n"
8-
echo "Example:"
9-
echo " commit \"Hello world!\""
8+
echo -e " commit MESSAGE [options]\n"
9+
echo -e " commit -m MESSAGE [options]\n"
10+
echo "Examples:"
11+
echo " commit \"Add new feature\" # Commit with branch-prefixed message"
12+
echo " commit -m \"Fix bug\" # Commit with branch-prefixed message using -m"
13+
echo " commit \"Refactor code\" --no-verify # Commit with branch-prefixed message, skip verification"
14+
echo " commit -m \"Update docs\" --no-verify # Commit with branch-prefixed message using -m, skip verification"
1015
exit 1
16+
}
17+
18+
# Check for help flag
19+
if [ -z "${1:-}" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
20+
show_help
21+
fi
22+
23+
# Initialize EXTRA_ARGS as an empty array
24+
EXTRA_ARGS=()
25+
26+
# Determine if -m is used, and capture the message accordingly
27+
if [ "$1" = "-m" ]; then
28+
if [ -z "${2:-}" ]; then
29+
echo "Error: Commit message cannot be empty."
30+
exit 1
31+
fi
32+
COMMIT_MESSAGE="$2"
33+
EXTRA_ARGS=("${@:3}")
34+
else
35+
COMMIT_MESSAGE="$1"
36+
EXTRA_ARGS=("${@:2}")
1137
fi
1238

1339
CURRENT_BRANCH="$(git symbolic-ref --short HEAD)"
1440
GIT_ROOT_DIRECTORY=$(git rev-parse --show-toplevel)
1541
IGNORED_BRANCHES=("dev" "master" "main" "qa" "uat" "staging")
1642
CUSTOM_IGNORED_PATH="$GIT_ROOT_DIRECTORY/.smart-commit-ignore"
1743

44+
# Add custom ignored branches if .smart-commit-ignore file exists
1845
if [ -f "$CUSTOM_IGNORED_PATH" ]; then
1946
CUSTOM_BRANCHES=$(cat "$CUSTOM_IGNORED_PATH")
2047
BRANCHES=($CUSTOM_BRANCHES)
2148
IGNORED_BRANCHES=(${IGNORED_BRANCHES[@]} ${BRANCHES[@]})
2249
fi
2350

51+
# Check if the current branch is in the ignored branches list
2452
IS_IGNORED=false
25-
2653
for branch in "${IGNORED_BRANCHES[@]}"; do
27-
if [ "$CURRENT_BRANCH" == $branch ]; then
54+
if [ "$CURRENT_BRANCH" == "$branch" ]; then
2855
IS_IGNORED=true
2956
break
30-
fi
57+
fi
3158
done
3259

60+
# Build the commit command dynamically to avoid empty strings
3361
if [ "$IS_IGNORED" == false ]; then
34-
# Edit your config here
35-
git commit -m "$CURRENT_BRANCH: $1" ${@:2}
62+
if [ "${#EXTRA_ARGS[@]}" -eq 0 ]; then
63+
git commit -m "$CURRENT_BRANCH: $COMMIT_MESSAGE"
64+
else
65+
git commit -m "$CURRENT_BRANCH: $COMMIT_MESSAGE" "${EXTRA_ARGS[@]}"
66+
fi
3667
else
37-
git commit -m "$1" ${@:2}
68+
if [ "${#EXTRA_ARGS[@]}" -eq 0 ]; then
69+
git commit -m "$COMMIT_MESSAGE"
70+
else
71+
git commit -m "$COMMIT_MESSAGE" "${EXTRA_ARGS[@]}"
72+
fi
3873
fi

0 commit comments

Comments
 (0)