hooks: add sendemail-validate-series
PATCH RESEND 1/1 hooks: add sendemail-validate-series
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.
git hook run cannot be used since it closes the hook standard input. Run the hook directly.
---
Rebased on 140b9478dad5 ("The sixth batch")
Documentation/git-send-email.txt | 1 + Documentation/githooks.txt | 17 +++++++++++++ git-send-email.perl | 42 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+)
Documentation/git-send-email.txt +1 -0
@@ -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;
Documentation/githooks.txt +17 -0
@@ -600,6 +600,23 @@ 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, but for each e-mail to be sent +it receives on standard input a line of the format: + + <patch-file> LF + +where `<patch-file>` is a name of a file that holds an e-mail to be sent, + +If the hook exits with non-zero status, `git send-email` will abort before +sending any e-mails. + fsmonitor-watchman ~~~~~~~~~~~~~~~~~~
git-send-email.perl +42 -0
54 lines not on this page · raw message
Signature · 1 line
2.39.2
Quoted 10 lines · Robin Jarry
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.
It's not clear from this description whether the pathnames of the patches are fed to the hook on stdin or if the patch contents are fed on stdin.
Quoted 10 lines
git hook run cannot be used since it closes the hook standard input. Run the hook directly.
Signed-off-by Robin Jarry <[email protected]>---
Documentation/githooks.txt +3
-0@@ -600,6 +600,23 @@ the name of the file that holds the e-mail to be sent. Exiting with a +sendemail-validate-series +~~~~~~~~~~~~~~~~~~~~~~~~~ +
Documentation/githooks.txt the name of the file that holds the e-mail to be sent. Exiting with a · line 612
Quoted 10 lines
Documentation/githooks.txt +10
-0+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, but for each e-mail to be sent +it receives on standard input a line of the format: + + <patch-file> LF + +where `<patch-file>` is a name of a file that holds an e-mail to be sent,
This does a better job than the commit message of explaining that stdin receives the names of the patches rather than the content of the patches themselves. It's a nit, but it might be even clearer to say that <patch-file> is the _pathname_ of the file rather than merely the _name_.
Documentation/githooks.txt the name of the file that holds the e-mail to be sent. Exiting with a · line 618
Quoted 2 lines
Documentation/githooks.txt +2
-0+If the hook exits with non-zero status, `git send-email` will abort before +sending any e-mails.
It was a bit startling to see this spelled "e-mail" rather than "email", the latter of which is used far more frequently in the documentation. However, "e-mail" does indeed appear in githooks.txt more frequently than "email", so the use of "e-mail" here is probably fine.
I doubt that any of the above comments warrant a reroll.
Hi Robin
Quoted 5 lines · Robin Jarry
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.
Quoted 8 lines
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.
git hook run cannot be used since it closes the hook standard input. Run the hook directly.
I've left some comments about this lower down as "git hook run" now has a --to-stdin option.
Quoted 4 lines
Signed-off-by Robin Jarry <[email protected]>---
Documentation/githooks.txt +1
-0+sendemail-validate-series
Documentation/githooks.txt the name of the file that holds the e-mail to be sent. Exiting with a · line 613
Quoted 10 lines
Documentation/githooks.txt +10
-0+~~~~~~~~~~~~~~~~~~~~~~~~~ + +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, but for each e-mail to be sent +it receives on standard input a line of the format: + + <patch-file> LF
Usually git commands that produce or consume paths either use quoted paths terminated by LF or unquoted paths terminated by NUL. That way there is no ambiguity when a path contains LF.
Quoted 13 lines
Documentation/githooks.txt +5
-0+where `<patch-file>` is a name of a file that holds an e-mail to be sent, + +If the hook exits with non-zero status, `git send-email` will abort before +sending any e-mails. + fsmonitor-watchman ~~~~~~~~~~~~~~~~~~git-send-email.perl +0
-0
git-send-email.perl sub is_format_patch_arg { · line 803
Quoted 4 lines
git-send-email.perl +1
-0@@ -800,6 +800,7 @@ sub is_format_patch_arg { validate_patch($f, $target_xfer_encoding); } } + validate_patch_series(@files)
This happens fairly early, before the user has had a chance to edit the patches and before we have added all the recipient and in-reply-to headers to the patch files. Would it be more useful to validate what will actually be sent?
Quoted 6 lines
git-send-email.perl +0
-0} if (@files) {@@ -2125,6 +2126,47 @@ sub validate_patch { return; }
git-send-email.perl sub validate_patch { · line 2137
Quoted 10 lines
git-send-email.perl +9
-0+sub validate_patch_series { + my @files = @_; + + unless ($repo) { + return; + } + + my $hook_name = 'sendemail-validate-series'; + my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks');
$hooks_path maybe a relative path, this is problematic because we change directory before executing the hook (using "git hook run" would avoid this).
git-send-email.perl sub validate_patch { · line 2149
Quoted 12 lines
git-send-email.perl +12
-0+ require File::Spec; + my $validate_hook = File::Spec->catfile($hooks_path, $hook_name); + my $hook_error; + unless (-x $validate_hook) { + return; + } + + # The hook needs a correct cwd and GIT_DIR. + require Cwd; + my $cwd_save = Cwd::getcwd(); + chdir($repo->wc_path() or $repo->repo_path()) or die("chdir: $!"); + local $ENV{"GIT_DIR"} = $repo->repo_path();
This looks like it is copied from the existing code but why do we need to do this? I'm struggling to come up with a scenario where "git send-email" can find the repository but the hook cannot.
git-send-email.perl sub validate_patch { · line 2151
Quoted 2 lines
git-send-email.perl +2
-0+ # cannot use git hook run, it closes stdin before forking the hook + open(my $stdin, "|-", $validate_hook) or die("fork: $!");
This passes $validate_hook to the shell to execute which is not what we want as it will split the hook path on whitespace etc. I think it would be better to use "git hook run --to-stdin" (see 0414b3891c (hook: support a --to-stdin=<path> option, 2023-02-08))
Best Wishes
Phillip
Quoted 21 lines
git-send-email.perl +18
-0+ chdir($cwd_save) or die("chdir: $!"); + for my $fn (@files) { + unless (-p $fn) { + $fn = Cwd::abs_path($fn); + $stdin->print("$fn\n"); + } + } + close($stdin); # calls waitpid + if ($? & 0x7f) { + my $sig = $? & 0x7f; + die("fatal: hook $hook_name killed by signal $sig"); + } elsif ($? >> 8) { + my $err = $? >> 8; + die("fatal: hook $hook_name rejected patch series (exit code $err)"); + } + return; +} + sub handle_backup { my ($last, $lastlen, $file, $known_suffix) = @_; my ($suffix, $skip);
Hi Phillip,
Phillip Wood, Apr 03, 2023 at 16:09:
Quoted 5 lines
Quoted 1 line
+ <patch-file> LF
Usually git commands that produce or consume paths either use quoted paths terminated by LF or unquoted paths terminated by NUL. That way there is no ambiguity when a path contains LF.
I had never imagined that some twisted mind would insert LF in path names but since nothing will forbid it, I agree that it is a possibility.
I'm not sure what you mean by quoted paths, you mean adding literal double quotes before printing them to the hook stdin? That means the hook needs to handle de-quoting after reading, right?
Quoted 6 lines
Quoted 6 lines
git-send-email.perl +0
-0@@ -800,6 +800,7 @@ sub is_format_patch_arg { validate_patch($f, $target_xfer_encoding);
Quoted 8 lines
git-send-email.perl sub is_format_patch_arg { · line 803
Quoted 3 lines
git-send-email.perl +1
-0} } + validate_patch_series(@files)This happens fairly early, before the user has had a chance to edit the patches and before we have added all the recipient and in-reply-to headers to the patch files. Would it be more useful to validate what will actually be sent?
I agree that it would be better. I added the check here to be in line with the existing sendemail-validate hook. I could move it after edition and header finalization but then we would need to move sendemail-validate as well for consistency. What do you think?
Quoted 9 lines
git-send-email.perl sub validate_patch { · line 2149
Quoted 5 lines
git-send-email.perl +5
-0+ # The hook needs a correct cwd and GIT_DIR. + require Cwd; + my $cwd_save = Cwd::getcwd(); + chdir($repo->wc_path() or $repo->repo_path()) or die("chdir: $!"); + local $ENV{"GIT_DIR"} = $repo->repo_path();This looks like it is copied from the existing code but why do we need to do this? I'm struggling to come up with a scenario where "git send-email" can find the repository but the hook cannot.
Again, for consistency I assumed it would be best to keep the code similar in both hooks. If you think it is safe to skip that check, I'll remove it gladly.
Quoted 7 lines
git-send-email.perl sub validate_patch { · line 2151
Quoted 2 lines
git-send-email.perl +2
-0+ # cannot use git hook run, it closes stdin before forking the hook + open(my $stdin, "|-", $validate_hook) or die("fork: $!");This passes $validate_hook to the shell to execute which is not what we want as it will split the hook path on whitespace etc. I think it would be better to use "git hook run --to-stdin" (see
0414b3891c(hook:
support a --to-stdin=<path> option, 2023-02-08))
Ah that's a nice addition. I'll add that in v2.
Thanks for reviewing!
Hi Robin
Quoted 9 lines · Robin Jarry
Hi Phillip,
Phillip Wood, Apr 03, 2023 at 16:09:
Quoted 5 lines
Quoted 1 line
+ <patch-file> LF
Usually git commands that produce or consume paths either use quoted paths terminated by LF or unquoted paths terminated by NUL. That way there is no ambiguity when a path contains LF.
Quoted 7 lines
I had never imagined that some twisted mind would insert LF in path names but since nothing will forbid it, I agree that it is a possibility.
I'm not sure what you mean by quoted paths, you mean adding literal double quotes before printing them to the hook stdin? That means the hook needs to handle de-quoting after reading, right?
I meant quoted in the same way that diff and ls-files etc quote paths that contain control characters - see quote_c_style() in quote.c if you're interested in the details. It looks like Git.pm can unquote paths but has no code to quote them. It is probably easiest to use NUL termination here - bash and zsh can read NUL terminated lines and so can any scripting language.
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
git-send-email.perl +0
-0@@ -800,6 +800,7 @@ sub is_format_patch_arg { validate_patch($f, $target_xfer_encoding);Quoted 8 lines
git-send-email.perl sub is_format_patch_arg { · line 803
Quoted 3 lines
git-send-email.perl +1
-0} } + validate_patch_series(@files)This happens fairly early, before the user has had a chance to edit the patches and before we have added all the recipient and in-reply-to headers to the patch files. Would it be more useful to validate what will actually be sent?
Quoted 4 lines
I agree that it would be better. I added the check here to be in line with the existing sendemail-validate hook. I could move it after edition and header finalization but then we would need to move sendemail-validate as well for consistency. What do you think?
That would be my inclination but I'm only an occasional send-email user. The downside is that the user may edit a patch only for it to be rejected but we could offer for them to edit it again rather than just throwing their work away.
Quoted 10 lines
Quoted 9 lines
git-send-email.perl sub validate_patch { · line 2149
Quoted 5 lines
git-send-email.perl +5
-0+ # The hook needs a correct cwd and GIT_DIR. + require Cwd; + my $cwd_save = Cwd::getcwd(); + chdir($repo->wc_path() or $repo->repo_path()) or die("chdir: $!"); + local $ENV{"GIT_DIR"} = $repo->repo_path();This looks like it is copied from the existing code but why do we need to do this? I'm struggling to come up with a scenario where "git send-email" can find the repository but the hook cannot.
Quoted 3 lines
Again, for consistency I assumed it would be best to keep the code similar in both hooks. If you think it is safe to skip that check, I'll remove it gladly.
I suspect it is safe, hopefully someone will speak up if I'm mistaken. A while ago rebase stopped setting these variables when running an "exec" command as they were causing problems (see 434e0636db (sequencer: do not export GIT_DIR and GIT_WORK_TREE for 'exec', 2021-12-04))
Quoted 9 lines
Quoted 7 lines
git-send-email.perl sub validate_patch { · line 2151
Quoted 2 lines
git-send-email.perl +2
-0+ # cannot use git hook run, it closes stdin before forking the hook + open(my $stdin, "|-", $validate_hook) or die("fork: $!");This passes $validate_hook to the shell to execute which is not what we want as it will split the hook path on whitespace etc. I think it would be better to use "git hook run --to-stdin" (see
0414b3891c(hook:
support a --to-stdin=<path> option, 2023-02-08))Ah that's a nice addition. I'll add that in v2.
That's great, Best Wishes
Phillip
Quoted 1 line
Thanks for reviewing!
Quoted 6 lines · Phillip Wood
Quoted 6 lines
diff --git a/git-send-email.perl b/git-send-email.perl
index07f2a0cbeaad..bec4d0f4ab47100755git-send-email.perl +0
-0@@ -800,6 +800,7 @@ sub is_format_patch_arg { validate_patch($f, $target_xfer_encoding);
Quoted 8 lines
git-send-email.perl sub is_format_patch_arg { · line 803
Quoted 3 lines
git-send-email.perl +1
-0} } + validate_patch_series(@files)This happens fairly early, before the user has had a chance to edit the patches and before we have added all the recipient and in-reply-to headers to the patch files. Would it be more useful to validate what will actually be sent?
I actually think the original intent was to catch errors in the part of the file that can mechanically be created before letting the user spend time on editing, without realizing that a later stage will be rejected due to the auto-generated (e.g. came from a commit object) stuff. I do not know why we need another hook to do pretty much the same thing as the existing one (which could be taught to spool and then the last round to validate, in addition to each step rejecting incoming one as needed), but at least calling it there would be very much in line with the existing one, I would say.
Thanks for a careful review.
Junio C Hamano, Apr 03, 2023 at 17:42:
Quoted 5 lines
I do not know why we need another hook to do pretty much the same thing as the existing one (which could be taught to spool and then the last round to validate, in addition to each step rejecting incoming one as needed), but at least calling it there would be very much in line with the existing one, I would say.
If for example the validation would require trying to apply patches on top of another branch in a temp repository, you would need to know the number of patches and be able to determine whether you need to reset the branch (patch 1/N) before applying. For that you would need to parse the contents of the patches. This is not the end of the world but I assumed that it would be easier to handle with a hook that fires once with all patch files.
Another option would be to change sendemail-validate to be called only once with all patches. That would be the ideal solution since the existing hook is not always usable with series. But that would be a breaking change. I personally don't mind a small breakage like this but I don't know what is the project's policy.
Phillip Wood, Apr 03, 2023 at 16:09:
Quoted 3 lines
Usually git commands that produce or consume paths either use quoted paths terminated by LF or unquoted paths terminated by NUL. That way there is no ambiguity when a path contains LF.
Thinking again about that. The probability that a file path name generated by git-format-patch would contain LF is close to zero. However, reading per line is more natural and more in line with other hooks that read from stdin. Having that single hook separating stuff in stdin with NUL bytes is weird from a user point of view. Don't you think it would be an acceptable limitation?
Quoted 2 lines · Robin Jarry
Thinking again about that. The probability that a file path name generated by git-format-patch would contain LF is close to zero.
Close to zero is very different from absolutely zero, and in the case of format-patch generated patches, I think it is absolutely zero. At least, that was the case back when I designed and implemented it, and I do not think I accepted a patch to break it over the years.
But "git send-email" can be fed a list of files and even a directory (and enumerate files in it). The filenames are under end-users' control in this case, so "close to zero" has absolutely no relevance. If the end user means to feed you such a file, they can do so 100% of the time.
If we support such a file is a different issue. A good rule of thumb to decide if it is reasonable is to see if the main command already works with such filenames, e.g.
$ git format-patch -2
0001-foo.txt
0002-bar.txt
$ mv 0001-foo.txt '0001-foQuoted 1 line
o.txt'
$ mkdir dir
$ mv 000[12]*.txt dir/.may prepare two patch files that can be sent via send-email. One file (the first one) is deliberately given a filename with LF in it. Does send-email work on it correctly if you did e.g.
$ git send-email dir/000[12]*.txt
or something silly like
$ git send-email dir
or does it already choke on the first file because of the filename?
Junio C Hamano, Apr 04, 2023 at 00:52:
Quoted 11 lines
Close to zero is very different from absolutely zero, and in the case of format-patch generated patches, I think it is absolutely zero. At least, that was the case back when I designed and implemented it, and I do not think I accepted a patch to break it over the years.
But "git send-email" can be fed a list of files and even a directory (and enumerate files in it). The filenames are under end-users' control in this case, so "close to zero" has absolutely no relevance. If the end user means to feed you such a file, they can do so 100% of the time.
Ok that's a fair point. Even though I am having a hard time believing someone would do such a thing :D
Quoted 16 lines
If we support such a file is a different issue. A good rule of thumb to decide if it is reasonable is to see if the main command already works with such filenames, e.g.
$ git format-patch -2 0001-foo.txt 0002-bar.txt $ mv 0001-foo.txt '0001-foQuoted 1 line
o.txt'
$ mkdir dir $ mv 000[12]*.txt dir/.may prepare two patch files that can be sent via send-email. One file (the first one) is deliberately given a filename with LF in it. Does send-email work on it correctly if you did e.g.
Quoted 7 lines
$ git send-email dir/000[12]*.txtor something silly like
$ git send-email diror does it already choke on the first file because of the filename?
It seems to work with both. I guess, NUL bytes separation it is then...
Quoted 11 lines · Robin Jarry
Quoted 9 lines
it. Does send-email work on it correctly if you did e.g.
$ git send-email dir/000[12]*.txtor something silly like
$ git send-email diror does it already choke on the first file because of the filename?
It seems to work with both. I guess, NUL bytes separation it is then...
Feeding the filenames as the command line arguments would have been much simpler X-<, but either NUL termination or c-quoting the filenames would be needed _if_ we want to support crazy folks who feed us such garbage filenames. Letting hook scripts understand NUL termination is a chore and it still is debatable if it is reasonable to support, though. I'd say it would be sufficient to just declare "files whose name has LF in it is not given to the hook, ever" and users would avoid such a filename if they care.
Thanks.
Junio C Hamano, Apr 04, 2023 at 22:14:
Quoted 2 lines
Feeding the filenames as the command line arguments would have been much simpler X-<,
Just a thought. Instead of adding another hook, wouldn't it be better to add an option --validate-series (git config sendemail.validateSeries) that would change the behaviour of the sendemail-validate hook? Calling it with all patch files as command line arguments only once instead of once per file.
I know I was concerned with the max size of the command line args but is there really a chance that we hit that maximum? On my system, it is 2097152 bytes. Even with a 1000 patches series with 1000 bytes filenames, we wouldn't hit the limit.
This way, we support any crap filename that the user may send and we don't add a new hook which basically does the same thing than the existing one.
Thoughts?
Quoted 5 lines · Robin Jarry
Just a thought. Instead of adding another hook, wouldn't it be better to add an option --validate-series (git config sendemail.validateSeries) that would change the behaviour of the sendemail-validate hook? Calling it with all patch files as command line arguments only once instead of once per file.
I do not see much upside in doing so. Instead of having to write a new validate-series hook script to store it under a new name, users can update an existing validate-patch hook script to take a batch of patches in one go. Then the user now has to set a new configuration variable. Because the expected way to feed the hook script is *not* per invocation but depends solely on how the script is written, a command line option would not make much sense. Not having to rename the updated validate-patch script to validate-series might be a small win, but I do not think it is a compelling reason to take that approach.
Quoted 8 lines
I know I was concerned with the max size of the command line args but is there really a chance that we hit that maximum? On my system, it is 2097152 bytes. Even with a 1000 patches series with 1000 bytes filenames, we wouldn't hit the limit.
This way, we support any crap filename that the user may send and we don't add a new hook which basically does the same thing than the existing one.
Between "we may exceed command line argument limit" (which by the way is way lower on certain systems than what you expect, IIRC) and "the user may throw us a file with LF in its name", I'd find it simpler to punt on the latter and tell them "if it hurts, don't do it". As long as the limitation is clearly documented, I'd say it is OK.
Thanks.
PATCH v2 1/1 hooks: add sendemail-validate-series
In reply to Robin JarryWhen 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.
---
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(+)
Documentation/git-send-email.txt +1 -0
@@ -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;
Documentation/githooks.txt +19 -0
@@ -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 ~~~~~~~~~~~~~~~~~~
git-send-email.perl +36 -0
@@ -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);
Signature · 1 line
2.40.0
Quoted 16 lines · Robin Jarry
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 <[email protected]>---
Quoted 7 lines
Notes:
v1 -> v2:- Use
git hook run --to-stdinwith a temp file.
- Skip validation (with an explicit warning) for patch file names that contain newline characters.
- Updated docs.
At first glance I thought this was a re-roll of the patches to include the headers in the output, but I see that's a *different* series:
https://lore.kernel.org/git/[email protected]/
But that was waiting on the --to-stdin I recently added, which you're using here (good!).
But it seems to me that if that's integrated we'd end up with yet another interface, or not? Is this proposing that we use this interface instead for that use-case?
Quoted 67 lines
Documentation/git-send-email.txt | 1 + Documentation/githooks.txt | 19 +++++++++++++++++ git-send-email.perl | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+)Documentation/git-send-email.txt +1
-0@@ -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;Documentation/githooks.txt +19
-0@@ -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 ~~~~~~~~~~~~~~~~~~git-send-email.perl +5
-0@@ -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) {
git-send-email.perl sub validate_patch { · line 2142
Quoted 10 lines
git-send-email.perl +10
-0+ 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);
The code you mostly copy/pasted does "require Cwd", but can't we just combine this with the existing function somehow?
git-send-email.perl sub validate_patch { · line 2148
Quoted 6 lines
git-send-email.perl +6
-0+ 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"); + }
Re feedback from others, I think *if* we keep this we should pass this \0-delimited, that delimiter can be safley used with POSIX filenames.
Quoted 8 lines
git-send-email.perl +8
-0+ } + } + 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(
git-send-email.perl sub validate_patch { · line 2166
Quoted 10 lines
git-send-email.perl +7
-0+ __("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);
Honestly, I don't really get the use-case. If your 02/N depends on 01/N couldn't your hook just maintain its own state, e.g. in some file created in the passed $GIT_DIR?
With the upcoming parallel hooks, I'm also skeptical of a an interface that would preclude validating these in parallel.
I also don't understand the reason for the stdin interface. The "git-send-email" program itself takes a <file|directory>, so concerns about the files exceeding argument list seem out the window, i.e. we could just pass the dir/files, and as we'd have the same limitations here we should be able to pass the full set of files, no?
I.e. why not a sendemail-validate-all that just takes a dir or file(s)?
Quoted 6 lines · Ævar Arnfjörð Bjarmason
On Thu, Apr 06 2023, Robin Jarry wrote:
Honestly, I don't really get the use-case. If your 02/N depends on 01/N couldn't your hook just maintain its own state, e.g. in some file created in the passed $GIT_DIR?
A hook that wants to check some property of the whole series needs to know which patch is the final one. We could pass that via the environment as we do for external diff commands with GIT_DIFF_PATH_COUNTER and GIT_DIFF_PATH_TOTAL.
Quoted 2 lines
With the upcoming parallel hooks, I'm also skeptical of a an interface that would preclude validating these in parallel.
I'd not thought of that, I thought the idea of parallel hooks was to run different scripts for the same hook in parallel, not have multiple instances of the same script running simultaneously.
Quoted 5 lines
I also don't understand the reason for the stdin interface. The "git-send-email" program itself takes a <file|directory>, so concerns about the files exceeding argument list seem out the window, i.e. we could just pass the dir/files, and as we'd have the same limitations here we should be able to pass the full set of files, no?
No, not if the user passes something like "HEAD~1000.." instead of a list of paths.
Best Wishes
Phillip
Quoted 1 line
I.e. why not a sendemail-validate-all that just takes a dir or file(s)?
Phillip Wood, Apr 11, 2023 at 11:58:
Quoted 4 lines
A hook that wants to check some property of the whole series needs to know which patch is the final one. We could pass that via the environment as we do for external diff commands with
GIT_DIFF_PATH_COUNTERandGIT_DIFF_PATH_TOTAL.
That may be an appropriate solution and it would avoid adding another hook. And it would solve the issue of "\n" in filenames.
The only downside is that you would need to store state in an external file (maybe in GIT_DIR) so that successive calls of the hook script can pick up where the previous invocation ended.
It all comes down to ergonomics at this point. I don't mind either solutions as long as validating whole series is possible before sending emails.
Quoted 4 lines · Phillip Wood
A hook that wants to check some property of the whole series needs to know which patch is the final one. We could pass that via the environment as we do for external diff commands with
GIT_DIFF_PATH_COUNTERandGIT_DIFF_PATH_TOTAL.
Ahh, I forgot that we added them to deal with "I am called repeatedly, where is the end of the series of calls?" question, which exactly is the same issue. Glad that you brought it up.
Thanks.