我可以在git log输出中转义字符吗? [英] Can I escape chars in git log output?

查看:148
本文介绍了我可以在git log输出中转义字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要后处理 git log 的输出,并且已经使用 - pretty 设置进行播放。当我例如
$ b

   -  pretty = format:'{sha:%h,message:%B ,author:%aN<%aE>,commit:%cE,date:%cD} 

我得到了一些类似JSON的输出;当我放入 {} 甚至是进入提交消息,这会弄乱我的输出。



有没有办法告诉 git log 例如通过预先添加一个 \



有两个类似的问题将日志输出到XML,JSON或YAML ,但它们都没有解决转义特殊字符的问题(例如,如果在XML中在我的提交消息中放入了< foo> ,结果XML将被破坏)。方案

转义字符串不是Git的工作; git log 没有任何东西可以帮助您做到这一点。之后,你需要像 sed 这样的字符串编辑为你。



试试这个(应该在大多数shell中工作,但我只检查Cygwin bash):

 函数escape_chars {
sed -r's /(\ {\})/ \\\\1 / g'
}
函数格式{
sha = $(git log -n1 --pretty = format:%h $ 1 | escape_chars)
message = $(git log -n1 --pretty = format:%B $ 1 | escape_chars)
author = $(git log -n1 --pretty = format:'%aN<% aE>'$ 1 | escape_chars)
commit = $(git log -n1 --pretty = format:%cE $ 1 | escape_chars)
date = $(git log -n1 --pretty = format:% cD $ 1 | escape_chars)
echo{\sha \:\$ sha \,\message \:\$ message \,\author \:\$ author\,\commit\:\$ commit \,\date \:\$ date \}


为$(git rev-list)

格式$ hash
完成

以上将转义 {} 而不是 \ ,不过从 JSON.org \ { \} 都是无效转义;只需要 \ (替换 sed sed -r's /(\\)/ \\\\1 / g'来表示真正的JSON输出。 p>

尽管%cE 命令实际上给出了,但我仍然保留了commit值提交者的电子邮件地址;我不确定这是你的意图。



(现在包括一个正确但被拒绝的编辑通过 macrobug 。谢谢!)


I want to post process the output of git log and have been playing with the --pretty settings. When I e.g. do

--pretty=format:'{"sha":"%h","message":"%B","author":"%aN <%aE>","commit":"%cE","date":"%cD"}

I get some JSON-like output; when I put in a { or } or even a " into the commit message this messes up my output.

Is there a way to tell git log to escape those chars e.g. by prepending a \?

There are two similar questions Git log output to XML, JSON or YAML and Git log output preferably as XML, but they both do not address the escaping of the special chars (e.g. if in the XML case I put <foo> in my commit message, the resulting XML will be broken).

解决方案

Escaping strings isn't Git's job; git log doesn't have anything that'll help you do that. To achieve what you're after, you'll need something like sed to do the string editing for you.

Try this (should work in most shells, but I've only checked in Cygwin bash):

function escape_chars {
    sed -r 's/(\{\}")/\\\1/g'
}
function format {
    sha=$(git log -n1 --pretty=format:%h $1 | escape_chars)
    message=$(git log -n1 --pretty=format:%B $1 | escape_chars)
    author=$(git log -n1 --pretty=format:'%aN <%aE>' $1 | escape_chars)
    commit=$(git log -n1 --pretty=format:%cE $1 | escape_chars)
    date=$(git log -n1 --pretty=format:%cD $1 | escape_chars)
    echo "{\"sha\":\"$sha\",\"message\":\"$message\",\"author\":\"$author\",\"commit\":\"$commit\",\"date\":\"$date\"}"
}

for hash in $(git rev-list)
do
  format $hash
done

The above will escape { and } and not \, although from JSON.org both \{ and \} are invalid escapes; only \ and " need to be escaped. (Replace the sed expression with sed -r 's/("\\)/\\\1/g' for true JSON output.)

I've also left the "commit" value as it is in your example, although the %cE command actually gives the commiter's email address; I'm not sure if that's what you intended.

(This now includes a correct but rejected edit by macrobug. Thanks!)

这篇关于我可以在git log输出中转义字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆