Skip to content

Commit dd207c3

Browse files
committed
Merge branch 'jk/send-email-translate-aliases' into next
"git send-email" learned "--translate-aliases" option that reads addresses from the standard input and emits the result of applying aliases on them to the standard output. * jk/send-email-translate-aliases: send-email: teach git send-email option to translate aliases t9001-send-email.sh: update alias list used for pine test t9001-send-email.sh: fix quoting for mailrc --dump-aliases test
2 parents f85d609 + c038a6f commit dd207c3

File tree

3 files changed

+139
-7
lines changed

3 files changed

+139
-7
lines changed

Documentation/git-send-email.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SYNOPSIS
1212
'git send-email' [<options>] (<file>|<directory>)...
1313
'git send-email' [<options>] <format-patch-options>
1414
'git send-email' --dump-aliases
15+
'git send-email' --translate-aliases
1516

1617

1718
DESCRIPTION
@@ -475,6 +476,12 @@ Information
475476
that this only includes the alias name and not its expanded email addresses.
476477
See 'sendemail.aliasesFile' for more information about aliases.
477478

479+
--translate-aliases::
480+
Instead of the normal operation, read from standard input and
481+
interpret each line as an email alias. Translate it according to the
482+
configured alias file(s). Output each translated name and email
483+
address to standard output, one per line. See 'sendemail.aliasFile'
484+
for more information about aliases.
478485

479486
CONFIGURATION
480487
-------------

git-send-email.perl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ sub usage {
3131
git send-email [<options>] <file|directory>
3232
git send-email [<options>] <format-patch options>
3333
git send-email --dump-aliases
34+
git send-email --translate-aliases
3435
3536
Composing:
3637
--from <str> * Email From:
@@ -99,6 +100,10 @@ sub usage {
99100
100101
Information:
101102
--dump-aliases * Dump configured aliases and exit.
103+
--translate-aliases * Translate aliases read from standard
104+
input according to the configured email
105+
alias file(s), outputting the result to
106+
standard output.
102107
103108
EOT
104109
exit(1);
@@ -212,6 +217,7 @@ sub format_2822_time {
212217
my $compose_filename;
213218
my $force = 0;
214219
my $dump_aliases = 0;
220+
my $translate_aliases = 0;
215221

216222
# Variables to prevent short format-patch options from being captured
217223
# as abbreviated send-email options
@@ -476,11 +482,14 @@ sub config_regexp {
476482
my %dump_aliases_options = (
477483
"h" => \$help,
478484
"dump-aliases" => \$dump_aliases,
485+
"translate-aliases" => \$translate_aliases,
479486
);
480487
$rc = GetOptions(%dump_aliases_options);
481488
usage() unless $rc;
482489
die __("--dump-aliases incompatible with other options\n")
483-
if !$help and $dump_aliases and @ARGV;
490+
if !$help and ($dump_aliases or $translate_aliases) and @ARGV;
491+
die __("--dump-aliases and --translate-aliases are mutually exclusive\n")
492+
if !$help and $dump_aliases and $translate_aliases;
484493
my %options = (
485494
"sender|from=s" => \$sender,
486495
"in-reply-to=s" => \$initial_in_reply_to,
@@ -724,6 +733,16 @@ sub parse_sendmail_aliases {
724733
exit(0);
725734
}
726735

736+
if ($translate_aliases) {
737+
while (<STDIN>) {
738+
my @addr_list = parse_address_line($_);
739+
@addr_list = expand_aliases(@addr_list);
740+
@addr_list = sanitize_address_list(@addr_list);
741+
print "$_\n" for @addr_list;
742+
}
743+
exit(0);
744+
}
745+
727746
# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
728747
# $f is a revision list specification to be passed to format-patch.
729748
sub is_format_patch_arg {

t/t9001-send-email.sh

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,22 +2084,24 @@ test_dump_aliases '--dump-aliases mailrc format' \
20842084
'bob' \
20852085
'chloe' \
20862086
'eve' <<-\EOF
2087-
alias alice Alice W Land <awol@example.com>
2088-
alias eve Eve <eve@example.com>
2089-
alias bob Robert Bobbyton <bob@example.com>
2087+
alias alice "Alice W Land <awol@example.com>"
2088+
alias eve "Eve <eve@example.com>"
2089+
alias bob "Robert Bobbyton <bob@example.com>"
20902090
alias chloe chloe@example.com
20912091
EOF
20922092

20932093
test_dump_aliases '--dump-aliases pine format' \
20942094
'pine' \
20952095
'alice' \
2096+
'bcgrp' \
20962097
'bob' \
20972098
'chloe' \
20982099
'eve' <<-\EOF
2099-
alice Alice W Land <awol@example.com>
2100-
eve Eve <eve@example.com>
2101-
bob Robert Bobbyton <bob@example.com>
2100+
alice Alice W Land awol@example.com Friend
2101+
eve Eve eve@example.com
2102+
bob Robert Bobbyton bob@example.com
21022103
chloe chloe@example.com
2104+
bcgrp (bob, chloe, Other <o@example.com>)
21032105
EOF
21042106

21052107
test_dump_aliases '--dump-aliases gnus format' \
@@ -2118,6 +2120,110 @@ test_expect_success '--dump-aliases must be used alone' '
21182120
test_must_fail git send-email --dump-aliases --to=janice@example.com -1 refs/heads/accounting
21192121
'
21202122

2123+
test_translate_aliases () {
2124+
msg="$1" && shift &&
2125+
filetype="$1" && shift &&
2126+
aliases="$1" && shift &&
2127+
printf '%s\n' "$@" >expect &&
2128+
cat >.tmp-email-aliases &&
2129+
printf '%s\n' "$aliases" >aliases &&
2130+
2131+
test_expect_success $PREREQ "$msg" '
2132+
clean_fake_sendmail && rm -fr outdir &&
2133+
git config --replace-all sendemail.aliasesfile \
2134+
"$(pwd)/.tmp-email-aliases" &&
2135+
git config sendemail.aliasfiletype "$filetype" &&
2136+
git send-email --translate-aliases <aliases 2>errors >actual &&
2137+
test_cmp expect actual
2138+
'
2139+
}
2140+
2141+
test_translate_aliases '--translate-aliases sendmail format' \
2142+
'sendmail' \
2143+
'alice bcgrp' \
2144+
'Alice W Land <awol@example.com>' \
2145+
'Robert Bobbyton <bob@example.com>' \
2146+
'chloe@example.com' \
2147+
'Other <o@example.com>' <<-\EOF
2148+
alice: Alice W Land <awol@example.com>
2149+
bob: Robert Bobbyton <bob@example.com>
2150+
chloe: chloe@example.com
2151+
abgroup: alice, bob
2152+
bcgrp: bob, chloe, Other <o@example.com>
2153+
EOF
2154+
2155+
test_translate_aliases '--translate-aliases mutt format' \
2156+
'mutt' \
2157+
'donald bob' \
2158+
'Donald C Carlton <donc@example.com>' \
2159+
'Robert Bobbyton <bob@example.com>' <<-\EOF
2160+
alias alice Alice W Land <awol@example.com>
2161+
alias donald Donald C Carlton <donc@example.com>
2162+
alias bob Robert Bobbyton <bob@example.com>
2163+
alias chloe chloe@example.com
2164+
EOF
2165+
2166+
test_translate_aliases '--translate-aliases mailrc format' \
2167+
'mailrc' \
2168+
'chloe eve alice' \
2169+
'chloe@example.com' \
2170+
'Eve <eve@example.com>' \
2171+
'Alice W Land <awol@example.com>' <<-\EOF
2172+
alias alice "Alice W Land <awol@example.com>"
2173+
alias eve "Eve <eve@example.com>"
2174+
alias bob "Robert Bobbyton <bob@example.com>"
2175+
alias chloe chloe@example.com
2176+
EOF
2177+
2178+
test_translate_aliases '--translate-aliases pine format' \
2179+
'pine' \
2180+
'eve bob bcgrp' \
2181+
'eve@example.com' \
2182+
'bob@example.com' \
2183+
'bob@example.com' \
2184+
'chloe@example.com' \
2185+
'Other <o@example.com>' <<-\EOF
2186+
alice Alice W Land awol@example.com Friend
2187+
eve Eve eve@example.com
2188+
bob Robert Bobbyton bob@example.com
2189+
chloe chloe@example.com
2190+
bcgrp (bob, chloe, Other <o@example.com>)
2191+
EOF
2192+
2193+
test_translate_aliases '--translate-aliases gnus format' \
2194+
'gnus' \
2195+
'alice chloe eve' \
2196+
'awol@example.com' \
2197+
'chloe@example.com' \
2198+
'eve@example.com' <<-\EOF
2199+
(define-mail-alias "alice" "awol@example.com")
2200+
(define-mail-alias "eve" "eve@example.com")
2201+
(define-mail-alias "bob" "bob@example.com")
2202+
(define-mail-alias "chloe" "chloe@example.com")
2203+
EOF
2204+
2205+
test_expect_success $PREREQ '--translate-aliases passes valid addresses through' '
2206+
cat >expect <<-\EOF &&
2207+
Other <o@example.com>
2208+
EOF
2209+
cat >aliases <<-\EOF &&
2210+
Other <o@example.com>
2211+
EOF
2212+
git send-email --translate-aliases <aliases >actual &&
2213+
test_cmp expect actual
2214+
'
2215+
2216+
test_expect_success $PREREQ '--translate-aliases passes unknown aliases through' '
2217+
cat >expect <<-\EOF &&
2218+
blargh
2219+
EOF
2220+
cat >aliases <<-\EOF &&
2221+
blargh
2222+
EOF
2223+
git send-email --translate-aliases <aliases >actual &&
2224+
test_cmp expect actual
2225+
'
2226+
21212227
test_expect_success $PREREQ 'aliases and sendemail.identity' '
21222228
test_must_fail git \
21232229
-c sendemail.identity=cloud \

0 commit comments

Comments
 (0)