瀏覽代碼

commit: add no_verify flag to skip pre-commit hooks

Peter Rowlands 4 年之前
父節點
當前提交
0c225d32dd
共有 2 個文件被更改,包括 13 次插入6 次删除
  1. 4 2
      dulwich/porcelain.py
  2. 9 4
      dulwich/repo.py

+ 4 - 2
dulwich/porcelain.py

@@ -323,7 +323,8 @@ def symbolic_ref(repo, ref_name, force=False):
         repo_obj.refs.set_symbolic_ref(b'HEAD', ref_path)
 
 
-def commit(repo=".", message=None, author=None, committer=None, encoding=None):
+def commit(repo=".", message=None, author=None, committer=None, encoding=None,
+           no_verify=False):
     """Create a new commit.
 
     Args:
@@ -331,6 +332,7 @@ def commit(repo=".", message=None, author=None, committer=None, encoding=None):
       message: Optional commit message
       author: Optional author name and email
       committer: Optional committer name and email
+      no_verify: Skip pre-commit and commit-msg hooks
     Returns: SHA1 of the new commit
     """
     # FIXME: Support --all argument
@@ -344,7 +346,7 @@ def commit(repo=".", message=None, author=None, committer=None, encoding=None):
     with open_repo_closing(repo) as r:
         return r.do_commit(
                 message=message, author=author, committer=committer,
-                encoding=encoding)
+                encoding=encoding, no_verify=no_verify)
 
 
 def commit_tree(repo, tree, message=None, author=None, committer=None):

+ 9 - 4
dulwich/repo.py

@@ -822,7 +822,7 @@ class BaseRepo(object):
                   author=None, commit_timestamp=None,
                   commit_timezone=None, author_timestamp=None,
                   author_timezone=None, tree=None, encoding=None,
-                  ref=b'HEAD', merge_heads=None):
+                  ref=b'HEAD', merge_heads=None, no_verify=False):
         """Create a new commit.
 
         If not specified, `committer` and `author` default to
@@ -844,6 +844,7 @@ class BaseRepo(object):
           encoding: Encoding
           ref: Optional ref to commit to (defaults to current branch)
           merge_heads: Merge heads (defaults to .git/MERGE_HEADS)
+          no_verify: Skip pre-commit and commit-msg hooks
 
         Returns:
           New commit SHA1
@@ -859,7 +860,8 @@ class BaseRepo(object):
             c.tree = tree
 
         try:
-            self.hooks['pre-commit'].execute()
+            if not no_verify:
+                self.hooks['pre-commit'].execute()
         except HookError as e:
             raise CommitError(e)
         except KeyError:  # no hook defined, silent fallthrough
@@ -903,9 +905,12 @@ class BaseRepo(object):
             raise ValueError("No commit message specified")
 
         try:
-            c.message = self.hooks['commit-msg'].execute(message)
-            if c.message is None:
+            if no_verify:
                 c.message = message
+            else:
+                c.message = self.hooks['commit-msg'].execute(message)
+                if c.message is None:
+                    c.message = message
         except HookError as e:
             raise CommitError(e)
         except KeyError:  # no hook defined, message not modified