send-email: check for repo before invoking hook

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>

Jonathan Tan committed Jun 1, 2017 at 16:50 UTC 177409e5897988f03e0c8111c94db6ea0466b138
2 files changed +25 -15
git-send-email.perl
+17 -15
@@ -1739,21 +1739,23 @@ sub unique_email_list {
1739 sub validate_patch {
1740 my $fn = shift;
1741
1742 - my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1743 - 'sendemail-validate');
1744 - my $hook_error;
1745 - if (-x $validate_hook) {
1746 - my $target = abs_path($fn);
1747 - # The hook needs a correct cwd and GIT_DIR.
1748 - my $cwd_save = cwd();
1749 - chdir($repo->wc_path() or $repo->repo_path())
1750 - or die("chdir: $!");
1751 - local $ENV{"GIT_DIR"} = $repo->repo_path();
1752 - $hook_error = "rejected by sendemail-validate hook"
1753 - if system($validate_hook, $target);
1754 - chdir($cwd_save) or die("chdir: $!");
1755 - }
1756 - return $hook_error if $hook_error;
1742 + if ($repo) {
1743 + my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1744 + 'sendemail-validate');
1745 + my $hook_error;
1746 + if (-x $validate_hook) {
1747 + my $target = abs_path($fn);
1748 + # The hook needs a correct cwd and GIT_DIR.
1749 + my $cwd_save = cwd();
1750 + chdir($repo->wc_path() or $repo->repo_path())
1751 + or die("chdir: $!");
1752 + local $ENV{"GIT_DIR"} = $repo->repo_path();
1753 + $hook_error = "rejected by sendemail-validate hook"
1754 + if system($validate_hook, $target);
1755 + chdir($cwd_save) or die("chdir: $!");
1756 + }
1757 + return $hook_error if $hook_error;
1758 + }
1759
1760 open(my $fh, '<', $fn)
1761 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
t/t9001-send-email.sh
+8
@@ -1953,4 +1953,12 @@ test_expect_success $PREREQ 'invoke hook' '
1953 )
1954 '
1955
1956 +test_expect_success $PREREQ 'test that send-email works outside a repo' '
1957 + nongit git send-email \
1958 + --from="Example <nobody@example.com>" \
1959 + --to=nobody@example.com \
1960 + --smtp-server="$(pwd)/fake.sendmail" \
1961 + "$(pwd)/0001-add-master.patch"
1962 +'
1963 +
1964 test_done