13

I want to parse the output from git log. My current tool does this for svn by parsing the --xml option that svn log has. I can't seem to figure out how to output git log as xml. If xml is not an option, what is the best way to parse this output? I would really like to avoid parsing it as raw text, looking for "author" and "date" ect.

thanks

D.C.
  • 15,340
  • 19
  • 71
  • 102

1 Answers1

12

You could build your own simple xml output by using the formatting options.

git log --pretty=format:"<entry><author>%an</author><commit_date>%cd</commit_date><message_body>%b</message_body></entry>"

Just add whatever fields you want. (You'll need to script a bit if you want proper xml header etc.)

See man git-log PRETTY FORMATS section the list of fields you have access to.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 17
    actually this will NOT work as soon as any of the fields have pseudo-XML in their raw text. Proper support for such an approach would require knowledge of XML (at the very least for escaping) at the git level. But it is going to work correctly in most cases. – Oblomov Apr 26 '12 at 10:04
  • @Oblomov Yeah, i just got burned by that. I ended up wrapping the dangerous stuff in CDATA and later parse it as such. Seems to be working ok. thanks for the heads up. – D.C. Sep 23 '13 at 05:11
  • 3
    Even using CDATA is imperfect. I got burned by a sort of fake XML generator, it passed most text through, and the equivalent of a Git log—different source control system but same idea—had inside it actual XML containing an actual CDATA string. Attempting to wrap it in CDATA caused it to terminate at the embedded CDATA. Given that I had a broken XML generator that I could not fix, I just hacked it the best I could. But if you're going to generate XML, the truly correct method is to encode everything: e.g., use btoa/atob or base64. – torek Aug 03 '16 at 00:01