66import os .path
77import time
88
9+ from pre_commit import git
910from pre_commit .util import CalledProcessError
1011from pre_commit .util import cmd_output
1112from pre_commit .util import mkdirp
13+ from pre_commit .xargs import xargs
1214
1315
1416logger = logging .getLogger ('pre_commit' )
@@ -24,11 +26,22 @@ def _git_apply(patch):
2426
2527
2628@contextlib .contextmanager
27- def staged_files_only (patch_dir ):
28- """Clear any unstaged changes from the git working directory inside this
29- context.
30- """
31- # Determine if there are unstaged files
29+ def _intent_to_add_cleared ():
30+ intent_to_add = git .intent_to_add_files ()
31+ if intent_to_add :
32+ logger .warning ('Unstaged intent-to-add files detected.' )
33+
34+ xargs (('git' , 'rm' , '--cached' , '--' ), intent_to_add )
35+ try :
36+ yield
37+ finally :
38+ xargs (('git' , 'add' , '--intent-to-add' , '--' ), intent_to_add )
39+ else :
40+ yield
41+
42+
43+ @contextlib .contextmanager
44+ def _unstaged_changes_cleared (patch_dir ):
3245 tree = cmd_output ('git' , 'write-tree' )[1 ].strip ()
3346 retcode , diff_stdout_binary , _ = cmd_output (
3447 'git' , 'diff-index' , '--ignore-submodules' , '--binary' ,
@@ -71,3 +84,12 @@ def staged_files_only(patch_dir):
7184 # There weren't any staged files so we don't need to do anything
7285 # special
7386 yield
87+
88+
89+ @contextlib .contextmanager
90+ def staged_files_only (patch_dir ):
91+ """Clear any unstaged changes from the git working directory inside this
92+ context.
93+ """
94+ with _intent_to_add_cleared (), _unstaged_changes_cleared (patch_dir ):
95+ yield
0 commit comments