summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2017-11-19 15:57:17 +0100
committerGitHub <noreply@github.com>2017-11-19 15:57:17 +0100
commitd91ae75401b851b71fcc6f4dcf7eb29ed2a63369 (patch)
treea56d103e434f06ded3fde16f8dfa3706a6beebec
parent610d4c97485d2c0d4f65b87f2620a84e0df99341 (diff)
parenteae04bf7b0620a0ef950dd39af7f07f3c88fd15f (diff)
downloadgitpython-d91ae75401b851b71fcc6f4dcf7eb29ed2a63369.tar.gz
Merge pull request #693 from satahippy/master
commit-msg hook support
-rw-r--r--git/index/base.py19
-rw-r--r--git/index/fun.py7
-rw-r--r--git/test/test_index.py114
3 files changed, 108 insertions, 32 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
diff --git a/git/test/test_index.py b/git/test/test_index.py
index e8d38a09..cf746140 100644
--- a/git/test/test_index.py
+++ b/git/test/test_index.py
@@ -729,35 +729,6 @@ class TestIndex(TestBase):
assert fkey not in index.entries
index.add(files, write=True)
- if is_win:
- hp = hook_path('pre-commit', index.repo.git_dir)
- hpd = osp.dirname(hp)
- if not osp.isdir(hpd):
- os.mkdir(hpd)
- with open(hp, "wt") as fp:
- fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1")
- # end
- os.chmod(hp, 0o744)
- try:
- index.commit("This should fail")
- except HookExecutionError as err:
- if is_win:
- self.assertIsInstance(err.status, OSError)
- self.assertEqual(err.command, [hp])
- self.assertEqual(err.stdout, '')
- self.assertEqual(err.stderr, '')
- assert str(err)
- else:
- self.assertEqual(err.status, 1)
- self.assertEqual(err.command, hp)
- self.assertEqual(err.stdout, 'stdout\n')
- self.assertEqual(err.stderr, 'stderr\n')
- assert str(err)
- else:
- raise AssertionError("Should have cought a HookExecutionError")
- # end exception handling
- os.remove(hp)
- # end hook testing
nc = index.commit("2 files committed", head=False)
for fkey in keys:
@@ -859,3 +830,88 @@ class TestIndex(TestBase):
r = Repo.init(rw_dir)
r.index.add([fp])
r.index.commit('Added [.exe')
+
+ @with_rw_repo('HEAD', bare=True)
+ def test_pre_commit_hook_success(self, rw_repo):
+ index = rw_repo.index
+ hp = hook_path('pre-commit', index.repo.git_dir)
+ hpd = osp.dirname(hp)
+ if not osp.isdir(hpd):
+ os.mkdir(hpd)
+ with open(hp, "wt") as fp:
+ fp.write("#!/usr/bin/env sh\nexit 0")
+ os.chmod(hp, 0o744)
+ index.commit("This should not fail")
+
+ @with_rw_repo('HEAD', bare=True)
+ def test_pre_commit_hook_fail(self, rw_repo):
+ index = rw_repo.index
+ hp = hook_path('pre-commit', index.repo.git_dir)
+ hpd = osp.dirname(hp)
+ if not osp.isdir(hpd):
+ os.mkdir(hpd)
+ with open(hp, "wt") as fp:
+ fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1")
+ os.chmod(hp, 0o744)
+ try:
+ index.commit("This should fail")
+ except HookExecutionError as err:
+ if is_win:
+ self.assertIsInstance(err.status, OSError)
+ self.assertEqual(err.command, [hp])
+ self.assertEqual(err.stdout, '')
+ self.assertEqual(err.stderr, '')
+ assert str(err)
+ else:
+ self.assertEqual(err.status, 1)
+ self.assertEqual(err.command, [hp])
+ self.assertEqual(err.stdout, "\n stdout: 'stdout\n'")
+ self.assertEqual(err.stderr, "\n stderr: 'stderr\n'")
+ assert str(err)
+ else:
+ raise AssertionError("Should have cought a HookExecutionError")
+
+ @with_rw_repo('HEAD', bare=True)
+ def test_commit_msg_hook_success(self, rw_repo):
+ index = rw_repo.index
+ commit_message = u"commit default head by Frèderic Çaufl€"
+ from_hook_message = u"from commit-msg"
+
+ hp = hook_path('commit-msg', index.repo.git_dir)
+ hpd = osp.dirname(hp)
+ if not osp.isdir(hpd):
+ os.mkdir(hpd)
+ with open(hp, "wt") as fp:
+ fp.write('#!/usr/bin/env sh\necho -n " {}" >> "$1"'.format(from_hook_message))
+ os.chmod(hp, 0o744)
+
+ new_commit = index.commit(commit_message)
+ self.assertEqual(new_commit.message, u"{} {}".format(commit_message, from_hook_message))
+
+ @with_rw_repo('HEAD', bare=True)
+ def test_commit_msg_hook_fail(self, rw_repo):
+ index = rw_repo.index
+ hp = hook_path('commit-msg', index.repo.git_dir)
+ hpd = osp.dirname(hp)
+ if not osp.isdir(hpd):
+ os.mkdir(hpd)
+ with open(hp, "wt") as fp:
+ fp.write("#!/usr/bin/env sh\necho stdout; echo stderr 1>&2; exit 1")
+ os.chmod(hp, 0o744)
+ try:
+ index.commit("This should fail")
+ except HookExecutionError as err:
+ if is_win:
+ self.assertIsInstance(err.status, OSError)
+ self.assertEqual(err.command, [hp])
+ self.assertEqual(err.stdout, '')
+ self.assertEqual(err.stderr, '')
+ assert str(err)
+ else:
+ self.assertEqual(err.status, 1)
+ self.assertEqual(err.command, [hp])
+ self.assertEqual(err.stdout, "\n stdout: 'stdout\n'")
+ self.assertEqual(err.stderr, "\n stderr: 'stderr\n'")
+ assert str(err)
+ else:
+ raise AssertionError("Should have cought a HookExecutionError")