簡體   English   中英

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

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

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:

可以進行更復雜的搜索的內容,例如re.search()方法。 就我而言,這產生的結果與--grep =“ KEYWORD”的結果完全相同

不過,感謝您的幫助:)

看來git在內部將linux grep用作“ --grep ”參數,使得Windows無法正確運行此命令,因為它錯過了grep。

只要您的%PATH%包含<git>/usr/bin (為Windows編譯了200多個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 "STRING"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM