public inbox for tools@linux.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] b4: support send-email aliases
@ 2025-10-09 17:01 Jacob Keller
  2025-10-09 18:24 ` Konstantin Ryabitsev
  0 siblings, 1 reply; 8+ messages in thread
From: Jacob Keller @ 2025-10-09 17:01 UTC (permalink / raw)
  To: Kernel.org Tools, Konstantin Ryabitsev; +Cc: Jacob Keller

git send-email has supported the --translate-aliases option since v2.47, as
commit c038a6f1d748 ("send-email: teach git send-email option to translate
aliases", 2024-08-17)

The --translate-aliases option allows asking git send-email to convert an
alias into its real address according to an alias file in varying formats.
This enables using the same aliasing behavior as git, without needing to
implement separate parsing logic within b4.

To avoid breaking any users running older versions of git, the translation
is enabled only if the git version is new enough to contain the
translate-aliases option. The support was added by git commit c038a6f1d748
("send-email: teach git send-email option to translate aliases"). A quick
git describe shows this was released in git v2.47.

Make use of this functionality in cleanup_email_addrs(). To minimize
overhead, we first split the addresses into qualified and unqualified
addresses by the presence of the '@' symbol. Aliases won't be proper email
addresses.

The --translate-aliases mode of git send-email takes each alias on a
separate line and translates it in the output. Conveniently, if an input
can't be translated, it is returned unchanged.

Pass the unqualified addresses in as a string joined by newlines. Split the
standard output into translated addresses using email.utils.getaddresses().
Log what we translate each alias into, then recreate the new list of
addresses by combining the original set of qualified addresses with the
newly translated addresses.

This implementation minimizes overhead by only requiring a single git
send-email process, and by only translating addresses which are not yet
fully qualified.

Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
---
Git added support for exposing email address translation via git send-email
--translate-aliases in version 2.47. Add support to b4 to enable this
functionality.

Addresses are translated automatically if the git version is detected as
v2.47 or later. This is simple and avoids burdening uses with needing to
configure both git and b4.

This version minimizes overhead compared to previous versions by only using
a single git subprocess, and by only translating addresses once. Addresses
are split based on whether they are "qualified" with an '@' already or not.
It is assumed that no alias will contain the '@' character. This way, after
translating once, the address won't be passed in to git send-email again.
Further, non-qualified addresses are discarded by cleanup_email_addrs after
a translation attempt.

In my limited testing, this version has significantly less overhead than
the previous version, and additionally requires no configuration. Since we
check the git version, and since we don't attempt to translate any
qualified addresses, it should not break any existing users.
---
Changes in v3:
- Drop b4.send-translate-aliases config option
- Check git version before enabling
- Rewrite conversion algorithm to use a single git subprocess
- Only translate non-qualified addresses
- Link to v2: https://patch.msgid.link/20250709-jk-email-alias-support-v2-1-b0339905964e@intel.com

Changes in v2:
- Add b4.send-translate-aliases config option
- Link to v1: https://patch.msgid.link/20250627-jk-email-alias-support-v1-1-b79aa627cd71@intel.com
---
 src/b4/__init__.py | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index 20bce70ce41d..e8d614978090 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -4265,6 +4265,38 @@ def get_excluded_addrs() -> Set[str]:
 def cleanup_email_addrs(addresses: List[Tuple[str, str]], excludes: Set[str],
                         gitdir: Optional[str]) -> List[Tuple[str, str]]:
     global MAILMAP_INFO
+
+    # Translate aliases if support is available
+    if git_check_minimal_version("2.47"):
+        logger.debug('translating aliases via git send-email')
+
+        qual_addrs = []
+        unqual_addrs = []
+
+        # Assume aliases won't have '@' and are always unqualified
+        for e in list(addresses):
+            qual_addrs.append(e) if '@' in e[1] else unqual_addrs.append(e[1])
+
+        # Pass unqualified addresses to git send-email
+        if len(unqual_addrs) > 0:
+            data = '\n'.join(unqual_addrs).encode('utf-8')
+            args = ['send-email', '--translate-aliases']
+            ecode, out = git_run_command(gitdir,
+                                        ['send-email', '--translate-aliases'],
+                                        stdin=data)
+            if ecode == 0:
+                translated_addrs = email.utils.getaddresses(out.strip().splitlines())
+
+                for alias, entry in zip(unqual_addrs, translated_addrs):
+                    if alias != entry[1]:
+                        logger.debug('translated alias %s to qualified address "%s"',
+                                    alias, email.utils.formataddr(entry))
+
+                addresses = qual_addrs + translated_addrs
+            else:
+                logger.debug('git send-email --translate-aliases failed with exit code %s',
+                             ecode)
+
     for entry in list(addresses):
         # Only qualified addresses, please
         if not len(entry[1].strip()) or '@' not in entry[1]:

---
base-commit: b87af2e0dc6228c52ebd1c9df7b4c13aa4093f05
change-id: 20250627-jk-email-alias-support-d66b261cb5d9

Best regards,
--  
Jacob Keller <jacob.e.keller@intel.com>


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-10-09 22:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-09 17:01 [PATCH v3] b4: support send-email aliases Jacob Keller
2025-10-09 18:24 ` Konstantin Ryabitsev
2025-10-09 19:01   ` Jacob Keller
2025-10-09 19:49   ` Jacob Keller
2025-10-09 20:21     ` Konstantin Ryabitsev
2025-10-09 22:47       ` Jacob Keller
2025-10-09 22:47       ` Jacob Keller
2025-10-09 20:22   ` Jacob Keller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox