简体   繁体   English

Git - 在 Linux 上使用 Linux 样式的行结尾,在 Windows 上使用 Windows 样式的行结尾

[英]Git - Use Linux-style Line Endings on Linux and Windows-style Line Endings on Windows

I have code (written in C#) that will run on both Linux and Windows.我有可以在 Linux 和 Windows 上运行的代码(用 C# 编写)。 As part of the automated build process, unit/integration tests are run on TeamCity build machines.作为自动化构建过程的一部分,单元/集成测试在 TeamCity 构建机器上运行。 If a change is made, it will trigger both a Linux and Windows build, and the same unit tests will be executed on Linux and Windows.如果进行更改,它将同时触发 Linux 和 Windows 构建,并且将在 Linux 和 Linux 和 ZAEA234406EB289CE0AACDB6406.EB28CE0AACDB6406.

Those build agents use git to pull down the source/build/test.这些构建代理使用git来下拉源/构建/测试。

The desired behavior is for the Linux agent to have source code using Linux style (LF) line endings, while the Windows agent's code will have Windows style (CR LF) line endings. The desired behavior is for the Linux agent to have source code using Linux style (LF) line endings, while the Windows agent's code will have Windows style (CR LF) line endings. The reason for this is that the code uses multiline string literals as input to some of the unit tests, and the code under test uses Environment.NewLine原因是代码使用多行字符串文字作为某些单元测试的输入,而被测代码使用Environment.NewLine

var example = @"This is a really,
really long string";

In both Linux and Windows I want to have the system defined Environment.NewLine appear in the string, but I've been unable to accomplish this given my constraints.在 Linux 和 Windows 中,我想让系统定义的 Environment.NewLine 出现在字符串中,但鉴于我的限制,我一直无法做到这一点。

I do not have access to the build agents.我无权访问构建代理。 Any settings or values that it would need to do it's job need to be captured in my source code, not some local value on the machine.它需要完成工作的任何设置或值都需要在我的源代码中捕获,而不是机器上的某些本地值。 My limited understanding tells me that I should rely on a .gitattributes file, and while I've seen, and even slightly understand, some of the various settings around line endings that I can use within the .gitattributes file.我有限的理解告诉我,我应该依赖.gitattributes文件,虽然我已经看到,甚至稍微了解了一些可以在.gitattributes文件中使用的行尾的各种设置。

I can't just configure the Windows machines to use core.autocrlf=true and the Linux machines to use core.autocrlf=false or some variation, unless I can do it only by adding or modifying files contained within my repository.我不能只配置 Windows 机器使用core.autocrlf=true和 Linux 机器使用core.autocrlf=false或一些变体,除非我只能通过添加或修改包含在我的存储库中的文件来做到这一点。

Is there a way to accomplish this using git, given my constraints?考虑到我的限制,有没有办法使用 git 来实现这一点?

I can't just configure the Windows machines to use core.autocrlf=true and the Linux machines to use core.autocrlf=false我不能只配置 Windows 机器使用 core.autocrlf=true 和 Linux 机器使用 core.autocrlf=false

Perfect, core.autocrlf should (almost) always be set to false, anyway .完美, core.autocrlf应该(几乎)始终设置为 false

The code itself should be LF only: it will be interpreted correctly by an IDE both on Windows and Linux.代码本身只能是 LF:它将被 Windows 和 Linux 上的 IDE 正确解释。

As for the string, the unit test code should:至于字符串,单元测试代码应该:

  • detect the OS检测操作系统
  • transform said string used in test to use LF or CRLF depending on the OS根据操作系统将测试中使用的所述字符串转换为使用 LF 或 CRLF

Meaning: Git has nothing to do with how the unit test should run.含义: Git 与单元测试应该如何运行无关。
Said unit tests should not depend on an external tool (here a version control one) being correctly configured or not.所述单元测试不应依赖于是否正确配置了外部工具(这里是版本控制工具)。 They should run correctly independently of any .gitattributes : their code should take care of building the expected result.它们应该独立于任何.gitattributes正确运行:它们的代码应该负责构建预期结果。

Git by default performs line ending conversions on text files. Git 默认对文本文件执行行结束转换。 If you want files to have their line endings converted, you can mark them as text by flagging them in your .gitattributes file:如果您希望文件的行尾被转换,您可以通过在.gitattributes文件中标记它们来将它们标记为文本:

*.cs text

If you'd like Git to guess about which files are text automatically, then you should write something like this:如果您希望 Git 自动猜测哪些文件是文本,那么您应该编写如下内容:

* text=auto

Assuming your build agents do not have custom Git configuration that overrides line endings, that should be sufficient for Git to convert line endings to the native ones.假设您的构建代理没有覆盖行尾的自定义 Git 配置,这足以让 Git 将行尾转换为本地行尾。 If you'd like to be explicit, you can perform the clone with core.eol set to native , like so: git -c core.eol=native clone URL .如果您想明确一点,可以将core.eol设置为native执行克隆,如下所示: git -c core.eol=native clone URL

However, having said that, it's generally not a good idea in your code to make assumptions about line endings.但是,话虽如此,在您的代码中对行尾进行假设通常不是一个好主意。 If your users are using the Windows Subsystem for Linux, they may well be using a Unix Git and use Unix line endings in their working tree, and that shouldn't affect the correctness of your unit tests. If your users are using the Windows Subsystem for Linux, they may well be using a Unix Git and use Unix line endings in their working tree, and that shouldn't affect the correctness of your unit tests. Line endings should be set either explicitly to LF or CRLF because of functional reasons (eg, shell scripts won't work with CRLF endings on any platform) or left to the user's desires based on their development environment.出于功能原因(例如,shell 脚本在任何平台上都不能与 CRLF 结尾一起使用),行尾应明确设置为 LF 或 CRLF,或者根据用户的开发环境自行决定。

If you need to explicitly use the platform's native line endings in a test, you should write an explicit string that will be parsed appropriately based on the platform, or wrap your test strings in a helper function that converts them based on the platform.如果您需要在测试中显式使用平台的本机行尾,您应该编写一个显式字符串,该字符串将根据平台进行适当解析,或者将您的测试字符串包装在基于平台转换它们的帮助程序 function 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM