2015-12-02 22 views
3

我使用下面的代碼來獲得其已在過去改變提交的文件的名稱:解析git的日誌文件名以JSON

git log -1 --stat 

現在我需要解析的結果,以JSON。我知道,我可以用漂亮的格式向所有「git的日誌」數據解析成JSON(pretty-formats)是這樣的:

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/},]/}]/' 

,但有沒有辦法也解析此JSON變化文件名爲例?

回答

1

你可以做到以下幾點:

function getcommit { \ 
    git show --pretty="format:" --name-only $1 | \ 
    perl -pe's/^\n//g;' | \ 
    sed 's/\(.*\)/"\1"/g' | \ 
    perl -0pe 's/\n(?!\Z)/,\n/g'; \ 
} 

export -f getcommit 

git log -1 --pretty=format:'{%n "commit": "%H",%n "author": "%an <%ae>",%n "date": "%ad",%n "message": "%f",%n "files": [ COMMIT_HASH_%H ]%n},' | \ 
perl -pe 'BEGIN{print "["}; END{print "]\n"}' | \ 
perl -pe 's/},]/}]/;s/COMMIT_HASH_(\w+)/`echo -n "";getcommit $1`/e' 

基本上,我換成犯哈希以固定字符串COMMIT_HASH_哈希本身前面,然後用git show --pretty="format:" --name-only $COMMIT_HASH結果替換此哈希。

所有更改的文件都放入一個json數組「文件」中。這是工作在過去的X提交

下面是2爲例最後提交:

[{ 
    "commit": "1edcef90b42afee11fbd31dcc458ae0f15a3bb6e", 
    "author": "Bertrand Martel <[email protected]>", 
    "date": "Tue Oct 13 17:35:34 2015 +0200", 
    "message": "update-readme", 
    "files": [ "README.md", 
"device.png", 
"screenshot.png" 
    ] 
}, 
{ 
    "commit": "8aa2ce64e58b770122a3561b8ef41d807ce36abc", 
    "author": "Bertrand Martel <[email protected]>", 
    "date": "Mon Oct 12 19:36:18 2015 +0200", 
    "message": "fix-async-bluetooth-command-bug-bluetoooth-state-check", 
    "files": [ "android/app/src/main/java/fr/bmartel/android/bluetooth/BluetoothCustomManager.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/GattTask.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/GattUtils.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/IBluetoothCustomManager.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/IBluetoothManagerEventListener.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/ICharacteristicListener.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/IDevice.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/IDeviceInitListener.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/IScanListListener.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/connection/BluetoothDeviceAbstr.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/connection/BluetoothDeviceConn.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/connection/IBluetoothDeviceConn.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/listener/IPushListener.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/notti/INottiDevice.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/notti/INottiListener.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/notti/NottiDevice.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/shared/ActionFilterGatt.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/shared/ISharedActivity.java", 
"android/app/src/main/java/fr/bmartel/android/bluetooth/shared/LeDeviceListAdapter.java", 
"android/app/src/main/java/fr/bmartel/android/notti/NottiActivity.java", 
"android/app/src/main/java/fr/bmartel/android/notti/NottiBtService.java", 
"android/app/src/main/java/fr/bmartel/android/notti/NottiDeviceActivity.java" 
    ] 
}] 

這裏是一個腳本,需要在參數犯指數和返回JSON信息,包括文件的更改:https://gist.github.com/akinaru/a4ed5d76562e74d77282