我知道Git会以某种方式自动检测文件是二进制还是文本,如果需要,可以使用gitattributes手动设置它.但是有没有办法向GIT询问它如何处理文件?
所以我们可以说我有在这两个文件一个Git仓库:一个ascii.dat含纯文本和文件binary.dat含随机二进制的东西文件.Git将第一个dat文件作为文本处理,将辅助文件作为二进制文件处理.现在我想写一个Git webfrontend,它有一个文本文件查看器和二进制文件的特殊查看器(例如,显示十六进制转储).当然,我可以实现自己的文本/二进制检查,但如果查看器依赖于Git如何处理这些文件的信息,那将会更有用.
那么我怎么能问Git它是否将文件视为文本或二进制文件?
Chr*_*sen 35
builtin_diff()1个调用调用diff_filespec_is_binary()哪个调用buffer_is_binary()检查前8000字节中是否出现零字节(NUL"字符")(如果更短则调整整个长度).
我没有看到这个"它是二进制吗?"测试是在任何命令中明确公开的.
git merge-file直接使用buffer_is_binary(),所以你可以使用它:
git merge-file /dev/null /dev/null file-to-test
Run Code Online (Sandbox Code Playgroud)
它似乎产生错误消息,error: Cannot merge binary files: file-to-test并在给定二进制文件时产生255的退出状态.我不确定我是否愿意依赖这种行为.
也许git diff --numstat会更可靠:
isBinary() {
p=$(printf '%s\t-\t' -)
t=$(git diff --no-index --numstat /dev/null "$1")
case "$t" in "$p"*) return 0 ;; esac
return 1
}
isBinary file-to-test && echo binary || echo not binary
Run Code Online (Sandbox Code Playgroud)
对于二进制文件,--numstat输出应以-TAB -TAB 开头,因此我们只测试它.
1
builtin_diff()有这样的字符串Binary files %s and %s differ应该是熟悉的.
cst*_*ork 27
git grep -I --name-only --untracked -e . -- ascii.dat binary.dat ...
Run Code Online (Sandbox Code Playgroud)
将返回git解释为文本文件的文件的名称.
你可以使用通配符,例如
git grep -I --name-only --untracked -e . -- *.ps1
Run Code Online (Sandbox Code Playgroud)
Set*_*son 18
我不喜欢这个答案,但你可以解析git-diff-tree的输出,看看它是否是二进制的.例如:
git diff-tree -p 4b825dc642cb6eb9a060e54bf8d69288fbee4904 HEAD -- MegaCli
diff --git a/megaraid/MegaCli b/megaraid/MegaCli
new file mode 100755
index 0000000..7f0e997
Binary files /dev/null and b/megaraid/MegaCli differ
Run Code Online (Sandbox Code Playgroud)
而不是:
git diff-tree -p 4b825dc642cb6eb9a060e54bf8d69288fbee4904 HEAD -- megamgr
diff --git a/megaraid/megamgr b/megaraid/megamgr
new file mode 100755
index 0000000..50fd8a1
--- /dev/null
+++ b/megaraid/megamgr
@@ -0,0 +1,78 @@
+#!/bin/sh
[…]
Run Code Online (Sandbox Code Playgroud)
哦,BTW,4b825d ......是一个神奇的SHA,代表空树(它是空树的SHA,但是git特别注意这个魔法).
Que*_*l33 11
# considered binary (or with bare CR) file
git ls-files --eol | grep -E '^(i/-text)'
# files that do not have any line-ending characters (including empty files) - unlikely that this is a true binary file ?
git ls-files --eol | grep -E '^(i/none)'
# via experimentation
# ------------------------
# "-text" binary (or with bare CR) file : not auto-normalized
# "none" text file without any EOL : not auto-normalized
# "lf" text file with LF : is auto-normalized when gitattributes text=auto
# "crlf" text file with CRLF : is auto-normalized when gitattributes text=auto
# "mixed" text file with mixed line endings : is auto-normalized when gitattributes text=auto
# (LF or CRLF, but not bare CR)
Run Code Online (Sandbox Code Playgroud)
来源: https: //git-scm.com/docs/git-ls-files#Documentation/git-ls-files.txt---eol https://github.com/git/git/commit/a7630bd4274a0dff7cff8b92de3d3f064e321359
哦,顺便说一下:设置.gitattributes文本属性时要小心,例如*.abc text。因为在这种情况下,所有带有 的文件都*.abc将被标准化,即使它们是二进制文件(二进制文件中找到的内部 CRLF 将被标准化为 LF)。这与自动行为不同。
小智 5
@bonh 在评论中给出了可行的答案
git diff --numstat 4b825dc642cb6eb9a060e54bf8d69288fbee4904 头 -- | grep“^-”| 切-f 3
它显示 git 解释为二进制文件的所有文件。