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

Skip to content

Commit 264161c

Browse files
authored
Merge pull request #973 from DanielChabrowski/try-repo-fix
Fix try-repo for staged untracked changes
2 parents 71c238d + c7b369a commit 264161c

5 files changed

Lines changed: 34 additions & 3 deletions

File tree

pre_commit/commands/try_repo.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pre_commit.store import Store
1616
from pre_commit.util import cmd_output
1717
from pre_commit.util import tmpdir
18+
from pre_commit.xargs import xargs
1819

1920
logger = logging.getLogger(__name__)
2021

@@ -32,9 +33,15 @@ def _repo_ref(tmpdir, repo, ref):
3233
shadow = os.path.join(tmpdir, 'shadow-repo')
3334
cmd_output('git', 'clone', repo, shadow)
3435
cmd_output('git', 'checkout', ref, '-b', '_pc_tmp', cwd=shadow)
36+
3537
idx = git.git_path('index', repo=shadow)
3638
objs = git.git_path('objects', repo=shadow)
3739
env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs)
40+
41+
staged_files = git.get_staged_files(cwd=repo)
42+
if staged_files:
43+
xargs(('git', 'add', '--'), staged_files, cwd=repo, env=env)
44+
3845
cmd_output('git', 'add', '-u', cwd=repo, env=env)
3946
git.commit(repo=shadow)
4047

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

pre_commit/xargs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def xargs(cmd, varargs, **kwargs):
109109
"""
110110
negate = kwargs.pop('negate', False)
111111
target_concurrency = kwargs.pop('target_concurrency', 1)
112+
max_length = kwargs.pop('_max_length', _get_platform_max_length())
112113
retcode = 0
113114
stdout = b''
114115
stderr = b''
@@ -118,10 +119,10 @@ def xargs(cmd, varargs, **kwargs):
118119
except parse_shebang.ExecutableNotFoundError as e:
119120
return e.to_output()
120121

121-
partitions = partition(cmd, varargs, target_concurrency, **kwargs)
122+
partitions = partition(cmd, varargs, target_concurrency, max_length)
122123

123124
def run_cmd_partition(run_cmd):
124-
return cmd_output(*run_cmd, encoding=None, retcode=None)
125+
return cmd_output(*run_cmd, encoding=None, retcode=None, **kwargs)
125126

126127
threads = min(len(partitions), target_concurrency)
127128
with _thread_mapper(threads) as thread_map:

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'))

tests/xargs_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,13 @@ def test_thread_mapper_concurrency_uses_threadpoolexecutor_map():
208208
def test_thread_mapper_concurrency_uses_regular_map():
209209
with xargs._thread_mapper(1) as thread_map:
210210
assert thread_map is map
211+
212+
213+
def test_xargs_propagate_kwargs_to_cmd():
214+
env = {'PRE_COMMIT_TEST_VAR': 'Pre commit is awesome'}
215+
cmd = ('bash', '-c', 'echo $PRE_COMMIT_TEST_VAR', '--')
216+
cmd = parse_shebang.normalize_cmd(cmd)
217+
218+
ret, stdout, _ = xargs.xargs(cmd, ('1',), env=env)
219+
assert ret == 0
220+
assert b'Pre commit is awesome' in stdout

0 commit comments

Comments
 (0)