1

I'm trying to turn this standard git log --stat output:

commit 8a3f15205922e524d59524fe89c4304849dd6c8c
Author: FooManChu <[email protected]>
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 <[email protected]>
Date:   Fri Jul 15 00:11:24 2016 -0400

    Initial commit

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

Into the xml file structure seen below. It doesn't have to be mapped directly like below, but still close enough to be customized if needed.

<?xml version="1.0" encoding="utf-8"?>
<stats>
    <commit sha1='8a3f15205922e524d59524fe89c4304849dd6c8c'>
        <author email='[email protected]'>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='[email protected]'>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>

I've looked at Parsing Git Log Output and Understand Git Log Stat Output, but I've been unable to duplicate the --stat format.

My question is therefore can this be done in git? or should I be trying to code a parsing program instead that takes --stat as input and outputs my xml file?

3
  • You might use the --numstat flag instead of --stat so you dont have to count plusses and minuses. Commented Aug 2, 2016 at 23:22
  • gist.github.com/textarcana/1306223 claims to output into JSON format. Also see stackoverflow.com/questions/4600445/… Commented Aug 2, 2016 at 23:24
  • I'll try that JSON format. At least it's one step closer in the right direction. Commented Aug 3, 2016 at 23:46

1 Answer 1

1

Here is the original answer: Parse git log file names to json

Parse XML file should be similar to parse JSON file.

This is an example to export git log -stat as json.

We define a function to load the files changed in one 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

Then use git log -1 to show the latest one commit info:

    git log -1 --pretty=format:'{%n  "commit": "%H",%n  "author": "%an ",%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'

Sample result

    [{
    "commit": "1edcef90b42afee11fbd31dcc458ae0f15a3bb6e",
    "author": "Bertrand Martel ",
    "date": "Tue Oct 13 17:35:34 2015 +0200",
    "message": "update-readme",
    "files": [ "README.md",
        "device.png",
        "screenshot.png"
        ]
    }]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.