windows中的“git log--grep=”string“有等价物吗?
我为linux编写了一个python程序,它需要读取包含git对象中特定字符串的提交日志。这在Linux中运行得很好,但是当我在Windows中运行同一个程序时,git log--grep=“string”什么也抓不到。
这是代码片段。(FETCHE.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日志本身运行没有任何问题,
我只是在不使用--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%
包含<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\
添加python的路径,就可以“linux grep”而不出现任何问题。