简体   繁体   English

如何限制Git / Mercurial日志的提交至少包含两个字符串之一?

[英]How can I limit the Git/Mercurial log to commits whose logs contain at least one of two strings?

Is there a way where I can send two choices for git/Mercurial commands? 有没有一种方法可以为git / Mercurial命令发送两个选择?

I want to search the history of git repository and Mercurial repository for specific word, say "commit". 我想搜索git存储库和Mercurial存储库的历史记录中的特定单词,说“提交”。 However, I am not sure how is it written in commit message "commit" or "com-mit"? 但是,我不确定它是如何写在提交消息“ commit”或“ com-mit”中的? So, I want to send these options with the command. 因此,我想使用命令发送这些选项。 I know that if I want to search I will type: 我知道如果我想搜索,我会输入:

In git: 在git中:

git log --grep="commit"

In mercurial: 在水银中:

hg log --keyword commit

Any help? 有什么帮助吗?

The most flexible way to do this with Mercurial is to use revsets. 使用Mercurial进行此操作最灵活的方法是使用修订集。 For your example: 例如:

hg log -r 'grep("\\b(com-mit|commit)\\b")'

In Python regex syntax, \\b indicates a word boundary, and the backslash needs to be escaped. 在Python regex语法中, \\b表示单词边界,并且需要转义反斜杠。 The (a|b) syntax is used to match one or the other. (a|b)语法用于匹配一个或另一个。 The single quotes surrounding the argument are there so that the shell won't interpret the metacharacters itself. 引号周围有单引号,因此shell不会解释元字符本身。

You can also simplify the regular expression by combining revsets with and : 您还可以通过将revset与and结合使用来简化正则表达式:

hg log -r 'grep("\\bcom-mit\\b") or grep("\\bcommit\\b")'

If you don't need to match words exactly (eg if you want to match "committer" and "committed" too, not just "commit"), you can use the keyword revsets: 如果您不需要完全匹配单词(例如,如果您也想匹配“ committer”和“ committed”,而不仅仅是“ commit”),则可以使用关键字revsets:

hg log -r 'keyword("com-mit") or keyword("commit")'

By using and instead of or , you can find revisions that match both rather than one of them; 通过使用and而不是or ,您可以找到匹配两者而不是其中之一的修订版本。 also, you can use reverse(...) to list matches in the reverse order, eg: 同样,您可以使用reverse(...)以相反的顺序列出匹配项,例如:

hg log -r 'reverse(keyword("com-mit") or keyword("commit"))'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM