git日志--grep在windows中不工作

git日志--grep在windows中不工作,windows,git,grep,git-log,Windows,Git,Grep,Git Log,在windows中是否有与“git log--grep=“STRING”等效的功能? 我已经为linux编写了一个python程序,它需要读取包含git对象中特定字符串的提交日志。这在linux中运行得很好,但是当我在windows中运行相同的程序时,git log--grep=“string”什么也捕捉不到 下面是代码片段。(fetcher.py) git似乎在内部使用linux grep作为“-grep”参数,因此Windows无法正确运行它,因为它没有使用grep 谢谢你的帮助 因为我在

windows中是否有与“git log--grep=“STRING”等效的功能?

我已经为linux编写了一个python程序,它需要读取包含git对象中特定字符串的提交日志。这在linux中运行得很好,但是当我在windows中运行相同的程序时,git log--grep=“string”什么也捕捉不到

下面是代码片段。(fetcher.py)

git似乎在内部使用linux grep作为“-grep”参数,因此Windows无法正确运行它,因为它没有使用grep

谢谢你的帮助


因为我在24小时内没有得到任何答复, 我建议我自己的解决方案,它不使用grep

因为git log本身运行没有任何问题, 我只是在没有--grep='STRING'选项的情况下运行了该命令,然后从shell(或文件)读取输出,以使用正则表达式过滤包含“STRING”的提交日志

import os
import re

command = "git log --all > log.txt"
os.system(command) # run the command in the shell and store output in log.txt

with open('log.txt', 'r') as fp:
    gitlogoutput = fp.readlines() # read the redirected output

if gitlogoutput:
    commits = re.split('[\n](?=commit\s\w{40}\nAuthor:\s)', gitlogoutput)
    # it splits the output string into each commits
    # at every '\n' which is followed by the string 'commit shahash(40bytes)\nAuthor: '

    for commit it commits:
        if 'KEYWORD' is in commit:
            print commit
该方法需要您添加一些代码,但我相信它的功能与原始命令相同。为了获得更好的结果,您可以更改最后一条if语句,即

if 'KEYWORD' is in commit:
可以进行更复杂的搜索,例如re.search()方法。 在我的例子中,这产生了与--grep=“KEYWORD”完全相同的结果

不过,我还是很感谢你的帮助:)

git似乎在内部使用linux grep作为“
--grep
”参数,这样Windows就无法正确运行它,因为它没有使用grep

当然可以,只要您的
%PATH%
包含
/usr/bin
(其中有200多个针对Windows编译的Linux命令)

请参见此简化路径:

set G=c:\path\to\latest\git
set PATH=%G%\bin;%G%\usr\bin;%G%\mingw64\bin
set PATH=%PATH%;C:\windows\system32;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\

添加python的路径,您就可以“Linux grep”而不会出现任何问题。

在Windows中,使用以下格式(将
=
符号替换为空格):

set G=c:\path\to\latest\git
set PATH=%G%\bin;%G%\usr\bin;%G%\mingw64\bin
set PATH=%PATH%;C:\windows\system32;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\
git log --grep "STRING"