在结帐期间使用 .gitattributes 管理行结尾

发布于 2025-01-10 00:12:53 字数 1202 浏览 1 评论 0原文

使用 SmartGit 版本 21.2.1(git 版本 2.30.2.windows.1)。 我有一个现有的 git 存储库,其中一些文件使用 CRLF 作为行结尾,有些文件使用 LF。我发现 https://git-scm.com/docs/gitattributes 并正在尝试什么发生在结账时。该文档似乎说以下 .gitattributes 应该规范所有文件以在签入期间使用 LF 结尾,并在签出期间让我控制它们的外观:

* text=auto eol=crlf
*.py eol=lf

第 1 行是否意味着:

默认情况下,评估所有文件以确定它们是否是文本文件。 如果不是,就别管他们了。如果它们是文本文件,则在签出时将行结尾转换为 CRLF 并在签入时 转换为 LF。

对于 Python 文件,第 2 行是否会这样覆盖第 1 行:

将 Python 文件视为文本文件,并在签出和签入时进行转换 行结尾为 LF。

我尝试了这个实验并得到了意想不到的结果。初始条件:全局 core.autocrlf=false,repo 中没有 .gitattributes 文件。

  1. 使用这些文件创建一个“旧版”存储库(名称表示签入时的行结尾): CRLF.txt, LF.txt, CRLF.py, LF.py
  2. 签出不同的分支,然后回到原来的分支;确认结帐未更改任何文件的行结尾。
  3. 签入包含上述两行的 .gitattributes 文件
  4. 签出不同的分支,然后返回到原始分支。

在第 4 步之后,我期望找到这些行结尾:(

CRLF in CRLF.txt
CRLF in LF.txt
LF in CRLF.py
LF in LF.py

我期望第 1 行处理 .txt 文件,第 2 行处理 .py 文件)

但这就是我实际得到的:

CRLF in CRLF.txt
CRLF in LF.txt
CRLF in CRLF.py
LF in LF.py

TXT 文件看起来不错,但为什么没有’ t git Convert CRLF.py 在结账后以 LF 结尾?

Using SmartGit version 21.2.1 (git version 2.30.2.windows.1).
I have an existing git repo in which some files use CRLF for line endings and some use LF. I found https://git-scm.com/docs/gitattributes and am experimenting with what happens during checkout. The doc seems to say the following .gitattributes should normalize all files to use LF endings during checkin, and during checkout give me control over how they look:

* text=auto eol=crlf
*.py eol=lf

Does Line 1 mean:

By default, evaluate all files to determine if they are text files.
If they aren't, leave them alone. If they are text files then on checkout convert line endings to CRLF and on checkin
convert to LF.

Does Line 2 override Line 1 for Python files this way:

Treat Python files as text files and on checkout and checkin convert
line endings to LF.

I tried this experiment and got unexpected results. Initial conditions: global core.autocrlf=false, no .gitattributes file in the repo.

  1. Create a "legacy" repo with these files (names indicate line endings at checkin time):
    CRLF.txt, LF.txt, CRLF.py, LF.py
  2. Check out a different branch and then go back to the original branch; confirm checkout did not change any file's line endings.
  3. Check in a .gitattributes file containing the above 2 lines
  4. Check out a different branch and then go back to the original branch.

After step 4 I was expecting to find these line endings:

CRLF in CRLF.txt
CRLF in LF.txt
LF in CRLF.py
LF in LF.py

(I expected Line 1 to handle the .txt files and Line 2 to handle the .py files)

But this is what I actually got:

CRLF in CRLF.txt
CRLF in LF.txt
CRLF in CRLF.py
LF in LF.py

TXT files look good, but why didn't git convert CRLF.py to end in LF after the checkout?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

錯遇了你 2025-01-17 00:12:53

看来我误解了 git 文档。 eol 属性仅在设置为时在结帐时进行转换:

eol=crlf

而该值仅充当“转换阻止程序”:

eol=lf

因此您可以获得索引中的任何内容。不过,在这两种情况下,eol 在签入时都会将文件标准化为 LF,但我在实验期间没有修改/签入任何文件。

我发现了这一点,因为当我将文本属性添加到第 2 行时:

*.py text eol=lf

因为然后我被提示检查 CRLF.py(即使我没有更改它),因此它在索引中标准化为 LF。文档中的这一行听起来像是没有必要将文本属性添加到第 2 行:

eol:启用行尾转换,无需任何内容检查,
有效设置文本属性

但似乎没有文本属性,您不会收到签入/规范化 .py 文件的提示,因此直到您下次因其他原因手动更改文件并将其签入时才会发生这种情况。

It appears I misinterpreted the git docs. The eol attribute only converts on checkout when it is set to:

eol=crlf

Whereas this value acts only as a "conversion blocker":

eol=lf

so you get whatever was in the index. In both cases, though, eol normalizes files to LF on checkin, but I wasn't modifying/checking in any of the files during my experiment.

I figured this out because when I added the text attribute to Line 2:

*.py text eol=lf

because then I got prompted to check CRLF.py in (even though I hadn't changed it), thus it got normalized to LF in the index. This line in the docs make it sound like adding the text attribute to Line 2 should be unnecessary:

eol: Enables end-of-line conversion without any content checks,
effectively setting the text attribute.

But seemingly without the text attribute you don't get prompted to checkin/normalize the .py files and so this wouldn't happen until the next time you manually change the files for some other reason and check it in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文