From f82a2263762a955518e2efb7616f7d58a300819c Mon Sep 17 00:00:00 2001
From: Jamie Madill <jmadill@chromium.org>
Date: Wed, 27 Nov 2013 13:20:27 -0500
Subject: [PATCH] Honor commit-msg hook for 'new' and 'edit'.
The commit-msg hook is important for gerrit users, where it
Change-Id: Ic1fde9e13e5e903ac8807be37feb7a88a9844dce
diff --git a/stgit/commands/new.py b/stgit/commands/new.py
index d5c5382..3328042 100644
--- a/stgit/commands/new.py
+++ b/stgit/commands/new.py
from stgit.commands import common
from stgit.lib import git as gitlib, transaction
from stgit.config import config
+from stgit.lib import edit
help = 'Create a new, empty patch'
author = gitlib.Person.author(), committer = gitlib.Person.committer())
cd = common.update_commit_data(cd, options)
+ # run the commit-msg hook
+ cd = cd.set_message(edit.run_commit_msg_hook(stack.repository, cd.message))
if options.save_template:
options.save_template(cd.message)
return utils.STGIT_SUCCESS
diff --git a/stgit/lib/edit.py b/stgit/lib/edit.py
index c8d29f6..5993754 100644
from stgit.commands import common
from stgit.lib import git
+from tempfile import NamedTemporaryFile
+def run_commit_msg_hook(repo, message):
+ """Run the commit-msg git hook manually when editing a patch.
+ Return the edited commit message, or the original message if there
+ is no commit-msg hook."""
+ hook_path = os.path.join(repo.directory, 'hooks', 'commit-msg')
+ if not os.access(hook_path, os.X_OK):
+ # message hook exists, make a temporary file and run on the temp file
+ tf = NamedTemporaryFile("w", delete=False)
+ run.Run('bash', hook_path, tf.name).run()
+ new_msg = open(tf.name, 'r').read()