summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRonny Pfannschmidt <opensource@ronnypfannschmidt.de>2019-12-13 23:12:14 +0100
committerRonny Pfannschmidt <opensource@ronnypfannschmidt.de>2019-12-13 23:12:14 +0100
commit6e1c37cf17b2052c2915e999369d39bc30a122ad (patch)
tree035b554781ce07aca4bf880acb7fbb442c2f1e3b
parentb26710a3eb67e2b1b68259273ac146daa2fecc07 (diff)
downloadsetuptools-scm-6e1c37cf17b2052c2915e999369d39bc30a122ad.tar.gz
fix #381 - clean out env vars from the git hook system
-rw-r--r--CHANGELOG.rst2
-rw-r--r--src/setuptools_scm/utils.py25
-rwxr-xr-xtesting/play_out_381.bash23
-rw-r--r--testing/test_git.py11
4 files changed, 59 insertions, 2 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index c30f2bc..c800c01 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -6,6 +6,8 @@ v3.4.0
* fix #305 - ensure the git file finder closes filedescriptors even when errors happen
+* fix #381 - clean out env vars from the git hook system to ensure correct function from within
+
v3.3.3
======
diff --git a/src/setuptools_scm/utils.py b/src/setuptools_scm/utils.py
index d2c5b63..15ca83e 100644
--- a/src/setuptools_scm/utils.py
+++ b/src/setuptools_scm/utils.py
@@ -20,6 +20,27 @@ PY3 = sys.version_info > (3,)
string_types = (str,) if PY3 else (str, unicode) # noqa
+def no_git_env(env):
+ # adapted from pre-commit
+ # Too many bugs dealing with environment variables and GIT:
+ # https://github.com/pre-commit/pre-commit/issues/300
+ # In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running
+ # pre-commit hooks
+ # In git 1.9.1 (maybe others), git exports GIT_DIR and GIT_INDEX_FILE
+ # while running pre-commit hooks in submodules.
+ # GIT_DIR: Causes git clone to clone wrong thing
+ # GIT_INDEX_FILE: Causes 'error invalid object ...' during commit
+ for k, v in env.items():
+ if k.startswith("GIT_"):
+ trace(k, v)
+ return {
+ k: v
+ for k, v in env.items()
+ if not k.startswith("GIT_")
+ or k in ("GIT_EXEC_PATH", "GIT_SSH", "GIT_SSH_COMMAND")
+ }
+
+
def trace(*k):
if DEBUG:
print(*k)
@@ -48,7 +69,6 @@ def _always_strings(env_dict):
def _popen_pipes(cmd, cwd):
-
return subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
@@ -56,7 +76,8 @@ def _popen_pipes(cmd, cwd):
cwd=str(cwd),
env=_always_strings(
dict(
- os.environ,
+ no_git_env(os.environ),
+ # os.environ,
# try to disable i18n
LC_ALL="C",
LANGUAGE="",
diff --git a/testing/play_out_381.bash b/testing/play_out_381.bash
new file mode 100755
index 0000000..be9d23c
--- /dev/null
+++ b/testing/play_out_381.bash
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+set -euxo pipefail
+
+rm -rf y z home venv tmp
+
+[ ! -d black ] && git clone https://github.com/psf/black
+export SETUPTOOLS_SCM_DEBUG=1
+export PRE_COMMIT_HOME="$PWD/home"
+export TMPDIR="$PWD/tmp"
+
+git init y
+git init z
+git -C z commit --allow-empty -m 'commit!'
+git -C y submodule add "$PWD/z"
+cat > "$PWD/y/.git/modules/z/hooks/pre-commit" <<EOF
+#!/usr/bin/env bash
+virtualenv "$PWD/venv"
+"$PWD/venv/bin/pip" install -e "$1"
+"$PWD/venv/bin/pip" install --no-clean "$PWD/black"
+EOF
+chmod +x "$PWD/y/.git/modules/z/hooks/pre-commit"
+cd y/z
+git commit -m "test"
diff --git a/testing/test_git.py b/testing/test_git.py
index 0e025f1..1a89da4 100644
--- a/testing/test_git.py
+++ b/testing/test_git.py
@@ -229,3 +229,14 @@ def test_not_matching_tags(wd):
tag_regex=r"^apache-arrow-([\.0-9]+)$",
git_describe_command="git describe --dirty --tags --long --exclude *js* ",
).startswith("0.11.2")
+
+
+@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/381")
+def test_gitdir(monkeypatch, wd):
+ """
+ """
+ wd.commit_testfile()
+ normal = wd.version
+ # git hooks set this and break subsequent setuptools_scm unless we clean
+ monkeypatch.setenv("GIT_DIR", __file__)
+ assert wd.version == normal