Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于搜索提交消息的Git别名_Git_Search_Alias_Message_Commit - Fatal编程技术网

用于搜索提交消息的Git别名

用于搜索提交消息的Git别名,git,search,alias,message,commit,Git,Search,Alias,Message,Commit,我确实在网上找到了别名: find = "log --decorate -i --all --date=short --grep" search = "!f() { git log --decorate --grep=$1 -i --all --date=short; }; f" 它们都执行相同的操作:在提交消息中进行搜索。是否有一个比另一个更“优越”?为什么? 问题--我们如何编写别名来搜索消息中包含特定单词的提交 例如,其中: git search-msg foo bar baz 是否

我确实在网上找到了别名:

find = "log --decorate -i --all --date=short --grep"

search = "!f() { git log --decorate --grep=$1 -i --all --date=short; }; f"
它们都执行相同的操作:在提交消息中进行搜索。是否有一个比另一个更“优越”?为什么?

问题--我们如何编写别名来搜索消息中包含特定单词的提交

例如,其中:

git search-msg foo bar baz
是否将包含单词
foo
bar
baz
的提交按哪个顺序进行匹配

答案——这里的格式更好(来自LeGEC):


通常,通过使用git命令和匿名bash函数,我们可以访问其他功能

这是我创建的搜索别名的输出,它符合您的要求。我用提交消息“1”、“2”、“3”、“4”创建了4个提交:


您可能希望通过将
rev list
添加到您的
git日志中,来添加搜索整个存储库的历史记录

git log --grep 'Something relevant in my commits' $(git rev-list --all)
类似的内容应该可以在.gitconfig文件中使用

[alias]
    search = "!f() { git log --grep \"$1\" $(git rev-list --all); }; f"
[log]
    date = iso

date=iso
是为了得到一个更好的日期(当前格式是
2019-11-26 17:35:30+0100

首先介绍一下git日志:

  • 您可以多次指定它
  • 默认的多个grep是“匹配任何模式”
  • 还有一个附加选项,指示“匹配所有模式”
例如:

# two commits contain 'foo' in their message (one of them also contains 'bar'):
$ git log --oneline --grep foo
b76121c bar baz foo
b7a342f foo

# two commits contain 'bar' in their message (one of them also contains 'foo'):
$ git log --oneline --grep bar
bbb27f3 (HEAD -> master) bar
b76121c bar baz foo

# if I look for both patterns : I get the union of both searches (3 commits)
$ git log --oneline --grep foo --grep bar
bbb27f3 (HEAD -> master) bar
b76121c bar baz foo
b7a342f foo

# if I add '--all-match' : only the commit which contains both is kept
$ git log --oneline --grep foo --grep bar --all-match
b76121c bar baz foo

回答您的问题:下面是一个示例脚本,它获取参数列表,并将它们转换为
git log--all match--grep aaa--grep bbb
命令:

# in file git-search-msg :

#!/bin/bash

cmd=("log" "--all-match")

for var in "$@"
do
        cmd+=('--grep')
        cmd+=("$var")
done

git "${cmd[@]}"
如果您将其粘贴到名为
git search msg
的文件中,并将其放在
路径的某个位置,则
git search msg foo bar baz
将执行您想要的操作


以下内容在纯
sh
中工作,但使用
eval

#!/bin/sh
str="git log --all-match"

for var in "$@"
do
    # This loops takes arguments one by one and adds
    # them as '--grep' args to the 'git log' command.
    str="$str --grep '$var'"
done

# tech note : this sample script does not escape single quotes
eval $str
如果您设法将其联机(并正确转义),则可以在别名中使用它


注意与
eval
混合的转义问题,尽管…

谢谢,几乎就在那里-只有一个例外:在这里,它返回所有带有a、b或c的提交。虽然问题已经解决了,用a、b和c获取所有提交。亲爱的@Ben,感谢您的尝试,但这并不能解决仅以主题行中的任意顺序显示包含“foo”、“bar”和“baz”的提交的问题,当称为“
git search foo bar baz
”。它甚至似乎重复了消息行,虽然使用不同的提交id。。。从历史上看,是合并还是重定基础?这是我目前使用的别名,它没有显示任何重复项。它解析每个提交以搜索提交消息中的一个事件,因此我猜合并提交也应该返回,因为它们是提交,并且包含您正在查找的消息。我将尝试向您展示/理解为什么会有重复项(相同的作者、相同的提交消息、相同的日期和时间,但不同的哈希).想试着回答这个问题吗?因为别名允许查找包含string1和string2以及…和stringN的提交消息?作为一个脚本,它工作得很好(虽然它不区分大小写,但这是一个要添加的参数)。现在,它可以转换为git别名吗?我试过
!f(){…};f
schema,但没有成功…参数的
cmd
数组是
bash
语法,AFAIK git别名用
sh
解释,并且
sh
中没有数组。一种方法是用参数构建一个字符串并执行它(
eval
sh-c
),不过要小心转义。脚本版本有哪些限制?非常感谢。我从你的答案中学到了!它确实工作得很好。。。
[alias]
    search = "!f() { git log --grep \"$1\" $(git rev-list --all); }; f"
[log]
    date = iso
# two commits contain 'foo' in their message (one of them also contains 'bar'):
$ git log --oneline --grep foo
b76121c bar baz foo
b7a342f foo

# two commits contain 'bar' in their message (one of them also contains 'foo'):
$ git log --oneline --grep bar
bbb27f3 (HEAD -> master) bar
b76121c bar baz foo

# if I look for both patterns : I get the union of both searches (3 commits)
$ git log --oneline --grep foo --grep bar
bbb27f3 (HEAD -> master) bar
b76121c bar baz foo
b7a342f foo

# if I add '--all-match' : only the commit which contains both is kept
$ git log --oneline --grep foo --grep bar --all-match
b76121c bar baz foo
# in file git-search-msg :

#!/bin/bash

cmd=("log" "--all-match")

for var in "$@"
do
        cmd+=('--grep')
        cmd+=("$var")
done

git "${cmd[@]}"
#!/bin/sh
str="git log --all-match"

for var in "$@"
do
    # This loops takes arguments one by one and adds
    # them as '--grep' args to the 'git log' command.
    str="$str --grep '$var'"
done

# tech note : this sample script does not escape single quotes
eval $str