send-email: check for repo before invoking hook
[gitweb.git] / git-send-email.perl
index eea0a517f71b66f861db38f70fac0593f8c8cea4..fa559cca3fe846eabe1853a38f0a82e5c8b7706e 100755 (executable)
@@ -25,8 +25,9 @@
 use Text::ParseWords;
 use Term::ANSIColor;
 use File::Temp qw/ tempdir tempfile /;
-use File::Spec::Functions qw(catfile);
+use File::Spec::Functions qw(catdir catfile);
 use Error qw(:try);
+use Cwd qw(abs_path cwd);
 use Git;
 use Git::I18N;
 
@@ -1737,6 +1738,25 @@ sub unique_email_list {
 
 sub validate_patch {
        my $fn = shift;
+
+       if ($repo) {
+               my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
+                                           'sendemail-validate');
+               my $hook_error;
+               if (-x $validate_hook) {
+                       my $target = abs_path($fn);
+                       # The hook needs a correct cwd and GIT_DIR.
+                       my $cwd_save = cwd();
+                       chdir($repo->wc_path() or $repo->repo_path())
+                               or die("chdir: $!");
+                       local $ENV{"GIT_DIR"} = $repo->repo_path();
+                       $hook_error = "rejected by sendemail-validate hook"
+                               if system($validate_hook, $target);
+                       chdir($cwd_save) or die("chdir: $!");
+               }
+               return $hook_error if $hook_error;
+       }
+
        open(my $fh, '<', $fn)
                or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
        while (my $line = <$fh>) {