windows git set eol to lf

2024-01-14 · 6min
AI 生成的摘要

原因是在写Debbl/eslint-config的测试时,发现在测试Windows系统时总是报错,显示输入和输出不符合。经过测试发现是因为Windows默认在Git下载时的行尾符是CRLF。为了解决这个问题,在GitHub Action中添加了设置行尾符为LF的命令,并通过.gitattributes文件设置了text=auto eol=lf。参考了actions/checkout#135和https://git-scm.com/docs/gitattributes。

原因

在写 Debbl/eslint-config 的测试时

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 中有使用多个系统的测试

yml
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

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

通过 .gitattributes 设置

* text=auto eol=lf

引用