git-extras.plugin.zsh 8.69 KB
Newer Older
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
1
#compdef git
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
2
3
4
5
# ------------------------------------------------------------------------------
# Description
# -----------
#
spacewander's avatar
spacewander committed
6
#  Completion script for git-extras (http://github.com/tj/git-extras).
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
7
8
9
10
11
12
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Alexis GRIMALDI (https://github.com/agrimaldi)
spacewander's avatar
spacewander committed
13
#  * spacewander (https://github.com/spacewander)
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
14
15
16
17
18
#
# ------------------------------------------------------------------------------
# Inspirations
# -----------
#
spacewander's avatar
spacewander committed
19
#  * git-extras (http://github.com/tj/git-extras)
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
20
21
22
#  * git-flow-completion (http://github.com/bobthecow/git-flow-completion)
#
# ------------------------------------------------------------------------------
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
23
24
25
26
27
28
29
30
31
32
33


__git_command_successful () {
    if (( ${#pipestatus:#0} > 0 )); then
        _message 'not a git repository'
        return 1
    fi
    return 0
}


spacewander's avatar
spacewander committed
34
35
36
37
38
39
40
41
42
43
44
__git_commits() {
    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
}

Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
45
46
47
__git_tag_names() {
    local expl
    declare -a tag_names
spacewander's avatar
spacewander committed
48
    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
49
50
51
52
53
54
55
56
57
58
59
60
61
    __git_command_successful || return
    _wanted tag-names expl tag-name compadd $* - $tag_names
}


__git_branch_names() {
    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
}

spacewander's avatar
spacewander committed
62
__git_specific_branch_names() {
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
63
64
    local expl
    declare -a branch_names
spacewander's avatar
spacewander committed
65
    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
66
67
68
69
70
    __git_command_successful || return
    _wanted branch-names expl branch-name compadd $* - $branch_names
}


spacewander's avatar
spacewander committed
71
72
73
74
75
__git_feature_branch_names() {
    __git_specific_branch_names 'feature'
}


Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
76
__git_refactor_branch_names() {
spacewander's avatar
spacewander committed
77
    __git_specific_branch_names 'refactor'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
78
79
80
81
}


__git_bug_branch_names() {
spacewander's avatar
spacewander committed
82
    __git_specific_branch_names 'bug'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
}


__git_submodule_names() {
    local expl
    declare -a submodule_names
    submodule_names=(${(f)"$(_call_program branchrefs git submodule status | awk '{print $2}')"})
    __git_command_successful || return
    _wanted submodule-names expl submodule-name compadd $* - $submodule_names
}


__git_author_names() {
    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
103
# subcommands
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
104

spacewander's avatar
spacewander committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
_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 \
                        ':branch-name:__git_bug_branch_names'
                    ;;
            esac
    esac
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
130
131
132
}


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


spacewander's avatar
spacewander committed
139

Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
_git-contrib() {
    _arguments \
        ':author:__git_author_names'
}


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


_git-delete-branch() {
    _arguments \
        ':branch-name:__git_branch_names'
}


_git-delete-submodule() {
    _arguments \
        ':submodule-name:__git_submodule_names'
}


_git-delete-tag() {
    _arguments \
        ':tag-name:__git_tag_names'
}


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

Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
_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
194
        '(-v --version)'{-v,--version}'[show current version]'
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
}


_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 \
                        ':branch-name:__git_feature_branch_names'
                    ;;
            esac
    esac
}


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


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

_git-missing() {
    _arguments \
        ':first-branch-name:__git_branch_names' \
        ':second-branch-name:__git_branch_names'
}

Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
_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 \
                        ':branch-name:__git_refactor_branch_names'
                    ;;
            esac
    esac
}


spacewander's avatar
spacewander committed
273
274
275
276
_git-squash() {
    _arguments \
        ':branch-name:__git_branch_names'
}
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
277

spacewander's avatar
spacewander committed
278
279
280
_git-summary() {
    _arguments '--line[summarize with lines other than commits]'
    __git_commits
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
281
282
283
}


spacewander's avatar
spacewander committed
284
285
286
287
288
289
_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
290
zstyle ':completion:*:*:git:*' user-commands \
spacewander's avatar
spacewander committed
291
292
293
294
    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
295
    changelog:'populate changelog file with commits since the previous tag' \
spacewander's avatar
spacewander committed
296
    commits-since:'list commits since a given date' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
297
298
    contrib:'display author contributions' \
    count:'count commits' \
spacewander's avatar
spacewander committed
299
    create-branch:'create local and remote branch' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
300
    delete-branch:'delete local and remote branch' \
spacewander's avatar
spacewander committed
301
    delete-merged-brancees:'delete merged branches'\
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
302
303
    delete-submodule:'delete submodule' \
    delete-tag:'delete local and remote tag' \
spacewander's avatar
spacewander committed
304
    effort:'display effort statistics' \
Alexis GRIMALDI's avatar
Alexis GRIMALDI committed
305
306
    extras:'git-extras' \
    feature:'create a feature branch' \
spacewander's avatar
spacewander committed
307
308
309
310
    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
311
312
    ignore:'add patterns to .gitignore' \
    info:'show info about the repository' \
spacewander's avatar
spacewander committed
313
314
315
316
317
318
319
320
321
322
323
324
    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
325
    setup:'setup a git repository' \
spacewander's avatar
spacewander committed
326
327
328
    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
329
    touch:'one step creation of new files' \
spacewander's avatar
spacewander committed
330
331
    undo:'remove the latest commit' \
    unlock:'unlock a file excluded from version control'