windows git set eol to lf

Jan-14, 2024 · 3min

原因

在写 Debbl/eslint-config 的测试时

fixture.text.ts
await Promise.all(
  files.map(async (file) => {
    let content = await fs.readFile(join(target, file), 'utf-8')
    const source = await fs.readFile(join(from, file), 'utf-8')

    if (content === source) {
      content = '// unchanged\n'
    }

    await expect.soft(content).toMatchFileSnapshot(join(output, file))
  }),
)

在 github action 中有使用多个系统的测试

strategy:
  matrix:
    node: [lts/*]
    os: [ubuntu-latest, windows-latest, macos-latest]
  fail-fast: false

但是在测试 windows 时总是报错 test,显示 input 和 output 不符合,在测试后发现是 windows 默认的在 git 下载是的 eolcrlf

所以搜了一下在 GitHub Action 如何设置 eollf actions/checkout#135

最终添加如下命令 ci.yml

windows 可以通过以下命令设置为 lf

git config --global core.autocrlf false
git config --global core.eol lf
- name: Set git to use LF
  run: |
    git config --global core.autocrlf false
    git config --global core.eol lf

通过 .gitattributes 设置

* text=auto eol=lf

引用