Skip to content

Commit dd843c8

Browse files
Merge branch 'master' into master
2 parents cd1bb98 + 76917be commit dd843c8

File tree

6 files changed

+104
-20
lines changed

6 files changed

+104
-20
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

README.rst

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Unwanted files must be excluded by discarding them via ``MANIFEST.in``.
1515
.. image:: https://tidelift.com/badges/package/pypi/setuptools-scm
1616
:target: https://tidelift.com/subscription/pkg/pypi-setuptools-scm?utm_source=pypi-setuptools-scm&utm_medium=readme
1717

18+
1819
``pyproject.toml`` usage
1920
------------------------
2021

@@ -50,7 +51,7 @@ To enable version inference, add this section to your pyproject.toml:
5051
.. code:: toml
5152
5253
# pyproject.toml
53-
[tools.setuptools_scm]
54+
[tool.setuptools_scm]
5455
5556
Including this section is comparable to supplying
5657
``use_scm_version=True`` in ``setup.py``. Additionally,
@@ -60,7 +61,8 @@ to be supplied to ``get_version()``. For example:
6061
.. code:: toml
6162
6263
# pyproject.toml
63-
[tools.setuptools_scm]
64+
65+
[tool.setuptools_scm]
6466
write_to = "pkg/version.py"
6567
6668
@@ -103,20 +105,7 @@ Arguments to ``get_version()`` (see below) may be passed as a dictionary to
103105
...,
104106
)
105107
106-
Once configured, you can access the version number in your package via
107-
``pkg_resources`` (`PEP-0396 <https://www.python.org/dev/peps/pep-0396>`_). For
108-
example:
109-
110-
.. code:: python
111-
112-
from pkg_resources import get_distribution, DistributionNotFound
113-
try:
114-
__version__ = get_distribution(__name__).version
115-
except DistributionNotFound:
116-
# package is not installed
117-
pass
118-
119-
You can also confirm the version number locally via ``setup.py``:
108+
You can confirm the version number locally via ``setup.py``:
120109

121110
.. code-block:: shell
122111
@@ -129,8 +118,8 @@ You can also confirm the version number locally via ``setup.py``:
129118
not defined in ``setup.cfg``.
130119

131120

132-
``setup.cfg``
133-
-------------
121+
``setup.cfg`` usage
122+
-------------------
134123

135124
If using `setuptools 30.3.0
136125
<https://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files>`_
@@ -189,6 +178,43 @@ than the project's root, you can use:
189178
See `setup.py Usage`_ above for how to use this within ``setup.py``.
190179

191180

181+
Retrieving package version at runtime
182+
-------------------------------------
183+
184+
If you have opted not to hardcode the version number inside the package,
185+
you can retrieve it at runtime from PEP-0566_ metadata using
186+
``importlib.metadata`` from the standard library
187+
or the `importlib_metadata`_ backport:
188+
189+
.. code:: python
190+
191+
from importlib.metadata import version, PackageNotFoundError
192+
193+
try:
194+
__version__ = version(__name__)
195+
except PackageNotFoundError:
196+
# package is not installed
197+
pass
198+
199+
Alternatively, you can use ``pkg_resources`` which is included in
200+
``setuptools``:
201+
202+
.. code:: python
203+
204+
from pkg_resources import get_distribution, DistributionNotFound
205+
206+
try:
207+
__version__ = get_distribution(__name__).version
208+
except DistributionNotFound:
209+
# package is not installed
210+
pass
211+
212+
This does place a runtime dependency on ``setuptools``.
213+
214+
.. _PEP-0566: https://www.python.org/dev/peps/pep-0566/
215+
.. _importlib_metadata: https://pypi.org/project/importlib-metadata/
216+
217+
192218
Usage from Sphinx
193219
-----------------
194220

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)