Skip to content

Commit 860d07d

Browse files
cbirkholdasottile
authored andcommitted
don't leak GIT_* env into npm install for language: node
When a node hook environment is installed from `git commit` inside a submodule, git exports absolute GIT_DIR / GIT_INDEX_FILE paths. `npm install git+file://<store clone>` inherited them, so npm's checkout of the hook repository overwrote the submodule's index and the commit failed with `fatal: unable to read <sha>`. Run the install with no_git_env(), as is already done for the initial clone in store.py. Signed-off-by: Chris Birkhold <[email protected]>
1 parent 9ad8a0a commit 860d07d

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎pre_commit/languages/node.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pre_commit.envcontext import PatchesT
1414
from pre_commit.envcontext import UNSET
1515
from pre_commit.envcontext import Var
16+
from pre_commit.git import no_git_env
1617
from pre_commit.languages.python import bin_dir
1718
from pre_commit.prefix import Prefix
1819
from pre_commit.util import cmd_output
@@ -103,4 +104,4 @@ def install_environment(
103104
'npm', 'install', '--allow-git=root', '--install-links', '-g',
104105
*pkgs,
105106
)
106-
lang_base.setup_cmd(prefix, install)
107+
lang_base.setup_cmd(prefix, install, env=no_git_env())

‎tests/languages/node_test.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,29 @@ def test_node_hook_system(tmp_path):
144144
assert ret == (0, b'Hello World\n')
145145

146146

147+
def test_node_hook_install_does_not_write_callers_index(tmp_path):
148+
# git sets GIT_DIR / GIT_INDEX_FILE to absolute paths when running hooks
149+
# in a submodule, `npm install git+file://...` must not inherit them
150+
hook_repo = tmp_path.joinpath('hook')
151+
hook_repo.mkdir()
152+
_make_hello_world(hook_repo)
153+
154+
user_repo = tmp_path.joinpath('user')
155+
cmd_output_b('git', 'init', user_repo)
156+
user_repo.joinpath('f').touch()
157+
cmd_output_b('git', 'add', 'f', cwd=user_repo)
158+
159+
git_dir = user_repo.joinpath('.git')
160+
with envcontext.envcontext((
161+
('GIT_DIR', str(git_dir)),
162+
('GIT_INDEX_FILE', str(git_dir.joinpath('index'))),
163+
)):
164+
ret = run_language(hook_repo, node, 'node-hello')
165+
assert ret == (0, b'Hello World\n')
166+
167+
assert cmd_output('git', 'ls-files', cwd=user_repo)[1] == 'f\n'
168+
169+
147170
def test_node_with_user_config_set(tmp_path):
148171
cfg = tmp_path.joinpath('cfg')
149172
cfg.write_text('cache=/dne\n')

0 commit comments

Comments
 (0)