git和CR vs LF(但不是CRLF) [英] git and CR vs LF (but NOT CRLF)

查看:635
本文介绍了git和CR vs LF(但不是CRLF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来像一个多余的问题(可能是一个多余的问题),但我找不到答案。情况如下:

我的应用程序正在创建具有CR作为行结尾的文本文件。更具体地说,我没有明确地将行结尾设置为CR,它恰好是我用来获取文本正文的命令的输出。当然,我可以手动将CR转换为LF,但我不希望如果我可以避免它。



Git将这些文件视为单行(例如在差异)。我已通过此测试存储库确定了行结束是原因: https://github.com/jfletcher4d / diff-test



我实际上并不关心文件系统中的行尾是什么。这并不重要,至少现在还没有(我可能最终会在乎是否需要导入这些文件,现在只能导出)。但是,如果我可以避免,我不会 将CR转换为LF,这是因为性能方面的原因以及肛门残留原因:)。这不是如何创建文本文件的问题,而是如何强制回购中的每个文本文件都只有LF。



可以将git配置为将所有所有行结尾都更改为LF,即使这些文件在其中提交了CR?



我在Windows上使用TortoiseGit和msysgit (和侧面的一个小TortoiseSVN)交换,而我学习git,如果这很重要。

解决方案

Git似乎并不支持CR行结尾,所以我会写一个过滤器来转换换行符。工作树中的文件将具有CR行结束符,并且在编制索引时它们将透明地转换为LF。过滤器有两部分:干净检查文件,涂抹检查文件。



.git / config

  [filtercr] 
clean = tr'\\\ \\ r''\\''
smudge = tr'\\''\\''

.git / info / attributes (或 .gitattributes 如果它应该)

  * filter = cr 

请注意,这会自动使 git-diff 开心,因为它将使用干净版本。



请记住将模式设置为您需要的文件,否则二进制文件将被损坏,并且所有文本文件都将使用CR行结束符检出。



另外请注意,如果过滤器没有配置,它将自动失败,因此在设置存储库的新副本时添加配置行。


This may sound like a redundant question (and may very well be a redundnant question) but I can't find the answer. Here's the situation:

My application is creating text files that have CR's as line endings. More specifically I'm not explicitly setting the line endings to CR, it just happens to be the output of the command I'm using to get the text body. Of course I could manually convert the CR's to LF's but I don't want to if I can avoid it.

Git's treating these files as a single line (e.g. during a diff). I have determined via this test repository that the line-endings are the cause: https://github.com/jfletcher4d/diff-test

I don't actually care what the line-endings are on the file system. It's not important, at least not yet (I may eventually care if I need to import these files, right now it's only export). But I do not want to convert the CR's to LFs in my application if I can avoid it, for performance reasons as well as anal-retentativeness reasons :) I.e. this isn't a question of how to create text files but, rather, how to force every text file in the repo to have only LFs.

Can git be configured to change all line endings to LF, even if the files are committed with CR in them?

I'm on Windows, using TortoiseGit and msysgit (and a little TortoiseSVN on the side) interchangeably while I learn git, if that matters.

解决方案

Git doesn’t appear to support CR line endings, so I would write a filter to convert the newlines. The files in the work tree will have CR line endings, and they will be transparently converted to LF when they are indexed. The filter has two parts: "clean" checks the files in, and "smudge" checks the files out.

Use this in .git/config:

[filter "cr"]
    clean = tr '\\r' '\\n'
    smudge = tr '\\n' '\\r'

And .git/info/attributes (or .gitattributes if it should be versioned)

* filter=cr

Note that this automatically makes git-diff happy, since it will use the "clean" version.

Just remember to set the pattern to just the files you need, or binary files will be corrupted and all text files will check out with CR line endings.

Also note that if the filter is not configured, it will silently fail, so add the config line when setting up a new copy of the repository.

这篇关于git和CR vs LF(但不是CRLF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆