Skip to content

Commit d175b92

Browse files
committed
pygit2: workaround for codespaces system config
1 parent 8b40388 commit d175b92

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/scmrepo/git/backend/pygit2.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Union,
1616
)
1717

18-
from funcy import cached_property
18+
from funcy import cached_property, first
1919

2020
from scmrepo.exceptions import (
2121
CloneError,
@@ -32,6 +32,8 @@
3232

3333

3434
if TYPE_CHECKING:
35+
from pygit2 import Signature
36+
3537
from scmrepo.progress import GitProgressEvent
3638

3739

@@ -123,14 +125,32 @@ def _resolve_refish(self, refish: str):
123125
return commit, ref
124126

125127
@property
126-
def default_signature(self):
128+
def default_signature(self) -> "Signature":
127129
try:
128130
return self.repo.default_signature
129131
except KeyError as exc:
132+
signature = self._get_codespaces_signature()
133+
if signature is not None:
134+
return signature
130135
raise SCMError(
131136
"Git username and email must be configured"
132137
) from exc
133138

139+
def _get_codespaces_signature(self) -> Optional["Signature"]:
140+
from pygit2 import Config, Signature
141+
142+
if "CODESPACES" not in os.environ:
143+
return None
144+
try:
145+
config = Config("/usr/local/etc/gitconfig")
146+
name = first(config.get_multivar("user.name"))
147+
email = first(config.get_multivar("user.email"))
148+
if name and email:
149+
return Signature(name, email)
150+
except Exception: # pylint: disable=broad-except
151+
pass
152+
return None
153+
134154
@staticmethod
135155
def _get_checkout_strategy(strategy: Optional[int] = None):
136156
from pygit2 import (

0 commit comments

Comments
 (0)