Skip to content

Commit dee7c7f

Browse files
authored
feat: Implement completion for git coauthor (#1074)
1 parent e793f89 commit dee7c7f

File tree

4 files changed

+121
-25
lines changed

4 files changed

+121
-25
lines changed

etc/bash_completion.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ _git_authors(){
2626
__gitcomp "-l --list --no-email"
2727
}
2828

29+
_git_coauthor(){
30+
local oldIfs=$IFS
31+
IFS=$'\n'
32+
local cur="${COMP_WORDS[COMP_CWORD]}"
33+
local selection=
34+
if ((COMP_CWORD == 2)); then
35+
for line in $(git authors --list); do
36+
selection+="${line% *}"$'\n'
37+
done
38+
elif ((COMP_CWORD == 3)); then
39+
local chosen_name="${COMP_WORDS[COMP_CWORD-1]}"
40+
for line in $(git authors --list); do
41+
if [ "$chosen_name" = "${line% *}" ]; then
42+
local email=${line#*<}
43+
email=${email%>*}
44+
selection+="$email"$'\n'
45+
fi
46+
done
47+
fi
48+
compopt +o default
49+
compopt -o filenames
50+
COMPREPLY=($(compgen -W "$selection" -- "$cur"))
51+
IFS=$oldIfs
52+
}
53+
2954
_git_contrib(){
3055
# git completion function modified from
3156
# https://github.com/markgandolfo/git-bash-completion/blob/master/git-completion.bash

etc/git-extras-completion.zsh

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,33 @@ __gitex_author_names() {
101101
_wanted author-names expl author-name compadd $* - $author_names
102102
}
103103

104+
__gitex_author_emails2() {
105+
echo "$g s
106+
# read -Ac words
107+
# read -cn cword
108+
109+
local expl
110+
declare -a author_names=($state)
111+
112+
local name="${words[2]}" # name of the author supplied on the command line
113+
local -a authors=("${(f)$(git authors --list)}")
114+
for line in "${authors[@]}"; do
115+
if [ "${line% *}" = "$name" ]; then
116+
local email=${line#*<}
117+
email=${email%>*}
118+
119+
author_names+=("${email#* }")
120+
fi
121+
done
122+
123+
# _wanted author-names expl author-name compadd $* - $author_names
124+
reply=(a b c)
125+
}
126+
127+
__gitex_author_emails() {
128+
compctl -K __gitex_author_emails2
129+
}
130+
104131
# subcommands
105132
# new subcommand should be added in alphabetical order
106133
_git-authors() {
@@ -121,9 +148,10 @@ _git-clear() {
121148
}
122149
123150
_git-coauthor() {
124-
_arguments \
125-
':co-author[co-author to add]' \
126-
':co-author-email[email address of co-author to add]'
151+
compctl -K __gitex_author_emails2 a
152+
# _arguments \
153+
# ':co-author[co-author to add]:__gitex_author_names' \
154+
# ':co-author-email[email address of co-author to add]:__gitex_author_emails'
127155
}
128156
129157
_git-contrib() {

etc/git-extras.fish

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,20 @@ set __fish_git_extras_commands \
7171
"unlock:Unlock a file excluded from version control"
7272

7373
# completion for git-extras itself
74-
complete -c git -f -n '__fish_git_needs_command' -a 'extras' -d 'GIT utilities: repo summary, repl, changelog population, and more'
74+
complete -c git -f -n __fish_git_needs_command -a extras -d 'GIT utilities: repo summary, repl, changelog population, and more'
7575
complete -c git -f -n '__fish_git_using_command extras' -s h -l help -d 'Show the help message, can be used for any git-extras commands'
7676
complete -c git -f -n '__fish_git_using_command extras' -s v -l version -d 'Show git-extras version number'
77-
complete -c git -f -n '__fish_git_using_command extras; and not contains -- update (commandline -opc)' -a "update" -d 'Self update'
77+
complete -c git -f -n '__fish_git_using_command extras; and not contains -- update (commandline -opc)' -a update -d 'Self update'
7878

7979
# completion for git-extras provided commands
8080
set __fish_git_extras_commands (printf -- '%s\n' $__fish_git_extras_commands | sed 's/:/\textras:/' | string collect | string escape)
81-
complete -c git -n '__fish_git_needs_command' -a "$__fish_git_extras_commands"
81+
complete -c git -n __fish_git_needs_command -a "$__fish_git_extras_commands"
8282
# authors
8383
complete -c git -f -n '__fish_git_using_command authors' -s l -l list -d 'show authors'
8484
complete -c git -f -n '__fish_git_using_command authors' -l no-email -d 'without email'
8585
# bulk
86-
complete -c git -n '__fish_git_using_command bulk' -s a -d 'Run a git command on all workspaces and their repositories'
87-
complete -c git -n '__fish_git_using_command bulk' -s g -d 'Ask the user for confirmation on every execution'
86+
complete -c git -n '__fish_git_using_command bulk' -s a -d 'Run a git command on all workspaces and their repositories'
87+
complete -c git -n '__fish_git_using_command bulk' -s g -d 'Ask the user for confirmation on every execution'
8888
complete -c git -x -n '__fish_git_using_command bulk' -s w -d 'Run on specified workspace'
8989
complete -c git -x -n '__fish_git_using_command bulk' -l addworkspace -d 'Register a workspace for builk operations'
9090
complete -c git -x -n '__fish_git_using_command bulk; and contains addworkspace (commandline -opc)' -l addworkspace -d 'the URL or file with URLs to be added'
@@ -103,6 +103,31 @@ complete -c git -f -n '__fish_git_using_command changelog' -s n -l no-merges -d
103103
complete -c git -f -n '__fish_git_using_command changelog' -s m -l merges-only -d 'Uses only merge commits (commits with more than 1 parent) for generated changelog'
104104
complete -c git -f -n '__fish_git_using_command changelog' -s p -l prune-old -d 'Replace existing changelog entirely with newly generated content'
105105
complete -c git -f -n '__fish_git_using_command changelog' -s x -l stdout -d 'Write output to stdout instead of to a new changelog file'
106+
# coauthor
107+
function __fish_git_arg_number -a number
108+
set -l cmd (commandline -opc)
109+
test (count $cmd) -eq $number
110+
end
111+
function __fish_git_extra_coauthor_name
112+
for line in (git authors --list)
113+
printf '%s\n' $line | string replace --regex ' <.*' ''
114+
end
115+
end
116+
function __fish_git_extra_coauthor_email
117+
set -l cmd (commandline -opc)
118+
# name provided in the previous positional argument
119+
set -l name $cmd[3]
120+
121+
for line in (git authors --list)
122+
set -l loop_name (printf '%s\n' $line | string replace --regex ' <.*' '')
123+
124+
if test "$name" = "$loop_name"
125+
printf '%s\n' $line | string replace --regex '.*?<' '' | string replace --regex '>.*?' ''
126+
end
127+
end
128+
end
129+
complete -c git -f -n '__fish_git_using_command coauthor; and __fish_git_arg_number 2' -a '(__fish_git_extra_coauthor_name)'
130+
complete -c git -f -n '__fish_git_using_command coauthor; and __fish_git_arg_number 3' -a '(__fish_git_extra_coauthor_email)'
106131
# count
107132
complete -c git -f -n '__fish_git_using_command count' -l all -d 'detailed commit count'
108133
# create-branch
@@ -119,18 +144,18 @@ complete -c git -x -n "__fish_git_using_command delete-tag" -a '(__fish_git for-
119144
complete -c git -f -n '__fish_git_using_command effort' -l above -d 'ignore file with less than x commits'
120145
# feature
121146
complete -c git -x -n '__fish_git_using_command feature' -s a -l alias -d 'use branch_prefix instead of feature'
122-
complete -c git -f -n '__fish_git_using_command feature; and not contains -- finish (commandline -opc)' -a "finish" -d 'merge and delete the feature branch'
147+
complete -c git -f -n '__fish_git_using_command feature; and not contains -- finish (commandline -opc)' -a finish -d 'merge and delete the feature branch'
123148
complete -c git -f -n '__fish_git_using_command feature; and contains -- finish (commandline -opc)' -l squash -d 'Run a squash merge'
124149
complete -c git -x -n '__fish_git_using_command feature; and contains -- finish (commandline -opc)' -a '(__fish_git for-each-ref --format="%(refname)" 2>/dev/null | grep "refs/heads/feature")' -d 'name of feature branch'
125150
complete -c git -x -n '__fish_git_using_command feature; and not contains -- finish (commandline -opc)' -s r -l remote -a '(__fish_git_unique_remote_branches)' -d 'Setup a remote tracking branch'
126151
# graft
127-
complete -c git -x -n '__fish_git_using_command graft' -s r -l remote -a '(__fish_git_branches)' -d 'src-branch-name'
128-
complete -c git -x -n '__fish_git_using_command graft' -s r -l remote -a '(__fish_git_branches)' -d 'dest-branch-name'
152+
complete -c git -x -n '__fish_git_using_command graft' -s r -l remote -a '(__fish_git_branches)' -d src-branch-name
153+
complete -c git -x -n '__fish_git_using_command graft' -s r -l remote -a '(__fish_git_branches)' -d dest-branch-name
129154
# guilt
130155
complete -c git -f -n '__fish_git_using_command guilt' -s w -l ignore-whitespace -d 'ignore whitespace only changes'
131156
complete -c git -f -n '__fish_git_using_command guilt' -s e -l email -d 'display author emails instead of names'
132157
complete -c git -f -n '__fish_git_using_command guilt' -s d -l debug -d 'output debug information'
133-
complete -c git -f -n '__fish_git_using_command guilt' -s h -d 'output usage information'
158+
complete -c git -f -n '__fish_git_using_command guilt' -s h -d 'output usage information'
134159
# ignore
135160
complete -c git -f -n '__fish_git_using_command ignore' -s l -l local -d 'show local gitignore'
136161
complete -c git -f -n '__fish_git_using_command ignore' -s g -l global -d 'show global gitignore'
@@ -149,7 +174,7 @@ complete -c git -x -n '__fish_git_using_command ignore-io' -s s -l search -d 'se
149174
complete -c git -x -n '__fish_git_using_command ignore-io' -s t -l show-update-time -d 'Show the last modified time of ~/.gi_list'
150175
complete -c git -x -n '__fish_git_using_command ignore-io' -s u -l update -d 'Update ~/.gi_list'
151176
# merge-into
152-
complete -c git -n '__fish_git_using_command merge-into' -l ff-only -d 'merge only fast-forward'
177+
complete -c git -n '__fish_git_using_command merge-into' -l ff-only -d 'merge only fast-forward'
153178
complete -c git -x -n '__fish_git_using_command merge-into' -a '(__fish_git_branches)'
154179
# missing
155180
complete -c git -x -n '__fish_git_using_command missing' -a '(__fish_git_branches)'
@@ -159,23 +184,23 @@ complete -c git -x -n '__fish_git_using_command squash' -l squash-msg -d 'commit
159184
# stamp
160185
complete -c git -x -n '__fish_git_using_command stamp' -s r -l replace -d 'replace stamps with same id'
161186
# standup
162-
complete -c git -x -n '__fish_git_using_command standup' -s a -d 'Specify the author of commits. Use "all" to specify all authors'
187+
complete -c git -x -n '__fish_git_using_command standup' -s a -d 'Specify the author of commits. Use all to specify all authors'
163188
complete -c git -x -n '__fish_git_using_command standup' -s m -d 'The depth of recursive directory search'
164189
complete -c git -x -n '__fish_git_using_command standup' -s d -d 'Show history since N days ago'
165190
complete -c git -x -n '__fish_git_using_command standup' -s D -d 'Specify the date format displayed in commit history'
166-
complete -c git -n '__fish_git_using_command standup' -s f -d 'Fetch commits before showing history'
167-
complete -c git -n '__fish_git_using_command standup' -s g -d 'Display GPG signed info'
168-
complete -c git -n '__fish_git_using_command standup' -s h -l help -d 'Display help message'
169-
complete -c git -n '__fish_git_using_command standup' -s L -d 'Enable the inclusion of symbolic links'
170-
complete -c git -n '__fish_git_using_command standup' -s B -d 'Display the commits in branch group'
191+
complete -c git -n '__fish_git_using_command standup' -s f -d 'Fetch commits before showing history'
192+
complete -c git -n '__fish_git_using_command standup' -s g -d 'Display GPG signed info'
193+
complete -c git -n '__fish_git_using_command standup' -s h -l help -d 'Display help message'
194+
complete -c git -n '__fish_git_using_command standup' -s L -d 'Enable the inclusion of symbolic links'
195+
complete -c git -n '__fish_git_using_command standup' -s B -d 'Display the commits in branch group'
171196
complete -c git -x -n '__fish_git_using_command standup' -s n -d 'Limit the number of commits displayed per group'
172197
# summary
173-
complete -c git -n '__fish_git_using_command summary' -l line -d 'summarize with lines rather than commits'
174-
complete -c git -n '__fish_git_using_command summary' -l dedup-by-email -d 'remove duplicate users by the email address'
175-
complete -c git -n '__fish_git_using_command summary' -l no-merges -d 'exclude merge commits'
198+
complete -c git -n '__fish_git_using_command summary' -l line -d 'summarize with lines rather than commits'
199+
complete -c git -n '__fish_git_using_command summary' -l dedup-by-email -d 'remove duplicate users by the email address'
200+
complete -c git -n '__fish_git_using_command summary' -l no-merges -d 'exclude merge commits'
176201
# release
177202
complete -c git -x -n '__fish_git_using_command release' -s c -d 'Generates/populates the changelog with all commit message since the last tag'
178-
complete -c git -x -n '__fish_git_using_command release' -s r -d 'The "remote" repository that is destination of a push operation'
203+
complete -c git -x -n '__fish_git_using_command release' -s r -d 'The remote repository that is destination of a push operation'
179204
complete -c git -x -n '__fish_git_using_command release' -s m -d 'use the custom commit information instead of the default message'
180205
complete -c git -x -n '__fish_git_using_command release' -s s -d 'Create a signed and annotated tag'
181206
complete -c git -x -n '__fish_git_using_command release' -s u -d 'Create a tag, annotated and signed with the given key'
@@ -184,5 +209,3 @@ complete -c git -x -n '__fish_git_using_command release' -l no-empty-commit -d '
184209
# undo
185210
complete -c git -x -n '__fish_git_using_command undo' -s s -l soft -d 'only rolls back the commit but changes remain un-staged'
186211
complete -c git -x -n '__fish_git_using_command undo' -s h -l hard -d 'wipes your commit(s)'
187-
188-

etc/test.fish

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
complete -e blah
2+
3+
function __fish_git_arg_number -a number
4+
set -l cmd (commandline -opc)
5+
test (count $cmd) -eq $number
6+
end
7+
8+
function __fish_git_extra_coauthor_name
9+
printf '%s\n' 'a' 'apple' 'ann'
10+
end
11+
12+
function __fish_git_extra_coauthor_email
13+
set -l cmd (commandline -opc)
14+
15+
set -l value $cmd[3]
16+
printf '%s\n' 'n' 'n1' 'n2' "$value"
17+
end
18+
19+
complete -c blah -f -n '__fish_git_using_command coauthor; and __fish_git_arg_number 2' -a '(__fish_git_extra_coauthor_name)'
20+
complete -c blah -f -n '__fish_git_using_command coauthor; and __fish_git_arg_number 3' -a '(__fish_git_extra_coauthor_email)'

0 commit comments

Comments
 (0)