mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-14 04:50:20 -06:00
Fix try-repo for staged untracked changes
This commit is contained in:
@@ -32,9 +32,15 @@ def _repo_ref(tmpdir, repo, ref):
|
||||
shadow = os.path.join(tmpdir, 'shadow-repo')
|
||||
cmd_output('git', 'clone', repo, shadow)
|
||||
cmd_output('git', 'checkout', ref, '-b', '_pc_tmp', cwd=shadow)
|
||||
|
||||
idx = git.git_path('index', repo=shadow)
|
||||
objs = git.git_path('objects', repo=shadow)
|
||||
env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs)
|
||||
|
||||
staged_files = git.get_staged_files(cwd=repo)
|
||||
if (len(staged_files) > 0):
|
||||
cmd_output('git', 'add', *staged_files, cwd=repo, env=env)
|
||||
|
||||
cmd_output('git', 'add', '-u', cwd=repo, env=env)
|
||||
git.commit(repo=shadow)
|
||||
|
||||
|
||||
@@ -91,11 +91,12 @@ def get_conflicted_files():
|
||||
return set(merge_conflict_filenames) | set(merge_diff_filenames)
|
||||
|
||||
|
||||
def get_staged_files():
|
||||
def get_staged_files(cwd=None):
|
||||
return zsplit(cmd_output(
|
||||
'git', 'diff', '--staged', '--name-only', '--no-ext-diff', '-z',
|
||||
# Everything except for D
|
||||
'--diff-filter=ACMRTUXB',
|
||||
cwd=cwd,
|
||||
)[1])
|
||||
|
||||
|
||||
|
||||
@@ -123,3 +123,15 @@ def test_try_repo_uncommitted_changes(cap_out, tempdir_factory):
|
||||
config,
|
||||
)
|
||||
assert rest == 'modified name!...........................................................Passed\n' # noqa: E501
|
||||
|
||||
|
||||
def test_try_repo_staged_changes(tempdir_factory):
|
||||
repo = make_repo(tempdir_factory, 'modified_file_returns_zero_repo')
|
||||
|
||||
with cwd(repo):
|
||||
open('staged-file', 'a').close()
|
||||
open('second-staged-file', 'a').close()
|
||||
cmd_output('git', 'add', '.')
|
||||
|
||||
with cwd(git_dir(tempdir_factory)):
|
||||
assert not try_repo(try_repo_opts(repo, hook='bash_hook'))
|
||||
|
||||
Reference in New Issue
Block a user