可以将 git 换行符更改为句点(以获得更好的基于句子的差异)?

Possible to change git line break to period (for better sentence-based diffs)?

是否可以将 git 使用的换行符更改为默认 \n 以外的换行符(例如句点 . 或句点加空格)?

我问是因为这样可以更容易地使用 git 来管理文档和降价文件等文本文件。我看过一些文章建议人们将每个句子放在自己的行中,以便 git 将其视为一个单元(而不是较长段落的一部分),这很尴尬。因此这里的问题。

我做了一些互联网搜索无济于事。

有趣的想法!但是很抱歉,没有。

我对你的问题投了赞成票,因为我喜欢这个主意。不幸的是答案是:不,Git不支持这个。

git config 文档中所述,core.eol 的有效值为 lfcrlf:

Sets the line ending type to use in the working directory for files that are marked as text (either by having the text attribute set, or by having text=auto and Git auto-detecting the contents as text). Alternatives are lf, crlf and native, which uses the platform’s native line ending. The default value is native. See gitattributes[5] for more information on end-of-line conversion. Note that this value is ignored if core.autocrlf is set to true or input.

其他相关 git 配置设置为 core.safecrlfcore.autocrlfgitattributes documentation也这么说。

为什么 git 不太可能支持这个

lfcf 是具有非常特定含义的控制字符。句点 . 等常规字符根据上下文有多种含义。在许多语言中,它标志着一个句子的结束。但这在数字上意味着不同的东西。 ... 常用作省略号,不是三句结尾

所以 git 支持这样的选项会导致存储在 git 存储库中的许多文本文件变得混乱。

解决方法:使用 git 提交 hook 在文本文件中没有句点的每个句点后自动插入 lf

这将是一个非常简单的正则表达式。

通过尝试这种方法,您会发现以下两种情况之一:

  • (a) 太棒了,对我有用!我的文件仍然是普通的文本文件,我的 repo 仍然是正常的,所以其他人可以使用它。

  • (b) 哇,现在我知道他们为什么不支持这个了。真是#$*&#@CRLF 一团糟!

为什么你真的不需要这个

之所以有 "articles suggesting people to put each sentence in its own line" 是因为 git diff 过去只支持 行粒度 差异。行差异对代码非常有用,但对散文来说很糟糕。插入一个句子甚至编辑一个词都会导致整个段落被标记为已更改,除非该段落被分成几行。

但是 git diff 现在支持 字粒度 如果您使用 --word-diff[=<mode>]--word-diff-regex=<regex>--color-words[=<regex>] 选项。

键入 git help diff 或查看 git-diff Documentation 了解更多信息。