JSON保存git log历史记录里subject带有特殊字符的转义的处理

function escape_chars {

sed -r 's/"/\\\\"/g;s/'/\\\\'/g' #将双引号、单引号这样的特殊字符添加转义符

function format {

subject=$(git log -n1 --pretty=format:%s $1 | escape_chars) #只对subject部分做处理

author=$(git log -n1 --pretty=format:%aN $1)

commit=$(git log -n1 --pretty=format:%h $1)

date=$(git log -n1 --pretty=format:%aD $1)

email=$(git log -n1 --pretty=format:%aE $1)

echo "{\\"commit\\":\\"$commit\\",\\"subject\\":\\"$subject\\",\\"author\\":\\"$author\\",\\"email\\":\\"$email\\",\\"date\\":\\"$date\\"}," >> git.json

rm -rf git.json

for hash in $(git rev-list ${compareBranch} ^master)

format $hash