-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcommit
More file actions
executable file
·73 lines (65 loc) · 2.29 KB
/
commit
File metadata and controls
executable file
·73 lines (65 loc) · 2.29 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env bash
set -euo pipefail
# Function to display help message
show_help() {
echo -e "Create a commit prefixed with the current branch name.\n"
echo "Usage:"
echo -e " commit MESSAGE [options]\n"
echo -e " commit -m MESSAGE [options]\n"
echo "Examples:"
echo " commit \"Add new feature\" # Commit with branch-prefixed message"
echo " commit -m \"Fix bug\" # Commit with branch-prefixed message using -m"
echo " commit \"Refactor code\" --no-verify # Commit with branch-prefixed message, skip verification"
echo " commit -m \"Update docs\" --no-verify # Commit with branch-prefixed message using -m, skip verification"
exit 1
}
# Check for help flag
if [ -z "${1:-}" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
fi
# Initialize EXTRA_ARGS as an empty array
EXTRA_ARGS=()
# Determine if -m is used, and capture the message accordingly
if [ "$1" = "-m" ]; then
if [ -z "${2:-}" ]; then
echo "Error: Commit message cannot be empty."
exit 1
fi
COMMIT_MESSAGE="$2"
EXTRA_ARGS=("${@:3}")
else
COMMIT_MESSAGE="$1"
EXTRA_ARGS=("${@:2}")
fi
CURRENT_BRANCH="$(git symbolic-ref --short HEAD)"
GIT_ROOT_DIRECTORY=$(git rev-parse --show-toplevel)
IGNORED_BRANCHES=("dev" "master" "main" "qa" "uat" "staging")
CUSTOM_IGNORED_PATH="$GIT_ROOT_DIRECTORY/.smart-commit-ignore"
# Add custom ignored branches if .smart-commit-ignore file exists
if [ -f "$CUSTOM_IGNORED_PATH" ]; then
CUSTOM_BRANCHES=$(cat "$CUSTOM_IGNORED_PATH")
BRANCHES=($CUSTOM_BRANCHES)
IGNORED_BRANCHES=(${IGNORED_BRANCHES[@]} ${BRANCHES[@]})
fi
# Check if the current branch is in the ignored branches list
IS_IGNORED=false
for branch in "${IGNORED_BRANCHES[@]}"; do
if [ "$CURRENT_BRANCH" == "$branch" ]; then
IS_IGNORED=true
break
fi
done
# Build the commit command dynamically to avoid empty strings
if [ "$IS_IGNORED" == false ]; then
if [ "${#EXTRA_ARGS[@]}" -eq 0 ]; then
git commit -m "$CURRENT_BRANCH: $COMMIT_MESSAGE"
else
git commit -m "$CURRENT_BRANCH: $COMMIT_MESSAGE" "${EXTRA_ARGS[@]}"
fi
else
if [ "${#EXTRA_ARGS[@]}" -eq 0 ]; then
git commit -m "$COMMIT_MESSAGE"
else
git commit -m "$COMMIT_MESSAGE" "${EXTRA_ARGS[@]}"
fi
fi