git-extras.plugin.zsh 8.96 KB
Newer Older
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
1
2
3
4
# ------------------------------------------------------------------------------
# Description
# -----------
#
spacewander's avatar
spacewander committed
5
#  Completion script for git-extras (http://github.com/tj/git-extras).
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
6
#
7
8
9
10
#  This depends on and reueses some of the internals of the _git completion
#  function that ships with zsh itself. It will not work with the _git that ships
#  with git.
#
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
11
12
13
14
15
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Alexis GRIMALDI (https://github.com/agrimaldi)
spacewander's avatar
spacewander committed
16
#  * spacewander (https://github.com/spacewander)
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
17
18
19
20
21
#
# ------------------------------------------------------------------------------
# Inspirations
# -----------
#
spacewander's avatar
spacewander committed
22
#  * git-extras (http://github.com/tj/git-extras)
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
23
24
25
#  * git-flow-completion (http://github.com/bobthecow/git-flow-completion)
#
# ------------------------------------------------------------------------------
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
26
27


28
29
# Internal functions
# These are a lot like their __git_* equivalents inside _git
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
30

31
32
33
34
35
36
37
__gitex_command_successful () {
  if (( ${#*:#0} > 0 )); then
    _message 'not a git repository'
    return 1
  fi
  return 0
}
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
38

39
__gitex_commits() {
spacewander's avatar
spacewander committed
40
41
42
43
44
45
46
47
48
49
    declare -A commits
    git log --oneline -15 | sed 's/\([[:alnum:]]\{7\}\) /\1:/' | while read commit
    do
        hash=$(echo $commit | cut -d':' -f1)
        commits[$hash]="$commit"
    done
    local ret=1
    _describe -t commits commit commits && ret=0
}

50
__gitex_tag_names() {
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
51
52
    local expl
    declare -a tag_names
spacewander's avatar
spacewander committed
53
    tag_names=(${${(f)"$(_call_program tags git for-each-ref --format='"%(refname)"' refs/tags 2>/dev/null)"}#refs/tags/})
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
54
55
56
57
58
    __git_command_successful || return
    _wanted tag-names expl tag-name compadd $* - $tag_names
}


59
__gitex_branch_names() {
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
60
61
62
63
64
65
66
    local expl
    declare -a branch_names
    branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads 2>/dev/null)"}#refs/heads/})
    __git_command_successful || return
    _wanted branch-names expl branch-name compadd $* - $branch_names
}

67
__gitex_specific_branch_names() {
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
68
69
    local expl
    declare -a branch_names
spacewander's avatar
spacewander committed
70
    branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads/"$1" 2>/dev/null)"}#refs/heads/$1/})
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
71
72
73
74
    __git_command_successful || return
    _wanted branch-names expl branch-name compadd $* - $branch_names
}

75
76
__gitex_feature_branch_names() {
    __gitex_specific_branch_names 'feature'
spacewander's avatar
spacewander committed
77
78
}

79
80
__gitex_refactor_branch_names() {
    __gitex_specific_branch_names 'refactor'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
81
82
}

83
84
__gitex_bug_branch_names() {
    __gitex_specific_branch_names 'bug'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
85
86
}

87
__gitex_submodule_names() {
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
88
89
    local expl
    declare -a submodule_names
90
    submodule_names=(${(f)"$(_call_program branchrefs git submodule status | awk '{print $2}')"})  # '
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
91
92
93
94
95
    __git_command_successful || return
    _wanted submodule-names expl submodule-name compadd $* - $submodule_names
}


96
__gitex_author_names() {
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
97
98
99
100
101
102
103
    local expl
    declare -a author_names
    author_names=(${(f)"$(_call_program branchrefs git log --format='%aN' | sort -u)"})
    __git_command_successful || return
    _wanted author-names expl author-name compadd $* - $author_names
}

spacewander's avatar
spacewander committed
104
# subcommands
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
105

spacewander's avatar
spacewander committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
_git-bug() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
        ': :->command' \
        '*:: :->option-or-argument' && ret=0

    case $state in
        (command)
            declare -a commands
            commands=(
                'finish:merge bug into the current branch'
            )
            _describe -t commands command commands && ret=0
            ;;
        (option-or-argument)
            curcontext=${curcontext%:*}-$line[1]:
            case $line[1] in
                (finish)
                    _arguments -C \
127
                        ':branch-name:__gitex_bug_branch_names'
spacewander's avatar
spacewander committed
128
129
130
                    ;;
            esac
    esac
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
131
132
133
}


spacewander's avatar
spacewander committed
134
_git-changelog() {
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
135
    _arguments \
spacewander's avatar
spacewander committed
136
        '(-l --list)'{-l,--list}'[list commits]' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
137
138
139
}


spacewander's avatar
spacewander committed
140

Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
141
142
_git-contrib() {
    _arguments \
143
        ':author:__gitex_author_names'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
144
145
146
147
148
149
150
151
152
153
154
}


_git-count() {
    _arguments \
        '--all[detailed commit count]'
}


_git-delete-branch() {
    _arguments \
155
        ':branch-name:__gitex_branch_names'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
156
157
158
159
160
}


_git-delete-submodule() {
    _arguments \
161
        ':submodule-name:__gitex_submodule_names'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
162
163
164
165
166
}


_git-delete-tag() {
    _arguments \
167
        ':tag-name:__gitex_tag_names'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
168
169
170
}


spacewander's avatar
spacewander committed
171
172
173
174
175
_git-effort() {
    _arguments \
        '--above[ignore file with less than x commits]'
}

176

Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
_git-extras() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
        ': :->command' \
        '*:: :->option-or-argument' && ret=0

    case $state in
        (command)
            declare -a commands
            commands=(
                'update:update git-extras'
            )
            _describe -t commands command commands && ret=0
            ;;
    esac

    _arguments \
spacewander's avatar
spacewander committed
196
        '(-v --version)'{-v,--version}'[show current version]'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
}


_git-feature() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
        ': :->command' \
        '*:: :->option-or-argument' && ret=0

    case $state in
        (command)
            declare -a commands
            commands=(
                'finish:merge feature into the current branch'
            )
            _describe -t commands command commands && ret=0
            ;;
        (option-or-argument)
            curcontext=${curcontext%:*}-$line[1]:
            case $line[1] in
                (finish)
                    _arguments -C \
221
                        ':branch-name:__gitex_feature_branch_names'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
222
223
224
225
226
227
                    ;;
            esac
    esac
}


spacewander's avatar
spacewander committed
228
229
_git-graft() {
    _arguments \
230
231
        ':src-branch-name:__gitex_branch_names' \
        ':dest-branch-name:__gitex_branch_names'
spacewander's avatar
spacewander committed
232
233
234
235
236
237
238
239
240
}


_git-ignore() {
    _arguments  -C \
        '(--local -l)'{--local,-l}'[show local gitignore]' \
        '(--global -g)'{--global,-g}'[show global gitignore]'
}

241

spacewander's avatar
spacewander committed
242
243
_git-missing() {
    _arguments \
244
245
        ':first-branch-name:__gitex_branch_names' \
        ':second-branch-name:__gitex_branch_names'
spacewander's avatar
spacewander committed
246
247
}

248

Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
_git-refactor() {
    local curcontext=$curcontext state line ret=1
    declare -A opt_args

    _arguments -C \
        ': :->command' \
        '*:: :->option-or-argument' && ret=0

    case $state in
        (command)
            declare -a commands
            commands=(
                'finish:merge refactor into the current branch'
            )
            _describe -t commands command commands && ret=0
            ;;
        (option-or-argument)
            curcontext=${curcontext%:*}-$line[1]:
            case $line[1] in
                (finish)
                    _arguments -C \
270
                        ':branch-name:__gitex_refactor_branch_names'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
271
272
273
274
275
276
                    ;;
            esac
    esac
}


spacewander's avatar
spacewander committed
277
278
_git-squash() {
    _arguments \
279
        ':branch-name:__gitex_branch_names'
spacewander's avatar
spacewander committed
280
}
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
281

spacewander's avatar
spacewander committed
282
_git-summary() {
283
284
    _arguments '--line[summarize with lines rather than commits]'
    __gitex_commits
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
285
286
287
}


spacewander's avatar
spacewander committed
288
289
290
291
292
293
_git-undo(){
    _arguments  -C \
        '(--soft -s)'{--soft,-s}'[only rolls back the commit but changes remain un-staged]' \
        '(--hard -h)'{--hard,-h}'[wipes your commit(s)]'
}

Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
294
zstyle ':completion:*:*:git:*' user-commands \
spacewander's avatar
spacewander committed
295
296
297
298
    alias:'define, search and show aliases' \
    archive-file:'export the current HEAD of the git repository to a archive' \
    back:'undo and stage latest commits' \
    bug:'create a bug branch' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
299
    changelog:'populate changelog file with commits since the previous tag' \
spacewander's avatar
spacewander committed
300
    commits-since:'list commits since a given date' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
301
302
    contrib:'display author contributions' \
    count:'count commits' \
spacewander's avatar
spacewander committed
303
    create-branch:'create local and remote branch' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
304
    delete-branch:'delete local and remote branch' \
Satoshi Ohmori's avatar
Satoshi Ohmori committed
305
    delete-merged-branches:'delete merged branches'\
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
306
307
    delete-submodule:'delete submodule' \
    delete-tag:'delete local and remote tag' \
spacewander's avatar
spacewander committed
308
    effort:'display effort statistics' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
309
310
    extras:'git-extras' \
    feature:'create a feature branch' \
spacewander's avatar
spacewander committed
311
312
313
314
    fork:'fork a repo on github' \
    fresh-branch:'create empty local branch' \
    gh-pages:'create the GitHub Pages branch' \
    graft:'merge commits from source branch to destination branch' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
315
316
    ignore:'add patterns to .gitignore' \
    info:'show info about the repository' \
spacewander's avatar
spacewander committed
317
318
319
320
321
322
323
324
325
326
327
328
    local-commits:'list unpushed commits on the local branch' \
    lock:'lock a file excluded from version control' \
    locked:'ls files that have been locked' \
    missing:'show commits missing from another branch' \
    pr:'checks out a pull request locally' \
    rebase-patch:'rebases a patch' \
    refactor:'create a refactor branch' \
    release:'commit, tag and push changes to the repository' \
    rename-tag:'rename a tag' \
    repl:'read-eval-print-loop' \
    reset-file:'reset one file' \
    root:'show path of root' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
329
    setup:'setup a git repository' \
spacewander's avatar
spacewander committed
330
331
332
    show-tree:'show branch tree of commit history' \
    squash:'merge commits from source branch into the current one as a single commit' \
    summary:'repository summary' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
333
    touch:'one step creation of new files' \
spacewander's avatar
spacewander committed
334
335
    undo:'remove the latest commit' \
    unlock:'unlock a file excluded from version control'