Mercurial pre commit hook - stop commit based on file contents?

后端 未结 3 1011
余生分开走
余生分开走 2020-12-14 09:32

How can I setup a pre-commit hook , that will search for a string in the committed files and If found stop the commit ?

3条回答
  •  别那么骄傲
    2020-12-14 10:00

    Ry4an's answer is almost correct :) but you need to replace "hg export tip" with "hg diff".
    tip is the last commited changeset, but are interested in local uncommited changes - so diff is what u need. for my needs i added the following to my hgrc

    precommit.removeDebug = hg diff -S | grep -v '^-' | (! egrep '(var_dump)|(exit)|(print_r)')
    

    the -S includes subrepos (maye not need, and may be still buggy).
    the grep -v '^-' removes lines from the diff that indicate lines that were removed. i removed the -q so i at least have a idea what to remove, but unfortunatly this method cannot print you the file and linenumber of the occurence (as it is piped). maybe someone has a better way to do it.

提交回复
热议问题