From ab9100c6d14fd7982529a13d7d94442157613bfc Mon Sep 17 00:00:00 2001 From: Xiao Di Guan Date: Mon, 7 Feb 2022 13:15:28 +1100 Subject: Suppress GPG signature when getting HEAD committer date (#548) --- src/setuptools_scm/git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/setuptools_scm/git.py b/src/setuptools_scm/git.py index 3fe8038..30c7010 100644 --- a/src/setuptools_scm/git.py +++ b/src/setuptools_scm/git.py @@ -69,7 +69,7 @@ class GitWorkdir(Workdir): return branch def get_head_date(self): - timestamp, err, ret = self.do_ex("git log -n 1 HEAD --format=%cI") + timestamp, err, ret = self.do_ex("git -c log.showSignature=false log -n 1 HEAD --format=%cI") if ret: trace("timestamp err", timestamp, err, ret) return -- cgit v1.2.1 From affd0fb1c972c41be002826d200ec441dd03ed0f Mon Sep 17 00:00:00 2001 From: Xiao Di Guan Date: Mon, 7 Feb 2022 13:57:41 +1100 Subject: Reformat with Black --- src/setuptools_scm/git.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/setuptools_scm/git.py b/src/setuptools_scm/git.py index 30c7010..f6933ff 100644 --- a/src/setuptools_scm/git.py +++ b/src/setuptools_scm/git.py @@ -69,7 +69,9 @@ class GitWorkdir(Workdir): return branch def get_head_date(self): - timestamp, err, ret = self.do_ex("git -c log.showSignature=false log -n 1 HEAD --format=%cI") + timestamp, err, ret = self.do_ex( + "git -c log.showSignature=false log -n 1 HEAD --format=%cI" + ) if ret: trace("timestamp err", timestamp, err, ret) return -- cgit v1.2.1 From 38d79e03f64b25d6b26623b8e0c26fe6cb9067d5 Mon Sep 17 00:00:00 2001 From: Xiao Di Guan Date: Mon, 7 Feb 2022 21:02:37 +1100 Subject: Support signed commit commands in tests --- testing/conftest.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/testing/conftest.py b/testing/conftest.py index 0621347..6d9a84a 100644 --- a/testing/conftest.py +++ b/testing/conftest.py @@ -29,6 +29,7 @@ def pytest_addoption(parser): class Wd: commit_command = None + signed_commit_command = None add_command = None def __repr__(self): @@ -61,19 +62,22 @@ class Wd: else: return given_reason - def add_and_commit(self, reason=None): + def add_and_commit(self, reason=None, **kwargs): self(self.add_command) - self.commit(reason) + self.commit(reason, **kwargs) - def commit(self, reason=None): + def commit(self, reason=None, signed=False): reason = self._reason(reason) - self(self.commit_command, reason=reason) + self( + self.commit_command if not signed else self.signed_commit_command, + reason=reason, + ) - def commit_testfile(self, reason=None): + def commit_testfile(self, reason=None, **kwargs): reason = self._reason(reason) self.write("test.txt", "test {reason}", reason=reason) self(self.add_command) - self.commit(reason=reason) + self.commit(reason=reason, **kwargs) def get_version(self, **kw): __tracebackhide__ = True -- cgit v1.2.1 From e23cbb6f37d24f33e705dc343a4e7cd080d8cf1d Mon Sep 17 00:00:00 2001 From: Xiao Di Guan Date: Tue, 8 Feb 2022 00:14:55 +1100 Subject: Add test case for #548 --- testing/test_git.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/testing/test_git.py b/testing/test_git.py index b1b42ac..0e42905 100644 --- a/testing/test_git.py +++ b/testing/test_git.py @@ -402,3 +402,35 @@ def test_git_getdate_badgit( git_wd = git.GitWorkdir(os.fspath(wd.cwd)) with patch.object(git_wd, "do_ex", Mock(return_value=("%cI", "", 0))): assert git_wd.get_head_date() is None + + +@pytest.fixture +def signed_commit_wd(tmp_path, monkeypatch, wd): + if not has_command("gpg", warn=False): + pytest.skip("gpg executable not found") + + gpg_batch_params = tmp_path / "gpg_batch_params" + gpg_batch_params.write_text( + """\ +%no-protection +%transient-key +Key-Type: default +Name-Real: a test +Name-Email: test@example.com +Expire-Date: 0 +""" + ) + monkeypatch.setenv("GNUPGHOME", str(tmp_path)) + wd(f"gpg --batch --generate-key {gpg_batch_params}") + + wd("git config log.showSignature true") + wd.signed_commit_command = "git commit -S -m test-{reason}" + return wd + + +@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/548") +def test_git_getdate_signed_commit(signed_commit_wd): + today = date.today() + signed_commit_wd.commit_testfile(signed=True) + git_wd = git.GitWorkdir(os.fspath(signed_commit_wd.cwd)) + assert git_wd.get_head_date() == today -- cgit v1.2.1 From c496aaf5b068e98d06732915bd4d6178a3a77228 Mon Sep 17 00:00:00 2001 From: Xiao Di Guan Date: Tue, 8 Feb 2022 00:50:02 +1100 Subject: Make implicit `"help"` program argument overridable in `has_command` --- src/setuptools_scm/utils.py | 7 +++++-- testing/test_git.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/setuptools_scm/utils.py b/src/setuptools_scm/utils.py index 5591384..3c80fe0 100644 --- a/src/setuptools_scm/utils.py +++ b/src/setuptools_scm/utils.py @@ -8,6 +8,8 @@ import shlex import subprocess import sys import warnings +from typing import List +from typing import Optional DEBUG = bool(os.environ.get("SETUPTOOLS_SCM_DEBUG")) @@ -118,9 +120,10 @@ def function_has_arg(fn, argname): return argname in argspec -def has_command(name: str, warn: bool = True) -> bool: +def has_command(name: str, args: Optional[List[str]] = None, warn: bool = True) -> bool: try: - p = _popen_pipes([name, "help"], ".") + cmd = [name, "help"] if args is None else [name, *args] + p = _popen_pipes(cmd, ".") except OSError: trace(*sys.exc_info()) res = False diff --git a/testing/test_git.py b/testing/test_git.py index 0e42905..10922c8 100644 --- a/testing/test_git.py +++ b/testing/test_git.py @@ -406,7 +406,7 @@ def test_git_getdate_badgit( @pytest.fixture def signed_commit_wd(tmp_path, monkeypatch, wd): - if not has_command("gpg", warn=False): + if not has_command("gpg", args=["--version"], warn=False): pytest.skip("gpg executable not found") gpg_batch_params = tmp_path / "gpg_batch_params" -- cgit v1.2.1 From ced42420c838ed74f60870f07f8c9bd316d5d79c Mon Sep 17 00:00:00 2001 From: Xiao Di Guan Date: Wed, 9 Feb 2022 09:16:19 +1100 Subject: Install GnuPG where necessary for test CI --- .github/workflows/python-tests.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 542b221..459f6e1 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -38,6 +38,24 @@ jobs: msystem: MINGW64 install: git mingw-w64-x86_64-python mingw-w64-x86_64-python-setuptools update: true + - name: Setup GnuPG + # At present, the Windows VMs only come with the copy of GnuPG that's bundled + # with Git for Windows. If we want to use this version _and_ be able to set + # arbitrary GnuPG home directories, then the test would need to figure out when + # to convert Windows-style paths into Unix-style paths with cygpath, which is + # unreasonable. + # + # Instead, we'll install a version of GnuPG that can handle Windows-style paths. + # However, due to , installation fails if the PATH + # environment variable is too long. Consequently, we need to shorten PATH to + # something minimal before we can install GnuPG. For further details, see + # . + run: | + $env:PATH = "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\Chocolatey\bin" + [Environment]::SetEnvironmentVariable("Path", $env:PATH, "Machine") + choco install gnupg -y --no-progress + echo "C:\Program Files (x86)\gnupg\bin" >> $env:GITHUB_PATH + if: runner.os == 'Windows' - run: pip install -U 'setuptools>=45' if: matrix.python_version != 'msys2' - run: pip install -e .[toml,test] -- cgit v1.2.1 From 96a22aa2ccfe50beeba713e8bec5f6694473134a Mon Sep 17 00:00:00 2001 From: Xiao Di Guan Date: Wed, 9 Feb 2022 21:00:02 +1100 Subject: Pick and configure an explicit `Key-Type` If the default `Key-Type` happens to be an ECC, then `Key-Curve` would be a required parameter, so let's just pick RSA2048 and be done with it. --- testing/test_git.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testing/test_git.py b/testing/test_git.py index 10922c8..2682db5 100644 --- a/testing/test_git.py +++ b/testing/test_git.py @@ -414,7 +414,8 @@ def signed_commit_wd(tmp_path, monkeypatch, wd): """\ %no-protection %transient-key -Key-Type: default +Key-Type: RSA +Key-Length: 2048 Name-Real: a test Name-Email: test@example.com Expire-Date: 0 -- cgit v1.2.1 From 3d06305e5b5b6b214a7fbf7a0e1aa049e43ed73d Mon Sep 17 00:00:00 2001 From: Xiao Di Guan Date: Wed, 9 Feb 2022 23:10:12 +1100 Subject: Explicitly set `gpg.program` to avoid --- .github/workflows/python-tests.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 459f6e1..0778236 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -50,11 +50,16 @@ jobs: # environment variable is too long. Consequently, we need to shorten PATH to # something minimal before we can install GnuPG. For further details, see # . + # + # Additionally, we'll explicitly set `gpg.program` to ensure Git for Windows + # doesn't invoke the bundled GnuPG, otherwise we'll run into + # . See also: . run: | - $env:PATH = "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\Chocolatey\bin" + $env:PATH = "C:\Program Files\Git\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\Chocolatey\bin" [Environment]::SetEnvironmentVariable("Path", $env:PATH, "Machine") choco install gnupg -y --no-progress echo "C:\Program Files (x86)\gnupg\bin" >> $env:GITHUB_PATH + git config --system gpg.program "C:\Program Files (x86)\gnupg\bin\gpg.exe" if: runner.os == 'Windows' - run: pip install -U 'setuptools>=45' if: matrix.python_version != 'msys2' -- cgit v1.2.1 From ab34b17178b0dd687632910cf3304f51833248c5 Mon Sep 17 00:00:00 2001 From: Xiao Di Guan Date: Thu, 10 Feb 2022 18:41:42 +1100 Subject: Avoid having to quote full path to GPG batch params file --- testing/test_git.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/testing/test_git.py b/testing/test_git.py index 2682db5..ffb3d3c 100644 --- a/testing/test_git.py +++ b/testing/test_git.py @@ -405,12 +405,12 @@ def test_git_getdate_badgit( @pytest.fixture -def signed_commit_wd(tmp_path, monkeypatch, wd): +def signed_commit_wd(monkeypatch, wd): if not has_command("gpg", args=["--version"], warn=False): pytest.skip("gpg executable not found") - gpg_batch_params = tmp_path / "gpg_batch_params" - gpg_batch_params.write_text( + wd.write( + ".gpg_batch_params", """\ %no-protection %transient-key @@ -419,10 +419,10 @@ Key-Length: 2048 Name-Real: a test Name-Email: test@example.com Expire-Date: 0 -""" +""", ) - monkeypatch.setenv("GNUPGHOME", str(tmp_path)) - wd(f"gpg --batch --generate-key {gpg_batch_params}") + monkeypatch.setenv("GNUPGHOME", str(wd.cwd.resolve(strict=True))) + wd("gpg --batch --generate-key .gpg_batch_params") wd("git config log.showSignature true") wd.signed_commit_command = "git commit -S -m test-{reason}" -- cgit v1.2.1 From 6117629a1b403cb37f58e35795edcd8d4aefeb1b Mon Sep 17 00:00:00 2001 From: Xiao Di Guan Date: Thu, 10 Feb 2022 19:01:26 +1100 Subject: Update changelog --- CHANGELOG.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e171cc8..74f0f5a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +v6.4.3 +====== + +* fix #548: correctly handle parsing the commit timestamp of HEAD when ``log.showSignature`` is set + v6.4.2 ====== -- cgit v1.2.1