mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
experiments: commit with --no-verify in executors (#5078)
* git: add no_verify flag for commit() * experiments: commit with --no-verify in executors * git: wrap hook exceptions with SCMError * update hook file perms * update dulwich to 0.20.15 * update pygit2 commit def * skip hook tests on windows
This commit is contained in:
@@ -376,7 +376,7 @@ class BaseExecutor(ABC):
|
||||
logger.debug("Commit to new experiment branch '%s'", branch)
|
||||
|
||||
scm.gitpython.repo.git.add(update=True)
|
||||
scm.commit(f"dvc: commit experiment {exp_hash}")
|
||||
scm.commit(f"dvc: commit experiment {exp_hash}", no_verify=True)
|
||||
new_rev = scm.get_rev()
|
||||
scm.set_ref(branch, new_rev, old_ref=old_ref)
|
||||
scm.set_ref(EXEC_BRANCH, branch, symbolic=True)
|
||||
|
||||
@@ -56,7 +56,7 @@ class BaseGitBackend(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def commit(self, msg: str):
|
||||
def commit(self, msg: str, no_verify: bool = False):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
|
||||
@@ -145,10 +145,14 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
# NOTE: this doesn't check gitignore, same as GitPythonBackend.add
|
||||
self.repo.stage(relpath(fpath, self.root_dir))
|
||||
|
||||
def commit(self, msg: str):
|
||||
def commit(self, msg: str, no_verify: bool = False):
|
||||
from dulwich.errors import CommitError
|
||||
from dulwich.porcelain import commit
|
||||
|
||||
commit(self.root_dir, message=msg)
|
||||
try:
|
||||
commit(self.root_dir, message=msg, no_verify=no_verify)
|
||||
except CommitError as exc:
|
||||
raise SCMError("Git commit failed") from exc
|
||||
|
||||
def checkout(
|
||||
self, branch: str, create_new: Optional[bool] = False, **kwargs,
|
||||
|
||||
@@ -200,8 +200,13 @@ class GitPythonBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
|
||||
logger.exception(msg)
|
||||
|
||||
def commit(self, msg: str):
|
||||
self.repo.index.commit(msg)
|
||||
def commit(self, msg: str, no_verify: bool = False):
|
||||
from git.exc import HookExecutionError
|
||||
|
||||
try:
|
||||
self.repo.index.commit(msg, skip_hooks=no_verify)
|
||||
except HookExecutionError as exc:
|
||||
raise SCMError("Git pre-commit hook failed") from exc
|
||||
|
||||
def checkout(
|
||||
self, branch: str, create_new: Optional[bool] = False, **kwargs
|
||||
|
||||
@@ -86,7 +86,7 @@ class Pygit2Backend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
def add(self, paths: Iterable[str]):
|
||||
raise NotImplementedError
|
||||
|
||||
def commit(self, msg: str):
|
||||
def commit(self, msg: str, no_verify: bool = False):
|
||||
raise NotImplementedError
|
||||
|
||||
def checkout(
|
||||
|
||||
@@ -50,7 +50,7 @@ install_requires = [
|
||||
"colorama>=0.3.9",
|
||||
"configobj>=5.0.6",
|
||||
"gitpython>3",
|
||||
"dulwich>=0.20.14",
|
||||
"dulwich>=0.20.15",
|
||||
"pygit2>=1.4.0",
|
||||
"setuptools>=34.0.0",
|
||||
"nanotime>=0.5.2",
|
||||
|
||||
@@ -301,3 +301,23 @@ def test_ignore_remove_empty(tmp_dir, scm, git):
|
||||
|
||||
git.ignore_remove(test_entries[1]["path"])
|
||||
assert not path_to_gitignore.exists()
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
os.name == "nt", reason="Git hooks not supported on Windows"
|
||||
)
|
||||
@pytest.mark.parametrize("hook", ["pre-commit", "commit-msg"])
|
||||
def test_commit_no_verify(tmp_dir, scm, git, hook):
|
||||
import stat
|
||||
|
||||
hook_file = os.path.join(".git", "hooks", hook)
|
||||
tmp_dir.gen(
|
||||
hook_file, "#!/usr/bin/env python\nimport sys\nsys.exit(1)",
|
||||
)
|
||||
os.chmod(hook_file, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
|
||||
|
||||
tmp_dir.gen("foo", "foo")
|
||||
git.add(["foo"])
|
||||
with pytest.raises(SCMError):
|
||||
git.commit("commit foo")
|
||||
git.commit("commit foo", no_verify=True)
|
||||
|
||||
Reference in New Issue
Block a user