def _print_user_skipped(hook, write, args): write(get_hook_message( _hook_msg_start(hook, args.verbose), end_msg='Skipped', end_color=color.YELLOW, use_color=args.color, ))
def _print_no_files_skipped(hook, write, args): write(get_hook_message( _hook_msg_start(hook, args.verbose), postfix='(no files to check) ', end_msg='Skipped', end_color=color.TURQUOISE, use_color=args.color, ))
def test_case_with_end_msg_using_color(): ret = get_hook_message( 'start', end_msg='end', end_color=color.RED, use_color=True, cols=15, ) assert ret == 'start' + '.' * 6 + color.RED + 'end' + color.NORMAL + '\n'
def test_case_with_end_msg(): ret = get_hook_message( 'start', end_msg='end', end_color='', use_color=False, cols=15, ) assert ret == 'start' + '.' * 6 + 'end' + '\n'
def test_case_with_postfix_message(): ret = get_hook_message( 'start', postfix='post ', end_msg='end', end_color='', use_color=False, cols=20, ) assert ret == 'start' + '.' * 6 + 'post ' + 'end' + '\n'
def _run_single_hook(hook, repo, args, write, skips=frozenset()): filenames = get_filenames(args, hook['files'], hook['exclude']) if hook['id'] in skips: _print_user_skipped(hook, write, args) return 0 elif not filenames and not hook['always_run']: _print_no_files_skipped(hook, write, args) return 0 # Print the hook and the dots first in case the hook takes hella long to # run. write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6)) sys.stdout.flush() diff_before = cmd_output('git', 'diff', retcode=None, encoding=None) retcode, stdout, stderr = repo.run_hook(hook, tuple(filenames)) diff_after = cmd_output('git', 'diff', retcode=None, encoding=None) file_modifications = diff_before != diff_after # If the hook makes changes, fail the commit if file_modifications: retcode = 1 if retcode: retcode = 1 print_color = color.RED pass_fail = 'Failed' else: retcode = 0 print_color = color.GREEN pass_fail = 'Passed' write(color.format_color(pass_fail, print_color, args.color) + '\n') if (stdout or stderr or file_modifications) and (retcode or args.verbose): write('hookid: {0}\n'.format(hook['id'])) write('\n') # Print a message if failing due to file modifications if file_modifications: write('Files were modified by this hook.') if stdout or stderr: write(' Additional output:\n') write('\n') for output in (stdout, stderr): assert type(output) is bytes, type(output) if output.strip(): write(output.strip() + b'\n') write('\n') return retcode
def _run_single_hook(hook, repo, args, write, skips=frozenset()): filenames = get_filenames(args, hook['files'], hook['exclude']) if hook['id'] in skips: _print_user_skipped(hook, write, args) return 0 elif not filenames: _print_no_files_skipped(hook, write, args) return 0 # Print the hook and the dots first in case the hook takes hella long to # run. write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6)) sys.stdout.flush() diff_before = cmd_output('git', 'diff', retcode=None, encoding=None) retcode, stdout, stderr = repo.run_hook(hook, filenames) diff_after = cmd_output('git', 'diff', retcode=None, encoding=None) file_modifications = diff_before != diff_after # If the hook makes changes, fail the commit if file_modifications: retcode = 1 if retcode: retcode = 1 print_color = color.RED pass_fail = 'Failed' else: retcode = 0 print_color = color.GREEN pass_fail = 'Passed' write(color.format_color(pass_fail, print_color, args.color) + '\n') if (stdout or stderr or file_modifications) and (retcode or args.verbose): write('hookid: {0}\n'.format(hook['id'])) write('\n') # Print a message if failing due to file modifications if file_modifications: write('Files were modified by this hook.') if stdout or stderr: write(' Additional output:\n') write('\n') for output in (stdout, stderr): assert type(output) is bytes, type(output) if output.strip(): write(output.strip() + b'\n') write('\n') return retcode
def test_make_sure_postfix_is_not_colored(): ret = get_hook_message( 'start', postfix='post ', end_msg='end', end_color=color.RED, use_color=True, cols=20, ) assert ret == ('start' + '.' * 6 + 'post ' + color.RED + 'end' + color.NORMAL + '\n')
def test_make_sure_postfix_is_not_colored(): ret = get_hook_message( 'start', postfix='post ', end_msg='end', end_color=color.RED, use_color=True, cols=20, ) assert ret == ( 'start' + '.' * 6 + 'post ' + color.RED + 'end' + color.NORMAL + '\n' )
def _run_single_hook(runner, repository, hook, args, write, skips=set()): if args.origin and args.source: get_filenames = git.get_files_matching( lambda: get_changed_files(args.origin, args.source), ) elif args.files: get_filenames = git.get_files_matching(lambda: args.files) elif args.all_files: get_filenames = git.get_all_files_matching elif git.is_in_merge_conflict(): get_filenames = git.get_conflicted_files_matching else: get_filenames = git.get_staged_files_matching filenames = get_filenames(hook['files'], hook['exclude']) if hook['id'] in skips: _print_user_skipped(hook, write, args) return 0 elif not filenames: _print_no_files_skipped(hook, write, args) return 0 # Print the hook and the dots first in case the hook takes hella long to # run. write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6)) sys.stdout.flush() retcode, stdout, stderr = repository.run_hook(hook, filenames) if retcode != hook['expected_return_value']: retcode = 1 print_color = color.RED pass_fail = 'Failed' else: retcode = 0 print_color = color.GREEN pass_fail = 'Passed' write(color.format_color(pass_fail, print_color, args.color) + '\n') if (stdout or stderr) and (retcode or args.verbose): write('hookid: {0}\n'.format(hook['id'])) write('\n') for output in (stdout, stderr): if output.strip(): write(output.strip() + '\n') write('\n') return retcode
def _run_single_hook(runner, repository, hook, args, write, skips=set()): if args.all_files: get_filenames = git.get_all_files_matching elif git.is_in_merge_conflict(): get_filenames = git.get_conflicted_files_matching else: get_filenames = git.get_staged_files_matching filenames = get_filenames(hook['files'], hook['exclude']) if hook['id'] in skips: _print_user_skipped(hook, write, args) return 0 elif not filenames: _print_no_files_skipped(hook, write, args) return 0 # Print the hook and the dots first in case the hook takes hella long to # run. write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6)) sys.stdout.flush() retcode, stdout, stderr = repository.run_hook(hook, filenames) if retcode != hook['expected_return_value']: retcode = 1 print_color = color.RED pass_fail = 'Failed' else: retcode = 0 print_color = color.GREEN pass_fail = 'Passed' write(color.format_color(pass_fail, print_color, args.color) + '\n') if (stdout or stderr) and (retcode or args.verbose): write('hookid: {0}\n'.format(hook['id'])) write('\n') for output in (stdout, stderr): if output.strip(): write(output.strip() + '\n') write('\n') return retcode
def _run_single_hook(hook, repo, args, write, skips=frozenset()): filenames = get_filenames(args, hook['files'], hook['exclude']) if hook['id'] in skips: _print_user_skipped(hook, write, args) return 0 elif not filenames: _print_no_files_skipped(hook, write, args) return 0 # Print the hook and the dots first in case the hook takes hella long to # run. write(get_hook_message(_hook_msg_start(hook, args.verbose), end_len=6)) sys.stdout.flush() retcode, stdout, stderr = repo.run_hook(hook, filenames) if retcode != hook['expected_return_value']: retcode = 1 print_color = color.RED pass_fail = 'Failed' else: retcode = 0 print_color = color.GREEN pass_fail = 'Passed' write(color.format_color(pass_fail, print_color, args.color) + '\n') if (stdout or stderr) and (retcode or args.verbose): write('hookid: {0}\n'.format(hook['id'])) write('\n') for output in (stdout, stderr): assert type(output) is bytes, type(output) if output.strip(): write(output.strip() + b'\n') write('\n') return retcode
def _run_single_hook(classifier, hook, skips, cols, verbose, use_color): filenames = classifier.filenames_for_hook(hook) if hook.id in skips or hook.alias in skips: output.write( get_hook_message( hook.name, end_msg=SKIPPED, end_color=color.YELLOW, use_color=use_color, cols=cols, ), ) duration = None retcode = 0 files_modified = False out = b'' elif not filenames and not hook.always_run: output.write( get_hook_message( hook.name, postfix=NO_FILES, end_msg=SKIPPED, end_color=color.TURQUOISE, use_color=use_color, cols=cols, ), ) duration = None retcode = 0 files_modified = False out = b'' else: # print hook and dots first in case the hook takes a while to run output.write(get_hook_message(hook.name, end_len=6, cols=cols)) diff_cmd = ('git', 'diff', '--no-ext-diff') diff_before = cmd_output_b(*diff_cmd, retcode=None) filenames = tuple(filenames) if hook.pass_filenames else () time_before = time.time() retcode, out = hook.run(filenames, use_color) duration = round(time.time() - time_before, 2) or 0 diff_after = cmd_output_b(*diff_cmd, retcode=None) # if the hook makes changes, fail the commit files_modified = diff_before != diff_after if retcode or files_modified: print_color = color.RED status = 'Failed' else: print_color = color.GREEN status = 'Passed' output.write_line(color.format_color(status, print_color, use_color)) if verbose or hook.verbose or retcode or files_modified: _subtle_line('- hook id: {}'.format(hook.id), use_color) if (verbose or hook.verbose) and duration is not None: _subtle_line('- duration: {}s'.format(duration), use_color) if retcode: _subtle_line('- exit code: {}'.format(retcode), use_color) # Print a message if failing due to file modifications if files_modified: _subtle_line('- files were modified by this hook', use_color) if out.strip(): output.write_line() output.write_line(out.strip(), logfile_name=hook.log_file) output.write_line() return files_modified or bool(retcode)
def _run_single_hook(classifier, hook, args, skips, cols, use_color): filenames = classifier.filenames_for_hook(hook) if hook.language == 'pcre': logger.warning( '`{}` (from {}) uses the deprecated pcre language.\n' 'The pcre language is scheduled for removal in pre-commit 2.x.\n' 'The pygrep language is a more portable (and usually drop-in) ' 'replacement.'.format(hook.id, hook.src), ) if hook.id in skips or hook.alias in skips: output.write( get_hook_message( _hook_msg_start(hook, args.verbose), end_msg=SKIPPED, end_color=color.YELLOW, use_color=args.color, cols=cols, ), ) return 0 elif not filenames and not hook.always_run: output.write( get_hook_message( _hook_msg_start(hook, args.verbose), postfix=NO_FILES, end_msg=SKIPPED, end_color=color.TURQUOISE, use_color=args.color, cols=cols, ), ) return 0 # Print the hook and the dots first in case the hook takes hella long to # run. output.write( get_hook_message( _hook_msg_start(hook, args.verbose), end_len=6, cols=cols, ), ) sys.stdout.flush() diff_before = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None) filenames = tuple(filenames) if hook.pass_filenames else () retcode, out = hook.run(filenames, use_color) diff_after = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None) file_modifications = diff_before != diff_after # If the hook makes changes, fail the commit if file_modifications: retcode = 1 if retcode: retcode = 1 print_color = color.RED pass_fail = 'Failed' else: retcode = 0 print_color = color.GREEN pass_fail = 'Passed' output.write_line(color.format_color(pass_fail, print_color, args.color)) if ((out or file_modifications) and (retcode or args.verbose or hook.verbose)): output.write_line('hookid: {}\n'.format(hook.id)) # Print a message if failing due to file modifications if file_modifications: output.write('Files were modified by this hook.') if out: output.write_line(' Additional output:') output.write_line() if out.strip(): output.write_line(out.strip(), logfile_name=hook.log_file) output.write_line() return retcode
def test_case_with_end_len(): ret = get_hook_message('start', end_len=5, cols=15) assert ret == 'start' + '.' * 4
def _run_single_hook(hook, repo, args, skips, cols): filenames = get_filenames(args, hook.get('files', '^$'), hook['exclude']) if hook['id'] in skips: output.write(get_hook_message( _hook_msg_start(hook, args.verbose), end_msg=SKIPPED, end_color=color.YELLOW, use_color=args.color, cols=cols, )) return 0 elif not filenames and not hook['always_run']: output.write(get_hook_message( _hook_msg_start(hook, args.verbose), postfix=NO_FILES, end_msg=SKIPPED, end_color=color.TURQUOISE, use_color=args.color, cols=cols, )) return 0 # Print the hook and the dots first in case the hook takes hella long to # run. output.write(get_hook_message( _hook_msg_start(hook, args.verbose), end_len=6, cols=cols, )) sys.stdout.flush() diff_before = cmd_output( 'git', 'diff', '--no-ext-diff', retcode=None, encoding=None, ) retcode, stdout, stderr = repo.run_hook( hook, tuple(filenames) if hook['pass_filenames'] else (), ) diff_after = cmd_output( 'git', 'diff', '--no-ext-diff', retcode=None, encoding=None, ) file_modifications = diff_before != diff_after # If the hook makes changes, fail the commit if file_modifications: retcode = 1 if retcode: retcode = 1 print_color = color.RED pass_fail = 'Failed' else: retcode = 0 print_color = color.GREEN pass_fail = 'Passed' output.write_line(color.format_color(pass_fail, print_color, args.color)) if (stdout or stderr or file_modifications) and (retcode or args.verbose): output.write_line('hookid: {}\n'.format(hook['id'])) # Print a message if failing due to file modifications if file_modifications: output.write('Files were modified by this hook.') if stdout or stderr: output.write_line(' Additional output:') output.write_line() for out in (stdout, stderr): assert type(out) is bytes, type(out) if out.strip(): output.write_line(out.strip(), logfile_name=hook['log_file']) output.write_line() return retcode
def _run_single_hook(filenames, hook, repo, args, skips, cols): include, exclude = hook['files'], hook['exclude'] filenames = _filter_by_include_exclude(filenames, include, exclude) types, exclude_types = hook['types'], hook['exclude_types'] filenames = _filter_by_types(filenames, types, exclude_types) if hook['language'] == 'pcre': logger.warning( '`{}` (from {}) uses the deprecated pcre language.\n' 'The pcre language is scheduled for removal in pre-commit 2.x.\n' 'The pygrep language is a more portable (and usually drop-in) ' 'replacement.'.format(hook['id'], repo.repo_config['repo']), ) if hook['id'] in skips: output.write( get_hook_message( _hook_msg_start(hook, args.verbose), end_msg=SKIPPED, end_color=color.YELLOW, use_color=args.color, cols=cols, )) return 0 elif not filenames and not hook['always_run']: output.write( get_hook_message( _hook_msg_start(hook, args.verbose), postfix=NO_FILES, end_msg=SKIPPED, end_color=color.TURQUOISE, use_color=args.color, cols=cols, )) return 0 # Print the hook and the dots first in case the hook takes hella long to # run. output.write( get_hook_message( _hook_msg_start(hook, args.verbose), end_len=6, cols=cols, )) sys.stdout.flush() diff_before = cmd_output( 'git', 'diff', '--no-ext-diff', retcode=None, encoding=None, ) retcode, stdout, stderr = repo.run_hook( hook, tuple(filenames) if hook['pass_filenames'] else (), ) diff_after = cmd_output( 'git', 'diff', '--no-ext-diff', retcode=None, encoding=None, ) file_modifications = diff_before != diff_after # If the hook makes changes, fail the commit if file_modifications: retcode = 1 if retcode: retcode = 1 print_color = color.RED pass_fail = 'Failed' else: retcode = 0 print_color = color.GREEN pass_fail = 'Passed' output.write_line(color.format_color(pass_fail, print_color, args.color)) if ((stdout or stderr or file_modifications) and (retcode or args.verbose or hook['verbose'])): output.write_line('hookid: {}\n'.format(hook['id'])) # Print a message if failing due to file modifications if file_modifications: output.write('Files were modified by this hook.') if stdout or stderr: output.write_line(' Additional output:') output.write_line() for out in (stdout, stderr): assert type(out) is bytes, type(out) if out.strip(): output.write_line(out.strip(), logfile_name=hook['log_file']) output.write_line() return retcode
def test_get_hook_message_raises(kwargs): with pytest.raises(ValueError): get_hook_message('start', **kwargs)
def _run_single_hook(hook, repo, args, skips, cols): filenames = get_filenames(args, hook['files'], hook['exclude']) filenames = filter_filenames_by_types( filenames, hook['types'], hook['exclude_types'], ) if hook['id'] in skips: output.write( get_hook_message( _hook_msg_start(hook, args.verbose), end_msg=SKIPPED, end_color=color.YELLOW, use_color=args.color, cols=cols, )) return 0 elif not filenames and not hook['always_run']: output.write( get_hook_message( _hook_msg_start(hook, args.verbose), postfix=NO_FILES, end_msg=SKIPPED, end_color=color.TURQUOISE, use_color=args.color, cols=cols, )) return 0 # Print the hook and the dots first in case the hook takes hella long to # run. output.write( get_hook_message( _hook_msg_start(hook, args.verbose), end_len=6, cols=cols, )) sys.stdout.flush() diff_before = cmd_output( 'git', 'diff', '--no-ext-diff', retcode=None, encoding=None, ) retcode, stdout, stderr = repo.run_hook( hook, tuple(filenames) if hook['pass_filenames'] else (), ) diff_after = cmd_output( 'git', 'diff', '--no-ext-diff', retcode=None, encoding=None, ) file_modifications = diff_before != diff_after # If the hook makes changes, fail the commit if file_modifications: retcode = 1 if retcode: retcode = 1 print_color = color.RED pass_fail = 'Failed' else: retcode = 0 print_color = color.GREEN pass_fail = 'Passed' output.write_line(color.format_color(pass_fail, print_color, args.color)) if (stdout or stderr or file_modifications) and (retcode or args.verbose): output.write_line('hookid: {}\n'.format(hook['id'])) # Print a message if failing due to file modifications if file_modifications: output.write('Files were modified by this hook.') if stdout or stderr: output.write_line(' Additional output:') output.write_line() for out in (stdout, stderr): assert type(out) is bytes, type(out) if out.strip(): output.write_line(out.strip(), logfile_name=hook['log_file']) output.write_line() return retcode
def _run_single_hook(classifier, hook, args, skips, cols): filenames = classifier.filenames_for_hook(hook) if hook.language == 'pcre': logger.warning( '`{}` (from {}) uses the deprecated pcre language.\n' 'The pcre language is scheduled for removal in pre-commit 2.x.\n' 'The pygrep language is a more portable (and usually drop-in) ' 'replacement.'.format(hook.id, hook.src), ) if hook.id in skips or hook.alias in skips: output.write( get_hook_message( _hook_msg_start(hook, args.verbose), end_msg=SKIPPED, end_color=color.YELLOW, use_color=args.color, cols=cols, ), ) return 0 elif not filenames and not hook.always_run: output.write( get_hook_message( _hook_msg_start(hook, args.verbose), postfix=NO_FILES, end_msg=SKIPPED, end_color=color.TURQUOISE, use_color=args.color, cols=cols, ), ) return 0 # Print the hook and the dots first in case the hook takes hella long to # run. output.write( get_hook_message( _hook_msg_start(hook, args.verbose), end_len=6, cols=cols, ), ) sys.stdout.flush() diff_before = cmd_output( 'git', 'diff', '--no-ext-diff', retcode=None, encoding=None, ) retcode, stdout, stderr = hook.run( tuple(filenames) if hook.pass_filenames else (), ) diff_after = cmd_output( 'git', 'diff', '--no-ext-diff', retcode=None, encoding=None, ) file_modifications = diff_before != diff_after # If the hook makes changes, fail the commit if file_modifications: retcode = 1 if retcode: retcode = 1 print_color = color.RED pass_fail = 'Failed' else: retcode = 0 print_color = color.GREEN pass_fail = 'Passed' output.write_line(color.format_color(pass_fail, print_color, args.color)) if ( (stdout or stderr or file_modifications) and (retcode or args.verbose or hook.verbose) ): output.write_line('hookid: {}\n'.format(hook.id)) # Print a message if failing due to file modifications if file_modifications: output.write('Files were modified by this hook.') if stdout or stderr: output.write_line(' Additional output:') output.write_line() for out in (stdout, stderr): assert type(out) is bytes, type(out) if out.strip(): output.write_line(out.strip(), logfile_name=hook.log_file) output.write_line() return retcode