feat(git-commit): use git-dash commands (#12628)

Co-authored-by: Carlo Sala <carlosalag@protonmail.com>
This commit is contained in:
mattmc3
2026-10-20 11:44:08 +02:00
committed by GitHub
co-authored by Carlo Sala
parent f888be4bec
commit 0eeaa529bb
15 changed files with 97 additions and 63 deletions
+5 -8
View File
@@ -1,7 +1,6 @@
# git-commit plugin # git-commit plugin
The git-commit plugin adds several The git-commit plugin adds several git subcommands for
[git aliases](https://www.git-scm.com/docs/git-config#Documentation/git-config.txt-alias) for
[conventional commit](https://www.conventionalcommits.org/en/v1.0.0/#summary) messages. [conventional commit](https://www.conventionalcommits.org/en/v1.0.0/#summary) messages.
To use it, add `git-commit` to the plugins array in your zshrc file: To use it, add `git-commit` to the plugins array in your zshrc file:
@@ -31,15 +30,13 @@ Where `type` is one of the following:
- `test` - `test`
- `wip` - `wip`
> NOTE: the alias for `revert` type is `rev`, as otherwise it conflicts with the git command of the same name. > NOTE: the subcommand for `revert` type is `rev`, as otherwise it conflicts with the
> It will still generate a commit message in the format `revert: <message>` > git command of the same name. It will still generate a commit message in the format
> `revert: <message>`
> ⚠️ Enabling this plugin will (potentially) overwrite all `alias.<type>` that you manually set. Use with
> care!
## Examples ## Examples
| Git alias | Command | | Git subcommand | Command |
| --------------------------------------------- | ---------------------------------------------------- | | --------------------------------------------- | ---------------------------------------------------- |
| `git style "remove trailing whitespace"` | `git commit -m "style: remove trailing whitespace"` | | `git style "remove trailing whitespace"` | `git commit -m "style: remove trailing whitespace"` |
| `git wip "work in progress"` | `git commit -m "work in progress"` | | `git wip "work in progress"` | `git commit -m "work in progress"` |
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+1
View File
@@ -0,0 +1 @@
./omz_git_commit
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env zsh
_omz_git_commit() {
0=${(%):-%x}
if [[ "$0" != */git-* ]]; then
echo >&2 "Expecting command named git-foo. Got '$0'."
return 1
fi
local _type="${0##*/git-}"
# The subcommand is shortened to avoid Git's built-in revert command.
if [[ "$_type" == rev ]]; then
_type=revert
fi
local _scope _attention _message
while [ $# -ne 0 ]; do
case $1 in
-s | --scope )
if [ -z $2 ]; then
echo >&2 "Missing scope!"
return 1
fi
_scope="$2"
shift 2
;;
-a | --attention )
_attention="!"
shift 1
;;
* )
_message="${_message} $1"
shift 1
;;
esac
done
git commit -m "${_type}${_scope:+(${_scope})}${_attention}:${_message}"
}
_omz_git_commit "$@"
+42 -55
View File
@@ -1,58 +1,45 @@
local _rev="$(git -C $ZSH rev-parse HEAD 2> /dev/null)" # Add git-commit commands directory to path
if [[ $_rev == $(git config --global --get oh-my-zsh.git-commit-alias 2> /dev/null) ]]; then 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
return 0="${${(M)0:#/*}:-$PWD/$0}"
fi path=("${0:a:h}/git-commands" $path)
git config --global oh-my-zsh.git-commit-alias "$_rev"
local -a _git_commit_aliases # Append completions for custom git commands
_git_commit_aliases=( () {
'build' local -a git_user_commands
'chore' zstyle -a ':completion:*:*:git:*' user-commands 'git_user_commands' \
'ci' || git_user_commands=()
'docs' git_user_commands+=(
'feat' build:'Commit with a message indicating a build' \
'fix' chore:'Commit with a message indicating a chore' \
'perf' ci:'Commit with a message indicating a CI change' \
'refactor' docs:'Commit with a message indicating an update to the documentation' \
'revert' feat:'Commit with a message indicating a feature' \
'style' fix:'Commit with a message indicating a fix' \
'test' perf:'Commit with a message indicating a performance enhancement' \
'wip' refactor:'Commit with a message indicating a refactor' \
) rev:'Commit with a message indicating a revert' \
style:'Commit with a message indicating a style change' \
test:'Commit with a message indicating updates to tests' \
wip:'Commit with a message indicating a work in progress'
)
zstyle ':completion:*:*:git:*' user-commands "${git_user_commands[@]}"
}
local _alias _type # Clean up aliases from the prior implementation of git-commit. Keep this migration
for _type in "${_git_commit_aliases[@]}"; do # for users upgrading from versions that still wrote aliases to .gitconfig.
# an alias can't be named "revert" because the git command takes precedence () {
# https://stackoverflow.com/a/3538791 git config --global --get oh-my-zsh.git-commit-alias &> /dev/null || return 0
case "$_type" in local -a old_git_aliases=(
revert) _alias=rev ;; 'build' 'chore' 'ci'
*) _alias=$_type ;; 'docs' 'feat' 'fix'
esac 'perf' 'refactor' 'rev'
'style' 'test' 'wip'
local _func='!a() { )
local _scope _attention _message local git_alias
while [ $# -ne 0 ]; do for git_alias in $old_git_aliases; do
case $1 in if [[ "$(git config --global --get alias.$git_alias | tr '\n' ' ')" == "!a() { local _scope _attention _message"* ]]; then
-s | --scope ) git config --global --unset alias.$git_alias
if [ -z $2 ]; then
echo "Missing scope!"
return 1
fi fi
_scope="$2" done
shift 2 git config --global --unset oh-my-zsh.git-commit-alias
;; }
-a | --attention )
_attention="!"
shift 1
;;
* )
_message="${_message} $1"
shift 1
;;
esac
done
git commit -m "'$_type'${_scope:+(${_scope})}${_attention}:${_message}"
}; a'
git config --global alias.$_alias "$_func"
done