move scm.install to dvc.install (#6849)

scm.install does 3 different things, and all of those are
very much tied to DVC. So, as we are splitting scm out,
it should now belong to `dvc.install`.
This commit is contained in:
Saugat Pachhai
2021-10-22 14:53:33 +03:00
committed by GitHub
parent 501256708d
commit 719e93192f
4 changed files with 102 additions and 93 deletions
+69 -2
View File
@@ -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)
-8
View File
@@ -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),
+23 -73
View File
@@ -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"))
+10 -10
View File
@@ -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(