Skip to content

Commit 120d602

Browse files
author
Michael Vincent
committed
Improve performance by ignoring submodules
When git status runs in a repo with submodules, it'll recursively run git status in every submodule as well by default (sequentially). git status is substantially slower on Windows than on Linux. git diff behaves similarly to git status in terms of running recursively within all submodules. In repos with hundreds of submodules, this quickly adds up when git status/diff are called multiple times. Pre-commit runs git status once at the beginning of an operation and then runs git diff before and after each hook. These calls quickly add up and make pre-commit unusable in large repos with lots of submodules. This commit drastically improves performance in repos with lots of submodules and fixes pre-commit#1701 by telling git status and git diff to ignore submodules. This change is not expected to have any negative effect on existing hooks because each submodule should manage its own hooks instead of relying on superproject hooks to manipulate their contents.
1 parent 8419586 commit 120d602

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

pre_commit/commands/run.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ def _all_filenames(args: argparse.Namespace) -> Collection[str]:
258258

259259

260260
def _get_diff() -> bytes:
261-
_, out, _ = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
261+
_, out, _ = cmd_output_b(
262+
'git', 'diff', '--no-ext-diff', '--ignore-submodules', retcode=None,
263+
)
262264
return out
263265

264266

pre_commit/git.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ def get_staged_files(cwd: Optional[str] = None) -> List[str]:
130130

131131

132132
def intent_to_add_files() -> List[str]:
133-
_, stdout, _ = cmd_output('git', 'status', '--porcelain', '-z')
133+
_, stdout, _ = cmd_output(
134+
'git', 'status', '--ignore-submodules', '--porcelain', '-z',
135+
)
134136
parts = list(reversed(zsplit(stdout)))
135137
intent_to_add = []
136138
while parts:

0 commit comments

Comments
 (0)