-
-
Notifications
You must be signed in to change notification settings - Fork 769
/
staged_files_only.py
73 lines (64 loc) · 2.43 KB
/
staged_files_only.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import unicode_literals
import contextlib
import io
import logging
import os.path
import time
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
from pre_commit.util import mkdirp
logger = logging.getLogger('pre_commit')
def _git_apply(patch):
args = ('apply', '--whitespace=nowarn', patch)
try:
cmd_output('git', *args, encoding=None)
except CalledProcessError:
# Retry with autocrlf=false -- see #570
cmd_output('git', '-c', 'core.autocrlf=false', *args, encoding=None)
@contextlib.contextmanager
def staged_files_only(patch_dir):
"""Clear any unstaged changes from the git working directory inside this
context.
"""
# Determine if there are unstaged files
tree = cmd_output('git', 'write-tree')[1].strip()
retcode, diff_stdout_binary, _ = cmd_output(
'git', 'diff-index', '--ignore-submodules', '--binary',
'--exit-code', '--no-color', '--no-ext-diff', tree, '--',
retcode=None,
encoding=None,
)
if retcode and diff_stdout_binary.strip():
patch_filename = 'patch{}'.format(int(time.time()))
patch_filename = os.path.join(patch_dir, patch_filename)
logger.warning('Unstaged files detected.')
logger.info(
'Stashing unstaged files to {}.'.format(patch_filename),
)
# Save the current unstaged changes as a patch
mkdirp(patch_dir)
with io.open(patch_filename, 'wb') as patch_file:
patch_file.write(diff_stdout_binary)
# Clear the working directory of unstaged changes
cmd_output('git', 'checkout', '--', '.')
try:
yield
finally:
# Try to apply the patch we saved
try:
_git_apply(patch_filename)
except CalledProcessError:
logger.warning(
'Stashed changes conflicted with hook auto-fixes... '
'Rolling back fixes...',
)
# We failed to apply the patch, presumably due to fixes made
# by hooks.
# Roll back the changes made by hooks.
cmd_output('git', 'checkout', '--', '.')
_git_apply(patch_filename)
logger.info('Restored changes from {}.'.format(patch_filename))
else:
# There weren't any staged files so we don't need to do anything
# special
yield