Skip to content

Commit d021bbf

Browse files
authored
Merge pull request pre-commit#2071 from pre-commit/submodule-recurse-bug
make sure to not discard changes even if submodule.recurse=1
2 parents e0e3fab + e9ed248 commit d021bbf

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

pre_commit/staged_files_only.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
logger = logging.getLogger('pre_commit')
1515

16+
# without forcing submodule.recurse=0, changes in nested submodules will be
17+
# discarded if `submodule.recurse=1` is configured
18+
# we choose this instead of `--no-recurse-submodules` because it works on
19+
# versions of git before that option was added to `git checkout`
20+
_CHECKOUT_CMD = ('git', '-c', 'submodule.recurse=0', 'checkout', '--', '.')
21+
1622

1723
def _git_apply(patch: str) -> None:
1824
args = ('apply', '--whitespace=nowarn', patch)
@@ -58,7 +64,7 @@ def _unstaged_changes_cleared(patch_dir: str) -> Generator[None, None, None]:
5864

5965
# prevent recursive post-checkout hooks (#1418)
6066
no_checkout_env = dict(os.environ, _PRE_COMMIT_SKIP_POST_CHECKOUT='1')
61-
cmd_output_b('git', 'checkout', '--', '.', env=no_checkout_env)
67+
cmd_output_b(*_CHECKOUT_CMD, env=no_checkout_env)
6268

6369
try:
6470
yield
@@ -74,7 +80,7 @@ def _unstaged_changes_cleared(patch_dir: str) -> Generator[None, None, None]:
7480
# We failed to apply the patch, presumably due to fixes made
7581
# by hooks.
7682
# Roll back the changes made by hooks.
77-
cmd_output_b('git', 'checkout', '--', '.', env=no_checkout_env)
83+
cmd_output_b(*_CHECKOUT_CMD, env=no_checkout_env)
7884
_git_apply(patch_filename)
7985

8086
logger.info(f'Restored changes from {patch_filename}.')

tests/staged_files_only_test.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,11 @@ def test_img_conflict(img_staged, patch_dir):
181181

182182

183183
@pytest.fixture
184-
def submodule_with_commits(tempdir_factory):
184+
def repo_with_commits(tempdir_factory):
185185
path = git_dir(tempdir_factory)
186186
with cwd(path):
187+
open('foo', 'a+').close()
188+
cmd_output('git', 'add', 'foo')
187189
git_commit()
188190
rev1 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
189191
git_commit()
@@ -196,18 +198,21 @@ def checkout_submodule(rev):
196198

197199

198200
@pytest.fixture
199-
def sub_staged(submodule_with_commits, tempdir_factory):
201+
def sub_staged(repo_with_commits, tempdir_factory):
200202
path = git_dir(tempdir_factory)
201203
with cwd(path):
204+
open('bar', 'a+').close()
205+
cmd_output('git', 'add', 'bar')
206+
git_commit()
202207
cmd_output(
203-
'git', 'submodule', 'add', submodule_with_commits.path, 'sub',
208+
'git', 'submodule', 'add', repo_with_commits.path, 'sub',
204209
)
205-
checkout_submodule(submodule_with_commits.rev1)
210+
checkout_submodule(repo_with_commits.rev1)
206211
cmd_output('git', 'add', 'sub')
207212
yield auto_namedtuple(
208213
path=path,
209214
sub_path=os.path.join(path, 'sub'),
210-
submodule=submodule_with_commits,
215+
submodule=repo_with_commits,
211216
)
212217

213218

@@ -242,6 +247,34 @@ def test_sub_something_unstaged(sub_staged, patch_dir):
242247
_test_sub_state(sub_staged, 'rev2', 'AM')
243248

244249

250+
def test_submodule_does_not_discard_changes(sub_staged, patch_dir):
251+
with open('bar', 'w') as f:
252+
f.write('unstaged changes')
253+
254+
foo_path = os.path.join(sub_staged.sub_path, 'foo')
255+
with open(foo_path, 'w') as f:
256+
f.write('foo contents')
257+
258+
with staged_files_only(patch_dir):
259+
with open('bar') as f:
260+
assert f.read() == ''
261+
262+
with open(foo_path) as f:
263+
assert f.read() == 'foo contents'
264+
265+
with open('bar') as f:
266+
assert f.read() == 'unstaged changes'
267+
268+
with open(foo_path) as f:
269+
assert f.read() == 'foo contents'
270+
271+
272+
def test_submodule_does_not_discard_changes_recurse(sub_staged, patch_dir):
273+
cmd_output('git', 'config', 'submodule.recurse', '1', cwd=sub_staged.path)
274+
275+
test_submodule_does_not_discard_changes(sub_staged, patch_dir)
276+
277+
245278
def test_stage_utf8_changes(foo_staged, patch_dir):
246279
contents = '\u2603'
247280
with open('foo', 'w', encoding='UTF-8') as foo_file:

0 commit comments

Comments
 (0)