BF: process included files before the rest by yarikoptic · Pull Request #700 · gitpython-developers/GitPython
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) hpd = osp.dirname(hp) if not osp.isdir(hpd): os.mkdir(hpd) with open(hp, "wt") as fp: fp.write(HOOKS_SHEBANG + content) if make_exec: os.chmod(hp, 0o744) return hp
class TestIndex(TestBase):
@with_rw_repo('HEAD', bare=True) def test_pre_commit_hook_fail(self, rw_repo): index = rw_repo.index hp = hook_path('pre-commit', index.repo.git_dir) hpd = osp.dirname(hp) if not osp.isdir(hpd): os.mkdir(hpd) with open(hp, "wt") as fp: fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1") os.chmod(hp, 0o744) hp = _make_hook( index.repo.git_dir, 'pre-commit', "echo stdout; echo stderr 1>&2; exit 1" ) try: index.commit("This should fail") except HookExecutionError as err:
@with_rw_repo('HEAD', bare=True) def test_commit_msg_hook_success(self, rw_repo): index = rw_repo.index commit_message = u"commit default head by Frèderic Çaufl€" from_hook_message = u"from commit-msg"
hp = hook_path('commit-msg', index.repo.git_dir) hpd = osp.dirname(hp) if not osp.isdir(hpd): os.mkdir(hpd) with open(hp, "wt") as fp: fp.write('#!/usr/bin/env sh\necho -n " {}" >> "$1"'.format(from_hook_message)) os.chmod(hp, 0o744)
index = rw_repo.index _make_hook( index.repo.git_dir, 'commit-msg', 'echo -n " {0}" >> "$1"'.format(from_hook_message) ) new_commit = index.commit(commit_message) self.assertEqual(new_commit.message, u"{} {}".format(commit_message, from_hook_message)) self.assertEqual(new_commit.message, u"{0} {1}".format(commit_message, from_hook_message))
@with_rw_repo('HEAD', bare=True) def test_commit_msg_hook_fail(self, rw_repo): index = rw_repo.index hp = hook_path('commit-msg', index.repo.git_dir) hpd = osp.dirname(hp) if not osp.isdir(hpd): os.mkdir(hpd) with open(hp, "wt") as fp: fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1") os.chmod(hp, 0o744) hp = _make_hook( index.repo.git_dir, 'commit-msg', "echo stdout; echo stderr 1>&2; exit 1" ) try: index.commit("This should fail") except HookExecutionError as err: