Search code examples
mercurialmercurial-hook

mercurial: including precommit-changed file


On commit to repository I have a hook defined in Mercurial:

[hooks]
precommit.exportDB=exportDB.bat

This creates/updates a SQL-dump from my database, which should be included in the commit. BUT: although this works, the Sql is marked as new, but not part of the now commiting changeset. How can I automatically include it so it gets into the set of changed files?

Hope this makes sense...

Thx Reinhard


Solution

  • This'll sound crazy, but you can do it in two steps. First change your precommit hook to a pre-commit hook -- yup, both exist and they're different. Without the dash the commit has already started and some lock has been acquired. With the dash it happens before the commit starts and you can still hg add the new file.

    On a unix like that total change would be:

    [hooks]
    pre-commit.exportDB=exportDB.sh && hg add resulting.sql
    

    presumably there's something similar on Windows, or you could make the hg add the last line of the batch file.

    P.S. Don't commit generated files. :)

    Update:

    I just tested this and it works as I suggested:

    ry4an@four:~$ hg init reinhard
    ry4an@four:~$ cd reinhard/
    ry4an@four:~/reinhard$ vi .hg/hgrc
    ry4an@four:~/reinhard$ cat .hg/hgrc 
    [hooks]
    pre-commit = hg add otherfile
    ry4an@four:~/reinhard$ echo text > afile
    ry4an@four:~/reinhard$ echo more > otherfile
    ry4an@four:~/reinhard$ hg add afile
    ry4an@four:~/reinhard$ hg status
    A afile
    ? otherfile
    ry4an@four:~/reinhard$ hg commit -m 'message'
    ry4an@four:~/reinhard$ hg status --all
    C afile
    C otherfile
    

    Notice that before the commit only 'afile' is added and 'otherfile' is unknown, and after the commit both files are 'C' (meaning "Clean' -- they've been added and committed).