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