Skip to content

Commit

Permalink
Merge branch 'jk/send-email-complete-aliases'
Browse files Browse the repository at this point in the history
Teach send-email to dump mail aliases, so that we can do tab completion
on the command line.

* jk/send-email-complete-aliases:
  completion: add support for completing email aliases
  sendemail: teach git-send-email to dump alias names
  • Loading branch information
gitster committed Dec 4, 2015
2 parents 2e5adec + dfbe5ee commit c69d08d
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Documentation/git-send-email.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SYNOPSIS
--------
[verse]
'git send-email' [options] <file|directory|rev-list options>...
'git send-email' --dump-aliases


DESCRIPTION
Expand Down Expand Up @@ -387,6 +388,16 @@ default to '--validate'.
Send emails even if safety checks would prevent it.


Information
~~~~~~~~~~~

--dump-aliases::
Instead of the normal operation, dump the shorthand alias names from
the configured alias file(s), one per line in alphabetical order. Note,
this only includes the alias name and not its expanded email addresses.
See 'sendemail.aliasesfile' for more information about aliases.


CONFIGURATION
-------------

Expand Down
16 changes: 16 additions & 0 deletions contrib/completion/git-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# *) local and remote tag names
# *) .git/remotes file names
# *) git 'subcommands'
# *) git email aliases for git-send-email
# *) tree paths within 'ref:path/to/file' expressions
# *) file paths within current working directory and index
# *) common --long-options
Expand Down Expand Up @@ -1711,6 +1712,15 @@ __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"

_git_send_email ()
{
case "$prev" in
--to|--cc|--bcc|--from)
__gitcomp "
$(git --git-dir="$(__gitdir)" send-email --dump-aliases 2>/dev/null)
" "" ""
return
;;
esac

case "$cur" in
--confirm=*)
__gitcomp "
Expand All @@ -1735,6 +1745,12 @@ _git_send_email ()
" "" "${cur##--thread=}"
return
;;
--to=*|--cc=*|--bcc=*|--from=*)
__gitcomp "
$(git --git-dir="$(__gitdir)" send-email --dump-aliases 2>/dev/null)
" "" "${cur#--*=}"
return
;;
--*)
__gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
--compose --confirm= --dry-run --envelope-sender
Expand Down
15 changes: 15 additions & 0 deletions git-send-email.perl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ package main;
sub usage {
print <<EOT;
git send-email [options] <file | directory | rev-list options >
git send-email --dump-aliases
Composing:
--from <str> * Email From:
Expand Down Expand Up @@ -101,6 +102,9 @@ sub usage {
`git format-patch` ones.
--force * Send even if safety checks would prevent it.
Information:
--dump-aliases * Dump configured aliases and exit.
EOT
exit(1);
}
Expand Down Expand Up @@ -180,6 +184,7 @@ sub format_2822_time {
my $format_patch;
my $compose_filename;
my $force = 0;
my $dump_aliases = 0;

# Handle interactive edition of files.
my $multiedit;
Expand Down Expand Up @@ -291,6 +296,11 @@ sub signal_handler {

my $help;
my $rc = GetOptions("h" => \$help,
"dump-aliases" => \$dump_aliases);
usage() unless $rc;
die "--dump-aliases incompatible with other options\n"
if !$help and $dump_aliases and @ARGV;
$rc = GetOptions(
"sender|from=s" => \$sender,
"in-reply-to=s" => \$initial_reply_to,
"subject=s" => \$initial_subject,
Expand Down Expand Up @@ -551,6 +561,11 @@ sub parse_sendmail_aliases {
}
}

if ($dump_aliases) {
print "$_\n" for (sort keys %aliases);
exit(0);
}

# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
# $f is a revision list specification to be passed to format-patch.
sub is_format_patch_arg {
Expand Down
82 changes: 82 additions & 0 deletions t/t9001-send-email.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,88 @@ test_expect_success $PREREQ 'sendemail.aliasfile=~/.mailrc' '
grep "^!someone@example\.org!$" commandline1
'

test_dump_aliases () {
msg="$1" && shift &&
filetype="$1" && shift &&
printf '%s\n' "$@" >expect &&
cat >.tmp-email-aliases &&

test_expect_success $PREREQ "$msg" '
clean_fake_sendmail && rm -fr outdir &&
git config --replace-all sendemail.aliasesfile \
"$(pwd)/.tmp-email-aliases" &&
git config sendemail.aliasfiletype "$filetype" &&
git send-email --dump-aliases 2>errors >actual &&
test_cmp expect actual
'
}

test_dump_aliases '--dump-aliases sendmail format' \
'sendmail' \
'abgroup' \
'alice' \
'bcgrp' \
'bob' \
'chloe' <<-\EOF
alice: Alice W Land <[email protected]>
bob: Robert Bobbyton <[email protected]>
chloe: [email protected]
abgroup: alice, bob
bcgrp: bob, chloe, Other <[email protected]>
EOF

test_dump_aliases '--dump-aliases mutt format' \
'mutt' \
'alice' \
'bob' \
'chloe' \
'donald' <<-\EOF
alias alice Alice W Land <[email protected]>
alias donald Donald C Carlton <[email protected]>
alias bob Robert Bobbyton <[email protected]>
alias chloe [email protected]
EOF

test_dump_aliases '--dump-aliases mailrc format' \
'mailrc' \
'alice' \
'bob' \
'chloe' \
'eve' <<-\EOF
alias alice Alice W Land <[email protected]>
alias eve Eve <[email protected]>
alias bob Robert Bobbyton <[email protected]>
alias chloe [email protected]
EOF

test_dump_aliases '--dump-aliases pine format' \
'pine' \
'alice' \
'bob' \
'chloe' \
'eve' <<-\EOF
alice Alice W Land <[email protected]>
eve Eve <[email protected]>
bob Robert Bobbyton <[email protected]>
chloe [email protected]
EOF

test_dump_aliases '--dump-aliases gnus format' \
'gnus' \
'alice' \
'bob' \
'chloe' \
'eve' <<-\EOF
(define-mail-alias "alice" "[email protected]")
(define-mail-alias "eve" "[email protected]")
(define-mail-alias "bob" "[email protected]")
(define-mail-alias "chloe" "[email protected]")
EOF

test_expect_success '--dump-aliases must be used alone' '
test_must_fail git send-email --dump-aliases [email protected] -1 refs/heads/accounting
'

test_sendmail_aliases () {
msg="$1" && shift &&
expect="$@" &&
Expand Down

0 comments on commit c69d08d

Please sign in to comment.