Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/scmrepo/git/backend/dulwich/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,36 @@ def commit(self, msg: str, no_verify: bool = False):
try:
commit(self.root_dir, message=msg, no_verify=no_verify)
except InvalidUserIdentity as exc:
raise SCMError(
"Git username and email must be configured"
) from exc
identity = self._get_codespaces_identity()
if identity is not None:
commit(
self.root_dir,
message=msg,
no_verify=no_verify,
committer=identity,
author=identity,
)
else:
raise SCMError(
"Git username and email must be configured"
) from exc
except TimezoneFormatError as exc:
raise SCMError("Invalid Git timestamp") from exc

def _get_codespaces_identity(self) -> Optional[bytes]:
from dulwich.config import ConfigFile, StackedConfig
from dulwich.repo import get_user_identity

if "CODESPACES" not in os.environ:
return None
try:
config = StackedConfig(
[ConfigFile.from_path("/usr/local/etc/gitconfig")]
)
return get_user_identity(config)
except Exception: # pylint: disable=broad-except
return None

def checkout(
self,
branch: str,
Expand Down
24 changes: 22 additions & 2 deletions src/scmrepo/git/backend/pygit2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Union,
)

from funcy import cached_property
from funcy import cached_property, first

from scmrepo.exceptions import (
CloneError,
Expand All @@ -32,6 +32,8 @@


if TYPE_CHECKING:
from pygit2 import Signature

from scmrepo.progress import GitProgressEvent


Expand Down Expand Up @@ -123,14 +125,32 @@ def _resolve_refish(self, refish: str):
return commit, ref

@property
def default_signature(self):
def default_signature(self) -> "Signature":
try:
return self.repo.default_signature
except KeyError as exc:
signature = self._get_codespaces_signature()
if signature is not None:
return signature
raise SCMError(
"Git username and email must be configured"
) from exc

def _get_codespaces_signature(self) -> Optional["Signature"]:
from pygit2 import Config, Signature

if "CODESPACES" not in os.environ:
return None
try:
config = Config("/usr/local/etc/gitconfig")
name = first(config.get_multivar("user.name"))
email = first(config.get_multivar("user.email"))
if name and email:
return Signature(name, email)
except Exception: # pylint: disable=broad-except
pass
return None

@staticmethod
def _get_checkout_strategy(strategy: Optional[int] = None):
from pygit2 import (
Expand Down