2015-12-09 44 views
1

工作是否有窗口任何等價物「混帳日誌--grep =」 STRING」git的日誌--grep無法在Windows

我已經寫了Linux下的Python程序要求提交包含來自git的對象某些字符串日誌的閱讀,這在linux下工作得很好,但是當我在Windows運行同樣的程序,git的日誌--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的日誌本身運行而沒有任何問題, 我剛剛運行的命令而不--grep =「STRING」選項,然後讀取來自外殼的輸出(或文件)來過濾提交日誌,其包含使用正則表達式的'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」

不過,我感謝您的幫助:)

回答

0

看來,git的內部使用了Linux的grep的「--grep」參數,使Windows無法正確運行它,因爲它錯過了grep。

這當然可以,只要你的%PATH%包括<git>/usr/bin(已200+ Linux命令編譯適用於Windows)

看到這個簡化路徑:

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」沒有任何問題。