Add support for commit-msg git hook

This commit is contained in:
Anthony Sottile
2017-07-22 15:16:12 -07:00
parent a6a4762f0d
commit d0b268c813
9 changed files with 107 additions and 57 deletions

View File

@@ -56,16 +56,21 @@ def install(
with io.open(hook_path, 'w') as pre_commit_file_obj:
if hook_type == 'pre-push':
with io.open(resource_filename('pre-push-tmpl')) as fp:
pre_push_contents = fp.read()
with io.open(resource_filename('pre-push-tmpl')) as f:
hook_specific_contents = f.read()
elif hook_type == 'commit-msg':
with io.open(resource_filename('commit-msg-tmpl')) as f:
hook_specific_contents = f.read()
elif hook_type == 'pre-commit':
hook_specific_contents = ''
else:
pre_push_contents = ''
raise AssertionError('Unknown hook type: {}'.format(hook_type))
skip_on_missing_conf = 'true' if skip_on_missing_conf else 'false'
contents = io.open(resource_filename('hook-tmpl')).read().format(
sys_executable=sys.executable,
hook_type=hook_type,
pre_push=pre_push_contents,
hook_specific=hook_specific_contents,
skip_on_missing_conf=skip_on_missing_conf,
)
pre_commit_file_obj.write(contents)

View File

@@ -58,6 +58,9 @@ def get_filenames(args, include_expr, exclude_expr):
getter = git.get_files_matching(
lambda: get_changed_files(args.origin, args.source),
)
elif args.hook_stage == 'commit-msg':
def getter(*_):
return (args.commit_msg_filename,)
elif args.files:
getter = git.get_files_matching(lambda: args.files)
elif args.all_files:

View File

@@ -76,7 +76,7 @@ def main(argv=None):
),
)
install_parser.add_argument(
'-t', '--hook-type', choices=('pre-commit', 'pre-push'),
'-t', '--hook-type', choices=('pre-commit', 'pre-push', 'commit-msg'),
default='pre-commit',
)
install_parser.add_argument(
@@ -149,6 +149,10 @@ def main(argv=None):
'--source', '-s',
help="The remote branch's commit_id when using `git push`.",
)
run_parser.add_argument(
'--commit-msg-filename',
help='Filename to check when running during `commit-msg`',
)
run_parser.add_argument(
'--allow-unstaged-config', default=False, action='store_true',
help=(
@@ -157,7 +161,8 @@ def main(argv=None):
),
)
run_parser.add_argument(
'--hook-stage', choices=('commit', 'push'), default='commit',
'--hook-stage', choices=('commit', 'push', 'commit-msg'),
default='commit',
help='The stage during which the hook is fired e.g. commit or push.',
)
run_parser.add_argument(

View File

@@ -0,0 +1 @@
args="run --hook-stage=commit-msg --commit-msg-filename=$1"

View File

@@ -52,7 +52,7 @@ if [ ! -f $CONF_FILE ]; then
fi
fi
{pre_push}
{hook_specific}
# Run pre-commit
if ((WHICH_RETV == 0)); then