Skip to content

Commit

Permalink
feat(pygit2): signature doen't break if env is set and git conf not
Browse files Browse the repository at this point in the history
  • Loading branch information
shcheklein authored and pmrowla committed Aug 22, 2023
1 parent 63a1b4b commit 283adbe
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
28 changes: 23 additions & 5 deletions src/scmrepo/git/backend/pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,32 @@ def committer(self) -> "Signature":
def _get_signature(self, name: str) -> "Signature":
from pygit2 import Signature

sig = self.default_signature
try:
sig = self.default_signature
except SCMError:
logger.debug("No default signature (config is not set in repo)")
sig = None

if os.environ.get(f"{name}_DATE"):
raise NotImplementedError("signature date override unsupported")

user_name = os.environ.get(f"{name}_NAME", sig.name if sig else None)
user_email = os.environ.get(f"{name}_EMAIL", sig.email if sig else None)

if not user_email or not user_name:
raise SCMError("Git user name and email must be configured")

if sig:
return Signature(
name=user_name,
email=user_email,
time=sig.time,
offset=sig.offset,
)

return Signature(
name=os.environ.get(f"{name}_NAME", sig.name),
email=os.environ.get(f"{name}_EMAIL", sig.email),
time=sig.time,
offset=sig.offset,
name=user_name,
email=user_email,
)

@staticmethod
Expand Down
38 changes: 37 additions & 1 deletion tests/test_pygit2.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# pylint: disable=unused-argument
import os

import pygit2
import pytest
from pytest_mock import MockerFixture
from pytest_test_utils import TmpDir

from scmrepo.exceptions import SCMError
from scmrepo.git import Git
from scmrepo.git.backend.pygit2 import Pygit2Backend

Expand Down Expand Up @@ -68,8 +71,41 @@ def test_pygit_stash_apply_conflicts(
"ssh://login@server.com:12345/repository.git",
],
)
def test_pygit2_ssh_error(tmp_dir: TmpDir, scm: Git, url):
def test_pygit_ssh_error(tmp_dir: TmpDir, scm: Git, url):
backend = Pygit2Backend(tmp_dir)
with pytest.raises(NotImplementedError):
with backend.get_remote(url):
pass


@pytest.mark.parametrize("name", ["committer", "author"])
def test_pygit_use_env_vars_for_signature(
tmp_dir: TmpDir, mocker: MockerFixture, name: str
):
from pygit2 import Signature

mocker.patch(
"scmrepo.git.Pygit2Backend.default_signature",
new=mocker.PropertyMock(side_effect=SCMError),
)
git = Git.init(tmp_dir)
with pytest.raises(SCMError):
git.pygit2.default_signature # pylint: disable=W0104

# Make sure that the environment variables are not set to not interfere with
# with the check below
for var in [f"GIT_{name.upper()}_NAME", f"GIT_{name.upper()}_EMAIL"]:
assert os.environ.get(var, None) is None

# Basic expected behavior if vars are not set. Another sanity check
with pytest.raises(SCMError):
getattr(git.pygit2, name)

mocker.patch.dict(os.environ, {f"GIT_{name.upper()}_EMAIL": "olivaw@iterative.ai"})
with pytest.raises(SCMError):
getattr(git.pygit2, name)

mocker.patch.dict(os.environ, {f"GIT_{name.upper()}_NAME": "R. Daneel Olivaw"})
assert getattr(git.pygit2, name) == Signature(
email="olivaw@iterative.ai", name="R. Daneel Olivaw"
)

0 comments on commit 283adbe

Please sign in to comment.