Skip to content

Commit 62a7db9

Browse files
committed
Add Zsh completion
Zsh completion is available in `completions/_git-elegant` file. It's configured to complete `git-elegant` command and `git` command (as the Git aliases need to have completion also). The installation script displays instructions for installation both Bash and Zsh completion as well as some recommendations. Also, Getting Started document has a significant update: - it starts from the installation and ends post-steps - the instructions are shorter now - completion recommendations are moved to the installation script #230
1 parent a6d7f6f commit 62a7db9

File tree

3 files changed

+173
-32
lines changed

3 files changed

+173
-32
lines changed

completions/_git-elegant

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#compdef git-elegant git
2+
#description enables Elegant Git completion
3+
#
4+
# It's recommended to use this completion with the following Git completion file:
5+
# https://raw.githubusercontent.com/zsh-users/zsh/master/Completion/Unix/Command/_git
6+
7+
_git-elegant (){
8+
# update this function if either new commands or options are added
9+
local curcontext='$curcontext' state line
10+
typeset -A opt_args
11+
_arguments -C ':command:->command' '*::option:->option'
12+
local all_commands=(
13+
'accept-work:applies a branch on top of \`master\` branch'
14+
'acquire-git:configures a Git installation'
15+
'acquire-repository:configures current repository'
16+
'amend-work:amends some changes to the most recent commit'
17+
'clone-repository:clones a repository and configures it'
18+
'deliver-work:publishes current branch to a remote repository'
19+
'init-repository:initializes a new repository and configures it'
20+
'obtain-work:checkouts a remote branch matching by a name'
21+
'polish-work:reapplies branch commits interactively'
22+
'prune-repository:removes useless local branches'
23+
'release-work:releases available work as a new annotated tag'
24+
'save-work:commits current modifications'
25+
'show-commands:prints available Elegant Git commands'
26+
'show-release-notes:prints a release log between two references'
27+
'show-work:shows a state of current work in progress'
28+
'start-work:creates a new branch'
29+
)
30+
31+
case ${state} in
32+
command)
33+
local options=(
34+
'--help:displays help'
35+
'--version:displays program version'
36+
'--no-workflows:disables available workflows'
37+
)
38+
_describe 'first' all_commands -- options
39+
;;
40+
option)
41+
if [[ ${line[1]} == --no-workflows ]]; then
42+
__ge_complete_commands_workflow
43+
else
44+
__ge_complete_commands
45+
fi
46+
;;
47+
esac
48+
}
49+
50+
__ge_complete_commands_workflow() {
51+
_arguments ':command:->command' '*::options:->options'
52+
case ${state} in
53+
command) _describe 'only commands' all_commands ;;
54+
options) __ge_complete_commands ;;
55+
esac
56+
}
57+
58+
__ge_complete_commands () {
59+
# update this function if a new command requires a competion
60+
# default completion is empty
61+
case ${line[1]} in
62+
accept-work|obtain-work) __ge_remotes ;;
63+
show-release-notes) __ge_show_release_notes ;;
64+
*) _arguments '--help' '--no-workflows' ;;
65+
esac
66+
}
67+
68+
__ge_remotes() {
69+
# completes first position with remote branches
70+
local remotes=(
71+
$(git for-each-ref --format='%(refname:strip=2)' refs/remotes 2>/dev/null || echo )
72+
)
73+
_arguments '--help' \
74+
'--no-workflows' \
75+
'1:branch:(${remotes[@]})'
76+
}
77+
78+
__ge_show_release_notes_modes(){
79+
local modes=(
80+
'simple:prints the messages as a plain text (default one)'
81+
'smart:prints the messages in a form of adopted for a git hosting'
82+
)
83+
_describe 'modes' modes
84+
}
85+
86+
__ge_show_release_notes() {
87+
local all=(
88+
$(git for-each-ref --format '%(refname:short)' refs 2>/dev/null || echo )
89+
)
90+
_arguments '--help' \
91+
'--no-workflows' \
92+
'1:mode:__ge_show_release_notes_modes' \
93+
'2:from:(${all[@]})' \
94+
'3:to:(${all[@]})'
95+
}

docs/getting-started.md

+30-23
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
1-
# Several important words
2-
You can install Elegant Git by either executing `bash` script or using `homebrew`. After the
3-
installation, please configure your environment by running
4-
[`git elegant acquire-git`](commands.md#acquire-git) and follow the instructions. To find out more,
5-
please read [the configuration approach](configuration.md).
1+
# Installation via Bash script
2+
Run the follwing command and follow the instructions
3+
```bash
4+
curl https://raw.githubusercontent.com/bees-hive/elegant-git/master/install.bash | $(which bash)
5+
```
6+
7+
Elegant Git will be installed in `~/.elegant-git` directory. That's why if you want to remove
8+
the installation, just remove the directory.
9+
10+
# Installation via Homebrew
11+
On macOS, you can install [Homebrew](https://brew.sh/) if you haven't already, then run
12+
```
13+
brew install git bees-hive/hive/elegant-git
14+
```
15+
The command will
16+
17+
- install Git as it has to be installed with Homebrew for enabling Bash/Zsh completion
18+
- install Elegant Git
19+
- configure Bash completion for both Git and Elegant Git
20+
21+
P.S. If you need Zsh completion for all Git commands, consider using
22+
<https://raw.githubusercontent.com/zsh-users/zsh/master/Completion/Unix/Command/_git>
23+
(see <https://github.com/bees-hive/elegant-git/blob/master/install.bash> for the details).
24+
25+
# Post-installation actions
26+
Configure your environment by running [`git elegant acquire-git`](commands.md#acquire-git)
27+
and follow the instructions. To find out more, please read
28+
[the configuration approach](configuration.md).
629

730
You can access Elegant Git in CLI using any of
831
```bash
932
git <command>
1033
git elegant <command>
1134
git-elegant <command>
1235
```
13-
where `<command>` is one of the available commands which are described on [commands](commands.md)
14-
page or are printed in a terminal by running `git elegant`.
36+
where `<command>` is one of the commands described on the [commands](commands.md) page or
37+
printed in a terminal after running `git elegant`.
1538

1639
Also, please use [`git elegant clone-repository`](commands.md#clone-repository) or
1740
[`git elegant init-repository`](commands.md#init-repository) instead of regular `clone` or `init`
1841
Git's commands in order to get Elegant Git working by default.
19-
20-
# `bash` installation
21-
Run `curl https://raw.githubusercontent.com/bees-hive/elegant-git/master/install.bash | $(which bash)`
22-
and follow the provided instructions to install the tool. `${HOME}/.elegant-git` directory will host
23-
all required files. That's why if you want to remove installation, you need to remove this directory
24-
only (`rm -r ${HOME}/.elegant-git`).
25-
26-
Elegant Git's BASH completion does not work without regular Git BASH completion. If you don't have
27-
it, please follow <https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion>
28-
in addition to Elegant Git's configuration.
29-
30-
# Homebrew installation
31-
On macOS, you can install [Homebrew](https://brew.sh/) if you haven't already, then run
32-
`brew install git` (we must install Git with Homebrew in order to have a working BASH completion)
33-
and `brew install bees-hive/hive/elegant-git` (please visit
34-
<https://github.com/bees-hive/homebrew-hive> if you need details).

install.bash

+48-9
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ copy(){
2424
update-path() {
2525
local INSTALL_PATH=${1}
2626
cat <<TEXT
27-
You need to add Elegant Git to the PATH by adding to
28-
a "~/.bashrc" or "~/.bash_profile" file:
27+
You need to add Elegant Git to the PATH by adding the folowing line
28+
to relevant shell configuration file ('~/.bashrc', '~/.bash_profile',
29+
'~/.zshrc', etc.):
2930
3031
export PATH=${INSTALL_PATH}/bin:\$PATH
3132
@@ -38,12 +39,52 @@ TEXT
3839

3940
update-completion() {
4041
local INSTALL_PATH=${1}
41-
local COMPLETION_FILE="${INSTALL_PATH}/completions/git-elegant.bash"
42+
local BASH_COMPLETION="${INSTALL_PATH}/completions/git-elegant.bash"
43+
local ZSH_COMPLETION="${INSTALL_PATH}/completions/_git-elegant"
4244
cat <<TEXT
43-
You need to add loading of BASH completion file to
44-
a "~/.bashrc" or "~/.bash_profile" file:
4545
46-
[ -f ${COMPLETION_FILE} ] && . ${COMPLETION_FILE}
46+
Completion installation
47+
=======================
48+
49+
Bash competion
50+
--------------
51+
You need to load a Bash completion file from a relevant shell
52+
configuration file ('~/.bashrc', '~/.bash_profile', etc.) by
53+
adding the following line:
54+
55+
[ -f ${BASH_COMPLETION} ] && source ${BASH_COMPLETION}
56+
57+
Elegant Git's Bash completion does not work without regular Git
58+
completion. If you don't have it, please install it following
59+
https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion.
60+
61+
Zsh completion
62+
--------------
63+
1. link completion file into a completion directory
64+
65+
mkdir -p ~/.zsh/completion
66+
ln -s ${ZSH_COMPLETION} ~/.zsh/completion/
67+
68+
2. include the completion directory in your '\${fpath}'
69+
70+
fpath=(~/.zsh/completion \${fpath})
71+
72+
3. make sure compinit is loaded or do it by adding in '~/.zshrc:'
73+
74+
autoload -Uz compinit && compinit -i
75+
76+
4. (optional) consider using
77+
https://raw.githubusercontent.com/zsh-users/zsh/master/Completion/Unix/Command/_git
78+
as Git completion file that provides great completions for Git commands and plugins
79+
Elegant Git's completion file.
80+
81+
curl -L https://raw.githubusercontent.com/zsh-users/zsh/master/Completion/Unix/Command/_git \\
82+
> ~/.zsh/completion/_git
83+
84+
85+
P.S.
86+
----
87+
Please restart terminal session in order to activate completion.
4788
4889
TEXT
4990
}
@@ -78,9 +119,7 @@ main() {
78119
else
79120
update-path ${INSTALL_PATH}
80121
fi
81-
if ! complete -p git-elegant 1>/dev/null 2>&1; then
82-
update-completion ${INSTALL_PATH}
83-
fi
122+
update-completion ${INSTALL_PATH}
84123
}
85124

86125
main $@

0 commit comments

Comments
 (0)