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

后端 未结 3 1012
余生分开走
余生分开走 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:04

    Chapter 10 of the mercurial book covers this exactly:

    $ cat .hg/hgrc
    [hooks]
    pretxncommit.whitespace = hg export tip | (! egrep -q '^\+.*[ \t]$')
    $ echo 'a ' > a
    $ hg commit -A -m 'test with trailing whitespace'
    adding a
    transaction abort!
    rollback completed
    abort: pretxncommit.whitespace hook exited with status 1
    $ echo 'a' > a
    $ hg commit -A -m 'drop trailing whitespace and try again'
    

    In this example, we introduce a simple pretxncommit hook that checks for trailing whitespace. This hook is short, but not very helpful. It exits with an error status if a change adds a line with trailing whitespace to any file, but does not print any information that might help us to identify the offending file or line. It also has the nice property of not paying attention to unmodified lines; only lines that introduce new trailing whitespace cause problems.

    Just change the regular expression from '^\+.*[ \t]$' to whatever string you're looking for.

提交回复
热议问题