简体   繁体   English

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

[英]git log --grep does not work in windows

Is there any equivalent for "git log --grep="STRING" in windows? Windows中是否有等同于“ git log --grep =” STRING“的东西

I've written a python program for linux which requires a reading of commit logs that contain certain string from the git object. 我已经为Linux编写了一个python程序,该程序需要读取包含git对象中某些字符串的提交日志。 This worked fine in linux, but when I ran the same program in windows, git log --grep="STRING" catches nothing. 这在linux上工作正常,但是当我在Windows中运行相同的程序时, git log --grep =“ STRING”什么也没发现。

Here's the code snippet. 这是代码片段。 (fetcher.py) (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
...

It seems that git internally uses the linux grep for the "--grep" argument such that Windows cannot run this correctly as it misses grep. 看来git在内部将linux grep用作“ --grep”参数,这样Windows会因为错过了grep而无法正确运行它。

Thanks for your help. 谢谢你的帮助。


As I am not getting any answer for 24 hrs, I suggest my own solution, which does not utilize grep. 由于24小时未收到任何答复,因此我建议使用不使用grep的解决方案。

Because git log itself runs without any problem, I just ran the command without the --grep='STRING' option, then read the output from the shell (or a file) to filter the commit logs which contain 'STRING' by the use of regular expression. 因为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

The approach requires you to add some code, but I believe it does the same thing as the original command does. 该方法要求您添加一些代码,但是我相信它的作用与原始命令相同。 For better results, you can change the last if statement which is, 为了获得更好的结果,您可以更改最后一个if语句,

if 'KEYWORD' is in commit:

into something that can do more sophisticated search eg re.search() method. 可以进行更复杂的搜索的内容,例如re.search()方法。 In my case, this produced exactly the same result as that of --grep="KEYWORD" 就我而言,这产生的结果与--grep =“ KEYWORD”的结果完全相同

Still, I appreciate your help :) 不过,感谢您的帮助:)

It seems that git internally uses the linux grep for the " --grep " argument such that Windows cannot run this correctly as it misses grep. 看来git在内部将linux grep用作“ --grep ”参数,使得Windows无法正确运行此命令,因为它错过了grep。

It certainly can, provided your %PATH% includes <git>/usr/bin (which has 200+ Linux commands compiled for Windows) 只要您的%PATH%包含<git>/usr/bin (为Windows编译了200多个Linux命令),它肯定可以

See this simplified path: 请参阅以下简化路径:

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\

Add the PATH for python, and you can "Linux grep" without any issue. 添加python的PATH,就可以毫无问题地“ Linux grep”。

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

git log --grep "STRING"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM