问题描述
在某些项目中,某些文件包含^ M作为换行符分隔符。对这些文件进行分解显然是不可能的,因为git-diff将整个文件视为一行。
与以前版本的差异如何?
是否有像在处理时将^ M作为换行符的选项?
提示> git-diffHEAD ^ - MyFile.as
diff --git a / myproject / MyFile.as b / myproject / MyFile.as
index be78321..a393ba3 100644
- - a / myproject / MyFile.cpp
+++ b / myproject / MyFile.cpp
@@ -1 +1 @@
- < U + FEFF> import flash.events。 MouseEvent; ^ Mimport mx.controls。*; ^ Mimport mx.utils.Delegate
\在文件结尾没有新行
+< U + FEFF> import flash.events.MouseEvent; ^ Mimport mx .controls。*; ^ Mimport mx.utils.Delegate
\不在文件末尾换行
prompt>
更新:
现在我已经编写了一个脚本,用于检查最新的10个修订版本,并将CR转换为LF。
需要'fileutils'
如果ARGV.size!= 3
puts必须提供git-path
puts必须提供文件名
puts
putsexample:
putsruby gitcrdiff.rb project / dir1 / dir2 / dir3 / SomeFile.cpp tmp_somefile
exit(1)
end
gitpath = ARGV [0]
filename = ARGV [1]
resultdir = ARGV [2]
除非FileTest.exist ?(.git)
puts此命令必须在与.git所在的目录相同的目录中运行
exit(1)
end
if FileTest.exist?(resultdir)
putsresult dir must not exist
exit(1)
end
FileUtils.mkdir(resultdir)
10.times do | i |
revision =^* i
cmd =git show HEAD#{revision}:#{gitpath}#{filename} | tr'\\'r''\\' >#{resultdir} /#{filename} _rev#{i}
puts cmd
system cmd
end
,你应该确保只使用\\\
作为git处理仓库中的换行符。有一个选项可以自动转换:
$ git config --global core.autocrlf true
当然,据说将crlf转换为lf,而您想将cr转换为lf。我希望这仍然有效...
然后转换您的文件:
#从索引
$ git rm --cached -r中删除所有内容。
#将所有已删除的文件重新添加到索引
#您应该得到很多消息,如:警告:CRLF将在< file>中被LF替换。
$ git diff --cached --name-only -z | xargs -0 git add
#提交
$ git commit -m修复CRLF
core.autocrlf在中描述。
In a project where some of the files contains ^M as newline separators. Diffing these files are apparently impossible, since git-diff sees it as the entire file is just a single line.
How does one diff with the previous version?
Is there an option like "treat ^M as newline when diffing" ?
prompt> git-diff "HEAD^" -- MyFile.as
diff --git a/myproject/MyFile.as b/myproject/MyFile.as
index be78321..a393ba3 100644
--- a/myproject/MyFile.cpp
+++ b/myproject/MyFile.cpp
@@ -1 +1 @@
-<U+FEFF>import flash.events.MouseEvent;^Mimport mx.controls.*;^Mimport mx.utils.Delegate
\ No newline at end of file
+<U+FEFF>import flash.events.MouseEvent;^Mimport mx.controls.*;^Mimport mx.utils.Delegate
\ No newline at end of file
prompt>
UPDATE:
now I have written a script that checks out the latest 10 revisions and converts CR to LF.
require 'fileutils'
if ARGV.size != 3
puts "a git-path must be provided"
puts "a filename must be provided"
puts "a result-dir must be provided"
puts "example:"
puts "ruby gitcrdiff.rb project/dir1/dir2/dir3/ SomeFile.cpp tmp_somefile"
exit(1)
end
gitpath = ARGV[0]
filename = ARGV[1]
resultdir = ARGV[2]
unless FileTest.exist?(".git")
puts "this command must be run in the same dir as where .git resides"
exit(1)
end
if FileTest.exist?(resultdir)
puts "the result dir must not exist"
exit(1)
end
FileUtils.mkdir(resultdir)
10.times do |i|
revision = "^" * i
cmd = "git show HEAD#{revision}:#{gitpath}#{filename} | tr '\\r' '\\n' > #{resultdir}/#{filename}_rev#{i}"
puts cmd
system cmd
end
GitHub suggests that you should make sure to only use \n as a newline character in git-handled repos. There's an option to auto-convert:
$ git config --global core.autocrlf true
Of course, this is said to convert crlf to lf, while you want to convert cr to lf. I hope this still works …
And then convert your files:
# Remove everything from the index
$ git rm --cached -r .
# Re-add all the deleted files to the index
# You should get lots of messages like: "warning: CRLF will be replaced by LF in <file>."
$ git diff --cached --name-only -z | xargs -0 git add
# Commit
$ git commit -m "Fix CRLF"
core.autocrlf is described on the man page.
这篇关于git-diff忽略^ M的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!