◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
15 changes: 14 additions & 1 deletion git/index/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# NOTE: Autodoc hates it if this is a docstring

from io import BytesIO
import os
from stat import (
S_IFDIR,
Expand All @@ -21,6 +22,7 @@
force_text,
force_bytes,
is_posix,
safe_decode,
)
from git.exc import (
Expand Down Expand Up @@ -76,6 +78,10 @@ def hook_path(name: str, git_dir: PathLike) -> str:
return osp.join(git_dir, 'hooks', name)


def run_commit_hook(name: str, index: 'IndexFile', *args: str) -> None:
"""Run the commit hook of the given name. Silently ignores hooks that do not exist.
:param name: name of hook, like 'pre-commit'
Expand All @@ -89,8 +95,15 @@ def run_commit_hook(name: str, index: 'IndexFile', *args: str) -> None:
env = os.environ.copy()
env['GIT_INDEX_FILE'] = safe_decode(str(index.path))
env['GIT_EDITOR'] = ':'
try:
cmd = subprocess.Popen([hp] + list(args),
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand Down
9 changes: 6 additions & 3 deletions test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)
import tempfile
from unittest import skipIf

from git import (
IndexFile,
Expand Down Expand Up @@ -52,8 +53,9 @@

HOOKS_SHEBANG = "#!/usr/bin/env sh\n"


@skipIf(HIDE_WINDOWS_KNOWN_ERRORS, "TODO: fix hooks execution on Windows: #703")
def _make_hook(git_dir, name, content, make_exec=True):
"""A helper to create a hook"""
hp = hook_path(name, git_dir)
Expand Down Expand Up @@ -881,7 +883,7 @@ def test_pre_commit_hook_fail(self, rw_repo):
try:
index.commit("This should fail")
except HookExecutionError as err:
if is_win:
self.assertIsInstance(err.status, OSError)
self.assertEqual(err.command, [hp])
self.assertEqual(err.stdout, '')
Expand All @@ -896,6 +898,7 @@ def test_pre_commit_hook_fail(self, rw_repo):
else:
raise AssertionError("Should have caught a HookExecutionError")

@with_rw_repo('HEAD', bare=True)
def test_commit_msg_hook_success(self, rw_repo):
commit_message = "commit default head by Frèderic Çaufl€"
Expand All @@ -920,7 +923,7 @@ def test_commit_msg_hook_fail(self, rw_repo):
try:
index.commit("This should fail")
except HookExecutionError as err:
if is_win:
self.assertIsInstance(err.status, OSError)
self.assertEqual(err.command, [hp])
self.assertEqual(err.stdout, '')
Expand Down
Toggle all file notes Toggle all file annotations