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

标签 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"完全相同的结果

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

最佳答案

It seems that git internally uses the linux grep for the "--grep" argument such that Windows cannot run this correctly as it misses 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的PATH,你就可以毫无问题地“Linux grep”。

关于windows - git log --grep 在 Windows 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34172707/

相关文章:

git - Phabricator git 克隆错误 403

git - 版本控制 couchdb map-reduce 函数

linux - Bash:组合多个命令的输出

bash - 如何编写 bash 别名/函数来 grep 所有子目录中的所有文件以获取字符串?

mysql - 重置mysql密码错误

windows - 窗口定位导致 Windows 10 上窗口周围的空间

c++ - 识别文件 (dll/exe) 是否被进程或库锁定

git - 浏览复杂版本控制系统历史的最先进的用户界面是什么?

unix - 计算文件中的空白行数

.net - 在 .NET 代码中使用 Windows 资源管理器?