Mercurial 预提交钩子 - 根据文件内容停止提交? [英] Mercurial pre commit hook - stop commit based on file contents?

查看:71
本文介绍了Mercurial 预提交钩子 - 根据文件内容停止提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何设置一个预提交钩子,它会在提交的文件中搜索一个字符串,如果找到就停止提交?

解决方案

善变的书的noreferrer">第 10 章正好涵盖了这一点:

$ cat .hg/hgrc[钩子]pretxncommit.whitespace = hg 导出提示 |(!egrep -q '^\+.*[ \t]$')$ echo 'a' >一种$ hg commit -A -m '测试尾随空格'添加一个交易中止!回滚完成中止:pretxncommit.whitespace 钩子以状态 1 退出$ echo 'a' >一种$ hg commit -A -m '删除尾随空格并重试'

<块引用>

在这个例子中,我们引入了一个简单的pretxncommit 钩子检查尾随空格.这个钩子是简短,但不是很有帮助.它退出如果更改添加,则会显示错误状态一行尾随空格到任何文件,但不打印任何可能有助于我们的信息识别有问题的文件或行.它还具有不注意未修改的线条;只有引入新尾随的行空格会导致问题.

只需将正则表达式从 '^\+.*[ \t]$' 更改为您要查找的任何字符串.

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

解决方案

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.

这篇关于Mercurial 预提交钩子 - 根据文件内容停止提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆