sendemail: teach git-send-email to dump alias names
authorJacob Keller <jacob.keller@gmail.com>
Thu, 19 Nov 2015 22:52:11 +0000 (14:52 -0800)
committerJeff King <peff@peff.net>
Fri, 20 Nov 2015 13:02:06 +0000 (08:02 -0500)
Add an option "--dump-aliases" which changes the default behavior of
git-send-email. This mode will simply read the alias files configured by
sendemail.aliasesfile and sendemail.aliasfiletype and dump a list of all
configured aliases, one per line. The intended use case for this option
is the bash-completion script which will use it to autocomplete aliases
on the options which take addresses.

Add some tests for the new option using various alias file formats.

A possible future extension to the alias dump format could be done by
extending the --dump-aliases to take an optional argument defining the
format to display. This has not been done in this patch as no user of
this information has been identified.

Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Documentation/git-send-email.txt
git-send-email.perl
t/t9001-send-email.sh
index b9134d234f538c5893fda4440d9ed7283ba8ba9a..771a7b5b09157cc1ac19d1f998600f96f1f9a470 100644 (file)
@@ -10,6 +10,7 @@ SYNOPSIS
 --------
 [verse]
 'git send-email' [options] <file|directory|rev-list options>...
+'git send-email' --dump-aliases
 
 
 DESCRIPTION
@@ -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
 -------------
 
index e907e0eacf31c21dcb75fa102eb980d6a51f96c3..a475b0d7537006df2d541764c7580062518088a4 100755 (executable)
@@ -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:
@@ -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);
 }
@@ -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;
@@ -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,
@@ -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 {
index 5b4a5ce06b94355725fafc84c31f9ed67b8b15ed..3c49536e0e8d2ae0e046b498ad6efaccab8e9f1b 100755 (executable)
@@ -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 <awol@example.com>
+       bob: Robert Bobbyton <bob@example.com>
+       chloe: chloe@example.com
+       abgroup: alice, bob
+       bcgrp: bob, chloe, Other <o@example.com>
+       EOF
+
+test_dump_aliases '--dump-aliases mutt format' \
+       'mutt' \
+       'alice' \
+       'bob' \
+       'chloe' \
+       'donald' <<-\EOF
+       alias alice Alice W Land <awol@example.com>
+       alias donald Donald C Carlton <donc@example.com>
+       alias bob Robert Bobbyton <bob@example.com>
+       alias chloe chloe@example.com
+       EOF
+
+test_dump_aliases '--dump-aliases mailrc format' \
+       'mailrc' \
+       'alice' \
+       'bob' \
+       'chloe' \
+       'eve' <<-\EOF
+       alias alice   Alice W Land <awol@example.com>
+       alias eve     Eve <eve@example.com>
+       alias bob     Robert Bobbyton <bob@example.com>
+       alias chloe   chloe@example.com
+       EOF
+
+test_dump_aliases '--dump-aliases pine format' \
+       'pine' \
+       'alice' \
+       'bob' \
+       'chloe' \
+       'eve' <<-\EOF
+       alice   Alice W Land    <awol@example.com>
+       eve     Eve     <eve@example.com>
+       bob     Robert  Bobbyton <bob@example.com>
+       chloe           chloe@example.com
+       EOF
+
+test_dump_aliases '--dump-aliases gnus format' \
+       'gnus' \
+       'alice' \
+       'bob' \
+       'chloe' \
+       'eve' <<-\EOF
+       (define-mail-alias "alice" "awol@example.com")
+       (define-mail-alias "eve" "eve@example.com")
+       (define-mail-alias "bob" "bob@example.com")
+       (define-mail-alias "chloe" "chloe@example.com")
+       EOF
+
+test_expect_success '--dump-aliases must be used alone' '
+       test_must_fail git send-email --dump-aliases --to=janice@example.com -1 refs/heads/accounting
+'
+
 test_sendmail_aliases () {
        msg="$1" && shift &&
        expect="$@" &&