Skip to content

Instantly share code, notes, and snippets.

@JonDotsoy
Last active January 19, 2023 15:45
Show Gist options
  • Save JonDotsoy/80f495333905a9b702fb681cd1a8faf2 to your computer and use it in GitHub Desktop.
Save JonDotsoy/80f495333905a9b702fb681cd1a8faf2 to your computer and use it in GitHub Desktop.
Conventional Commits to Terminal (zsh, sh)

Conventional Commits to Terminal (zsh, sh)

Git alias conventional-commit and m to create conventional commits so fast. And prepare the scope with the alias git scope <scope-name>.

image

How to install

Run the next line in your shell terminal

curl -L https://gist.github.com/JonDotsoy/80f495333905a9b702fb681cd1a8faf2/raw/install.sh | sh

Starship Integration

Add custom command to look the current scope setted.

image

Open the starship.toml file and add the next command:

[custom.git-commit-conventional_scope]
command = 'echo "$(git config --local -z --get git-commit-conventional.scope)$([[ $(git config --local --get git-commit-conventional.breaking-change) == "true" ]] && echo "!" )"'
when = 'git config --local -z --get git-commit-conventional.scope'
style = "bold green"
symbol = "❇️"
format = "[$symbol ($output )]($style)"
#!/usr/bin/env sh
function _git_commit_conventional_valid_type() {
TYPE=$1
if [[ $TYPE == "build" || $TYPE == "chore" || $TYPE == "ci" || $TYPE == "docs" || $TYPE == "feat" || $TYPE == "fix" || $TYPE == "perf" || $TYPE == "refactor" || $TYPE == "revert" || $TYPE == "style" || $TYPE == "test" ]]; then
return 0
else
return 1
fi
}
function _git_commit_conventional_reset() {
git config --local --remove-section git-commit-conventional
}
function _git_commit_conventional_get_commit() {
TYPE=$1
SCOPE=$(git config --local -z --get git-commit-conventional.scope)
BREAKING_CHANGE=$(git config --local -z --get git-commit-conventional.breaking-change)
BODY_FILE=$(git config --local -z --get git-commit-conventional.body-file-path)
_git_commit_conventional_valid_type $TYPE
if [[ $? != 0 ]]; then
if [[ -z $TYPE ]]; then
echo "type: <EMPTY> is not valid"
else
echo "type: $TYPE is not valid"
fi
exit 1
fi
HEAD="$TYPE"
if [[ ! -z $SCOPE ]]; then
HEAD="$TYPE($SCOPE)"
fi
if [[ $BREAKING_CHANGE == "true" ]]; then
HEAD="$HEAD!"
fi
HEAD="$HEAD: ${@:2}"
if [[ ! -z $BODY_FILE ]]; then
if [[ -f $BODY_FILE ]]; then
BODY=$(cat $BODY_FILE)
HEAD="$HEAD\n\n$BODY"
fi
fi
echo "$HEAD\c"
}
function _git_commit_conventional_set_breaking_change() {
breaking_change=$([[ -z $1 ]] && echo "false" || [[ $1 == "true" || $1 == "on" || $1 == "1" ]] && echo "true" || echo "false")
if [[ $breaking_change == "true" ]]; then
echo "Breaking change: true"
git config --local --replace-all --bool git-commit-conventional.breaking-change true
else
echo "Breaking change: false"
git config --local --unset git-commit-conventional.breaking-change
fi
}
function _git_commit_conventional_set_body_file() {
git config --local --path --replace-all git-commit-conventional.body-file-path $1
}
function _git_commit_conventional_set_scope() {
SCOPE=$1
# check scope is not empty
if [[ "" == "$SCOPE" ]]; then
git config --local --unset git-commit-conventional.scope
echo "Scope Removed"
return 1
fi
git config --local --replace-all git-commit-conventional.scope $SCOPE
echo "Scope Chaned: $SCOPE"
}
function _git_commit_conventional_commits() {
git commit -m "$(_git_commit_conventional_get_commit $@)"
}
function help() {
cat $HOME/.commit_conventional_commits/help.txt
}
function commit() {
if [[ $1 == "build" || $1 == "chore" || $1 == "ci" || $1 == "docs" || $1 == "feat" || $1 == "fix" || $1 == "perf" || $1 == "refactor" || $1 == "revert" || $1 == "style" || $1 == "test" ]]; then
_git_commit_conventional_commits "$1" "${@:2}"
return 0
elif [[ $1 == "scope" || $1 == "s" ]]; then
_git_commit_conventional_set_scope ${@:2}
return 0
elif [[ $1 == "breaking-change" || $1 == "bc" ]]; then
_git_commit_conventional_set_breaking_change ${@:2}
return 0
elif [[ $1 == "message" || $1 == "m" ]]; then
_git_commit_conventional_get_commit ${@:2}
return 0
elif [[ $1 == "reset" ]]; then
_git_commit_conventional_reset
return 0
elif [[ $1 == "body-file" ]]; then
_git_commit_conventional_set_body_file $2
return 0
elif [[ $1 == "help" ]]; then
help
return 0
else
echo "Type not found. Please use one of the following types: build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test."
echo ""
help
return 1
fi
}
commit $@
usage: git conventional-commit <type> <message>
git m <type> <message>
git scope <scope>
git conventional-commit help
git m help
git breaking-change <on|true|1|false|0|off>
git bc <on|true|1|false|0|off>
commands git:
scope Set the scope of the commit
conventional-commit, m Create a conventional commit, require type and message
breaking-change, bc Set the breaking change flag
commands git conventional-commit:
message Print the message to be committed
help Print this help
Type:
build Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
ci Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
docs Documentation only changes
feat A new feature
fix A bug fix
perf A code change that improves performance
refactor A code change that neither fixes a bug nor adds a feature
style Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
test Adding missing tests or correcting existing tests
chore Common used to make the version number or changelog
Type revert:
If the commit reverts a previous commit, it should begin with revert: , followed by the header of the reverted commit. In the body it should say: This reverts commit <hash>., where the hash is the SHA of the commit being reverted.
#!/usr/bin/env sh
PATH_COMMIT_BASE="$HOME/.commit_conventional_commits"
PATH_COMMIT_SCRIPT="$HOME/.commit_conventional_commits/commit.sh"
GIT_ALIAS_COMMAND='!sh'
GIT_ALIAS_COMMAND="$GIT_ALIAS_COMMAND $PATH_COMMIT_SCRIPT"
mkdir -p $PATH_COMMIT_BASE
curl -L -o $PATH_COMMIT_SCRIPT https://gist.github.com/JonDotsoy/80f495333905a9b702fb681cd1a8faf2/raw/commit.sh
git config --global --replace-all alias.conventional-commit "$GIT_ALIAS_COMMAND"
git config --global --replace-all alias.m "conventional-commit"
git config --global --replace-all alias.scope "conventional-commit scope"
git config --global --replace-all alias.bc "conventional-commit breaking-change"
git config --global --replace-all alias.breaking-change "conventional-commit breaking-change"
echo ""
echo "Success installed 🎉"
echo ""
echo "To use, just run:"
echo ""
echo " git scope <scope>"
echo ""
echo " git m <type> <message>"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment