Skip to content

Ignore -m option when used #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions commit
Original file line number Diff line number Diff line change
@@ -1,38 +1,73 @@
#!/usr/bin/env bash
set -euo pipefail

if [ -z "${1:-}" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
# 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\n"
echo "Example:"
echo " commit \"Hello world!\""
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
if [ "$CURRENT_BRANCH" == "$branch" ]; then
IS_IGNORED=true
break
fi
fi
done

# Build the commit command dynamically to avoid empty strings
if [ "$IS_IGNORED" == false ]; then
# Edit your config here
git commit -m "$CURRENT_BRANCH: $1" ${@:2}
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
git commit -m "$1" ${@:2}
if [ "${#EXTRA_ARGS[@]}" -eq 0 ]; then
git commit -m "$COMMIT_MESSAGE"
else
git commit -m "$COMMIT_MESSAGE" "${EXTRA_ARGS[@]}"
fi
fi