Skip to content

Commit 76917be

Browse files
Merge pull request #382 from RonnyPfannschmidt/check-381
fix #381 - clean out env vars from the git hook system
2 parents d34d6dc + 479cc9d commit 76917be

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ v3.4.0
66

77
* fix #305 - ensure the git file finder closes filedescriptors even when errors happen
88

9+
* fix #381 - clean out env vars from the git hook system to ensure correct function from within
10+
911
v3.3.3
1012
======
1113

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ include tox.ini
88
include *.rst
99
include LICENSE
1010
include *.toml
11+
recursive-include testing *.bash

src/setuptools_scm/utils.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020
string_types = (str,) if PY3 else (str, unicode) # noqa
2121

2222

23+
def no_git_env(env):
24+
# adapted from pre-commit
25+
# Too many bugs dealing with environment variables and GIT:
26+
# https://github.com/pre-commit/pre-commit/issues/300
27+
# In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running
28+
# pre-commit hooks
29+
# In git 1.9.1 (maybe others), git exports GIT_DIR and GIT_INDEX_FILE
30+
# while running pre-commit hooks in submodules.
31+
# GIT_DIR: Causes git clone to clone wrong thing
32+
# GIT_INDEX_FILE: Causes 'error invalid object ...' during commit
33+
for k, v in env.items():
34+
if k.startswith("GIT_"):
35+
trace(k, v)
36+
return {
37+
k: v
38+
for k, v in env.items()
39+
if not k.startswith("GIT_")
40+
or k in ("GIT_EXEC_PATH", "GIT_SSH", "GIT_SSH_COMMAND")
41+
}
42+
43+
2344
def trace(*k):
2445
if DEBUG:
2546
print(*k)
@@ -48,15 +69,15 @@ def _always_strings(env_dict):
4869

4970

5071
def _popen_pipes(cmd, cwd):
51-
5272
return subprocess.Popen(
5373
cmd,
5474
stdout=subprocess.PIPE,
5575
stderr=subprocess.PIPE,
5676
cwd=str(cwd),
5777
env=_always_strings(
5878
dict(
59-
os.environ,
79+
no_git_env(os.environ),
80+
# os.environ,
6081
# try to disable i18n
6182
LC_ALL="C",
6283
LANGUAGE="",

testing/play_out_381.bash

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
set -euxo pipefail
3+
4+
rm -rf y z home venv tmp
5+
6+
[ ! -d black ] && git clone https://github.com/psf/black
7+
export SETUPTOOLS_SCM_DEBUG=1
8+
export PRE_COMMIT_HOME="$PWD/home"
9+
export TMPDIR="$PWD/tmp"
10+
11+
git init y
12+
git init z
13+
git -C z commit --allow-empty -m 'commit!'
14+
git -C y submodule add "$PWD/z"
15+
cat > "$PWD/y/.git/modules/z/hooks/pre-commit" <<EOF
16+
#!/usr/bin/env bash
17+
virtualenv "$PWD/venv"
18+
"$PWD/venv/bin/pip" install -e "$1"
19+
"$PWD/venv/bin/pip" install --no-clean "$PWD/black"
20+
EOF
21+
chmod +x "$PWD/y/.git/modules/z/hooks/pre-commit"
22+
cd y/z
23+
git commit -m "test"

testing/test_git.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,14 @@ def test_not_matching_tags(wd):
229229
tag_regex=r"^apache-arrow-([\.0-9]+)$",
230230
git_describe_command="git describe --dirty --tags --long --exclude *js* ",
231231
).startswith("0.11.2")
232+
233+
234+
@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/381")
235+
def test_gitdir(monkeypatch, wd):
236+
"""
237+
"""
238+
wd.commit_testfile()
239+
normal = wd.version
240+
# git hooks set this and break subsequent setuptools_scm unless we clean
241+
monkeypatch.setenv("GIT_DIR", __file__)
242+
assert wd.version == normal

0 commit comments

Comments
 (0)