如何在 SVN 操作期间将所有行结尾转换为 CRLF、LF 或 CR
因此,您已准备好进行大型 SVN 提交,但它会失败,因为某些文件中的行结尾不一致。 有趣的是,您正在查看跨越数十个不同深度的文件夹的 1,000 个文件。
你做什么工作?
So, you are all ready to do a big SVN Commit and it bombs because you have inconsistent line endings in some of your files. Fun part is, you're looking at 1,000s of files spanning dozens of folders of different depths.
What do you do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
添加一个预提交钩子,它解析文件内容并执行在写入 SVN 之前为您修改 CRLF/LF/CR/etc。
Add a pre-commit hook which parses the file content and performs the munging of CRLF/LF/CR/etc for you before it's written to SVN.
您可以考虑使用 Linux 的 dos2unix 等命令进行转换。 作为一个 Linux 命令,很容易在批处理模式下通过脚本等使用它。我不知道其他操作系统是否有等效的命令。
You may consider using a command like Linux's dos2unix for the conversion. Being a Linux command, it is easy to use it in batch mode with scripts etc. I do not know whether there is an equivalent for other operating systems.
您可以使用notepad++批量转换行结尾。
进行正则表达式搜索:
([^\r])\n
并将其替换为
$1\r\n
然后你应该选择一堆测试文件,例如:
*.xml;*.txt;*.csv;...asf。
这可以避免您意外修改二进制文件
注意:正则表达式模式会跳过空行,因此您必须使用
\n\n
运行第二个替换作业并将其替换为 <代码>\n\r\nyou can use notepad++ to batch convert line endings.
Make regex search:
([^\r])\n
and replace it with
$1\r\n
you then should choose a bunch of test files like:
*.xml;*.txt;*.csv;...
asf.this avoids that you accidently modify binary files
NOTE: the regex patterns skip empty lines, so you have to run a second replace job with
\n\n
and replace it with\n\r\n
我不认为 预提交 钩子实际上可以更改正在提交的数据 - 它可以禁止提交,但我认为它不能为您进行转换。
听起来您想要 属性 'svn:eol -style' 设置为 'native' - 这会自动将换行符转换为您的平台上使用的任何内容(使用 'CRLF'、'CR' 或 'LF' 来获取这些换行符,无论操作系统想要什么)。
您可以使用 auto-properties 以便您将来创建的所有文件都将具有此属性设置(自动道具在客户端处理,因此您必须为每个用户进行设置)。
I don't think the pre-commit hook can actually change the data that is being committed - it can disallow a commit, but I don't think it can do the conversion for you.
It sounds like you want the property 'svn:eol-style' set to 'native' - this will automatically convert newlines to whatever is used on your platform (use 'CRLF', 'CR' or 'LF' to get those regardless of what the OS wants).
You can use auto-properties so that all future files you create will have this property set (auto props are handled client-side, so you'd have to set this up for each user).
首先是清理一切。 您使用的是 Windows 还是 Unix/Linux/Mac?
如果您使用的是 Unix/Linux/Mac,您可以尝试如下操作:
前提是您的机器上有
dos2unix
。 它不在我的 Mac 上,也不在我们拥有的六台 Linux 机器上。 看来我们没有安装这个特定的软件包。 幸运的是,它很容易找到。请小心使用它,因为您不想修改二进制文件。
清理完所有内容后,您应该将 svn:eol-style 属性添加到文件中。 将其设置为native将检出具有适合您的计算机的正确行结尾的文件,但将它们存储为 Unix 行结尾格式。 其他三个选项是“LF”(适用于 Unix)、“CRLF”(适用于 Windows)和“CR”(适用于 Mac OS X 之前的 Mac)。 大多数人认为“本土”的锻炼效果最好。 Native 的唯一问题是它不会签入具有混合行结尾的文件,而“LF”和“CRLF”则会签入。
完成此操作后,您应该会获得一个 预提交挂钩,它将允许您在特定文件上强制执行行结尾。 然后,教您的开发人员使用 自动属性。 预提交挂钩将阻止任何提交,除非该属性放置在文件上。开发人员的提交被拒绝一两次,他们将自行设置自动属性。
First is to clean everything up. Are you on Windows or Unix/Linux/Mac?
If you're on Unix/Linux/Mac, you can try something like this:
That's if you have
dos2unix
on your box. It's not on my Mac or any of the six Linux machines we have. Seems like we didn't install this particular package. Fortunately, it's easy enough to find.Be careful using it because you don't want to to munge binary files.
Once you've cleaned everything up, you should put the
svn:eol-style
property on your files. Setting it to native will checkout the file with the correct line ending for your machine, but store them in Unix line ending format. The other three options are "LF" for Unix, "CRLF" for Windows, and "CR" for pre Mac OS X Macs. Most people find "native" to work out the best. The only problem with Native is that it won't check in a file with mixed line endings while "LF" and "CRLF" will.Once you do that, you should get a pre-commit hook that will allow you to enforce line endings on particular files. Then, teach your developers to use autoproperties. The pre-commit hook will prevent any commits unless the property is placed on the file A developer gets their commit rejected once or twice, and they'll setup auto properties on their own.