2010-06-11 102 views
6

当“dcommitting”为svn时,我应该如何将作者(或提交者)名称/日期添加到日志 消息中?Git to svn:添加提交日期以记录消息

例如,如果在Git的日志信息是:

This is a nice modif

我想在SVN的消息是这样的:

This is a nice modif 
----- 
Author: John Doo <[email protected]> 2010-06-10 12:38:22 
Committer: Nice Guy <[email protected]> 2010-06-10 14:05:42

(请注意,我主要感兴趣的日期,因为我已经在.svn-authors中映射svn用户)

任何简单的方法?胡克需要?其他建议?
(另请参阅:http://article.gmane.org/gmane.comp.version-control.git/148861

回答

-1

难道只是改变日志输出格式吗?做到这一点

git log --pretty="format:%s %an %ae %cn %d" 
git help log 
+0

不完全。 这是关于重写日志,当你做“git svn dcommit”。 – 2010-06-11 11:37:36

+0

我明白了。抱歉误会,我没有'git-svn'的经验。 – takeshin 2010-06-11 12:00:11

3

一种方式是通过使用脚本,该GIT_EDITOR环境变量和dcommit--edit选项。

将下列内容保存到一个文件,姑且称之为svnmessage.sh

#!/bin/sh 
c=`git rev-parse HEAD` 
t=`git cat-file -t $c` 
m=`cat "$1"` 
if [ "commit" = "$t" ]; then 
    o=`git cat-file $t $c` 
    o_a=`echo "$o" | grep '^author '` 
    o_c=`echo "$o" | grep '^committer '` 
    author=`echo "$o_a" | sed -e 's/^author \(.*>\).*$/\1/'` 
    authorts=`echo "$o_a" | sed -e 's/^author .*> \([0-9]\+\) .*$/\1/'` 
    authordt=`date -d @$authorts +"%Y-%m-%d %H:%M:%S %z"` 
    committer=`echo "$o_c" | sed -e 's/^committer \(.*>\).*$/\1/'` 
    committerts=`echo "$o_c" | sed -e 's/^committer .*> \([0-9]\+\) .*$/\1/'` 
    committerdt=`date -d @$committerts +"%Y-%m-%d %H:%M:%S %z"` 
    m="$m 
----- 
Author: $author $authordt 
Committer: $committer $committerdt" 
fi 
echo "$m" > "$1" 

确保脚本是可执行的:chmod +x svnmessage.sh。并运行dcommit,如:使用GIT_EDITOR环境变量用于处理提交信息

GIT_EDITOR="/path/to/script/svnmessage.sh" git svn dcommit --edit 

--edit选项将edit the commit message before committing to SVN。有关更多信息,请参见git-svngit-var

您可以创建别名以使事情变得更简单:

git config --global alias.dcommit-edit '!GIT_EDITOR="$HOME/bin/svnmessage.sh" git svn dcommit --edit' 

然后,只需使用git dcommit-edit


脚本依赖于如何git-svn.perl虹吸管的git cat-file输出创建SVN提交信息。使用相同的技术来提取作者和提交者信息。一个简单的承诺可能看起来像:

$ git cat-file commit 24aef4f 
tree eba872d9caad7246406f310c926427cfc5e73c8d 
parent 7dd9de9b5c68b9de1fc3b798edbab2e350ae6eac 
author User <[email protected]>54806 -0500 
committer User <[email protected]>54806 -0500 

foo-27 

脚本通常有.git/COMMIT_EDITMSG传递给它的参数;其内容将包含将用于SVN提交消息的Git提交消息。

+0

感谢您的广泛答复。这听起来很棒!但是,我正在运行Windows ...您是否也有Windows示例?此外,这是否仅仅为当前操作更改GIT_EDITOR环境变量,还是会永久更改所有其他操作的GIT_EDITOR环境变量? – 2011-11-11 17:17:32

+0

@DanielHilgarth:对不起,我没有安装Windows;也许有人用Windows可以试试这个,并提供反馈。使用命令设置'GIT_EDITOR'(或任何环境变量)会替代该命令的'GIT_EDITOR';它不会永久改变它。 – 2011-11-11 17:24:23

+0

感谢您的评论。我认为编写一个与你的脚本相同的小程序不应该太难 - 至少如果我理解它的话:)请你简单地解释一下脚本的作用,特别是那些sed命令?一个具体的例子将有所帮助 – 2011-11-11 17:47:35