|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 | set -euo pipefail
|
3 | 3 |
|
4 |
| -if [ -z "${1:-}" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then |
| 4 | +# Function to display help message |
| 5 | +show_help() { |
5 | 6 | echo -e "Create a commit prefixed with the current branch name.\n"
|
6 | 7 | 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" |
10 | 15 | 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}") |
11 | 37 | fi
|
12 | 38 |
|
13 | 39 | CURRENT_BRANCH="$(git symbolic-ref --short HEAD)"
|
14 | 40 | GIT_ROOT_DIRECTORY=$(git rev-parse --show-toplevel)
|
15 | 41 | IGNORED_BRANCHES=("dev" "master" "main" "qa" "uat" "staging")
|
16 | 42 | CUSTOM_IGNORED_PATH="$GIT_ROOT_DIRECTORY/.smart-commit-ignore"
|
17 | 43 |
|
| 44 | +# Add custom ignored branches if .smart-commit-ignore file exists |
18 | 45 | if [ -f "$CUSTOM_IGNORED_PATH" ]; then
|
19 | 46 | CUSTOM_BRANCHES=$(cat "$CUSTOM_IGNORED_PATH")
|
20 | 47 | BRANCHES=($CUSTOM_BRANCHES)
|
21 | 48 | IGNORED_BRANCHES=(${IGNORED_BRANCHES[@]} ${BRANCHES[@]})
|
22 | 49 | fi
|
23 | 50 |
|
| 51 | +# Check if the current branch is in the ignored branches list |
24 | 52 | IS_IGNORED=false
|
25 |
| - |
26 | 53 | for branch in "${IGNORED_BRANCHES[@]}"; do
|
27 |
| - if [ "$CURRENT_BRANCH" == $branch ]; then |
| 54 | + if [ "$CURRENT_BRANCH" == "$branch" ]; then |
28 | 55 | IS_IGNORED=true
|
29 | 56 | break
|
30 |
| - fi |
| 57 | + fi |
31 | 58 | done
|
32 | 59 |
|
| 60 | +# Build the commit command dynamically to avoid empty strings |
33 | 61 | 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 |
36 | 67 | 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 |
38 | 73 | fi
|
0 commit comments