我几乎设法将整个git log
输出转换为有效JSON
格式,现在唯一缺少的是去掉JSON
数组末尾的尾随逗号(见下面的输出)。
由于awk
到目前为止我已经使用它来使其正常工作,因此我更愿意继续使用它,除非我可以使用其他东西获得完全相同的输出。我愿意接受任何建议!
该脚本放置在git
存储库的根目录中,并从终端运行,如:
./gitlog.sh
(考虑到您已经 cd'ed 到存储库的根目录)
剧本:
git log --all --no-merges --shortstat --pretty=format:'commit_hash %H commit_hash_abbreviated %h tree_hash %T tree_hash_abbreviated %t parent_hashes %P parent_hashes_abbreviated %p author_email %ae author_date %ad author_date_unix_timestamp %at author_date_iso_8601 %ai committer_email %ce' | paste -d " " - - - | tail -r | awk -v q='"' -v c=':' -v cl='{' -v cr='}' -v bl='[' -v br=']' -v e=',' -v t=' ' '
BEGIN {
print cl;
print t q "commits" q c bl;
}
{
count++;
print t t cl;
print t t t q "commit_nr" q c q count q e;
print t t t q "commit_hash" q c q $2 q e;
print t t t q "commit_hash_abbreviated" q c q $4 q e;
print t t t q "tree_hash" q c q $6 q e;
print t t t q "tree_hash_abbreviated" q c q $8 q e;
if ( count == 1 ) {
print t t t q "parent_hashes" q c q "" q e;
print t t t q "parent_hashes_abbreviated" q c q "" q e;
print t t t q "author_email" q c q $12 q e;
print t t t q "date_day_week" q c q $14 q e;
print t t t q "date_month_name" q c q $15 q e;
print t t t q "date_month_day" q c q $16 q e;
print t t t q "date_hour" q c q $17 q e;
print t t t q "date_year" q c q $18 q e;
print t t t q "date_hour_gmt" q c q $19 q e;
print t t t q "author_date_unix_timestamp" q c q $21 q e;
print t t t q "date_iso_8601" q c q $23 q e;
print t t t q "committer_email" q c q $27 q e;
print t t t q "files_changed" q c q $28 q e;
print t t t q "insertions" q c q $31 q e;
print t t t q "deletions" q c q $33 q e;
print t t t q "impact" q c q $31 - $33 q;
} else {
print t t t q "parent_hashes" q c q $10 q e;
print t t t q "parent_hashes_abbreviated" q c q $12 q e;
print t t t q "author_email" q c q $14 q e;
print t t t q "date_day_week" q c q $16 q e;
print t t t q "date_month_name" q c q $17 q e;
print t t t q "date_month_day" q c q $18 q e;
print t t t q "date_hour" q c q $19 q e;
print t t t q "date_year" q c q $20 q e;
print t t t q "date_hour_gmt" q c q $21 q e;
print t t t q "author_date_unix_timestamp" q c q $23 q e;
print t t t q "date_iso_8601" q c q $25 q e;
print t t t q "committer_email" q c q $29 q e;
print t t t q "files_changed" q c q $30 q e;
print t t t q "insertions" q c q $33 q e;
print t t t q "deletions" q c q $35 q e;
print t t t q "impact" q c q $33 - $35 q;
}
print t t cr e;
} END {
print t br;
print cr;
}' > gitlog.json
输出:
为了示例而缩短
{
"commits":[
{
"commit_nr":"1",
"commit_hash":"c7a397928f814f29028bccb281de60066395eaa1",
"commit_hash_abbreviated":"c7a3979",
"tree_hash":"e38dac0e625f63e877baa329204511ae490cd944",
"tree_hash_abbreviated":"e38dac0",
"parent_hashes":"",
"parent_hashes_abbreviated":"",
"author_email":"i@dreamyguy.com",
"date_day_week":"Wed",
"date_month_name":"Mar",
"date_month_day":"6",
"date_hour":"09:54:27",
"date_year":"2013",
"date_hour_gmt":"-0800",
"author_date_unix_timestamp":"1362592467",
"date_iso_8601":"2013-03-06",
"committer_email":"i@dreamyguy.com",
"files_changed":"1",
"insertions":"4",
"deletions":"",
"impact":"4"
},
{
"commit_nr":"2",
"commit_hash":"ee3810c9ff8fe144c9ee58f48d99f59885f03462",
"commit_hash_abbreviated":"ee3810c",
"tree_hash":"c6d102d3311a32a2475ba1539247f7ecc26b23d1",
"tree_hash_abbreviated":"c6d102d",
"parent_hashes":"c7a397928f814f29028bccb281de60066395eaa1",
"parent_hashes_abbreviated":"c7a3979",
"author_email":"i@dreamyguy.com",
"date_day_week":"Thu",
"date_month_name":"Mar",
"date_month_day":"7",
"date_hour":"20:20:42",
"date_year":"2013",
"date_hour_gmt":"+0100",
"author_date_unix_timestamp":"1362684042",
"date_iso_8601":"2013-03-07",
"committer_email":"i@dreamyguy.com",
"files_changed":"2",
"insertions":"481133",
"deletions":"",
"impact":"481133"
},
{
"commit_nr":"3",
"commit_hash":"bc9a179663f00f134041ac750a56df8280e0b50b",
"commit_hash_abbreviated":"bc9a179",
"tree_hash":"f80d83c9e8e14ff55b2d77921cb0479bd173bae9",
"tree_hash_abbreviated":"f80d83c",
"parent_hashes":"ee3810c9ff8fe144c9ee58f48d99f59885f03462",
"parent_hashes_abbreviated":"ee3810c",
"author_email":"i@dreamyguy.com",
"date_day_week":"Thu",
"date_month_name":"Mar",
"date_month_day":"7",
"date_hour":"20:22:03",
"date_year":"2013",
"date_hour_gmt":"+0100",
"author_date_unix_timestamp":"1362684123",
"date_iso_8601":"2013-03-07",
"committer_email":"i@dreamyguy.com",
"files_changed":"1",
"insertions":"71",
"deletions":"3",
"impact":"68"
},
]
}
如您所见,这里的问题是该行为循环中的每一行(包括最后一行)输出一个逗号(通过我命名print t t cr e;
的预定义变量)。:(awk
e
我尝试过的:
我替换print t t cr e;
为:
if (getline == 0) { print t t cr; } else { print t t cr e; }
getline
当它到达最后一行时应该返回0
,但这不是一个可靠的解决方案:它停在我的 25 行输出(25 次提交)的第 13 行。我还使用官方 git 存储库 ( https://github.com/git/git/commits/master ) 测试了这种方法。最后一次提交后,我仍然得到尾随逗号。
由于脚本中的某些缺陷,可能getline
无法获得真实的行数。但两者都NR
返回count
准确的行数。
任何帮助是极大的赞赏!