From: Junio C Hamano Date: Wed, 10 May 2023 17:23:28 +0000 (-0700) Subject: Merge branch 'ms/send-email-feed-header-to-validate-hook' X-Git-Tag: v2.41.0-rc0~30 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b6e9521956b752be4c666efedd7b91bdd05f9756;p=thirdparty%2Fgit.git Merge branch 'ms/send-email-feed-header-to-validate-hook' "git send-email" learned to give the e-mail headers to the validate hook by passing an extra argument from the command line. * ms/send-email-feed-header-to-validate-hook: send-email: expose header information to git-send-email's sendemail-validate hook send-email: refactor header generation functions --- b6e9521956b752be4c666efedd7b91bdd05f9756 diff --cc Documentation/githooks.txt index c8e55b2613,9896ffafaf..86f804720a --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@@ -595,33 -595,30 +595,52 @@@ processed by rebase sendemail-validate ~~~~~~~~~~~~~~~~~~ - This hook is invoked by linkgit:git-send-email[1]. It takes a single parameter, - 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. + This hook is invoked by linkgit:git-send-email[1]. + + It takes these command line arguments. They are, + 1. the name of the file which holds the contents of the email to be sent. + 2. The name of the file which holds the SMTP headers of the email. + + The SMTP headers are passed in the exact same way as they are passed to the + user's Mail Transport Agent (MTA). In effect, the email given to the user's + MTA, is the contents of $2 followed by the contents of $1. + + An example of a few common headers is shown below. Take notice of the + capitalization and multi-line tab structure. + + From: Example + To: to@example.com + Cc: cc@example.com, + A , + One , + two@example.com + Subject: PATCH-STRING + + Exiting with a non-zero status causes `git send-email` to abort + before sending any e-mails. +The following environment variables are set when executing the hook. + +`GIT_SENDEMAIL_FILE_COUNTER`:: + A 1-based counter incremented by one for every file holding an e-mail + to be sent (excluding any FIFOs). This counter does not follow the + patch series counter scheme. It will always start at 1 and will end at + GIT_SENDEMAIL_FILE_TOTAL. + +`GIT_SENDEMAIL_FILE_TOTAL`:: + The total number of files that will be sent (excluding any FIFOs). This + counter does not follow the patch series counter scheme. It will always + be equal to the number of files being sent, whether there is a cover + letter or not. + +These variables may for instance be used to validate patch series. + +The sample `sendemail-validate` hook that comes with Git checks that all sent +patches (excluding the cover letter) can be applied on top of the upstream +repository default branch without conflicts. Some placeholders are left for +additional validation steps to be performed after all patches of a given series +have been applied. + fsmonitor-watchman ~~~~~~~~~~~~~~~~~~ diff --cc git-send-email.perl index 66c9171109,10c450ef68..26e18eebd9 --- a/git-send-email.perl +++ b/git-send-email.perl @@@ -792,31 -792,31 +792,46 @@@ if (@rev_list_opts) @rev_list_opts); } - @files = handle_backup_files(@files); + if (defined $sender) { + $sender =~ s/^\s+|\s+$//g; + ($sender) = expand_aliases($sender); + } else { + $sender = $repoauthor->() || $repocommitter->() || ''; + } + + # $sender could be an already sanitized address + # (e.g. sendemail.from could be manually sanitized by user). + # But it's a no-op to run sanitize_address on an already sanitized address. + $sender = sanitize_address($sender); + + $time = time - scalar $#files; if ($validate) { + # FIFOs can only be read once, exclude them from validation. + my @real_files = (); foreach my $f (@files) { unless (-p $f) { - pre_process_file($f, 1); - validate_patch($f, $target_xfer_encoding); + push(@real_files, $f); } } + + # Run the loop once again to avoid gaps in the counter due to FIFO + # arguments provided by the user. + my $num = 1; + my $num_files = scalar @real_files; + $ENV{GIT_SENDEMAIL_FILE_TOTAL} = "$num_files"; + foreach my $r (@real_files) { + $ENV{GIT_SENDEMAIL_FILE_COUNTER} = "$num"; ++ pre_process_file($r, 1); + validate_patch($r, $target_xfer_encoding); + $num += 1; + } + delete $ENV{GIT_SENDEMAIL_FILE_COUNTER}; + delete $ENV{GIT_SENDEMAIL_FILE_TOTAL}; } + @files = handle_backup_files(@files); + if (@files) { unless ($quiet) { print $_,"\n" for (@files);