【问题标题】:How to output entire git log --stat to xml如何将整个 git log --stat 输出到 xml
【发布时间】:2016-08-02 22:58:11
【问题描述】:

我正在尝试将这个标准的 git log --stat 输出:

commit 8a3f15205922e524d59524fe89c4304849dd6c8c
Author: FooManChu <fooMC@email.com>
Date:   Fri Jul 15 20:33:17 2016 -0400

    Some Commit Message Body

 foodir/foofile1.foo |    8 +
 foodir/foofile2.foo |   47 +
 foodir/foofile3.foo | 7049 +++++++++++++++++++++++
 foodir/foofile4.foo | 3563 ++++++++++++
 foodir/foofile5.foo |   24 +
 foodir/foofile6.foo |    0
 foodir/foofile7.foo |   41 +
 7 files changed, 10732 insertions(+)

commit c8bd4ca8d683c20f3cf206fdeb8dbec2f185536e
Author: FooGirlChu <fooGC@email.com>
Date:   Fri Jul 15 00:11:24 2016 -0400

    Initial commit

 FOO       | 674 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 README.md |   1 +
 2 files changed, 675 insertions(+)

进入下图所示的xml文件结构。它不必像下面那样直接映射,但仍然足够接近,可以在需要时进行定制。

<?xml version="1.0" encoding="utf-8"?>
<stats>
    <commit sha1='8a3f15205922e524d59524fe89c4304849dd6c8c'>
        <author email='fooMC@email.com'>FooManChu</author>
        <date>Fri Jul 15 20:33:17 2016 -0400</date>
        <body>Some Commit Message Body</body>
        <diff-stat>
            <filepathname insertions='8'>foodir/foofile1.foo</filepathname>
            <filepathname insertions='47'>foodir/foofile2.foo</filepathname>
            <filepathname insertions='7049'>foodir/foofile3.foo</filepathname>
            <filepathname insertions='3563'>foodir/foofile4.foo</filepathname>
            <filepathname insertions='24'>foodir/foofile5.foo</filepathname>
            <filepathname insertions='0'>foodir/foofile6.foo</filepathname>
            <filepathname insertions='41'>foodir/foofile7.foo</filepathname>
        </diff-stat>
        <summary changed='7' insertions='10732'/>
    </commit>
    <commit sha1='c8bd4ca8d683c20f3cf206fdeb8dbec2f185536e'>
        <author email='fooGC@email.com'>FooGirlChu</author>
        <date>Fri Jul 15 00:11:24 2016 -0400</date>
        <body>Initial commit</body>
        <diff-stat>
            <filepathname insertions='674'>FOO</filepathname>
            <filepathname insertions='1'>README.md</filepathname>
        </diff-stat>
        <summary changed='2' insertions='675'/>
    </commit>
</stats>

我查看了 Parsing Git Log OutputUnderstand Git Log Stat Output,但无法复制 --stat 格式。

因此我的问题是这可以在 git 中完成吗?还是我应该尝试编写一个解析程序来代替 --stat 作为输入并输出我的 xml 文件?

【问题讨论】:

标签: xml git


【解决方案1】:

这是原始答案:Parse git log file names to json

解析 XML 文件应该类似于解析 JSON 文件。

这是一个将 git log -stat 导出为 json 的示例。

我们定义了一个函数来加载在一次 git commit 中更改的文件

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 显示最新的一次提交信息:

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

示例结果

[{ “提交”:“1edcef90b42afee11fbd31dcc458ae0f15a3bb6e”, “作者”:“伯特兰·马特尔”, "日期": "2015 年 10 月 13 日星期二 17:35:34 +0200", “消息”:“更新自述文件”, “文件”:[“README.md”, "设备.png", “截图.png” ] }]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-22
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多