英文:
Check if git branch contains commit with given subject
问题
给定一个确切的提交主题行,我想知道一个分支是否包含具有该主题的提交。
我现在有的是:git log --format="%s" -F --grep="$msg" "$branch" | grep -Fq --max-count=1 -- "$msg"
也就是说,使用固定字符串的grep搜索日志并打印主题。然后使用相同的固定字符串搜索该主题,并在找到第一个匹配项时停止。
第二个grep是必需的,因为git log --grep
可能会在提交消息的任何位置找到模式(例如 Fixes "$msg"
)。
然而,这样做的缺点是它似乎总是遍历该分支的整个历史记录,这需要相当长的时间。
作为测试,我运行了git log --format="%s" -F --grep="$msg" "$branch" | grep -Fq --max-count=1 -- "$msg"
和 git log --format="%s" -F --grep="$msg" "$branch"
,它们花费了相同的时间,尽管要搜索的提交非常快(对于第二个来说)。
所以是否有一种方法可以更快地直接按给定主题在一个分支中找到一个提交(使用固定字符串,因为 $msg
来自另一个命令,可能包含类似正则表达式的字符),或者至少使我的日志grep管道在成功时更快地退出?
英文:
Given an exact subject line of a commit I want to know if a branch contains a commit with that subject.
What I have right now is: git log --format="%s" -F --grep="$msg" "$branch" | grep -Fq --max-count=1 -- "$msg"
I.e. search the log with a fixed-string grep and print the subject. Then search that subject with the same fixed-string and stop at the first match.
The 2nd grep is required because git log --grep
may find the pattern anywhere in the commit message (e.g Fixes "$msg"
)
However this has the downside that it seemingly always walks the entire history of that branch which takes quite long.
As a test I ran git log --format="%s" -F --grep="$msg" "$branch" | grep -Fq --max-count=1 -- "$msg"
and git log --format="%s" -F --grep="$msg" "$branch"
and they both took the same time although the commit to grep for is found/printed very fast (for the 2nd)
So is there a way to directly find a commit in a branch by a given subject (using fixed strings as $msg
comes from another command and may contain regex-like characters) faster or at least make my log-grep-pipe exit faster (on success)?
答案1
得分: 3
尝试这个:
git log --format="%s" "$branch" | grep -Fqx --max-count=1 -- "$msg"
这里的 x
将匹配整行,而 max-count
将返回第一个匹配。
英文:
Try this:
git log --format="%s" "$branch" | grep -Fqx --max-count=1 -- "$msg"
Here x
will match the entire line.. and max-count will return the first match.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论