Mercurial 预提交挂钩 - 根据文件内容停止提交?

发布于 2024-10-02 14:14:38 字数 42 浏览 8 评论 0原文

如何设置预提交挂钩,它将在提交的文件中搜索字符串,如果找到则停止提交?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小伙你站住 2024-10-09 14:14:38

第 10 章 of mercurial 书 准确地涵盖了这一点:

$ cat .hg/hgrc
[hooks]
pretxncommit.whitespace = hg export tip | (! egrep -q '^\+.*[ \t]

在这个例子中,我们介绍一个简单的
pretxncommit 钩子检查
尾随空白。这个钩子是
很短,但不是很有帮助。它退出
如果添加了更改,则显示错误状态
任何结尾带有空格的行
文件,但不打印任何内容
可能帮助我们的信息
识别有问题的文件或行。
它还有一个很好的特性:不
注意未修改的线路;
仅引入新尾随的行
空格会导致问题。

只需将正则表达式从 '^\+.*[ \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'

在这个例子中,我们介绍一个简单的
pretxncommit 钩子检查
尾随空白。这个钩子是
很短,但不是很有帮助。它退出
如果添加了更改,则显示错误状态
任何结尾带有空格的行
文件,但不打印任何内容
可能帮助我们的信息
识别有问题的文件或行。
它还有一个很好的特性:不
注意未修改的线路;
仅引入新尾随的行
空格会导致问题。

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

Chapter 10 of the mercurial book covers this exactly:

$ cat .hg/hgrc
[hooks]
pretxncommit.whitespace = hg export tip | (! egrep -q '^\+.*[ \t]

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.

) $ 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.

秋心╮凉 2024-10-09 14:14:38

Ry4an 的答案几乎是正确的:)
但您需要将“hg导出提示”替换为“hg diff”。
Tip 是最后提交的更改集,但对本地未提交的更改感兴趣 - 所以 diff 是您所需要的。
为了我的需要,我将以下内容添加到我的 hgrc 中,

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

-S 包括子存储库(可能不需要,并且可能仍然有错误)。
grep -v '^-' 从 diff 中删除指示已删除行的行。
我删除了 -q,所以我至少知道要删除什么,但不幸的是,此方法无法打印发生的文件和行号(因为它是通过管道传输的)。也许有人有更好的方法来做到这一点。

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.

狼性发作 2024-10-09 14:14:38

顺便说一句,在 Windows 上您可以使用

[hooks]
pretxncommit.nocommit = hg export tip | findstr NOCOMMIT && EXIT /B 1 || EXIT /B 0

如果您的源包含字符串“NOCOMMIT”,此挂钩将失败

BTW, on Windows you can use

[hooks]
pretxncommit.nocommit = hg export tip | findstr NOCOMMIT && EXIT /B 1 || EXIT /B 0

This hook will fail if your sources contains string "NOCOMMIT"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文