git-svn 如何处理换行符?

36

我对Git本身如何处理行尾很满意,通过 core.autocrlfcore.eol 和 gitattributes(Tim的帖子非常好)。

我有一个Windows Git存储库,其中 autocrlf 设置为 true。所以,所有文本文件都存储在仓库中作为 LF 并在工作目录中作为 CRLF 存在。这个仓库是从一个SVN仓库克隆而来的,我们仍然使用SVN仓库进行推送/拉取(SVN仓库是我们的中央、授权仓库,用于触发CI等)。

但我不知道 git-svn 在推送/拉取操作期间如何处理行尾。

有人能解释一下在这种情况下 git-svn 做了什么吗?

1个回答

16
我也对此很感兴趣。假设您有一个通过git svn clone创建的存储库,我认为您可以将其分解为三个不同的问题:
1. 在从svn移动提交到git存储库时,是否会在git svn fetch时间进行任何git换行符规范化/更改? 2. 本地向具有svn远程的存储库正常提交期间,在git提交时间是否会进行任何git换行符规范化/更改?合并/变基时间呢? 3. 在推送/重播/任何git提交到svn时,是否会在git svn dcommit时间进行任何git换行符规范化/更改?
我很想听听这些问题的理论答案,但现在我做了一个小实验,似乎至少在情况#1中没有换行符规范化。
rem We'll make a svn repo with CRLF newlines, clone it into git with
rem autocrlf enabled, and try to see if that results in LF-only newlines
rem getting stored in the git repo

cd c:\code

rem Step 1. Prepare SVN repo with CRLF type newlines.
rem The pre-1.4 flag is to prevent an error during git clone.

svnadmin create --pre-1.4-compatible svnrepo
svn checkout file:///C:/code/svnrepo svnworking
cd svnworking
echo "First line" > file.txt
echo "Second line" >> file.txt
echo "Third line" >> file.txt
rem NOTE: At this point file.txt has CRLF newlines
svn add file.txt
svn commit -m "Add file.txt"
rem NOTE: At this point file.txt still has CRLF newlines
cd ..

rem Step 2. Clone the svn repo into git and inspect work copy newline type
git svn clone file:///C:/code/svnrepo gitrepo
rem The following outputs true on my machine
git config --get core.autocrlf
cd gitrepo
rem The following also outputs true on my machine
git config --get core.autocrlf
git svn fetch
rem NOTE: At this point file.txt (git working dir copy) has CRLF newlines

rem Step 3. Disable autocrlf to inspect repo's inner newline type
rem Use the following and my editor to set core.autocrlf to false:
git config --edit --local
rem This now prints false:
git config --get core.autocrlf
git checkout .
rem NOTE: At this point file.txt (git working dir copy) still has CRLF newlines
del file.txt
git checkout .
rem NOTE: Even after explicitly deleting the old one and checking out again,
rem file.txt still has CRLF newlines

如果在我的git svn pull过程中进行了git换行符转换,相反,我期望file.txt在所有这些内容的结尾只有LF换行符。
以下是一个健全性检查,用于验证步骤3实际上是否实现了对存储库是否具有仅LF换行符的有效测试:
rem We'll a git repo with core.autocrlf on, then switch it off to
rem pull out a file

rem The following outputs true
git config --get core.autocrlf
git init gitcrtest
cd gitcrtest
rem The following still outputs true
git config --get core.autocrlf
echo "First line" > file.txt
echo "Second line" >> file.txt
echo "Third line" >> file.txt
git add file.txt
git commit -m "Add file.txt"
rem NOTE: At this point file.txt (git working dir copy) has CRLF newlines
rem Use the following to set core.autocrlf to false
git config --edit --local
git checkout .
rem NOTE: Now file.txt (git working dir copy) has LF-only newlines

总之:根据上述内容,当git-svn从svn拉取时,即使启用了autocrlf,svn提交也会被添加到git提交图中而没有进行任何crlf转换。也就是说,无论您的svn仓库中的文件使用哪种类型的换行符,它们在您的git克隆版本中也将使用相同的换行符。(但是您的git工作副本可能具有不同的换行符类型。)
请注意,这与“git help attributes”中关于行尾规范化的讨论非常一致;在那里,规范化被呈现为发生在将东西从repo拉到您的工作目录中的命令(例如checkout或merge)或将东西从您的工作目录移动到索引/ repo(例如add或commit)中的命令。 “Git svn fetch”似乎不执行这些操作,因此在此时不进行行尾规范化是有意义的。我对dcommit的操作不太清楚,因此我不确定是否应在此时预期行尾规范化。
请注意,如果在您的repo /机器上设置了SVN的svn:eol-style属性,则会出现其他问题。我认为SVN的默认设置是在其端口上不执行行尾转换,但我不确定。

更新: 关于换行符的实际从svn迁移到git的经验,请参阅Tim Abell的描述。如果git-svn没有将CRLF换行符转换为LF-only换行符,则会产生非理想的结果,如果保留了git的自动行尾规范化。解决方案是在git中规范化行尾或禁用行尾规范化。


2
我认为SVN的默认设置是不对其进行行末转换。根据http://svnbook.red-bean.com/en/1.7/svn.advanced.props.file-portability.html,“默认情况下,Subversion不会关注您的文件中使用的行末(EOL)标记类型。” - Adrian Smith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接