send-email: check for repo before invoking hook
authorJonathan Tan <jonathantanmy@google.com>
Thu, 1 Jun 2017 23:50:55 +0000 (16:50 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 2 Jun 2017 01:58:25 +0000 (10:58 +0900)
Unless --no-validate is passed, send-email will invoke
$repo->repo_path() in its search for a validate hook regardless of
whether a Git repo is actually present. Teach send-email to first check
for repo existence.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-send-email.perl
t/t9001-send-email.sh
index c314cc2b51e363defcd216391df1ef6dd5897f16..fa559cca3fe846eabe1853a38f0a82e5c8b7706e 100755 (executable)
@@ -1739,21 +1739,23 @@ sub unique_email_list {
 sub validate_patch {
        my $fn = shift;
 
-       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;
+       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, $!);
index 15128c755a4eb820b9735e1a94616a694b1b8054..d1e4e8ad19f3d3e005e177b98e4eee16f4ef1461 100755 (executable)
@@ -1953,4 +1953,12 @@ test_expect_success $PREREQ 'invoke hook' '
        )
 '
 
+test_expect_success $PREREQ 'test that send-email works outside a repo' '
+       nongit git send-email \
+               --from="Example <nobody@example.com>" \
+               --to=nobody@example.com \
+               --smtp-server="$(pwd)/fake.sendmail" \
+               "$(pwd)/0001-add-master.patch"
+'
+
 test_done