/usr/share/pyshared/dulwich/hooks.py is in python-dulwich 0.9.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | # hooks.py -- for dealing with git hooks
# Copyright (C) 2012-2013 Jelmer Vernooij and others.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License or (at your option) a later version of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
"""Access to hooks."""
import os
import subprocess
import tempfile
from dulwich.errors import (
HookError,
)
class Hook(object):
"""Generic hook object."""
def execute(self, *args):
"""Execute the hook with the given args
:param args: argument list to hook
:raise HookError: hook execution failure
:return: a hook may return a useful value
"""
raise NotImplementedError(self.execute)
class ShellHook(Hook):
"""Hook by executable file
Implements standard githooks(5) [0]:
[0] http://www.kernel.org/pub/software/scm/git/docs/githooks.html
"""
def __init__(self, name, path, numparam,
pre_exec_callback=None, post_exec_callback=None):
"""Setup shell hook definition
:param name: name of hook for error messages
:param path: absolute path to executable file
:param numparam: number of requirements parameters
:param pre_exec_callback: closure for setup before execution
Defaults to None. Takes in the variable argument list from the
execute functions and returns a modified argument list for the
shell hook.
:param post_exec_callback: closure for cleanup after execution
Defaults to None. Takes in a boolean for hook success and the
modified argument list and returns the final hook return value
if applicable
"""
self.name = name
self.filepath = path
self.numparam = numparam
self.pre_exec_callback = pre_exec_callback
self.post_exec_callback = post_exec_callback
def execute(self, *args):
"""Execute the hook with given args"""
if len(args) != self.numparam:
raise HookError("Hook %s executed with wrong number of args. \
Expected %d. Saw %d. %s"
% (self.name, self.numparam, len(args)))
if (self.pre_exec_callback is not None):
args = self.pre_exec_callback(*args)
try:
ret = subprocess.call([self.filepath] + list(args))
if ret != 0:
if (self.post_exec_callback is not None):
self.post_exec_callback(0, *args)
raise HookError("Hook %s exited with non-zero status"
% (self.name))
if (self.post_exec_callback is not None):
return self.post_exec_callback(1, *args)
except OSError: # no file. silent failure.
if (self.post_exec_callback is not None):
self.post_exec_callback(0, *args)
class PreCommitShellHook(ShellHook):
"""pre-commit shell hook"""
def __init__(self, controldir):
filepath = os.path.join(controldir, 'hooks', 'pre-commit')
ShellHook.__init__(self, 'pre-commit', filepath, 0)
class PostCommitShellHook(ShellHook):
"""post-commit shell hook"""
def __init__(self, controldir):
filepath = os.path.join(controldir, 'hooks', 'post-commit')
ShellHook.__init__(self, 'post-commit', filepath, 0)
class CommitMsgShellHook(ShellHook):
"""commit-msg shell hook
:param args[0]: commit message
:return: new commit message or None
"""
def __init__(self, controldir):
filepath = os.path.join(controldir, 'hooks', 'commit-msg')
def prepare_msg(*args):
(fd, path) = tempfile.mkstemp()
f = os.fdopen(fd, 'wb')
try:
f.write(args[0])
finally:
f.close()
return (path,)
def clean_msg(success, *args):
if success:
f = open(args[0], 'rb')
try:
new_msg = f.read()
finally:
f.close()
os.unlink(args[0])
return new_msg
os.unlink(args[0])
ShellHook.__init__(self, 'commit-msg', filepath, 1,
prepare_msg, clean_msg)
|