sendemail: teach git-send-email to dump alias names

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>

Jacob Keller committed Nov 19, 2015 at 14:52 UTC 17b7a8324402ab1e5bb66adb287e5463757c9326
3 files changed +108
Documentation/git-send-email.txt
+11
@@ -10,6 +10,7 @@ SYNOPSIS
10 --------
11 [verse]
12 'git send-email' [options] <file|directory|rev-list options>...
13 +'git send-email' --dump-aliases
14
15
16 DESCRIPTION
@@ -387,6 +388,16 @@ default to '--validate'.
388 Send emails even if safety checks would prevent it.
389
390
391 +Information
392 +~~~~~~~~~~~
393 +
394 +--dump-aliases::
395 + Instead of the normal operation, dump the shorthand alias names from
396 + the configured alias file(s), one per line in alphabetical order. Note,
397 + this only includes the alias name and not its expanded email addresses.
398 + See 'sendemail.aliasesfile' for more information about aliases.
399 +
400 +
401 CONFIGURATION
402 -------------
403
git-send-email.perl
+15
@@ -46,6 +46,7 @@ package main;
46 sub usage {
47 print <<EOT;
48 git send-email [options] <file | directory | rev-list options >
49 +git send-email --dump-aliases
50
51 Composing:
52 --from <str> * Email From:
@@ -101,6 +102,9 @@ git send-email [options] <file | directory | rev-list options >
102 `git format-patch` ones.
103 --force * Send even if safety checks would prevent it.
104
105 + Information:
106 + --dump-aliases * Dump configured aliases and exit.
107 +
108 EOT
109 exit(1);
110 }
@@ -180,6 +184,7 @@ my ($quiet, $dry_run) = (0, 0);
184 my $format_patch;
185 my $compose_filename;
186 my $force = 0;
187 +my $dump_aliases = 0;
188
189 # Handle interactive edition of files.
190 my $multiedit;
@@ -291,6 +296,11 @@ $SIG{INT} = \&signal_handler;
296
297 my $help;
298 my $rc = GetOptions("h" => \$help,
299 + "dump-aliases" => \$dump_aliases);
300 +usage() unless $rc;
301 +die "--dump-aliases incompatible with other options\n"
302 + if !$help and $dump_aliases and @ARGV;
303 +$rc = GetOptions(
304 "sender|from=s" => \$sender,
305 "in-reply-to=s" => \$initial_reply_to,
306 "subject=s" => \$initial_subject,
@@ -551,6 +561,11 @@ if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
561 }
562 }
563
564 +if ($dump_aliases) {
565 + print "$_\n" for (sort keys %aliases);
566 + exit(0);
567 +}
568 +
569 # is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
570 # $f is a revision list specification to be passed to format-patch.
571 sub is_format_patch_arg {
t/t9001-send-email.sh
+82
@@ -1555,6 +1555,88 @@ test_expect_success $PREREQ 'sendemail.aliasfile=~/.mailrc' '
1555 grep "^!someone@example\.org!$" commandline1
1556 '
1557
1558 +test_dump_aliases () {
1559 + msg="$1" && shift &&
1560 + filetype="$1" && shift &&
1561 + printf '%s\n' "$@" >expect &&
1562 + cat >.tmp-email-aliases &&
1563 +
1564 + test_expect_success $PREREQ "$msg" '
1565 + clean_fake_sendmail && rm -fr outdir &&
1566 + git config --replace-all sendemail.aliasesfile \
1567 + "$(pwd)/.tmp-email-aliases" &&
1568 + git config sendemail.aliasfiletype "$filetype" &&
1569 + git send-email --dump-aliases 2>errors >actual &&
1570 + test_cmp expect actual
1571 + '
1572 +}
1573 +
1574 +test_dump_aliases '--dump-aliases sendmail format' \
1575 + 'sendmail' \
1576 + 'abgroup' \
1577 + 'alice' \
1578 + 'bcgrp' \
1579 + 'bob' \
1580 + 'chloe' <<-\EOF
1581 + alice: Alice W Land <awol@example.com>
1582 + bob: Robert Bobbyton <bob@example.com>
1583 + chloe: chloe@example.com
1584 + abgroup: alice, bob
1585 + bcgrp: bob, chloe, Other <o@example.com>
1586 + EOF
1587 +
1588 +test_dump_aliases '--dump-aliases mutt format' \
1589 + 'mutt' \
1590 + 'alice' \
1591 + 'bob' \
1592 + 'chloe' \
1593 + 'donald' <<-\EOF
1594 + alias alice Alice W Land <awol@example.com>
1595 + alias donald Donald C Carlton <donc@example.com>
1596 + alias bob Robert Bobbyton <bob@example.com>
1597 + alias chloe chloe@example.com
1598 + EOF
1599 +
1600 +test_dump_aliases '--dump-aliases mailrc format' \
1601 + 'mailrc' \
1602 + 'alice' \
1603 + 'bob' \
1604 + 'chloe' \
1605 + 'eve' <<-\EOF
1606 + alias alice Alice W Land <awol@example.com>
1607 + alias eve Eve <eve@example.com>
1608 + alias bob Robert Bobbyton <bob@example.com>
1609 + alias chloe chloe@example.com
1610 + EOF
1611 +
1612 +test_dump_aliases '--dump-aliases pine format' \
1613 + 'pine' \
1614 + 'alice' \
1615 + 'bob' \
1616 + 'chloe' \
1617 + 'eve' <<-\EOF
1618 + alice Alice W Land <awol@example.com>
1619 + eve Eve <eve@example.com>
1620 + bob Robert Bobbyton <bob@example.com>
1621 + chloe chloe@example.com
1622 + EOF
1623 +
1624 +test_dump_aliases '--dump-aliases gnus format' \
1625 + 'gnus' \
1626 + 'alice' \
1627 + 'bob' \
1628 + 'chloe' \
1629 + 'eve' <<-\EOF
1630 + (define-mail-alias "alice" "awol@example.com")
1631 + (define-mail-alias "eve" "eve@example.com")
1632 + (define-mail-alias "bob" "bob@example.com")
1633 + (define-mail-alias "chloe" "chloe@example.com")
1634 + EOF
1635 +
1636 +test_expect_success '--dump-aliases must be used alone' '
1637 + test_must_fail git send-email --dump-aliases --to=janice@example.com -1 refs/heads/accounting
1638 +'
1639 +
1640 test_sendmail_aliases () {
1641 msg="$1" && shift &&
1642 expect="$@" &&