048455ccefdc67c4f137b8f7886769c55c2e5417

Author
Marc Cornellà <marc@mcornella.com>
Committer
Marc Cornellà <marc@mcornella.com>
Date

Message

fix(git-commit): fix revert alias conflict (#12007)

Fixes #12007

Diff

 1diff --git a/plugins/git-commit/README.md b/plugins/git-commit/README.md
 2index dc10d8ad5cebd5a8b3311afcb0ead3922e86ef7a..91cc73b44f8db33da1cf77859afaa97171d432e1 100644
 3--- a/plugins/git-commit/README.md
 4+++ b/plugins/git-commit/README.md
 5@@ -10,7 +10,7 @@ plugins=(... git-commit)
 6 
 7 ## Syntax
 8 
 9-```zshrc
10+```zsh
11 git <type> [(-s, --scope) "<scope>"] "<message>"
12 ```
13 
14@@ -26,11 +26,17 @@ Where `type` is one of the following:
15 - `fix`
16 - `perf`
17 - `refactor`
18-- `revert`
19+- `rev`
20 - `style`
21 - `test`
22 
23+> NOTE: the alias for `revert` type is `rev`, as otherwise it conflicts with the git command of the same name.  
24+> It will still generate a commit message in the format `revert: <message>`
25+
26 ## Examples
27 
28-`git style "remove trailing whitespace"` -> `git commit -m "style: remove trailing whitespace"`  
29-`git fix -s "router" "correct redirect link"` -> `git commit -m "fix(router): correct redirect link"`
30+| Git alias                                     | Command                                              |
31+| --------------------------------------------- | ---------------------------------------------------- |
32+| `git style "remove trailing whitespace"`      | `git commit -m "style: remove trailing whitespace"`  |
33+| `git fix -s "router" "correct redirect link"` | `git commit -m "fix(router): correct redirect link"` |
34+| `git rev -s "api" "rollback v2"`              | `git commit -m "revert(api): rollback v2"`           |
35diff --git a/plugins/git-commit/git-commit.plugin.zsh b/plugins/git-commit/git-commit.plugin.zsh
36index 4ad4c01770a808336b6d3b7c9f90b21c0c4fa768..72cecb1d62d30f3b7bb38594b34dff6bf742d814 100644
37--- a/plugins/git-commit/git-commit.plugin.zsh
38+++ b/plugins/git-commit/git-commit.plugin.zsh
39@@ -1,9 +1,3 @@
40-function _git_commit_register {
41-  if ! git config --global --get-all alias.$1 >/dev/null 2>&1; then
42-    git config --global alias.$1 '!a() { if [ "$1" = "-s" ] || [ "$1" = "--scope" ]; then local scope="$2"; shift 2; git commit -m "'$1'(${scope}): ${@}"; else git commit -m "'$1': ${@}"; fi }; a'
43-  fi
44-}
45-
46 local -a _git_commit_aliases
47 _git_commit_aliases=(
48   'build'
49@@ -19,9 +13,19 @@ _git_commit_aliases=(
50   'test'
51 )
52 
53-for _alias in "${_git_commit_aliases[@]}"; do
54-  _git_commit_register $_alias
55+local alias type
56+for type in "${_git_commit_aliases[@]}"; do
57+  # an alias can't be named "revert" because the git command takes precedence
58+  # https://stackoverflow.com/a/3538791
59+  case "$type" in
60+  revert) alias=rev ;;
61+  *) alias=$type ;;
62+  esac
63+
64+  local func='!a() { if [ "$1" = "-s" ] || [ "$1" = "--scope" ]; then local scope="$2"; shift 2; git commit -m "'$type'(${scope}): ${@}"; else git commit -m "'$type': ${@}"; fi }; a'
65+  if ! git config --global --get-all alias.${alias} >/dev/null 2>&1; then
66+    git config --global alias.${alias} "$func"
67+  fi
68 done
69 
70-unfunction _git_commit_register
71-unset _alias
72+unset _git_commit_aliases alias type func