33import contextlib
44import io
55import logging
6+ import os .path
67import time
78
89from pre_commit .util import CalledProcessError
10+ from pre_commit .util import cmd_output
911
1012
1113logger = logging .getLogger ('pre_commit' )
1214
1315
14- def _git_apply (cmd_runner , patch ):
16+ def _git_apply (patch ):
1517 args = ('apply' , '--whitespace=nowarn' , patch )
1618 try :
17- cmd_runner . run (( 'git' ,) + args , encoding = None )
19+ cmd_output ( 'git' , * args , encoding = None )
1820 except CalledProcessError :
1921 # Retry with autocrlf=false -- see #570
20- cmd = ('git' , '-c' , 'core.autocrlf=false' ) + args
21- cmd_runner .run (cmd , encoding = None )
22+ cmd_output ('git' , '-c' , 'core.autocrlf=false' , * args , encoding = None )
2223
2324
2425@contextlib .contextmanager
25- def staged_files_only (cmd_runner ):
26+ def staged_files_only (patch_dir ):
2627 """Clear any unstaged changes from the git working directory inside this
2728 context.
28-
29- Args:
30- cmd_runner - PrefixedCommandRunner
3129 """
3230 # Determine if there are unstaged files
33- tree = cmd_runner .run (('git' , 'write-tree' ))[1 ].strip ()
34- retcode , diff_stdout_binary , _ = cmd_runner .run (
35- (
36- 'git' , 'diff-index' , '--ignore-submodules' , '--binary' ,
37- '--exit-code' , '--no-color' , '--no-ext-diff' , tree , '--' ,
38- ),
31+ tree = cmd_output ('git' , 'write-tree' )[1 ].strip ()
32+ retcode , diff_stdout_binary , _ = cmd_output (
33+ 'git' , 'diff-index' , '--ignore-submodules' , '--binary' ,
34+ '--exit-code' , '--no-color' , '--no-ext-diff' , tree , '--' ,
3935 retcode = None ,
4036 encoding = None ,
4137 )
4238 if retcode and diff_stdout_binary .strip ():
43- patch_filename = cmd_runner .path ('patch{}' .format (int (time .time ())))
39+ patch_filename = 'patch{}' .format (int (time .time ()))
40+ patch_filename = os .path .join (patch_dir , patch_filename )
4441 logger .warning ('Unstaged files detected.' )
4542 logger .info (
4643 'Stashing unstaged files to {}.' .format (patch_filename ),
@@ -50,13 +47,13 @@ def staged_files_only(cmd_runner):
5047 patch_file .write (diff_stdout_binary )
5148
5249 # Clear the working directory of unstaged changes
53- cmd_runner . run (( 'git' , 'checkout' , '--' , '.' ) )
50+ cmd_output ( 'git' , 'checkout' , '--' , '.' )
5451 try :
5552 yield
5653 finally :
5754 # Try to apply the patch we saved
5855 try :
59- _git_apply (cmd_runner , patch_filename )
56+ _git_apply (patch_filename )
6057 except CalledProcessError :
6158 logger .warning (
6259 'Stashed changes conflicted with hook auto-fixes... '
@@ -65,8 +62,8 @@ def staged_files_only(cmd_runner):
6562 # We failed to apply the patch, presumably due to fixes made
6663 # by hooks.
6764 # Roll back the changes made by hooks.
68- cmd_runner . run (( 'git' , 'checkout' , '--' , '.' ) )
69- _git_apply (cmd_runner , patch_filename )
65+ cmd_output ( 'git' , 'checkout' , '--' , '.' )
66+ _git_apply (patch_filename )
7067 logger .info ('Restored changes from {}.' .format (patch_filename ))
7168 else :
7269 # There weren't any staged files so we don't need to do anything
0 commit comments