Skip to content

Commit 283adbe

Browse files
shchekleinpmrowla
authored andcommitted
feat(pygit2): signature doen't break if env is set and git conf not
1 parent 63a1b4b commit 283adbe

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

src/scmrepo/git/backend/pygit2/__init__.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,32 @@ def committer(self) -> "Signature":
142142
def _get_signature(self, name: str) -> "Signature":
143143
from pygit2 import Signature
144144

145-
sig = self.default_signature
145+
try:
146+
sig = self.default_signature
147+
except SCMError:
148+
logger.debug("No default signature (config is not set in repo)")
149+
sig = None
150+
146151
if os.environ.get(f"{name}_DATE"):
147152
raise NotImplementedError("signature date override unsupported")
153+
154+
user_name = os.environ.get(f"{name}_NAME", sig.name if sig else None)
155+
user_email = os.environ.get(f"{name}_EMAIL", sig.email if sig else None)
156+
157+
if not user_email or not user_name:
158+
raise SCMError("Git user name and email must be configured")
159+
160+
if sig:
161+
return Signature(
162+
name=user_name,
163+
email=user_email,
164+
time=sig.time,
165+
offset=sig.offset,
166+
)
167+
148168
return Signature(
149-
name=os.environ.get(f"{name}_NAME", sig.name),
150-
email=os.environ.get(f"{name}_EMAIL", sig.email),
151-
time=sig.time,
152-
offset=sig.offset,
169+
name=user_name,
170+
email=user_email,
153171
)
154172

155173
@staticmethod

tests/test_pygit2.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# pylint: disable=unused-argument
2+
import os
3+
24
import pygit2
35
import pytest
46
from pytest_mock import MockerFixture
57
from pytest_test_utils import TmpDir
68

9+
from scmrepo.exceptions import SCMError
710
from scmrepo.git import Git
811
from scmrepo.git.backend.pygit2 import Pygit2Backend
912

@@ -68,8 +71,41 @@ def test_pygit_stash_apply_conflicts(
6871
"ssh://[email protected]:12345/repository.git",
6972
],
7073
)
71-
def test_pygit2_ssh_error(tmp_dir: TmpDir, scm: Git, url):
74+
def test_pygit_ssh_error(tmp_dir: TmpDir, scm: Git, url):
7275
backend = Pygit2Backend(tmp_dir)
7376
with pytest.raises(NotImplementedError):
7477
with backend.get_remote(url):
7578
pass
79+
80+
81+
@pytest.mark.parametrize("name", ["committer", "author"])
82+
def test_pygit_use_env_vars_for_signature(
83+
tmp_dir: TmpDir, mocker: MockerFixture, name: str
84+
):
85+
from pygit2 import Signature
86+
87+
mocker.patch(
88+
"scmrepo.git.Pygit2Backend.default_signature",
89+
new=mocker.PropertyMock(side_effect=SCMError),
90+
)
91+
git = Git.init(tmp_dir)
92+
with pytest.raises(SCMError):
93+
git.pygit2.default_signature # pylint: disable=W0104
94+
95+
# Make sure that the environment variables are not set to not interfere with
96+
# with the check below
97+
for var in [f"GIT_{name.upper()}_NAME", f"GIT_{name.upper()}_EMAIL"]:
98+
assert os.environ.get(var, None) is None
99+
100+
# Basic expected behavior if vars are not set. Another sanity check
101+
with pytest.raises(SCMError):
102+
getattr(git.pygit2, name)
103+
104+
mocker.patch.dict(os.environ, {f"GIT_{name.upper()}_EMAIL": "[email protected]"})
105+
with pytest.raises(SCMError):
106+
getattr(git.pygit2, name)
107+
108+
mocker.patch.dict(os.environ, {f"GIT_{name.upper()}_NAME": "R. Daneel Olivaw"})
109+
assert getattr(git.pygit2, name) == Signature(
110+
email="[email protected]", name="R. Daneel Olivaw"
111+
)

0 commit comments

Comments
 (0)