git log --grep在windows中不起作用

时间:2015-12-09 07:10:10

标签: windows git grep git-log

windows中是否有“git log --grep =”STRING“的等效内容?

我为linux编写了一个python程序,需要读取包含git对象中某些字符串的提交日志。这在linux中运行良好,但是当我在Windows中运行相同的程序时, git log --grep =“STRING”什么都没有。

这是代码段。 (fetcher.py)

import os
...
os.chdir(DIRECTORY) # where git obj is
command = "git log --all --grep='KEYWORD' > log.txt"
os.system(command) # run the command in the shell
...

似乎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:

可以做更复杂的搜索,例如研究方法。 在我的例子中,这产生了与--grep =“KEYWORD”

完全相同的结果

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

2 个答案:

答案 0 :(得分:0)

  

似乎git内部使用linux grep作为“--grep”参数,这样Windows就无法正常运行,因为它错过了grep。

当你的%PATH%包含<git>/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\

添加PATH for python,你可以“Linux grep”没有任何问题。

答案 1 :(得分:0)

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

git log --grep "STRING"