2010-06-11 50 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