git - 如何通过 .gitattributes 覆盖子目录中的行尾以无扩展名+指定扩展名?
问题描述
我需要将包含 bash 和 PowerShell 脚本的 repo 克隆到 MacOS 和 Windows 系统,在两者上通过 Atom 进行编辑,在 MacOS 上运行本机 bash 脚本或在 Windows 上通过 Ubuntu WSL shell,在 Windows 上的 PowerShell 窗口中运行 PowerShell 脚本。
在 MacOS 上,所有文件都应以 Native (LF) 结尾签出。
在 Windows 上,所有文件都应以 Native (CRLF) 结尾签出,但以下情况除外:
- *.sh 应始终使用 LF,无论位置如何
- *.ps1 应该使用本机(Windows 上的 CRLF,MacOS 上的 LF)
- *.bat,与 *.ps1 相同
- *.cmd,与 *.ps1 相同
- bin/* 应始终使用 LF,除非被
- bin/*.ps1 应该使用本机
- bin/*.bat 应该使用本机
- bin/*.cmd 应该使用本机
基本上,我们可以使用 * text=auto,然后覆盖特定的文件扩展名,但有一种特殊情况 - 任何级别的 bin 目录中的 bash 脚本通常不会有任何扩展名,包括 *.sh。
需要这样做的原因是,在正常行为下,以 Windows 文件系统结尾的 CRLF 行检出的 bash 脚本在通过 WSL 中的 /mnt/c 路径访问时无法运行。我们需要在提交之前编辑并运行它们。
我已经找到并尝试了此处发现的问题中显示的解决方案,这似乎表明我可以使用特定于目录的覆盖来覆盖默认的自动文本处理。这可行,但是更具体的其他覆盖不起作用。
/.git 属性:
# Autodetect text files
* text=auto
# Explicitly identify binary files
*.xls binary
*.xlsx binary
*.doc binary
*.docx binary
*.vsd binary
*.vsdx binary
*.pdf binary
*.png binary
*.jpg binary
*.gif binary
# Explicitly identify script files
*.sh text eol=lf
*.ps1 text
*.bat text
*.cmd text
bin/* text eol=lf
bin/*.ps1 text=auto
bin/*.bat text=auto
bin/*.cmd text=auto
# Explicitly identify text files
*.txt text
*.md text
*.json text
*.yaml text
Linux 上的预期结果 = 所有文件都使用本机的 LF 行结尾。这似乎有效。
Windows 上的预期结果:
/test - expect CRLF - correct
/test.sh - expect LF - correct
/test.ps1 - expect CRLF - correct
/bin/test - expect LF - correct
/bin/test.sh - expect LF - correct
/bin/test.ps1 - expect CRLF - INCORRECT, uses LF
/bin/test.bat - expect CRLF - INCORRECT, uses LF
我没有明确地测试过,但也期待这个:
/templates/test - expect CRLF
/templates/test.sh - expect LF
/templates/test.yaml - expect CRLF
/templates/test.ps1 - expect CRLF
/section1/test - expect CRLF
/section1/test.sh - expect LF
/section1/test.ps1 - expect CRLF
/section1/bin/test - expect LF
/section1/bin/test.sh - expect LF
/section1/bin/test.ps1 - expect CRLF
解决方案
“在 Windows 上”是否意味着“有core.autocrlf
设置”?Afaik Git 没有硬连线默认操作系统,但 Windows 安装的模板通常已经core.autocrlf
设置。
无论如何,我想你可能错过了
当多个模式与路径匹配时,后面的行会覆盖前面的行。这种覆盖是按属性完成的。
在属性文档中;你有一个明确的
bin/* text eol=lf
设置eol
中的所有内容bin
,以及您的bin/*.ps1
bin/*.ps1 text=auto
不会覆盖它,因此bin/*.ps1
继承了显式eol=lf
属性。git check-attr eol bin/any.ps1
您可以使用或进行验证git check-attr -a bin/any.ps1
。
因此,如果我的咖啡仍然正常运行,修复应该是添加-eol
到您的bin/*.ps1
属性模式以覆盖您之前的bin/* eol=lf
力量。
推荐阅读
- python - 如果计数 == 5 的倍数
- android - 更改 LiveData 日历的值不会触发观察者
- sql - PostgreSQL 按最后日期透视
- php - PHP - 为什么 parse_url() 为 /search/publication_year:2019 返回 false?
- c++ - 带有clang的CMake显示未定义的符号,并且带有正确的cl链接
- python-3.x - 当种群大小为 1 时除以零
- redis - 调用使用redis-rs创建redis连接的函数时如何防止生命周期问题
- javascript - 对 Fluent UI Form onChange 不工作做出反应(非常简单的代码)
- algorithm - 对于给定的函数 g(n),为什么 Θ (g(n)) 是函数集?
- postgresql - 关系“博客”的“标题”列中的空值违反非空约束