From 66caa345b1cc1a49cf5fcd1befe72cd052dbe834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Rowlands=20=28=EB=B3=80=EA=B8=B0=ED=98=B8=29?= Date: Thu, 1 Apr 2021 17:04:45 +0900 Subject: [PATCH] git: verify commit user is set (#5754) --- dvc/scm/git/backend/dulwich.py | 14 +++++++++++++- dvc/scm/git/backend/pygit2.py | 13 +++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/dvc/scm/git/backend/dulwich.py b/dvc/scm/git/backend/dulwich.py index 517978bd5..d7f91b7fe 100644 --- a/dvc/scm/git/backend/dulwich.py +++ b/dvc/scm/git/backend/dulwich.py @@ -182,11 +182,16 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method def commit(self, msg: str, no_verify: bool = False): from dulwich.errors import CommitError from dulwich.porcelain import 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 def checkout( self, @@ -528,6 +533,8 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method message: Optional[str] = None, include_untracked: Optional[bool] = False, ) -> Tuple[Optional[str], bool]: + from dulwich.repo import InvalidUserIdentity + from dvc.scm.git import Stash if include_untracked or ref == Stash.DEFAULT_STASH: @@ -537,7 +544,12 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method stash = self._get_stash(ref) message_b = message.encode("utf-8") if message else None - rev = stash.push(message=message_b) + try: + rev = stash.push(message=message_b) + except InvalidUserIdentity as exc: + raise SCMError( + "Git username and email must be configured" + ) from exc return os.fsdecode(rev), True def _stash_apply(self, rev: str): diff --git a/dvc/scm/git/backend/pygit2.py b/dvc/scm/git/backend/pygit2.py index a00c3aef8..1c8ab6194 100644 --- a/dvc/scm/git/backend/pygit2.py +++ b/dvc/scm/git/backend/pygit2.py @@ -80,6 +80,15 @@ class Pygit2Backend(BaseGitBackend): # pylint:disable=abstract-method return RefdbFsBackend(self.repo) + @property + def default_signature(self): + try: + return self.repo.default_signature + except KeyError as exc: + raise SCMError( + "Git username and email must be configured" + ) from exc + @staticmethod def clone( url: str, @@ -328,7 +337,7 @@ class Pygit2Backend(BaseGitBackend): # pylint:disable=abstract-method from dvc.scm.git import Stash oid = self.repo.stash( - self.repo.default_signature, + self.default_signature, message=message, include_untracked=include_untracked, ) @@ -503,7 +512,7 @@ class Pygit2Backend(BaseGitBackend): # pylint:disable=abstract-method raise MergeConflictError("Merge contained conflicts") if commit: - user = self.repo.default_signature + user = self.default_signature tree = self.repo.index.write_tree() merge_commit = self.repo.create_commit( "HEAD", user, user, msg, tree, [self.repo.head.target, rev]