c# - Git、Nuget 和行结尾。为什么一定要这么难?

标签 c# git nuget teamcity line-endings

我在 Windows 计算机上使用 git、nuget 和 Visual Studio 2015。

我制作了一个项目,该项目内置到仅内容的 nuget 包中。包的内容是两个文件:

File1.ttinclude
File2.ttinclude

These files MUST have CRLF line endings. I thought this was the default for Windows. I've tried all sorts of git settings and have settled on the following in my global git settings:

autocrlf = false

我可以将文件推送到远程存储库并克隆远程远程文件,并且文件似乎仍然具有 CRLF 行结尾。

我的问题是当我尝试将该 nuget 包包含到另一个项目中时。 每当我运行 install-package (或从包管理器执行此操作)时,文件都会以 LF 行结尾添加到项目中,一切都会崩溃。

我尝试过 autocrl=true,我尝试将 *.ttinclude text eol=crlf 添加到 nuget 包和需要的项目的 .gitattributes包括包。似乎什么都不起作用,我不知所措。

如何让 nuget 安装仅内容包并保持正确的行结尾?

我创建了以下 git 别名,当前在将该 nuget 包添加到项目时使用该别名。它修复了行结束问题,直到更新该 nuget 包。

alias.fixeol=!git add 。 -u && git commit -m “开始 eol 修复” && git rm --cache -r 。 && git reset --hard && git add . && git commit -m“结束 eol 修复”

更新1: 我只是想到了这一点,因为这也可能是问题所在。我们使用 TeamCity 作为构建服务器,这就是从我的源代码构建 nuget 包的原因。不过我没有设置它,所以我不是 100% 知道它是如何设置的。构建服务器上的 git 是否也需要以某种方式设置? TeamCity 设置怎么样?

更新2: 所以我只是检查了.nupkg的nuget包内容,行结尾是LF,所以它一定是构建服务器。现在我只需要弄清楚构建服务器上需要设置什么 git 以及更改它会搞砸其他项目吗?

更新 3 - 已解决: 这是 TeamCity 的问题。 https://confluence.jetbrains.com/pages/viewpage.action?pageId=48105844

将“将行结尾转换为 CRLF”选项设置为 true 可以解决此问题。当然,我不确定为什么这是一个问题。我本地有 autocrlf=false 。这些文件是 CRLF。如果不设置该功能相当于 TC 具有 autocrlf=false 那么它不应该起作用吗?目前我只在这个特定项目上将该选项设置为 true,因为它只是一个需要 CRLF 的仅内容包。我不确定这会如何影响其他项目。

最佳答案

I've tried adding *.ttinclude text eol=crlf to .gitattributes for the nuget package

这是正确的解决方案:始终将 core.autocrlf 设置为 false,并依赖 eol 指令。虽然看到“Why isn’t eol=crlf honored in .gitattributes?”:

I’d misunderstood what git actually does when marking a file with the text attribute. It always stores the files with LF line endings internally and only converts to CRLF on checkout

因此,如果您不需要比较版本并且这些 .ttinclude 文件没有太大变化或从未发生过变化,则另一种方法是:

  • 使用*.ttinclude -text
  • 使用 CRLF 保存并提交它们。

请注意,对于 TeamCity(使用 JGit),.gitattributes were not supported (直到最近?):

关于c# - Git、Nuget 和行结尾。为什么一定要这么难?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41471562/

相关文章:

c# - 在 linq to sql 中将图像存储在数据库中的最佳方法

git 冲突 : diff between BASE version and OTHER version

c# - 使用较新版本的 C# 构建的 NuGet 包是否可以在使用较旧版本的 C# 的项目中使用?

c# - 如何在Azure函数中绑定(bind)到MessageSender?

c# - 如何用我自己在 asp.net core 中的实现替换默认的 IControllerFactory?

C#: 类型名称 <type_name> 在类型 <type> 中不存在;在一个类中,调用另一个类的方法

linux - 将 git repo 从 public_html/stage 文件夹移动到根 public_html/

git - 在 github 上创建单个提交的 pull 请求

azure-devops - 我可以将现有的 NuGet 包上传到 Azure DevOps 工件源吗?

visual-studio - 如果至少有一个源不可用,则无法安装任何 NuGet 包