git-extras.plugin.zsh
13383 bytes
1# ------------------------------------------------------------------------------
2# Description
3# -----------
4#
5# Completion script for git-extras (https://github.com/tj/git-extras).
6#
7# This depends on and reuses some of the internals of the _git completion
8# function that ships with zsh itself. It will not work with the _git that ships
9# with git.
10#
11# ------------------------------------------------------------------------------
12# Authors
13# -------
14#
15# * Alexis GRIMALDI (https://github.com/agrimaldi)
16# * spacewander (https://github.com/spacewander)
17#
18# ------------------------------------------------------------------------------
19# Inspirations
20# -----------
21#
22# * git-extras (https://github.com/tj/git-extras)
23# * git-flow-completion (https://github.com/bobthecow/git-flow-completion)
24#
25# ------------------------------------------------------------------------------
26
27
28# Internal functions
29# These are a lot like their __git_* equivalents inside _git
30
31__gitex_command_successful () {
32 if (( ${#*:#0} > 0 )); then
33 _message 'not a git repository'
34 return 1
35 fi
36 return 0
37}
38
39__gitex_commits() {
40 declare -A commits
41 git log --oneline -15 | sed 's/\([[:alnum:]]\{7\}\) /\1:/' | while read commit
42 do
43 hash=$(echo $commit | cut -d':' -f1)
44 commits[$hash]="$commit"
45 done
46 local ret=1
47 _describe -t commits commit commits && ret=0
48}
49
50__gitex_remote_names() {
51 local expl
52 declare -a remote_names
53 remote_names=(${(f)"$(_call_program remotes git remote 2>/dev/null)"})
54 __gitex_command_successful || return
55 _wanted remote-names expl remote-name compadd $* - $remote_names
56}
57
58__gitex_tag_names() {
59 local expl
60 declare -a tag_names
61 tag_names=(${${(f)"$(_call_program tags git for-each-ref --format='"%(refname)"' refs/tags 2>/dev/null)"}#refs/tags/})
62 __gitex_command_successful || return
63 _wanted tag-names expl tag-name compadd $* - $tag_names
64}
65
66
67__gitex_branch_names() {
68 local expl
69 declare -a branch_names
70 branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads 2>/dev/null)"}#refs/heads/})
71 __gitex_command_successful || return
72 _wanted branch-names expl branch-name compadd $* - $branch_names
73}
74
75__gitex_specific_branch_names() {
76 local expl
77 declare -a branch_names
78 branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads/"$1" 2>/dev/null)"}#refs/heads/$1/})
79 __gitex_command_successful || return
80 _wanted branch-names expl branch-name compadd - $branch_names
81}
82
83__gitex_feature_branch_names() {
84 __gitex_specific_branch_names 'feature'
85}
86
87__gitex_submodule_names() {
88 local expl
89 declare -a submodule_names
90 submodule_names=(${(f)"$(_call_program branchrefs git submodule status | awk '{print $2}')"}) # '
91 __gitex_command_successful || return
92 _wanted submodule-names expl submodule-name compadd $* - $submodule_names
93}
94
95
96__gitex_author_names() {
97 local expl
98 declare -a author_names
99 author_names=(${(f)"$(_call_program branchrefs git log --format='%aN' | sort -u)"})
100 __gitex_command_successful || return
101 _wanted author-names expl author-name compadd $* - $author_names
102}
103
104# subcommands
105# new subcommand should be added in alphabetical order
106_git-authors() {
107 _arguments -C \
108 '(--list -l)'{--list,-l}'[show authors]' \
109 '--no-email[without email]' \
110}
111
112_git-changelog() {
113 _arguments \
114 '(-l --list)'{-l,--list}'[list commits]' \
115}
116
117_git-clear() {
118 _arguments \
119 '(-f --force)'{-f,--force}'[force clear]' \
120 '(-h --help)'{-h,--help}'[help message]' \
121}
122
123_git-coauthor() {
124 _arguments \
125 ':co-author[co-author to add]' \
126 ':co-author-email[email address of co-author to add]'
127}
128
129_git-contrib() {
130 _arguments \
131 ':author:__gitex_author_names'
132}
133
134
135_git-count() {
136 _arguments \
137 '--all[detailed commit count]'
138}
139
140_git-create-branch() {
141 local curcontext=$curcontext state line
142 _arguments -C \
143 ': :->command' \
144 '*:: :->option-or-argument'
145
146 case "$state" in
147 (command)
148 _arguments \
149 '(--remote -r)'{--remote,-r}'[setup remote tracking branch]'
150 ;;
151 (option-or-argument)
152 curcontext=${curcontext%:*}-$line[1]:
153 case $line[1] in
154 -r|--remote )
155 _arguments -C \
156 ':remote-name:__gitex_remote_names'
157 ;;
158 esac
159 esac
160}
161
162_git-delete-branch() {
163 _arguments \
164 ':branch-name:__gitex_branch_names'
165}
166
167_git-delete-squashed-branches() {
168 _arguments \
169 ':branch-name:__gitex_branch_names'
170}
171
172
173_git-delete-submodule() {
174 _arguments \
175 ':submodule-name:__gitex_submodule_names'
176}
177
178
179_git-delete-tag() {
180 _arguments \
181 ':tag-name:__gitex_tag_names'
182}
183
184
185_git-effort() {
186 _arguments \
187 '--above[ignore file with less than x commits]'
188}
189
190
191_git-extras() {
192 local curcontext=$curcontext state line ret=1
193 declare -A opt_args
194
195 _arguments -C \
196 ': :->command' \
197 '*:: :->option-or-argument' && ret=0
198
199 case $state in
200 (command)
201 declare -a commands
202 commands=(
203 'update:update git-extras'
204 )
205 _describe -t commands command commands && ret=0
206 ;;
207 esac
208
209 _arguments \
210 '(-v --version)'{-v,--version}'[show current version]'
211}
212
213
214_git-feature() {
215 local curcontext=$curcontext state line ret=1
216 declare -A opt_args
217
218 _arguments -C \
219 ': :->command' \
220 '*:: :->option-or-argument' && ret=0
221
222 case $state in
223 (command)
224 declare -a commands
225 commands=(
226 'finish:merge feature into the current branch'
227 )
228 _describe -t commands command commands && ret=0
229 ;;
230 (option-or-argument)
231 curcontext=${curcontext%:*}-$line[1]:
232 case $line[1] in
233 (finish)
234 _arguments -C \
235 '--squash[Use squash merge]' \
236 ':branch-name:__gitex_feature_branch_names'
237 ;;
238 -r|--remote )
239 _arguments -C \
240 ':remote-name:__gitex_remote_names'
241 ;;
242 esac
243 return 0
244 esac
245
246 _arguments \
247 '(--remote -r)'{--remote,-r}'[setup remote tracking branch]'
248}
249
250_git-graft() {
251 _arguments \
252 ':src-branch-name:__gitex_branch_names' \
253 ':dest-branch-name:__gitex_branch_names'
254}
255
256_git-guilt() {
257 _arguments -C \
258 '(--email -e)'{--email,-e}'[display author emails instead of names]' \
259 '(--ignore-whitespace -w)'{--ignore-whitespace,-w}'[ignore whitespace only changes]' \
260 '(--debug -d)'{--debug,-d}'[output debug information]' \
261 '-h[output usage information]'
262}
263
264_git-ignore() {
265 _arguments -C \
266 '(--local -l)'{--local,-l}'[show local gitignore]' \
267 '(--global -g)'{--global,-g}'[show global gitignore]' \
268 '(--private -p)'{--private,-p}'[show repo gitignore]'
269}
270
271
272_git-info() {
273 _arguments -C \
274 '(--color -c)'{--color,-c}'[use color for information titles]' \
275 '--no-config[do not show list all variables set in config file, along with their values]'
276}
277
278
279_git-merge-into() {
280 _arguments '--ff-only[merge only fast-forward]'
281 _arguments \
282 ':src:__gitex_branch_names' \
283 ':dest:__gitex_branch_names'
284}
285
286_git-missing() {
287 _arguments \
288 ':first-branch-name:__gitex_branch_names' \
289 ':second-branch-name:__gitex_branch_names'
290}
291
292_git-release() {
293 _arguments -C \
294 '-c[Generates/populates the changelog with all commit message since the last tag.]' \
295 '-r[The "remote" repository that is destination of a push operation.]' \
296 '-m[use the custom commit information instead of the default message.]' \
297 '-s[Create a signed and annotated tag.]' \
298 '-u[Create a tag, annotated and signed with the given key.]' \
299 '--semver[If the latest tag in your repo matches the semver format requirement, you could increase part of it as the new release tag.]' \
300 '--prefix[Add a prefix string to semver to allow more complex tags.]' \
301 '--no-empty-commit[Avoid creating empty commit if nothing could be committed.]' \
302 '--[The arguments listed after "--" separator will be passed to pre/post-release hook.]'
303}
304
305_git-squash() {
306 _arguments '--squash-msg[commit with the squashed commit messages]'
307 _arguments \
308 ':branch-name:__gitex_branch_names'
309}
310
311_git-stamp() {
312 _arguments -C \
313 '(--replace -r)'{--replace,-r}'[replace stamps with same id]'
314}
315
316_git-standup() {
317 _arguments -C \
318 '-a[Specify the author of commits. Use "all" to specify all authors.]' \
319 '-d[Show history since N days ago]' \
320 '-D[Specify the date format displayed in commit history]' \
321 '-f[Fetch commits before showing history]' \
322 '-g[Display GPG signed info]' \
323 '-h[Display help message]' \
324 '-L[Enable the inclusion of symbolic links]' \
325 '-m[The depth of recursive directory search]' \
326 '-B[Display the commits in branch groups]'
327}
328
329_git-summary() {
330 _arguments '--line[summarize with lines rather than commits]'
331 _arguments '--dedup-by-email[remove duplicate users by the email address]'
332 _arguments '--no-merges[exclude merge commits]'
333 __gitex_commits
334}
335
336_git-undo(){
337 _arguments -C \
338 '(--soft -s)'{--soft,-s}'[only rolls back the commit but changes remain un-staged]' \
339 '(--hard -h)'{--hard,-h}'[wipes your commit(s)]'
340}
341
342zstyle -g existing_user_commands ':completion:*:*:git:*' user-commands
343
344zstyle ':completion:*:*:git:*' user-commands $existing_user_commands \
345 alias:'define, search and show aliases' \
346 abort:'abort current revert, merge, rebase, or cherry-pick process' \
347 archive-file:'export the current head of the git repository to an archive' \
348 authors:'generate authors report' \
349 browse:'open repo website in browser' \
350 browse-ci:'open repo CI page in browser' \
351 bug:'create bug branch' \
352 bulk:'run bulk commands' \
353 brv:'list branches sorted by their last commit date'\
354 changelog:'generate a changelog report' \
355 chore:'create chore branch' \
356 clear-soft:'soft clean up a repository' \
357 clear:'rigorously clean up a repository' \
358 coauthor:'add a co-author to the last commit' \
359 commits-since:'show commit logs since some date' \
360 contrib:'show user contributions' \
361 count:'show commit count' \
362 create-branch:'create branches' \
363 delete-branch:'delete branches' \
364 delete-merged-branches:'delete merged branches' \
365 delete-squashed-branches:'delete squashed branches' \
366 delete-submodule:'delete submodules' \
367 delete-tag:'delete tags' \
368 delta:'lists changed files' \
369 effort:'show effort statistics on file(s)' \
370 extras:'awesome git utilities' \
371 feature:'create/merge feature branch' \
372 force-clone:'overwrite local repositories with clone' \
373 fork:'fork a repo on GitHub' \
374 fresh-branch:'create fresh branches' \
375 gh-pages:'create the GitHub pages branch' \
376 graft:'merge and destroy a given branch' \
377 guilt:'calculate change between two revisions' \
378 ignore-io:'get sample gitignore file' \
379 ignore:'add .gitignore patterns' \
380 info:'returns information on current repository' \
381 local-commits:'list local commits' \
382 lock:'lock a file excluded from version control' \
383 locked:'ls files that have been locked' \
384 magic:'commits everything with a generated message' \
385 merge-into:'merge one branch into another' \
386 merge-repo:'merge two repo histories' \
387 missing:'show commits missing from another branch' \
388 mr:'checks out a merge request locally' \
389 obliterate:'rewrite past commits to remove some files' \
390 paste:'send patches to pastebin sites' \
391 pr:'checks out a pull request locally' \
392 psykorebase:'rebase a branch with a merge commit' \
393 pull-request:'create pull request to GitHub project' \
394 reauthor:'replace the author and/or committer identities in commits and tags' \
395 rebase-patch:'rebases a patch' \
396 refactor:'create refactor branch' \
397 release:'commit, tag and push changes to the repository' \
398 rename-branch:'rename a branch' \
399 rename-tag:'rename a tag' \
400 rename-remote:'rename a remote' \
401 repl:'git read-eval-print-loop' \
402 reset-file:'reset one file' \
403 root:'show path of root' \
404 scp:'copy files to ssh compatible `git-remote`' \
405 sed:'replace patterns in git-controlled files' \
406 setup:'set up a git repository' \
407 show-merged-branches:'show merged branches' \
408 show-tree:'show branch tree of commit history' \
409 show-unmerged-branches:'show unmerged branches' \
410 squash:'import changes from a branch' \
411 stamp:'stamp the last commit message' \
412 standup:'recall the commit history' \
413 summary:'show repository summary' \
414 sync:'sync local branch with remote branch' \
415 touch:'touch and add file to the index' \
416 undo:'remove latest commits' \
417 unlock:'unlock a file excluded from version control' \
418 utimes:'change files modification time to their last commit date'