[PATCH 3/3] send-email: teach git send-email option to translate aliases

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Jacob Keller <jacob.keller@xxxxxxxxx>

Add a new "--translate-aliases" option to git send-email which allows
other programs to convert email aliases according to the configured
alias file. This is intended to allow b4 send the ability to use the
same aliases as git send-email.

There is one tricky part of handling the new option, since
--translate-aliases wants to consume the rest of @ARGV. Currently, the
pass_through option is set for perl's Getopt::Long::Configure, which
causes unknown options to get passed through to other option parsers.

This is required in order to handle passing format-patch options, and is
tricky to work around. --dump-aliases handles this by testing @ARGV
before calling the full option parser. We can't do this with
--translate-aliases because it wants to consume the arguments.

Instead, skip calling GetOptions a second time of --translate-aliases is
set. This has the effect that known options will instead be translated
as aliases instead of producing a warning, but this seems like the best
trade off of the available options.

Signed-off-by: Jacob Keller <jacob.keller@xxxxxxxxx>
---
 Documentation/git-send-email.txt |  7 ++++
 git-send-email.perl              | 17 +++++++-
 t/t9001-send-email.sh            | 89 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+), 1 deletion(-)

diff --git a/Documentation/git-send-email.txt b/Documentation/git-send-email.txt
index c5d664f4519b..6964c9914a9c 100644
--- a/Documentation/git-send-email.txt
+++ b/Documentation/git-send-email.txt
@@ -12,6 +12,7 @@ SYNOPSIS
 'git send-email' [<options>] (<file>|<directory>)...
 'git send-email' [<options>] <format-patch-options>
 'git send-email' --dump-aliases
+'git send-email' --translate-aliases (<alias>)...
 
 
 DESCRIPTION
@@ -475,6 +476,12 @@ Information
 	that this only includes the alias name and not its expanded email addresses.
 	See 'sendemail.aliasesFile' for more information about aliases.
 
+--translate-aliases::
+	Instead of the normal operation, interpret all command line
+	arguments as shorthand alias names using the configured alias
+	file(s). Output each translated email address, one per line, in the
+	order the aliases appear. See 'sendemail.aliasFile' for more
+	information about aliases.
 
 CONFIGURATION
 -------------
diff --git a/git-send-email.perl b/git-send-email.perl
index 72044e5ef3a8..2ae6cc0d7a36 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -31,6 +31,7 @@ sub usage {
 git send-email [<options>] <file|directory>
 git send-email [<options>] <format-patch options>
 git send-email --dump-aliases
+git send-email --translate-aliases <alias>
 
   Composing:
     --from                  <str>  * Email From:
@@ -99,6 +100,11 @@ sub usage {
 
   Information:
     --dump-aliases                 * Dump configured aliases and exit.
+    --translate-aliases            * Interpret all other command line arguments
+                                     as email aliases. Translate them
+                                     according to the configured alias file,
+                                     outputing each address one per line, then
+                                     exit.
 
 EOT
 	exit(1);
@@ -212,6 +218,7 @@ sub format_2822_time {
 my $compose_filename;
 my $force = 0;
 my $dump_aliases = 0;
+my $translate_aliases = 0;
 
 # Variables to prevent short format-patch options from being captured
 # as abbreviated send-email options
@@ -476,11 +483,14 @@ sub config_regexp {
 my %dump_aliases_options = (
 	"h" => \$help,
 	"dump-aliases" => \$dump_aliases,
+	"translate-aliases" => \$translate_aliases,
 );
 $rc = GetOptions(%dump_aliases_options);
 usage() unless $rc;
 die __("--dump-aliases incompatible with other options\n")
     if !$help and $dump_aliases and @ARGV;
+die __("--dump-aliases and --translate-aliases are mutually exclusive\n")
+    if !$help and $dump_aliases and $translate_aliases;
 my %options = (
 		    "sender|from=s" => \$sender,
 		    "in-reply-to=s" => \$initial_in_reply_to,
@@ -534,7 +544,7 @@ sub config_regexp {
 		    "git-completion-helper" => \$git_completion_helper,
 		    "v=s" => \$reroll_count,
 );
-$rc = GetOptions(%options);
+($rc = GetOptions(%options)) unless $translate_aliases;
 
 # Munge any "either config or getopt, not both" variables
 my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
@@ -724,6 +734,11 @@ sub parse_sendmail_aliases {
     exit(0);
 }
 
+if ($translate_aliases) {
+	print "$_\n" for (process_address_list(@ARGV));
+	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 {
diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
index c96d6955b9f2..78c451918145 100755
--- a/t/t9001-send-email.sh
+++ b/t/t9001-send-email.sh
@@ -2120,6 +2120,95 @@ test_expect_success '--dump-aliases must be used alone' '
 	test_must_fail git send-email --dump-aliases --to=janice@xxxxxxxxxxx -1 refs/heads/accounting
 '
 
+test_translate_aliases () {
+	msg="$1" && shift &&
+	filetype="$1" && shift &&
+	aliases="$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 --translate-aliases $aliases 2>errors >actual &&
+		test_cmp expect actual
+	'
+}
+
+test_translate_aliases '--translate-aliases sendmail format' \
+	'sendmail' \
+	'alice bcgrp' \
+	'Alice W Land <awol@xxxxxxxxxxx>' \
+	'Robert Bobbyton <bob@xxxxxxxxxxx>' \
+	'chloe@xxxxxxxxxxx' \
+	'Other <o@xxxxxxxxxxx>' <<-\EOF
+	alice: Alice W Land <awol@xxxxxxxxxxx>
+	bob: Robert Bobbyton <bob@xxxxxxxxxxx>
+	chloe: chloe@xxxxxxxxxxx
+	abgroup: alice, bob
+	bcgrp: bob, chloe, Other <o@xxxxxxxxxxx>
+	EOF
+
+test_translate_aliases '--translate-aliases mutt format' \
+	'mutt' \
+	'donald bob' \
+	'Donald C Carlton <donc@xxxxxxxxxxx>' \
+	'Robert Bobbyton <bob@xxxxxxxxxxx>' <<-\EOF
+	alias alice Alice W Land <awol@xxxxxxxxxxx>
+	alias donald Donald C Carlton <donc@xxxxxxxxxxx>
+	alias bob Robert Bobbyton <bob@xxxxxxxxxxx>
+	alias chloe chloe@xxxxxxxxxxx
+	EOF
+
+test_translate_aliases '--translate-aliases mailrc format' \
+	'mailrc' \
+	'chloe eve alice' \
+	'chloe@xxxxxxxxxxx' \
+	'Eve <eve@xxxxxxxxxxx>' \
+	'Alice W Land <awol@xxxxxxxxxxx>' <<-\EOF
+	alias alice   "Alice W Land <awol@xxxxxxxxxxx>"
+	alias eve     "Eve <eve@xxxxxxxxxxx>"
+	alias bob     "Robert Bobbyton <bob@xxxxxxxxxxx>"
+	alias chloe   chloe@xxxxxxxxxxx
+	EOF
+
+test_translate_aliases '--translate-aliases pine format' \
+	'pine' \
+	'eve bob bcgrp' \
+	'eve@xxxxxxxxxxx' \
+	'bob@xxxxxxxxxxx' \
+	'bob@xxxxxxxxxxx' \
+	'chloe@xxxxxxxxxxx' \
+	'Other <o@xxxxxxxxxxx>' <<-\EOF
+	alice	Alice W Land	awol@xxxxxxxxxxx		Friend
+	eve	Eve	eve@xxxxxxxxxxx
+	bob	Robert Bobbyton	bob@xxxxxxxxxxx
+	chloe		chloe@xxxxxxxxxxx
+	bcgrp		(bob, chloe, Other <o@xxxxxxxxxxx>)
+	EOF
+
+test_translate_aliases '--translate-aliases gnus format' \
+	'gnus' \
+	'alice chloe eve' \
+	'awol@xxxxxxxxxxx' \
+	'chloe@xxxxxxxxxxx' \
+	'eve@xxxxxxxxxxx' <<-\EOF
+	(define-mail-alias "alice" "awol@xxxxxxxxxxx")
+	(define-mail-alias "eve" "eve@xxxxxxxxxxx")
+	(define-mail-alias "bob" "bob@xxxxxxxxxxx")
+	(define-mail-alias "chloe" "chloe@xxxxxxxxxxx")
+	EOF
+
+test_expect_success '--translate-aliases passes unknown aliases through' '
+	cat >expect <<-\EOF &&
+	Other <o@xxxxxxxxxxx>
+	EOF
+	git send-email --translate-aliases "Other <o@xxxxxxxxxxx>" >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success $PREREQ 'aliases and sendemail.identity' '
 	test_must_fail git \
 		-c sendemail.identity=cloud \

-- 
2.46.0.124.g2dc1a81c8933





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux