Skip to content

Commit

Permalink
feat: Implement completion for git coauthor (#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperupcall authored Sep 22, 2023
1 parent e793f89 commit dee7c7f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 25 deletions.
25 changes: 25 additions & 0 deletions etc/bash_completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ _git_authors(){
__gitcomp "-l --list --no-email"
}

_git_coauthor(){
local oldIfs=$IFS
IFS=$'\n'
local cur="${COMP_WORDS[COMP_CWORD]}"
local selection=
if ((COMP_CWORD == 2)); then
for line in $(git authors --list); do
selection+="${line% *}"$'\n'
done
elif ((COMP_CWORD == 3)); then
local chosen_name="${COMP_WORDS[COMP_CWORD-1]}"
for line in $(git authors --list); do
if [ "$chosen_name" = "${line% *}" ]; then
local email=${line#*<}
email=${email%>*}
selection+="$email"$'\n'
fi
done
fi
compopt +o default
compopt -o filenames
COMPREPLY=($(compgen -W "$selection" -- "$cur"))
IFS=$oldIfs
}

_git_contrib(){
# git completion function modified from
# https://github.com/markgandolfo/git-bash-completion/blob/master/git-completion.bash
Expand Down
34 changes: 31 additions & 3 deletions etc/git-extras-completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ __gitex_author_names() {
_wanted author-names expl author-name compadd $* - $author_names
}

__gitex_author_emails2() {
echo "$g s
# read -Ac words
# read -cn cword
local expl
declare -a author_names=($state)
local name="${words[2]}" # name of the author supplied on the command line
local -a authors=("${(f)$(git authors --list)}")
for line in "${authors[@]}"; do
if [ "${line% *}" = "$name" ]; then
local email=${line#*<}
email=${email%>*}
author_names+=("${email#* }")
fi
done
# _wanted author-names expl author-name compadd $* - $author_names
reply=(a b c)
}
__gitex_author_emails() {
compctl -K __gitex_author_emails2
}
# subcommands
# new subcommand should be added in alphabetical order
_git-authors() {
Expand All @@ -121,9 +148,10 @@ _git-clear() {
}
_git-coauthor() {
_arguments \
':co-author[co-author to add]' \
':co-author-email[email address of co-author to add]'
compctl -K __gitex_author_emails2 a
# _arguments \
# ':co-author[co-author to add]:__gitex_author_names' \
# ':co-author-email[email address of co-author to add]:__gitex_author_emails'
}
_git-contrib() {
Expand Down
67 changes: 45 additions & 22 deletions etc/git-extras.fish
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ set __fish_git_extras_commands \
"unlock:Unlock a file excluded from version control"

# completion for git-extras itself
complete -c git -f -n '__fish_git_needs_command' -a 'extras' -d 'GIT utilities: repo summary, repl, changelog population, and more'
complete -c git -f -n __fish_git_needs_command -a extras -d 'GIT utilities: repo summary, repl, changelog population, and more'
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'
complete -c git -f -n '__fish_git_using_command extras' -s v -l version -d 'Show git-extras version number'
complete -c git -f -n '__fish_git_using_command extras; and not contains -- update (commandline -opc)' -a "update" -d 'Self update'
complete -c git -f -n '__fish_git_using_command extras; and not contains -- update (commandline -opc)' -a update -d 'Self update'

# completion for git-extras provided commands
set __fish_git_extras_commands (printf -- '%s\n' $__fish_git_extras_commands | sed 's/:/\textras:/' | string collect | string escape)
complete -c git -n '__fish_git_needs_command' -a "$__fish_git_extras_commands"
complete -c git -n __fish_git_needs_command -a "$__fish_git_extras_commands"
# authors
complete -c git -f -n '__fish_git_using_command authors' -s l -l list -d 'show authors'
complete -c git -f -n '__fish_git_using_command authors' -l no-email -d 'without email'
# bulk
complete -c git -n '__fish_git_using_command bulk' -s a -d 'Run a git command on all workspaces and their repositories'
complete -c git -n '__fish_git_using_command bulk' -s g -d 'Ask the user for confirmation on every execution'
complete -c git -n '__fish_git_using_command bulk' -s a -d 'Run a git command on all workspaces and their repositories'
complete -c git -n '__fish_git_using_command bulk' -s g -d 'Ask the user for confirmation on every execution'
complete -c git -x -n '__fish_git_using_command bulk' -s w -d 'Run on specified workspace'
complete -c git -x -n '__fish_git_using_command bulk' -l addworkspace -d 'Register a workspace for builk operations'
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'
Expand All @@ -103,6 +103,31 @@ complete -c git -f -n '__fish_git_using_command changelog' -s n -l no-merges -d
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'
complete -c git -f -n '__fish_git_using_command changelog' -s p -l prune-old -d 'Replace existing changelog entirely with newly generated content'
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'
# coauthor
function __fish_git_arg_number -a number
set -l cmd (commandline -opc)
test (count $cmd) -eq $number
end
function __fish_git_extra_coauthor_name
for line in (git authors --list)
printf '%s\n' $line | string replace --regex ' <.*' ''
end
end
function __fish_git_extra_coauthor_email
set -l cmd (commandline -opc)
# name provided in the previous positional argument
set -l name $cmd[3]

for line in (git authors --list)
set -l loop_name (printf '%s\n' $line | string replace --regex ' <.*' '')

if test "$name" = "$loop_name"
printf '%s\n' $line | string replace --regex '.*?<' '' | string replace --regex '>.*?' ''
end
end
end
complete -c git -f -n '__fish_git_using_command coauthor; and __fish_git_arg_number 2' -a '(__fish_git_extra_coauthor_name)'
complete -c git -f -n '__fish_git_using_command coauthor; and __fish_git_arg_number 3' -a '(__fish_git_extra_coauthor_email)'
# count
complete -c git -f -n '__fish_git_using_command count' -l all -d 'detailed commit count'
# create-branch
Expand All @@ -119,18 +144,18 @@ complete -c git -x -n "__fish_git_using_command delete-tag" -a '(__fish_git for-
complete -c git -f -n '__fish_git_using_command effort' -l above -d 'ignore file with less than x commits'
# feature
complete -c git -x -n '__fish_git_using_command feature' -s a -l alias -d 'use branch_prefix instead of feature'
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'
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'
complete -c git -f -n '__fish_git_using_command feature; and contains -- finish (commandline -opc)' -l squash -d 'Run a squash merge'
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'
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'
# graft
complete -c git -x -n '__fish_git_using_command graft' -s r -l remote -a '(__fish_git_branches)' -d 'src-branch-name'
complete -c git -x -n '__fish_git_using_command graft' -s r -l remote -a '(__fish_git_branches)' -d 'dest-branch-name'
complete -c git -x -n '__fish_git_using_command graft' -s r -l remote -a '(__fish_git_branches)' -d src-branch-name
complete -c git -x -n '__fish_git_using_command graft' -s r -l remote -a '(__fish_git_branches)' -d dest-branch-name
# guilt
complete -c git -f -n '__fish_git_using_command guilt' -s w -l ignore-whitespace -d 'ignore whitespace only changes'
complete -c git -f -n '__fish_git_using_command guilt' -s e -l email -d 'display author emails instead of names'
complete -c git -f -n '__fish_git_using_command guilt' -s d -l debug -d 'output debug information'
complete -c git -f -n '__fish_git_using_command guilt' -s h -d 'output usage information'
complete -c git -f -n '__fish_git_using_command guilt' -s h -d 'output usage information'
# ignore
complete -c git -f -n '__fish_git_using_command ignore' -s l -l local -d 'show local gitignore'
complete -c git -f -n '__fish_git_using_command ignore' -s g -l global -d 'show global gitignore'
Expand All @@ -149,7 +174,7 @@ complete -c git -x -n '__fish_git_using_command ignore-io' -s s -l search -d 'se
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'
complete -c git -x -n '__fish_git_using_command ignore-io' -s u -l update -d 'Update ~/.gi_list'
# merge-into
complete -c git -n '__fish_git_using_command merge-into' -l ff-only -d 'merge only fast-forward'
complete -c git -n '__fish_git_using_command merge-into' -l ff-only -d 'merge only fast-forward'
complete -c git -x -n '__fish_git_using_command merge-into' -a '(__fish_git_branches)'
# missing
complete -c git -x -n '__fish_git_using_command missing' -a '(__fish_git_branches)'
Expand All @@ -159,23 +184,23 @@ complete -c git -x -n '__fish_git_using_command squash' -l squash-msg -d 'commit
# stamp
complete -c git -x -n '__fish_git_using_command stamp' -s r -l replace -d 'replace stamps with same id'
# standup
complete -c git -x -n '__fish_git_using_command standup' -s a -d 'Specify the author of commits. Use "all" to specify all authors'
complete -c git -x -n '__fish_git_using_command standup' -s a -d 'Specify the author of commits. Use all to specify all authors'
complete -c git -x -n '__fish_git_using_command standup' -s m -d 'The depth of recursive directory search'
complete -c git -x -n '__fish_git_using_command standup' -s d -d 'Show history since N days ago'
complete -c git -x -n '__fish_git_using_command standup' -s D -d 'Specify the date format displayed in commit history'
complete -c git -n '__fish_git_using_command standup' -s f -d 'Fetch commits before showing history'
complete -c git -n '__fish_git_using_command standup' -s g -d 'Display GPG signed info'
complete -c git -n '__fish_git_using_command standup' -s h -l help -d 'Display help message'
complete -c git -n '__fish_git_using_command standup' -s L -d 'Enable the inclusion of symbolic links'
complete -c git -n '__fish_git_using_command standup' -s B -d 'Display the commits in branch group'
complete -c git -n '__fish_git_using_command standup' -s f -d 'Fetch commits before showing history'
complete -c git -n '__fish_git_using_command standup' -s g -d 'Display GPG signed info'
complete -c git -n '__fish_git_using_command standup' -s h -l help -d 'Display help message'
complete -c git -n '__fish_git_using_command standup' -s L -d 'Enable the inclusion of symbolic links'
complete -c git -n '__fish_git_using_command standup' -s B -d 'Display the commits in branch group'
complete -c git -x -n '__fish_git_using_command standup' -s n -d 'Limit the number of commits displayed per group'
# summary
complete -c git -n '__fish_git_using_command summary' -l line -d 'summarize with lines rather than commits'
complete -c git -n '__fish_git_using_command summary' -l dedup-by-email -d 'remove duplicate users by the email address'
complete -c git -n '__fish_git_using_command summary' -l no-merges -d 'exclude merge commits'
complete -c git -n '__fish_git_using_command summary' -l line -d 'summarize with lines rather than commits'
complete -c git -n '__fish_git_using_command summary' -l dedup-by-email -d 'remove duplicate users by the email address'
complete -c git -n '__fish_git_using_command summary' -l no-merges -d 'exclude merge commits'
# release
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'
complete -c git -x -n '__fish_git_using_command release' -s r -d 'The "remote" repository that is destination of a push operation'
complete -c git -x -n '__fish_git_using_command release' -s r -d 'The remote repository that is destination of a push operation'
complete -c git -x -n '__fish_git_using_command release' -s m -d 'use the custom commit information instead of the default message'
complete -c git -x -n '__fish_git_using_command release' -s s -d 'Create a signed and annotated tag'
complete -c git -x -n '__fish_git_using_command release' -s u -d 'Create a tag, annotated and signed with the given key'
Expand All @@ -184,5 +209,3 @@ complete -c git -x -n '__fish_git_using_command release' -l no-empty-commit -d '
# undo
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'
complete -c git -x -n '__fish_git_using_command undo' -s h -l hard -d 'wipes your commit(s)'


20 changes: 20 additions & 0 deletions etc/test.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
complete -e blah

function __fish_git_arg_number -a number
set -l cmd (commandline -opc)
test (count $cmd) -eq $number
end

function __fish_git_extra_coauthor_name
printf '%s\n' 'a' 'apple' 'ann'
end

function __fish_git_extra_coauthor_email
set -l cmd (commandline -opc)

set -l value $cmd[3]
printf '%s\n' 'n' 'n1' 'n2' "$value"
end

complete -c blah -f -n '__fish_git_using_command coauthor; and __fish_git_arg_number 2' -a '(__fish_git_extra_coauthor_name)'
complete -c blah -f -n '__fish_git_using_command coauthor; and __fish_git_arg_number 3' -a '(__fish_git_extra_coauthor_email)'

0 comments on commit dee7c7f

Please sign in to comment.