如何使用 git-lfs 跟踪具有正确行尾规范化的文本文件?

How can I track text files with proper line endings normalization using git-lfs?

我有一个存储库,我想向其中添加大型文本数据文件。由于它们的数量和大小(在某些情况下可能高达大约 100MB),我想用 git-lfs.

跟踪这些文件

我用git lfs track data.txt添加了这样一个文件,并在.gitattributes文件中将默认的-text(指定二进制文件)更改为text=auto(如 git-scm's gitattributes documentation 中所述)。这给了我一个 .gitattributes 看起来像:

data.txt filter=lfs diff=lfs merge=lfs text=auto

为了确定,我有 refreshed the repository。即便如此,该文件似乎仍被作为二进制对象进行跟踪,相应地,行尾转换过滤器未在签出时应用(即,文件正在签出时使用原始行尾进行签出-与)。

我也尝试过 text=crlf(和变体 text eol=crlf),结果相同。我看过许多关于使用 git-lfs 的文档和教程,但它们似乎都适用于跟踪二进制文件(例如 *.bin, images, audio files, ...)

有没有一种方法可以使用 git-lfs 将文件作为大型文本文件进行跟踪(并像常规文本文件那样对行尾进行标准化)?

我目前在 Windows 7 平台上使用 git-lfs 1.5.2 和 git for Windows 2.10.2(64 位版本),与core.autocrlf=true 配置。

阅读 git-scm's gitattributes and some tinkering, I was able to achieve this functionality by defining a custom filter based on git-lfs's own filter (which I found in ~/.gitconfig) and making use of Jonathan Leffler's unix-to-dos conversion with sed 后:

[filter "textlfs"]
  clean = sed $'s/$/\r/' %f | git-lfs clean
  smudge = git-lfs smudge -- %f | sed $'s/\r$//'
  required = true

然后可用于在具有 .gitattributes 条目的 Windows 机器上跟踪大型文本文件,例如:

data.txt filter=textlfs diff=textlfs merge=textlfs

然而,这会强制存储库用户包含此自定义过滤器定义。为方便起见,您可以 include it in a custom .gitconfig in your repository(请注意,这需要用户手动包含带有 git config --local include.path ../.gitconfig 的定义)。这应该适用于 Windows 平台上的用户,但不适用于具有不同行尾的平台(例如 Linux 和 Mac)上的用户。可以构造一个更复杂的过滤器来处理不同的平台,使用类似的东西:

[filter "textlfs"]
  clean = (if [ `uname -s` == "Linux" ]; then cat %f; else sed $'s/$/\r/' %f; fi) | git-lfs clean
  smudge = git-lfs smudge -- %f | (if [ `uname -s` == "Linux" ]; then cat; else sed $'s/\r$//'; fi)
  required = true

最后,请记住,除非您的大文本文件通常在更新之间发生显着变化,或者它们太大以至于超过文件大小限制 (such as GitHub's), it may still be advantageous to handle these text files as standard text files (i.e. without git-lfs) since git can efficiently pack text files.