问题
I've been trying
def debug_hook(ui, repo, **kwargs):
changectx = repo[None]
ui.status('change.desc: %s\n' % changectx.description())
return True
But it always prints an empty string. Is this because it is a precommit hook and the message isn't available yet? Or am I just missing something obvious?
回答1:
It turns out there are two things wrong with my initial approach:
- As jk pointed out, the
precommit
event happens before the commit so the meta data for the commit being processed doesn't exist yet. By usingpretxncommit
instead, the meta data exists, but the transaction hasn't been committed to the database yet. - Using
changectx = repo[None]
gives you the change context for the working directory. But we want the info for the current commit so usingchangectx = repo['tip']
instead gives us the most recent meta data.
Note that if you use changectx = repo['tip']
with the precommit
event, you'll actually get the last commit processed, not the current one you are working on.
回答2:
I think you are right that in precommit the message doesn't exist yet. if you use pretxncommit it will, but i'm not 100% sure what it allows you to do at that point as the transaction is almost complete.
来源:https://stackoverflow.com/questions/2451560/how-do-you-access-the-commit-message-in-a-mercurial-in-process-hook