summaryrefslogtreecommitdiff
path: root/git/index
diff options
context:
space:
mode:
authorsatahippy <satahippy@gmail.com>2017-10-30 23:00:18 +0200
committersatahippy <satahippy@gmail.com>2017-10-30 23:00:32 +0200
commiteae04bf7b0620a0ef950dd39af7f07f3c88fd15f (patch)
tree8bb4911b668b66a009ca4a45afc1e3a0e974d7be /git/index
parentc7f657fb20c063dfc2a653f050accc9c40d06a60 (diff)
downloadgitpython-eae04bf7b0620a0ef950dd39af7f07f3c88fd15f.tar.gz
IndexFile.commit() now runs pre-commit and post-commit and commit-msg hooks.
Diffstat (limited to 'git/index')
-rw-r--r--git/index/base.py19
-rw-r--r--git/index/fun.py7
2 files changed, 23 insertions, 3 deletions
diff --git a/git/index/base.py b/git/index/base.py
index 4fee2aae..a9e3a3c7 100644
--- a/git/index/base.py
+++ b/git/index/base.py
@@ -948,6 +948,11 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable):
:return: Commit object representing the new commit"""
if not skip_hooks:
run_commit_hook('pre-commit', self)
+
+ self._write_commit_editmsg(message)
+ run_commit_hook('commit-msg', self, self._commit_editmsg_filepath())
+ message = self._read_commit_editmsg()
+ self._remove_commit_editmsg()
tree = self.write_tree()
rval = Commit.create_from_tree(self.repo, tree, message, parent_commits,
head, author=author, committer=committer,
@@ -955,6 +960,20 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable):
if not skip_hooks:
run_commit_hook('post-commit', self)
return rval
+
+ def _write_commit_editmsg(self, message):
+ with open(self._commit_editmsg_filepath(), "wb") as commit_editmsg_file:
+ commit_editmsg_file.write(message.encode(defenc))
+
+ def _remove_commit_editmsg(self):
+ os.remove(self._commit_editmsg_filepath())
+
+ def _read_commit_editmsg(self):
+ with open(self._commit_editmsg_filepath(), "rb") as commit_editmsg_file:
+ return commit_editmsg_file.read().decode(defenc)
+
+ def _commit_editmsg_filepath(self):
+ return osp.join(self.repo.common_dir, "COMMIT_EDITMSG")
@classmethod
def _flush_stdin_and_wait(cls, proc, ignore_stdout=False):
diff --git a/git/index/fun.py b/git/index/fun.py
index 7f7518e1..c01a32b8 100644
--- a/git/index/fun.py
+++ b/git/index/fun.py
@@ -62,10 +62,11 @@ def hook_path(name, git_dir):
return osp.join(git_dir, 'hooks', name)
-def run_commit_hook(name, index):
+def run_commit_hook(name, index, *args):
"""Run the commit hook of the given name. Silently ignores hooks that do not exist.
:param name: name of hook, like 'pre-commit'
:param index: IndexFile instance
+ :param args: arguments passed to hook file
:raises HookExecutionError: """
hp = hook_path(name, index.repo.git_dir)
if not os.access(hp, os.X_OK):
@@ -75,7 +76,7 @@ def run_commit_hook(name, index):
env['GIT_INDEX_FILE'] = safe_decode(index.path) if PY3 else safe_encode(index.path)
env['GIT_EDITOR'] = ':'
try:
- cmd = subprocess.Popen(hp,
+ cmd = subprocess.Popen([hp] + list(args),
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
@@ -93,7 +94,7 @@ def run_commit_hook(name, index):
if cmd.returncode != 0:
stdout = force_text(stdout, defenc)
stderr = force_text(stderr, defenc)
- raise HookExecutionError(hp, cmd.returncode, stdout, stderr)
+ raise HookExecutionError(hp, cmd.returncode, stderr, stdout)
# end handle return code