From a9bc6cb4b501602db6947ba3ce90d040105151e0 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Mon, 4 May 2020 23:03:50 -0700 Subject: maildir-import-patch: Prepend "[PATCH fooproject imported]" Signed-off-by: Sean Whitton --- debian/changelog | 5 +++ maildir-import-patch | 97 +++++++++++++++++++++++++++------------------- maildir-import-patch.1.pod | 15 +++++++ 3 files changed, 78 insertions(+), 39 deletions(-) diff --git a/debian/changelog b/debian/changelog index e084188..53a9be1 100644 --- a/debian/changelog +++ b/debian/changelog @@ -12,6 +12,11 @@ mailscripts (0.20-1) UNRELEASED; urgency=medium - Add mailscripts-detach-head-from-existing-branch defcustom. - notmuch-extract-message-patches: set NO-STRICT-MIME argument of `mm-dissect-buffer' to enable extracting patches from more messages. + * maildir-import-patch: Prepend "[PATCH fooproject imported]" to the + subject lines of patches, unless the user supplies a --subject-prefix + option to be passed on to git-format-patch(1). + This makes patches generated by this script more easily + distinguishable from patches really received by e-mail. * Add mailing list & IRC channel info to CONTRIBUTING.rst. -- Sean Whitton Fri, 27 Mar 2020 11:50:27 -0700 diff --git a/maildir-import-patch b/maildir-import-patch index f69a5e6..0147c49 100755 --- a/maildir-import-patch +++ b/maildir-import-patch @@ -1,8 +1,8 @@ -#!/usr/bin/python3 +#!/usr/bin/perl # maildir-import-patch -- import a git patch series into a maildir -# Copyright (C) 2019 Sean Whitton +# Copyright (C) 2019-2020 Sean Whitton # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -17,40 +17,59 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import os -import sys -import mailbox -import shutil -import subprocess -import tempfile - -def eprint(*args, **kwargs): - print(*args, file=sys.stderr, **kwargs) - -us = os.path.basename(sys.argv[0]) - -if len(sys.argv) < 2: - eprint(us + ": usage: " + us + " MAILDIR [git-format-patch(1) args]") - sys.exit(1) - -if not shutil.which("git"): - eprint(us + ": this script requires git to be installed") - sys.exit(1) - -dest_path = sys.argv[1] -dest = mailbox.Maildir(dest_path) - -cmd = [ - "git", "format-patch", - "--thread=shallow", "--no-cc", "--no-to", - "--stdout", -] -user_args = sys.argv[2:] - -with tempfile.NamedTemporaryFile() as fp: - subprocess.run(cmd + user_args, stdout=fp) - source = mailbox.mbox(fp.name) - dest.lock() - for message in source: - dest.add(message) - dest.close() +use strict; +use warnings; + +use Cwd; +use File::Basename; +use File::Temp (); +use File::Which; +use Git::Wrapper; +use List::MoreUtils "firstidx"; +use Mail::Box::Manager; + +my $us = basename $0; +my $maildir_dir = shift + or die "$us: usage: $us MAILDIR [git-format-patch(1) args]\n"; +which "git" or die "$us: this script requires git to be installed\n"; + +my $git = Git::Wrapper->new(getcwd); +my $toplevel; +{ + local $@; + eval { ($toplevel) = $git->rev_parse({ show_toplevel => 1 }) }; + $@ and die "$us: pwd doesn't look like a git repository ..\n"; +} +$toplevel =~ s/\.git$//; + +if (firstidx(sub { /^--subject-prefix/ }, @ARGV) == -1) { + my $subject_prefix; + my $rfc_idx = firstidx { /^--rfc$/ } @ARGV; + if ($rfc_idx == -1) { + $subject_prefix = "PATCH"; + } else { + $subject_prefix = "RFC PATCH"; + splice @ARGV, $rfc_idx, 1; + } + my ($project) = $git->config(qw|--local --get --default|, + basename($toplevel), "mailscripts.project-shortname"); + unshift @ARGV, "--subject-prefix=$subject_prefix $project imported"; +} + +my $mgr = Mail::Box::Manager->new; +my $maildir + = $mgr->open($maildir_dir, type => "maildir", access => "w", create => 1); + +my $mbox_file = File::Temp->new; +my $pid = fork; +die "fork() failed: $!" unless defined $pid; +unless ($pid) { + open STDOUT, ">&=", $mbox_file->fileno + or die "couldn't reopen child's STDOUT: $!"; + exec qw(git format-patch --no-cc --no-to --stdout --thread=shallow), @ARGV; +} +wait; +$mbox_file->close; + +my $mbox = $mgr->open($mbox_file->filename, type => "mbox", access => "r"); +$mgr->copyMessage($maildir, $_) for $mbox->messages; diff --git a/maildir-import-patch.1.pod b/maildir-import-patch.1.pod index a3ef110..7d728e4 100644 --- a/maildir-import-patch.1.pod +++ b/maildir-import-patch.1.pod @@ -34,6 +34,21 @@ Over in your MUA, you can then reply to each e-mail in the new thread generated by B, providing line-by-line feedback on Bob's work. +=head1 GIT CONFIGURATION KEYS + +=over 4 + +=item B + +Short identifier for the project to be prepended to the subject lines +of generated patches, like this: "[PATCH shortname imported]". If +unset, defaults to the name of the root directory of the repository. + +Ignored if any I<--subject-prefix> option is present in the +arguments to be passed on to git-format-patch(1). + +=back + =head1 SEE ALSO notmuch-import-patch(1), git-format-patch(1), git-send-email(1) -- cgit v1.2.3