Skip to content

Commit 7a92ff5

Browse files
authored
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
1 parent fa4bf40 commit 7a92ff5

File tree

7 files changed

+37
-8
lines changed

7 files changed

+37
-8
lines changed

dvc/repo/experiments/executor/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def commit(
376376
logger.debug("Commit to new experiment branch '%s'", branch)
377377

378378
scm.gitpython.repo.git.add(update=True)
379-
scm.commit(f"dvc: commit experiment {exp_hash}")
379+
scm.commit(f"dvc: commit experiment {exp_hash}", no_verify=True)
380380
new_rev = scm.get_rev()
381381
scm.set_ref(branch, new_rev, old_ref=old_ref)
382382
scm.set_ref(EXEC_BRANCH, branch, symbolic=True)

dvc/scm/git/backend/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def add(self, paths: Iterable[str]):
5656
pass
5757

5858
@abstractmethod
59-
def commit(self, msg: str):
59+
def commit(self, msg: str, no_verify: bool = False):
6060
pass
6161

6262
@abstractmethod

dvc/scm/git/backend/dulwich.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,14 @@ def add(self, paths: Iterable[str]):
145145
# NOTE: this doesn't check gitignore, same as GitPythonBackend.add
146146
self.repo.stage(relpath(fpath, self.root_dir))
147147

148-
def commit(self, msg: str):
148+
def commit(self, msg: str, no_verify: bool = False):
149+
from dulwich.errors import CommitError
149150
from dulwich.porcelain import commit
150151

151-
commit(self.root_dir, message=msg)
152+
try:
153+
commit(self.root_dir, message=msg, no_verify=no_verify)
154+
except CommitError as exc:
155+
raise SCMError("Git commit failed") from exc
152156

153157
def checkout(
154158
self, branch: str, create_new: Optional[bool] = False, **kwargs,

dvc/scm/git/backend/gitpython.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,13 @@ def add(self, paths: Iterable[str]):
200200

201201
logger.exception(msg)
202202

203-
def commit(self, msg: str):
204-
self.repo.index.commit(msg)
203+
def commit(self, msg: str, no_verify: bool = False):
204+
from git.exc import HookExecutionError
205+
206+
try:
207+
self.repo.index.commit(msg, skip_hooks=no_verify)
208+
except HookExecutionError as exc:
209+
raise SCMError("Git pre-commit hook failed") from exc
205210

206211
def checkout(
207212
self, branch: str, create_new: Optional[bool] = False, **kwargs

dvc/scm/git/backend/pygit2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def dir(self) -> str:
8686
def add(self, paths: Iterable[str]):
8787
raise NotImplementedError
8888

89-
def commit(self, msg: str):
89+
def commit(self, msg: str, no_verify: bool = False):
9090
raise NotImplementedError
9191

9292
def checkout(

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def run(self):
5050
"colorama>=0.3.9",
5151
"configobj>=5.0.6",
5252
"gitpython>3",
53-
"dulwich>=0.20.14",
53+
"dulwich>=0.20.15",
5454
"pygit2>=1.4.0",
5555
"setuptools>=34.0.0",
5656
"nanotime>=0.5.2",

tests/unit/scm/test_git.py

+20
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,23 @@ def test_ignore_remove_empty(tmp_dir, scm, git):
301301

302302
git.ignore_remove(test_entries[1]["path"])
303303
assert not path_to_gitignore.exists()
304+
305+
306+
@pytest.mark.skipif(
307+
os.name == "nt", reason="Git hooks not supported on Windows"
308+
)
309+
@pytest.mark.parametrize("hook", ["pre-commit", "commit-msg"])
310+
def test_commit_no_verify(tmp_dir, scm, git, hook):
311+
import stat
312+
313+
hook_file = os.path.join(".git", "hooks", hook)
314+
tmp_dir.gen(
315+
hook_file, "#!/usr/bin/env python\nimport sys\nsys.exit(1)",
316+
)
317+
os.chmod(hook_file, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
318+
319+
tmp_dir.gen("foo", "foo")
320+
git.add(["foo"])
321+
with pytest.raises(SCMError):
322+
git.commit("commit foo")
323+
git.commit("commit foo", no_verify=True)

0 commit comments

Comments
 (0)