Skip to content

Commit 888787f

Browse files
Fix try-repo for staged untracked changes
1 parent 71c238d commit 888787f

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

pre_commit/commands/try_repo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ def _repo_ref(tmpdir, repo, ref):
3232
shadow = os.path.join(tmpdir, 'shadow-repo')
3333
cmd_output('git', 'clone', repo, shadow)
3434
cmd_output('git', 'checkout', ref, '-b', '_pc_tmp', cwd=shadow)
35+
3536
idx = git.git_path('index', repo=shadow)
3637
objs = git.git_path('objects', repo=shadow)
3738
env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs)
39+
40+
staged_files = git.get_staged_files(cwd=repo)
41+
if (len(staged_files) > 0):
42+
cmd_output('git', 'add', *staged_files, cwd=repo, env=env)
43+
3844
cmd_output('git', 'add', '-u', cwd=repo, env=env)
3945
git.commit(repo=shadow)
4046

pre_commit/git.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ def get_conflicted_files():
9191
return set(merge_conflict_filenames) | set(merge_diff_filenames)
9292

9393

94-
def get_staged_files():
94+
def get_staged_files(cwd=None):
9595
return zsplit(cmd_output(
9696
'git', 'diff', '--staged', '--name-only', '--no-ext-diff', '-z',
9797
# Everything except for D
9898
'--diff-filter=ACMRTUXB',
99+
cwd=cwd,
99100
)[1])
100101

101102

tests/commands/try_repo_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,15 @@ def test_try_repo_uncommitted_changes(cap_out, tempdir_factory):
123123
config,
124124
)
125125
assert rest == 'modified name!...........................................................Passed\n' # noqa: E501
126+
127+
128+
def test_try_repo_staged_changes(tempdir_factory):
129+
repo = make_repo(tempdir_factory, 'modified_file_returns_zero_repo')
130+
131+
with cwd(repo):
132+
open('staged-file', 'a').close()
133+
open('second-staged-file', 'a').close()
134+
cmd_output('git', 'add', '.')
135+
136+
with cwd(git_dir(tempdir_factory)):
137+
assert not try_repo(try_repo_opts(repo, hook='bash_hook'))

0 commit comments

Comments
 (0)