@@ -53,6 +53,7 @@ def _get_opts(
5353 no_stash = False ,
5454 origin = '' ,
5555 source = '' ,
56+ allow_unstaged_config = False ,
5657):
5758 # These are mutually exclusive
5859 assert not (all_files and files )
@@ -65,6 +66,7 @@ def _get_opts(
6566 no_stash = no_stash ,
6667 origin = origin ,
6768 source = source ,
69+ allow_unstaged_config = allow_unstaged_config ,
6870 )
6971
7072
@@ -334,3 +336,60 @@ def test_lots_of_files(mock_out_store_directory, tmpdir_factory):
334336 stderr = subprocess .STDOUT ,
335337 env = env ,
336338 )
339+
340+
341+ def test_allow_unstaged_config_option (repo_with_passing_hook ,
342+ mock_out_store_directory ):
343+
344+ with cwd (repo_with_passing_hook ):
345+ with io .open (
346+ '.pre-commit-config.yaml' , 'a+' ,
347+ ) as config_file :
348+ # writing a newline should be relatively harmless to get a change
349+ config_file .write ('\n ' )
350+
351+ args = _get_opts (allow_unstaged_config = True )
352+ ret , printed = _do_run (repo_with_passing_hook , args )
353+ common_msg = 'You have an unstaged config file'
354+ warning_msg = 'have specified the --allow-unstaged-config option.'
355+
356+ assert common_msg in printed
357+ assert warning_msg in printed
358+ assert ret == 0
359+
360+
361+ def test_no_allow_unstaged_config_option (repo_with_passing_hook ,
362+ mock_out_store_directory ):
363+
364+ with cwd (repo_with_passing_hook ):
365+ with io .open (
366+ '.pre-commit-config.yaml' , 'a+' ,
367+ ) as config_file :
368+ # writing a newline should be relatively harmless to get a change
369+ config_file .write ('\n ' )
370+
371+ args = _get_opts (allow_unstaged_config = False )
372+ ret , printed = _do_run (repo_with_passing_hook , args )
373+ common_msg = 'You have an unstaged config file'
374+ error_msg = 'have not specified the --allow-unstaged-config option.\n '
375+
376+ assert common_msg in printed
377+ assert error_msg in printed
378+ assert ret == 1
379+
380+
381+ def test_no_stash_suppresses_allow_unstaged_config_option (
382+ repo_with_passing_hook , mock_out_store_directory ):
383+
384+ with cwd (repo_with_passing_hook ):
385+ with io .open (
386+ '.pre-commit-config.yaml' , 'a+' ,
387+ ) as config_file :
388+ # writing a newline should be relatively harmless to get a change
389+ config_file .write ('\n ' )
390+
391+ args = _get_opts (allow_unstaged_config = False , no_stash = True )
392+ ret , printed = _do_run (repo_with_passing_hook , args )
393+ common_msg = 'You have an unstaged config file'
394+
395+ assert common_msg not in printed
0 commit comments