mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
fix for #157
This commit is contained in:
parent
cdf726bbed
commit
fdc2a889de
3 changed files with 86 additions and 0 deletions
|
|
@ -139,6 +139,18 @@ def _has_unmerged_paths(runner):
|
||||||
return bool(stdout.strip())
|
return bool(stdout.strip())
|
||||||
|
|
||||||
|
|
||||||
|
def _has_unstaged_config(runner):
|
||||||
|
retcode, stdout, stderr = runner.cmd_runner.run(
|
||||||
|
[
|
||||||
|
'git', 'diff', '--exit-code', runner.config_file_path
|
||||||
|
],
|
||||||
|
retcode=None,
|
||||||
|
encoding=None,
|
||||||
|
)
|
||||||
|
# be explicit, other git errors don't mean it has an unstaged config.
|
||||||
|
return retcode == 1
|
||||||
|
|
||||||
|
|
||||||
def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
|
def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
|
||||||
# Set up our logging handler
|
# Set up our logging handler
|
||||||
logger.addHandler(LoggingHandler(args.color, write=write))
|
logger.addHandler(LoggingHandler(args.color, write=write))
|
||||||
|
|
@ -151,6 +163,16 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
|
||||||
if bool(args.source) != bool(args.origin):
|
if bool(args.source) != bool(args.origin):
|
||||||
logger.error('Specify both --origin and --source.')
|
logger.error('Specify both --origin and --source.')
|
||||||
return 1
|
return 1
|
||||||
|
if _has_unstaged_config(runner) and not args.no_stash:
|
||||||
|
if args.allow_unstaged_config:
|
||||||
|
logger.warn('You have an unstaged config file and have '
|
||||||
|
'specified the --allow-unstaged-config option.\n'
|
||||||
|
'Note that your config will be stashed before the '
|
||||||
|
'config is parsed unless --no-stash is specified.')
|
||||||
|
else:
|
||||||
|
logger.error('You have an unstaged config file and have not '
|
||||||
|
'specified the --allow-unstaged-config option.\n')
|
||||||
|
return 1
|
||||||
|
|
||||||
# Don't stash if specified or files are specified
|
# Don't stash if specified or files are specified
|
||||||
if args.no_stash or args.all_files or args.files:
|
if args.no_stash or args.all_files or args.files:
|
||||||
|
|
|
||||||
|
|
@ -94,6 +94,11 @@ def main(argv=None):
|
||||||
'--source', '-s',
|
'--source', '-s',
|
||||||
help='The remote branch"s commit_id when using `git push`',
|
help='The remote branch"s commit_id when using `git push`',
|
||||||
)
|
)
|
||||||
|
run_parser.add_argument(
|
||||||
|
'--allow-unstaged-config', default=False, action='store_true',
|
||||||
|
help='Allow an unstaged config to be present. Note that this will'
|
||||||
|
'be stashed before parsing unless --no-stash is specified'
|
||||||
|
)
|
||||||
run_mutex_group = run_parser.add_mutually_exclusive_group(required=False)
|
run_mutex_group = run_parser.add_mutually_exclusive_group(required=False)
|
||||||
run_mutex_group.add_argument(
|
run_mutex_group.add_argument(
|
||||||
'--all-files', '-a', action='store_true', default=False,
|
'--all-files', '-a', action='store_true', default=False,
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,7 @@ def _get_opts(
|
||||||
no_stash=False,
|
no_stash=False,
|
||||||
origin='',
|
origin='',
|
||||||
source='',
|
source='',
|
||||||
|
allow_unstaged_config=False,
|
||||||
):
|
):
|
||||||
# These are mutually exclusive
|
# These are mutually exclusive
|
||||||
assert not (all_files and files)
|
assert not (all_files and files)
|
||||||
|
|
@ -65,6 +66,7 @@ def _get_opts(
|
||||||
no_stash=no_stash,
|
no_stash=no_stash,
|
||||||
origin=origin,
|
origin=origin,
|
||||||
source=source,
|
source=source,
|
||||||
|
allow_unstaged_config=allow_unstaged_config,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -334,3 +336,60 @@ def test_lots_of_files(mock_out_store_directory, tmpdir_factory):
|
||||||
stderr=subprocess.STDOUT,
|
stderr=subprocess.STDOUT,
|
||||||
env=env,
|
env=env,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_allow_unstaged_config_option(repo_with_passing_hook,
|
||||||
|
mock_out_store_directory):
|
||||||
|
|
||||||
|
with cwd(repo_with_passing_hook):
|
||||||
|
with io.open(
|
||||||
|
'.pre-commit-config.yaml', 'a+',
|
||||||
|
) as config_file:
|
||||||
|
# writing a newline should be relatively harmless to get a change
|
||||||
|
config_file.write('\n')
|
||||||
|
|
||||||
|
args = _get_opts(allow_unstaged_config=True)
|
||||||
|
ret, printed = _do_run(repo_with_passing_hook, args)
|
||||||
|
common_msg = 'You have an unstaged config file'
|
||||||
|
warning_msg = 'have specified the --allow-unstaged-config option.'
|
||||||
|
|
||||||
|
assert common_msg in printed
|
||||||
|
assert warning_msg in printed
|
||||||
|
assert ret == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_allow_unstaged_config_option(repo_with_passing_hook,
|
||||||
|
mock_out_store_directory):
|
||||||
|
|
||||||
|
with cwd(repo_with_passing_hook):
|
||||||
|
with io.open(
|
||||||
|
'.pre-commit-config.yaml', 'a+',
|
||||||
|
) as config_file:
|
||||||
|
# writing a newline should be relatively harmless to get a change
|
||||||
|
config_file.write('\n')
|
||||||
|
|
||||||
|
args = _get_opts(allow_unstaged_config=False)
|
||||||
|
ret, printed = _do_run(repo_with_passing_hook, args)
|
||||||
|
common_msg = 'You have an unstaged config file'
|
||||||
|
error_msg = 'have not specified the --allow-unstaged-config option.\n'
|
||||||
|
|
||||||
|
assert common_msg in printed
|
||||||
|
assert error_msg in printed
|
||||||
|
assert ret == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_stash_suppresses_allow_unstaged_config_option(
|
||||||
|
repo_with_passing_hook, mock_out_store_directory):
|
||||||
|
|
||||||
|
with cwd(repo_with_passing_hook):
|
||||||
|
with io.open(
|
||||||
|
'.pre-commit-config.yaml', 'a+',
|
||||||
|
) as config_file:
|
||||||
|
# writing a newline should be relatively harmless to get a change
|
||||||
|
config_file.write('\n')
|
||||||
|
|
||||||
|
args = _get_opts(allow_unstaged_config=False, no_stash=True)
|
||||||
|
ret, printed = _do_run(repo_with_passing_hook, args)
|
||||||
|
common_msg = 'You have an unstaged config file'
|
||||||
|
|
||||||
|
assert common_msg not in printed
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue