Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 158a3a6

Browse files
committed
Pre-commit stashes unstaged changes on run. Closes #30.
1 parent a3720c0 commit 158a3a6

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

pre_commit/run.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pre_commit import git
1212
from pre_commit.logging_handler import LoggingHandler
1313
from pre_commit.runner import Runner
14+
from pre_commit.staged_files_only import staged_files_only
1415
from pre_commit.util import entry
1516

1617

@@ -89,10 +90,11 @@ def _run(runner, args):
8990
logger.addHandler(LoggingHandler(args.color))
9091
logger.setLevel(logging.INFO)
9192

92-
if args.hook:
93-
return run_single_hook(runner, args.hook, args)
94-
else:
95-
return run_hooks(runner, args)
93+
with staged_files_only(runner.cmd_runner):
94+
if args.hook:
95+
return run_single_hook(runner, args.hook, args)
96+
else:
97+
return run_hooks(runner, args)
9698

9799

98100
@entry

pre_commit/staged_files_only.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11

22
import contextlib
3+
import logging
34
import time
45

56
from pre_commit.prefixed_command_runner import CalledProcessError
67

78

9+
logger = logging.getLogger('pre_commit')
10+
11+
812
@contextlib.contextmanager
913
def staged_files_only(cmd_runner):
1014
"""Clear any unstaged changes from the git working directory inside this
@@ -19,10 +23,12 @@ def staged_files_only(cmd_runner):
1923
retcode=None,
2024
)
2125
if retcode:
22-
# TODO: print a warning message that unstaged things are being stashed
26+
patch_filename = cmd_runner.path('patch{0}'.format(int(time.time())))
27+
logger.warning('Unstaged files detected.')
28+
logger.info(
29+
'Stashing unstaged files to {0}.'.format(patch_filename),
30+
)
2331
# Save the current unstaged changes as a patch
24-
# TODO: use a more unique patch filename
25-
patch_filename = cmd_runner.path('patch{0}'.format(time.time()))
2632
with open(patch_filename, 'w') as patch_file:
2733
cmd_runner.run(['git', 'diff', '--binary'], stdout=patch_file)
2834

@@ -35,12 +41,17 @@ def staged_files_only(cmd_runner):
3541
try:
3642
cmd_runner.run(['git', 'apply', patch_filename])
3743
except CalledProcessError:
44+
logger.warning(
45+
'Stashed changes conflicted with hook auto-fixes... '
46+
'Rolling back fixes...'
47+
)
3848
# TOOD: print a warning about rolling back changes made by hooks
3949
# We failed to apply the patch, presumably due to fixes made
4050
# by hooks.
4151
# Roll back the changes made by hooks.
4252
cmd_runner.run(['git', 'checkout', '--', '.'])
4353
cmd_runner.run(['git', 'apply', patch_filename])
54+
logger.info('Restored changes from {0}.'.format(patch_filename))
4455
else:
4556
# There weren't any staged files so we don't need to do anything
4657
# special

0 commit comments

Comments
 (0)