2017-06-01 25 views
1

我想輸出JSON格式的文件夾的GitHub歷史記錄,其中包含作者,日期,評論和受影響的文件(路徑)文件。我可以得到所有做,但用下面的命令文件:作爲包含日期,名稱評論和文件JSON的GitHub歷史記錄

git log --pretty=format:'{%n "commit": "%H" %n "author": "%aN <%aE>",%n "date": "%ad", %n "message": "%f" %n },' [email protected] | perl -pe 'BEGIN{print "["}; END{print "]\n"}' | perl -pe 's/},]/}]/' >log.json 

要獲得--name-只能與日誌命令使用的文件名,但我真的不能得到它的JSON作爲數組或「文件」。這裏是我現在所在:

git log --name-only --pretty=format:'{%n "commit": "%H" %n "author": "%aN <%aE>",%n "date": "%ad", %n "message": "%f" %n },' [email protected] | perl -pe 'BEGIN{print "["}; END{print "]\n"}' | perl -pe 's/},]/}]/' >log.json 

這將產生:

{ 
    "commit": "GUID" 
    "author": "My name <[email protected]", 
    "date": "Mon May 29 15:42:58 2017 +0300", 
    "message": "commit comment" 
}, 
/folder/subfolder/file.extention 
/folder/file.extention 

雖然我想在指定爲JSON數組文件的提交:

{ 
    "commit": "GUID" 
    "author": "My name <[email protected]", 
    "date": "Mon May 29 15:42:58 2017 +0300", 
    "message": "commit comment" 
    "files": [ 
    "/folder/subfolder/file.extention" 
    "/folder/file.extention" ] 
}, 
+0

退房[這](https://stackoverflow.com/questions/38106607/git-log-json-with-changed-files)問題和[gist](https://gist.github.com/dmegorov/b64dcea2eed31e02c916fc6ed9111f4f)我修改了回答 –

+0

謝謝@DmitryEgorov。出於某種原因,它不起作用,除了文件以外的所有內容都在執行時顯示。我注意到你使用%f,我在v3中沒有發現漂亮格式的規格。 – checho

+0

那麼,我們可以在聊天會話中進行調試,但您可能更願意嘗試我在答案中提出的方法。 –

回答

2

這裏主要的竅門是從生成的行中分離文件列表,但參數爲--pretty=format:。我建議縮進所有--pretty=format:行,這樣的文件可以很容易地與^(\S.*)正則表達式匹配。

第二個技巧是從文件列表中的最後一項中刪除,並添加右括號。最後一項後跟一個空字符串,爲了檢測這種情況,您可以將整個輸出讀取爲單個字符串(使用-0777),並將其與,\n\n正則表達式匹配。

相同-0777功能,我們可以插入開口[和通過分別匹配^$容易閉]

最後,我們最終用下面的命令:

git log --name-only --pretty=format:' {%n "commit": "%H",%n "author": "%aN <%aE>",%n "date": "%ad", %n "message": "%f", %n "files": [' [email protected] \ 
    | perl -pe 's/^(\S.*)/  "$1",/' \ 
    | perl -0777 -pe 's/^/[\n/; s/,\n\n/\n ]},\n/; s/,$/\n ]}\n]/' 
+0

不錯,只是我想要的,我改變了格式有點 'git log --name-only --pretty = format:'{%n「commit」:「%H」,%n「author」:「 %aN <%aE>「,%n」date「:」%ad「,%n」message「:」%f「,%n」files「:['$ @ \ | perl -pe's/^(\ S。*)/「$ 1」,/'\ | perl -0777 -pe's |^| [\ n | s |,\ n \ n | \ n]},\ n | g; s |,$ | \ n]} \ n] |'' –

相關問題