我正在尝试创建一个脚本,它将读取git log的输出并将其放置为.xml文件
这是脚本的一个例子。
#!/bin/bash
repo=(/srv/git/repositories)
list1=($repo/test.git)
cd "$list1"
echo '<?xml version="1.0" ?><rss version="2.0"><channel>' >> /tmp/test.xml
for i in $(git log --pretty=format:"%h")
do
for e in $(git log | grep "Author:" | awk '{print $2}')
do
#for f in $(git log --pretty=format:"%cn")
#do
#for g in $(git log --pretty=format:"%cD")
#do
cat << EOF >> /tmp/test.xml
<item><title>$i</title><description></description><author>$e</author><pubDate></pubDate></item>
EOF
#done
#done
done
done
echo '</channel></rss>' >> /tmp/test.xml
当我这样做时,这将导致每个提交号和作者将被多次读取和回显。所以我会得到一个像这样的.xml文件: 很多相同的提交号!!
<rss version="2.0">
<channel>
<item>
<title>906feb6</title>
<description/>
<author>test</author>
<pubDate/>
</item>
<item>
<title>906feb6</title>
<description/>
<author>test</author>
<pubDate/>
</item>
<item>
<title>906feb6</title>
<description/>
<author>test</author>
<pubDate/>
</item>
<item>
<title>**906feb6**</title>
<description/>
<author>test1</author>
<pubDate/>
</item>
<item>
<title>**906feb6**</title>
<description/>
<author>test1</author>
<pubDate/>
<item>
<title>**ffb521e**</title>
<description/>
<author>test1</author>
<pubDate/>
</item>
<channel></rss>
我想要的是每个提交号都有作者,描述和pubDate。但它必须从这些命令中获取信息。
我想要这样的输出,有人可以帮忙吗?
<item>
<title>906feb6</title>
<description/>test commit 1</description>
<author>test1</author>
<pubDate>Mar, 18<pubDate/>
<item>
<title>**ffb521e**</title>
<description>test commit 2</description>
<author>test2</author>
<pubDate>Mar, 18<pubDate/>
</item>
答案 0 :(得分:0)
作为@EtanReisner pointed out,您在内部循环中循环所有提交,而不仅仅是外部for
循环正在处理的提交。
以下是如何避免for
循环,并解决该问题。
#!/bin/sh
echo '<?xml version="1.0" ?><rss version="2.0"><channel>' > /tmp/test.xml # Make sure we start with an empty file
git log --pretty=format:"%h" |
while read -r i; do
# Presumably you want a single commit here
# See also https://stackoverflow.com/a/4082178/874188
# Also avoid Useless Use of grep
e=$(git log "$i" -1 | awk '/^Author:/{print $2}')
cat <<____EOF >> /tmp/test.xml
<item><title>$i</title><description></description><author>$e</author><pubDate></pubDate></item>
____EOF
done
echo '</channel></rss>' >> /tmp/test.xml
我认为没有理由将repo放在两个(sic!)数组中。 (如果它只是一个值,为什么要使用数组?)只需在你想要处理的任何repo中运行它。
有了这个,在这个剧本中没有Bashisms,所以我把shebang改为#!/bin/sh
。
要将描述等也包含在代码片段中,也许可能是这样的东西(包装为易读性;应该只是一行):
git log "$i" -1 --format=format:"<item>%n <title>%h</title>
%n <description>%s</description>%n <author>%an</author>
%n <pubDate>%ad</pubDate>%n</item>"