git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Robin Jarry <robin@jarry.cc>
To: git@vger.kernel.org
Cc: Tim Culverhouse <tim@timculverhouse.com>,
	Nicolas Dichtel <nicolas.dichtel@6wind.com>,
	Bagas Sanjaya <bagasdotme@gmail.com>,
	Junio C Hamano <gitster@pobox.com>,
	Eric Sunshine <sunshine@sunshineco.com>,
	Phillip Wood <phillip.wood123@gmail.com>,
	Robin Jarry <robin@jarry.cc>
Subject: [PATCH v2] hooks: add sendemail-validate-series
Date: Thu,  6 Apr 2023 01:13:05 +0200	[thread overview]
Message-ID: <20230405231305.96996-1-robin@jarry.cc> (raw)
In-Reply-To: <20230402185635.302653-1-robin@jarry.cc>

When sending patch series (with a cover-letter or not)
sendemail-validate is called with every email/patch file independently
from the others. When one of the patches depends on a previous one, it
may not be possible to use this hook in a meaningful way.

Changing sendemail-validate to take all patches as arguments would break
backward compatibility.

Add a new hook to allow validating patch series instead of patch by
patch. The patch files are provided in the hook script standard input,
one per line. Patch file names that contain LF characters are *not*
validated.

Signed-off-by: Robin Jarry <robin@jarry.cc>
---

Notes:
    v1 -> v2:
    
    - Use `git hook run --to-stdin` with a temp file.
    - Skip validation (with an explicit warning) for patch file names that
      contain newline characters.
    - Updated docs.

 Documentation/git-send-email.txt |  1 +
 Documentation/githooks.txt       | 19 +++++++++++++++++
 git-send-email.perl              | 36 ++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+)

diff --git a/Documentation/git-send-email.txt b/Documentation/git-send-email.txt
index 765b2df8530d..45113b928593 100644
--- a/Documentation/git-send-email.txt
+++ b/Documentation/git-send-email.txt
@@ -438,6 +438,7 @@ have been specified, in which case default to 'compose'.
 +
 --
 		*	Invoke the sendemail-validate hook if present (see linkgit:githooks[5]).
+		*	Invoke the sendemail-validate-series hook if present (see linkgit:githooks[5]).
 		*	Warn of patches that contain lines longer than
 			998 characters unless a suitable transfer encoding
 			('auto', 'base64', or 'quoted-printable') is used;
diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt
index 62908602e7be..0e8573c6c116 100644
--- a/Documentation/githooks.txt
+++ b/Documentation/githooks.txt
@@ -600,6 +600,25 @@ the name of the file that holds the e-mail to be sent.  Exiting with a
 non-zero status causes `git send-email` to abort before sending any
 e-mails.
 
+sendemail-validate-series
+~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This hook is invoked by linkgit:git-send-email[1].  It allows performing
+validation on a complete patch series at once, instead of patch by patch with
+`sendemail-validate`.
+
+`sendemail-validate-series` takes no arguments.  For each e-mail to be sent,
+it receives on standard input a line of the format:
+
+  <patch-file> LF
+
+where '<patch-file>' is an absolute path to a file that holds an e-mail to be
+sent.  Any '<patch-file>' that contains a 'LF' character will *not* be fed to
+the hook and an explicit warning will be printed instead.
+
+If the hook exits with non-zero status, `git send-email` will abort before
+sending any e-mails.
+
 fsmonitor-watchman
 ~~~~~~~~~~~~~~~~~~
 
diff --git a/git-send-email.perl b/git-send-email.perl
index 07f2a0cbeaad..b29050e14c06 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -800,6 +800,7 @@ sub is_format_patch_arg {
 			validate_patch($f, $target_xfer_encoding);
 		}
 	}
+	validate_patch_series(@files)
 }
 
 if (@files) {
@@ -2125,6 +2126,41 @@ sub validate_patch {
 	return;
 }
 
+sub validate_patch_series {
+	my @files = @_;
+
+	unless ($repo) {
+		return;
+	}
+	require File::Temp;
+	my $tmp = File::Temp->new(
+		TEMPLATE => "sendemail-series.XXXXXXXX",
+		UNLINK => 1,
+	);
+	for my $fn (@files) {
+		unless (-p $fn) {
+			$fn = Cwd::abs_path($fn);
+			if ($fn =~ /\n/) {
+				$fn =~ s/\n/'\\n'/g;
+				printf STDERR __("warning: file name contains '\\n': %s. Skipping validation.\n"), $fn;
+			} else {
+				$tmp->print("$fn\n");
+			}
+		}
+	}
+	my $hook_name = "sendemail-validate-series";
+	my @cmd = ("git", "hook", "run", "--ignore-missing",
+		   "--to-stdin", $tmp->filename, $hook_name, "--");
+	my $hook_error = system_or_msg(\@cmd, undef, "@cmd");
+	if ($hook_error) {
+		$hook_error = sprintf(
+		    __("fatal: series rejected by %s hook\n%s\nwarning: no patches were sent\n"),
+		    $hook_name, $hook_error);
+		die $hook_error;
+	}
+	return;
+}
+
 sub handle_backup {
 	my ($last, $lastlen, $file, $known_suffix) = @_;
 	my ($suffix, $skip);
-- 
2.40.0


  parent reply	other threads:[~2023-04-05 23:13 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-02 18:56 [PATCH RESEND] hooks: add sendemail-validate-series Robin Jarry
2023-04-03  0:17 ` Eric Sunshine
2023-04-03 14:09 ` Phillip Wood
2023-04-03 14:32   ` Robin Jarry
2023-04-03 15:20     ` Phillip Wood
2023-04-03 15:42   ` Junio C Hamano
2023-04-03 17:25     ` Robin Jarry
2023-04-03 22:29   ` Robin Jarry
2023-04-03 22:52     ` Junio C Hamano
2023-04-03 22:59       ` Robin Jarry
2023-04-04 20:14         ` Junio C Hamano
2023-04-05  8:31           ` Robin Jarry
2023-04-05 21:49             ` Junio C Hamano
2023-04-05 23:13 ` Robin Jarry [this message]
2023-04-06  8:56   ` [PATCH v2] " Ævar Arnfjörð Bjarmason
2023-04-11  9:58     ` Phillip Wood
2023-04-11 10:39       ` Robin Jarry
2023-04-11 15:58       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230405231305.96996-1-robin@jarry.cc \
    --to=robin@jarry.cc \
    --cc=bagasdotme@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=nicolas.dichtel@6wind.com \
    --cc=phillip.wood123@gmail.com \
    --cc=sunshine@sunshineco.com \
    --cc=tim@timculverhouse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).