diff --git a/dvc/repo/install.py b/dvc/repo/install.py index 38612d608..5d717a574 100644 --- a/dvc/repo/install.py +++ b/dvc/repo/install.py @@ -1,2 +1,69 @@ -def install(self, use_pre_commit_tool): - self.scm.install(use_pre_commit_tool) +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from dvc.repo import Repo + from dvc.scm.git import Git + + +def pre_commit_install(scm: "Git") -> None: + import os + + from dvc.utils.serialize import modify_yaml + + config_path = os.path.join(scm.root_dir, ".pre-commit-config.yaml") + with modify_yaml(config_path) as config: + entry = { + "repo": "https://github.com/iterative/dvc", + "rev": "master", + "hooks": [ + { + "id": "dvc-pre-commit", + "language_version": "python3", + "stages": ["commit"], + }, + { + "id": "dvc-pre-push", + "language_version": "python3", + "stages": ["push"], + }, + { + "id": "dvc-post-checkout", + "language_version": "python3", + "stages": ["post-checkout"], + "always_run": True, + }, + ], + } + + config["repos"] = config.get("repos", []) + if entry not in config["repos"]: + config["repos"].append(entry) + + +def install_hooks(scm: "Git") -> None: + hooks = ["post-checkout", "pre-commit", "pre-push"] + for hook in hooks: + scm.verify_hook(hook) + for hook in hooks: + scm.install_hook(hook, f"exec dvc git-hook {hook} $@") + + +def install(self: "Repo", use_pre_commit_tool: bool = False) -> None: + """Adds dvc commands to SCM hooks for the repo. + + If use_pre_commit_tool is set and pre-commit is installed it will be used + to install the hooks. + """ + from dvc.scm.git import Git + + scm = self.scm + if not isinstance(scm, Git): + return + + driver = "dvc git-hook merge-driver --ancestor %O --our %A --their %B " + scm.install_merge_driver("dvc", "DVC merge driver", driver) + + if use_pre_commit_tool: + return pre_commit_install(scm) + + return install_hooks(scm) diff --git a/dvc/scm/base.py b/dvc/scm/base.py index 30db684db..12f70f0e0 100644 --- a/dvc/scm/base.py +++ b/dvc/scm/base.py @@ -149,14 +149,6 @@ class Base: """Returns a list of commits in the repo.""" return [] - def install(self, use_pre_commit_tool=False): - """ - Adds dvc commands to SCM hooks for the repo. - - If use_pre_commit_tool is set and pre-commit is - installed it will be used to install the hooks. - """ - def cleanup_ignores(self): """ This method should clean up ignores (eg. entries in .gitignore), diff --git a/dvc/scm/git/__init__.py b/dvc/scm/git/__init__.py index 0566efae1..5d60ec232 100644 --- a/dvc/scm/git/__init__.py +++ b/dvc/scm/git/__init__.py @@ -94,6 +94,12 @@ class Git(Base): def dir(self): return first(self.backends.values()).dir + @cached_property + def hooks_dir(self): + from pathlib import Path + + return Path(self.root_dir) / self.GIT_DIR / "hooks" + @property def gitpython(self): return self.backends["gitpython"] @@ -209,67 +215,27 @@ class Git(Base): self.track_file(relpath(gitignore)) - def _install_hook(self, name): - hook = self._hook_path(name) - with open(hook, "w+", encoding="utf-8") as fobj: - fobj.write(f"#!/bin/sh\nexec dvc git-hook {name} $@\n") + def verify_hook(self, name): + if (self.hooks_dir / name).exists(): + raise GitHookAlreadyExistsError(name) - os.chmod(hook, 0o777) + def install_hook( + self, name: str, script: str, interpreter: str = "sh" + ) -> None: + import shutil - def _install_merge_driver(self): - self.gitpython.repo.git.config("merge.dvc.name", "DVC merge driver") - self.gitpython.repo.git.config( - "merge.dvc.driver", - ( - "dvc git-hook merge-driver " - "--ancestor %O " - "--our %A " - "--their %B " - ), - ) + self.hooks_dir.mkdir(exist_ok=True) + hook = self.hooks_dir / name - def install(self, use_pre_commit_tool=False): - self._install_merge_driver() + directive = f"#!{shutil.which(interpreter)}" + hook.write_text(f"{directive}\n{script}\n", encoding="utf-8") + hook.chmod(0o777) - if not use_pre_commit_tool: - self._verify_dvc_hooks() - self._install_hook("post-checkout") - self._install_hook("pre-commit") - self._install_hook("pre-push") - return - - from dvc.utils.serialize import modify_yaml - - config_path = os.path.join(self.root_dir, ".pre-commit-config.yaml") - with modify_yaml(config_path) as config: - entry = { - "repo": "https://github.com/iterative/dvc", - "rev": "master", - "hooks": [ - { - "id": "dvc-pre-commit", - "language_version": "python3", - "stages": ["commit"], - }, - { - "id": "dvc-pre-push", - "language_version": "python3", - "stages": ["push"], - }, - { - "id": "dvc-post-checkout", - "language_version": "python3", - "stages": ["post-checkout"], - "always_run": True, - }, - ], - } - - if "repos" not in config: - config["repos"] = [] - - if entry not in config["repos"]: - config["repos"].append(entry) + def install_merge_driver( + self, name: str, description: str, driver: str + ) -> None: + self.gitpython.repo.git.config(f"merge.{name}.name", description) + self.gitpython.repo.git.config(f"merge.{name}.driver", driver) def cleanup_ignores(self): for path in self.ignored_paths: @@ -319,22 +285,6 @@ class Git(Base): def close(self): self.backends.close_initialized() - @cached_property - def _hooks_home(self): - return os.path.join(self.root_dir, self.GIT_DIR, "hooks") - - def _hook_path(self, name): - return os.path.join(self._hooks_home, name) - - def _verify_hook(self, name): - if os.path.exists(self._hook_path(name)): - raise GitHookAlreadyExistsError(name) - - def _verify_dvc_hooks(self): - self._verify_hook("post-checkout") - self._verify_hook("pre-commit") - self._verify_hook("pre-push") - @property def no_commits(self): return not bool(self.get_ref("HEAD")) diff --git a/tests/func/test_install.py b/tests/func/test_install.py index 5f0517523..826403116 100644 --- a/tests/func/test_install.py +++ b/tests/func/test_install.py @@ -17,7 +17,7 @@ class TestInstall: return pathlib.Path(".git") / "hooks" / name def test_create_hooks(self, scm, dvc): - scm.install() + dvc.install() hooks_with_commands = [ ("post-checkout", "exec dvc git-hook post-checkout"), @@ -30,22 +30,22 @@ class TestInstall: assert hook_path.is_file() assert command in hook_path.read_text() - def test_install_pre_commit_tool(self, scm): - scm.install(use_pre_commit_tool=True) + def test_install_pre_commit_tool(self, scm, dvc): + dvc.install(use_pre_commit_tool=True) precommit_path = pathlib.Path(".") / ".pre-commit-config.yaml" assert precommit_path.is_file() - def test_fail_if_hook_exists(self, scm): + def test_fail_if_hook_exists(self, scm, dvc): self._hook("post-checkout").write_text("hook content") with pytest.raises(GitHookAlreadyExistsError): - scm.install() + dvc.install() 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") - scm.install() + dvc.install() # scm.commit bypasses hooks with pytest.raises(GitCommandError, match=r"modified:\s*file"): @@ -54,7 +54,7 @@ class TestInstall: def test_post_checkout(self, tmp_dir, scm, dvc): tmp_dir.dvc_gen({"file": "file content"}, commit="add") os.unlink("file") - scm.install() + dvc.install() scm.gitpython.git.checkout("-b", "new_branch") @@ -78,7 +78,7 @@ class TestInstall: scm.gitpython.repo.clone(os.fspath(git_remote)) scm.gitpython.repo.create_remote("origin", os.fspath(git_remote)) - scm.install() + dvc.install() assert not expected_storage_path.is_file() scm.gitpython.repo.git.push("origin", "master") @@ -100,7 +100,7 @@ def test_merge_driver_no_ancestor(tmp_dir, scm, dvc): # installing hook only before merge, as it runs `dvc` commands which makes # `checkouts` and `commits` above slower - scm.install() + dvc.install() (tmp_dir / ".gitattributes").write_text("*.dvc merge=dvc") scm.gitpython.repo.git.merge( @@ -137,7 +137,7 @@ def test_merge_driver(tmp_dir, scm, dvc): # installing hook only before merge, as it runs `dvc` commands on # `checkouts` and `commits` which slows tests down - scm.install() + dvc.install() (tmp_dir / ".gitattributes").write_text("*.dvc merge=dvc") scm.gitpython.repo.git.merge(