Skip to content

Instantly share code, notes, and snippets.

@awmv
Last active September 13, 2023 21:48
Show Gist options
  • Save awmv/82aa4b8615a5e78d49818a28a5948c15 to your computer and use it in GitHub Desktop.
Save awmv/82aa4b8615a5e78d49818a28a5948c15 to your computer and use it in GitHub Desktop.
Semantic commit fuzzy finder with Jira support

The script will automatically extract the Jira ticket number from the branch name and incorporate it into the commit message in this format:

prefix: [JIRA-1337] I use arch btw

Installation on Mac OS

  1. Run brew install fzf
  2. Save ci.sh somewhere
  3. Add git alias e.g. ci = "!~/ci.sh"
  4. Runchmod +x ~/ci.sh

I don't want to think

Install

/bin/bash -c "$(curl -fsSL https://gist.githubusercontent.com/awmv/82aa4b8615a5e78d49818a28a5948c15/raw/855bfb546a31869e55dfced1fcb5c02d1bfb7833/install-ci.sh)"

Uninstall

/bin/bash -c "$(curl -fsSL https://gist.githubusercontent.com/awmv/82aa4b8615a5e78d49818a28a5948c15/raw/855bfb546a31869e55dfced1fcb5c02d1bfb7833/uninstall-ci.sh)"

Usage

git ci "super amazing commit"
git ci super amazing commit
git ci 

Why

¯_(ツ)_/¯

#!/bin/bash
capitalize_first_letter() {
echo "$1" | awk '{ $1=toupper(substr($1,1,1)) substr($1,2); print }'
}
commit_types=("feat" "refactor" "fix" "chore" "style" "test" "docs" "localize" "init")
type=$(printf '%s\n' "${commit_types[@]}" | fzf)
if [ $? -ne 0 ]; then
echo "Commit aborted."
exit 1
fi
current_branch=$(git branch --show-current)
ticket_number=$(echo "$current_branch" | grep -oE '([A-Z]+-[0-9]+)')
if [ -z "$*" ]; then
read -p "Enter commit message: " commit_message
else
commit_message="$*"
fi
if [ -z "$ticket_number" ]; then
commit_message="$type: $(capitalize_first_letter "$commit_message")"
else
commit_message="$type: [$ticket_number] $(capitalize_first_letter "$commit_message")"
fi
if [[ -n "$type" ]]; then
git commit -m "$commit_message"
else
echo "Invalid selection. Commit aborted."
fi
#!/bin/bash
if ! command -v fzf &> /dev/null; then
echo "fzf not found. Installing it with Homebrew..."
brew install fzf
fi
script_path="/usr/local/bin/ci.sh"
script_content='#!/bin/bash
capitalize_first_letter() {
echo "$1" | awk '\''{ $1=toupper(substr($1,1,1)) substr($1,2); print }'\''
}
commit_types=("feat" "refactor" "fix" "chore" "style" "test" "docs" "localize" "init")
type=$(printf '\''%s\n'\'' "${commit_types[@]}" | fzf)
if [ $? -ne 0 ]; then
echo "Commit aborted."
exit 1
fi
current_branch=$(git branch --show-current)
ticket_number=$(echo "$current_branch" | grep -oE '\''([A-Z]+-[0-9]+)'\'')
if [ -z "$*" ]; then
read -p "Enter commit message: " commit_message
else
commit_message="$*"
fi
if [ -z "$ticket_number" ]; then
commit_message="$type: $(capitalize_first_letter "$commit_message")"
else
commit_message="$type: [$ticket_number] $(capitalize_first_letter "$commit_message")"
fi
if [[ -n "$type" ]]; then
git commit -m "$commit_message"
else
echo "Invalid selection. Commit aborted."
fi
'
echo "$script_content" | sudo tee "$script_path" > /dev/null
sudo chmod +x "$script_path"
git config --global alias.ci "!$script_path"
echo "Script installed as $script_path"
echo "Git alias 'ci' configured to use the script."
#!/bin/bash
if [ -f "/usr/local/bin/ci.sh" ]; then
rm "/usr/local/bin/ci.sh"
echo "Script '/usr/local/bin/ci.sh' has been removed."
else
echo "Script '/usr/local/bin/ci.sh' not found."
fi
if git config --global --get-regexp alias.ci >/dev/null 2>&1; then
git config --global --unset alias.ci
echo "Git alias 'ci' has been unset."
else
echo "Git alias 'ci' not found."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment