From 568abb3e28383152d5c80aaf7983570dca6c32bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saugat=20Pachhai=20=28=E0=A4=B8=E0=A5=8C=E0=A4=97=E0=A4=BE?= =?UTF-8?q?=E0=A4=A4=29?= Date: Tue, 23 Nov 2021 12:35:56 +0545 Subject: [PATCH] scm: remove dependency from DvcException --- dvc/exceptions.py | 12 ------------ dvc/repo/install.py | 12 +++++++++++- dvc/scm/exceptions.py | 8 ++++++++ dvc/scm/git/__init__.py | 4 ++-- tests/func/test_install.py | 10 ++++++++-- 5 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 dvc/scm/exceptions.py diff --git a/dvc/exceptions.py b/dvc/exceptions.py index e808cab23..f65f30713 100644 --- a/dvc/exceptions.py +++ b/dvc/exceptions.py @@ -216,18 +216,6 @@ class DvcIgnoreInCollectedDirError(DvcException): ) -class GitHookAlreadyExistsError(DvcException): - def __init__(self, hook_name): - from dvc.utils import format_link - - super().__init__( - "Hook '{}' already exists. Please refer to {} for more " - "info.".format( - hook_name, format_link("https://man.dvc.org/install") - ) - ) - - class FileTransferError(DvcException): _METHOD = "transfer" diff --git a/dvc/repo/install.py b/dvc/repo/install.py index 5d717a574..a801e9f1b 100644 --- a/dvc/repo/install.py +++ b/dvc/repo/install.py @@ -1,5 +1,7 @@ from typing import TYPE_CHECKING +from dvc.exceptions import DvcException + if TYPE_CHECKING: from dvc.repo import Repo from dvc.scm.git import Git @@ -41,9 +43,17 @@ def pre_commit_install(scm: "Git") -> None: def install_hooks(scm: "Git") -> None: + from dvc.scm.exceptions import GitHookAlreadyExists + from dvc.utils import format_link + hooks = ["post-checkout", "pre-commit", "pre-push"] for hook in hooks: - scm.verify_hook(hook) + try: + scm.verify_hook(hook) + except GitHookAlreadyExists as exc: + link = format_link("https://man.dvc.org/install") + raise DvcException(f"{exc}. Please refer to {link} for more info.") + for hook in hooks: scm.install_hook(hook, f"exec dvc git-hook {hook} $@") diff --git a/dvc/scm/exceptions.py b/dvc/scm/exceptions.py new file mode 100644 index 000000000..1c8202044 --- /dev/null +++ b/dvc/scm/exceptions.py @@ -0,0 +1,8 @@ +class SCMError(Exception): + """Base class for source control management errors.""" + + +class GitHookAlreadyExists(SCMError): + def __init__(self, name: str) -> None: + self.name = name + super().__init__(f"Hook '{name}' already exists") diff --git a/dvc/scm/git/__init__.py b/dvc/scm/git/__init__.py index 9e7ea9fe1..74db062a9 100644 --- a/dvc/scm/git/__init__.py +++ b/dvc/scm/git/__init__.py @@ -11,11 +11,11 @@ from typing import Dict, Iterable, Optional, Tuple, Type from funcy import cached_property, first from pathspec.patterns import GitWildMatchPattern -from dvc.exceptions import GitHookAlreadyExistsError from dvc.scm.base import Base, FileNotInRepoError, RevError from dvc.utils import relpath from dvc.utils.fs import path_isin +from ..exceptions import GitHookAlreadyExists from .backend.base import BaseGitBackend, NoGitBackendError from .backend.dulwich import DulwichBackend from .backend.gitpython import GitPythonBackend @@ -204,7 +204,7 @@ class Git(Base): def verify_hook(self, name): if (self.hooks_dir / name).exists(): - raise GitHookAlreadyExistsError(name) + raise GitHookAlreadyExists(name) def install_hook( self, name: str, script: str, interpreter: str = "sh" diff --git a/tests/func/test_install.py b/tests/func/test_install.py index 826403116..ee6fde298 100644 --- a/tests/func/test_install.py +++ b/tests/func/test_install.py @@ -5,8 +5,9 @@ import sys import pytest from git import GitCommandError -from dvc.exceptions import GitHookAlreadyExistsError +from dvc.exceptions import DvcException from dvc.utils import file_md5 +from tests.func.parsing.test_errors import escape_ansi @pytest.mark.skipif( @@ -39,9 +40,14 @@ class TestInstall: def test_fail_if_hook_exists(self, scm, dvc): self._hook("post-checkout").write_text("hook content") - with pytest.raises(GitHookAlreadyExistsError): + with pytest.raises(DvcException) as exc_info: dvc.install() + assert escape_ansi(str(exc_info.value)) == ( + "Hook 'post-checkout' already exists. " + "Please refer to for more info." + ) + def test_pre_commit_hook(self, tmp_dir, scm, dvc, caplog): tmp_dir.dvc_gen("file", "file content", commit="create foo") tmp_dir.gen("file", "file modified")