views:

95

answers:

2

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?

A: 

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.

jk
It turns out that's part of the answer (using pretxncommit). I'll post an answer with the details.
davidavr
+2  A: 

It turns out there are two things wrong with my initial approach:

  1. 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 using pretxncommit instead, the meta data exists, but the transaction hasn't been committed to the database yet.
  2. Using changectx = repo[None] gives you the change context for the working directory. But we want the info for the current commit so using changectx = 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.

davidavr
yes i was slightly concerned about this from the book though "This hook can access the metadata associated with the almost-new changeset, but it should not do anything permanent with this data. It must also not modify the working directory."
jk