From e318deca4c6d4869e39525cebf7ca481b7c2b570 Mon Sep 17 00:00:00 2001 From: Peter Rowlands Date: Fri, 14 Oct 2022 15:00:19 +0900 Subject: [PATCH] dulwich: reraise porcelain.Error when using porcelain methods --- src/scmrepo/git/backend/dulwich/__init__.py | 46 +++++++++++---------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/scmrepo/git/backend/dulwich/__init__.py b/src/scmrepo/git/backend/dulwich/__init__.py index 6e8185fa..ae8315b6 100644 --- a/src/scmrepo/git/backend/dulwich/__init__.py +++ b/src/scmrepo/git/backend/dulwich/__init__.py @@ -18,7 +18,7 @@ Union, ) -from funcy import cached_property +from funcy import cached_property, reraise from scmrepo.exceptions import ( AuthError, @@ -284,17 +284,18 @@ def add(self, paths: Union[str, Iterable[str]], update=False): def commit(self, msg: str, no_verify: bool = False): from dulwich.errors import CommitError - from dulwich.porcelain import commit + from dulwich.porcelain import Error, TimezoneFormatError, commit from dulwich.repo import InvalidUserIdentity - try: - commit(self.root_dir, message=msg, no_verify=no_verify) - except CommitError as exc: - raise SCMError("Git commit failed") from exc - except InvalidUserIdentity as exc: - raise SCMError( - "Git username and email must be configured" - ) from exc + with reraise((Error, CommitError), SCMError("Git commit failed")): + 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 + except TimezoneFormatError as exc: + raise SCMError("Invalid Git timestamp") from exc def checkout( self, @@ -311,16 +312,17 @@ def fetch( force: bool = False, unshallow: bool = False, ): - from dulwich.porcelain import fetch + from dulwich.porcelain import Error, fetch from dulwich.protocol import DEPTH_INFINITE - remote_b = os.fsencode(remote) if remote else b"origin" - fetch( - self.repo, - remote_location=remote_b, - force=force, - depth=DEPTH_INFINITE if unshallow else None, - ) + with reraise(Error, SCMError("Git fetch failed")): + remote_b = os.fsencode(remote) if remote else b"origin" + fetch( + self.repo, + remote_location=remote_b, + force=force, + depth=DEPTH_INFINITE if unshallow else None, + ) def pull(self, **kwargs): raise NotImplementedError @@ -768,11 +770,13 @@ def checkout_index( def status( self, ignored: bool = False, untracked_files: str = "all" ) -> Tuple[Mapping[str, Iterable[str]], Iterable[str], Iterable[str]]: + from dulwich.porcelain import Error from dulwich.porcelain import status as git_status - staged, unstaged, untracked = git_status( - self.root_dir, ignored=ignored, untracked_files=untracked_files - ) + with reraise(Error, SCMError("Git status failed")): + staged, unstaged, untracked = git_status( + self.root_dir, ignored=ignored, untracked_files=untracked_files + ) return ( {