Last active
November 5, 2023 23:13
-
-
Save Rubenmp/e73df2a36835e3d30b2295377c6058ab to your computer and use it in GitHub Desktop.
Script to git add all the folder changes, then commit them adding the task id (parsed from the branch name) prefix to the commit message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# This script will perform git add and commit all the changes in the current directory. | |
# It includes a "[task_key-<id>] " prefix to the commit message when | |
# the current branch name is like: | |
# "<branch_type>/task_key-<id><optional_extra_branch_name>". | |
# If the current branch name does not fit the previous pattern no prefix will be added. | |
# | |
# Usage: | |
# > commit "<commit_message>" | |
# | |
# Example: | |
# - For git branch "feature/JIRAKEY-324" this script can be executed like | |
# > commit "New feature" | |
# | |
# and it will perform the following commands | |
# > git add . | |
# > git commit -m "[JIRAKEY-324] New feature" | |
# | |
if [ $# -ne 1 ]; then | |
echo "Usage:" | |
echo " > commit \"<commit_message>\"" | |
return 1 | |
fi | |
git_add_and_commit() { | |
git add . | |
git commit -m "$1" | |
} | |
branch_name=$(git branch --show-current) | |
index=0 | |
task="" | |
for value in $(echo "$branch_name" | tr "/" "\n"); do | |
if [ $index -eq 1 ]; then | |
branch_without_type=$value | |
fi | |
index=$((index+1)) | |
done | |
index=0 | |
task="" | |
for jira_number in $(echo "$branch_without_type" | tr "-" "\n"); do | |
if [ $index -eq 0 ]; then | |
task="${task}$jira_number" | |
elif [ $index -eq 1 ]; then | |
task="${task}-" | |
if ! echo "$jira_number" | grep -Eq '^[0-9]+' ; then | |
break; | |
fi | |
task="${task}${jira_number}" | |
fi | |
index=$((index+1)) | |
done | |
if [ "$index" -eq 1 ]; then | |
task="" | |
fi | |
if [ -n "${task}" ]; then | |
task="[${task}] " | |
fi | |
git_add_and_commit "${task}$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment