From f82a2263762a955518e2efb7616f7d58a300819c Mon Sep 17 00:00:00 2001 From: Jamie Madill 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 generates a Change-Id. 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 @@ -20,6 +20,7 @@ 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' kind = 'patch' @@ -69,6 +70,9 @@ 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 --- a/stgit/lib/edit.py +++ b/stgit/lib/edit.py @@ -3,6 +3,31 @@ from stgit import utils from stgit.commands import common from stgit.lib import git +from stgit import run +from tempfile import NamedTemporaryFile +import os + +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): + return message + + # message hook exists, make a temporary file and run on the temp file + tf = NamedTemporaryFile("w", delete=False) + tf.write(message) + tf.close() + + run.Run('bash', hook_path, tf.name).run() + new_msg = open(tf.name, 'r').read() + os.remove(tf.name) + + return new_msg def update_patch_description(repo, cd, text, contains_diff): """Update the given L{CommitData} with the @@ -19,7 +44,7 @@ (git.Date.maybe(authdate), 'set_date')]: if val != None: a = getattr(a, setter)(val) - cd = cd.set_message(message).set_author(a) + cd = cd.set_message(run_commit_msg_hook(repo, message)).set_author(a) failed_diff = None if diff: tree = repo.apply(cd.parent.data.tree, diff, quiet = False)