Search code examples
pythonmercurialmercurial-hook

Get list selected for commit files in hg hook


I'm writing a hook, which is supposed to process files before they actually will be commited. So I found out that I can get list of all files have chanded recenlty like this:

def hook(ui, repo, node, **kwargs):
    changedFileList =  repo.status()[0]

So, this construction provides me with list of changed files. Now, suppose user selected just some files to be commited (via tortoise ui for example).


Solution

  • Just use the correct hook which for your case might be the pretxncommit hook. In that hook $HG_NODE of the commit already exists, but the commit is not yet done. But using $HG_NODE explicitly you can check all properties of that commit, including files it touches, e.g. by

    hg log -r$HG_NODE --template="{files}\n"
    

    or in python code

    _changedFiles = [os.path.abspath(file) for file in repo[_node].changeset()[3]]
    

    Check hg help hgrc (search for hook therein) and hg log templates how to finetune the log output in the way you need it.