我知道Git以某种方式自动检测一个文件是二进制的还是文本的,如果需要,可以使用.gitattributes
手动设置。但是有没有办法问Git它是如何处理文件的呢?
假设我有一个包含两个文件的Git存储库:一个包含纯文本的ascii.dat
文件和一个包含随机二进制内容的binary.dat
文件。Git将第一个.dat
文件作为文本处理,将第二个文件作为二进制文件处理。现在我想写一个Git web前端,它有一个文本文件查看器和一个特殊的二进制文件查看器(例如显示十六进制转储)。当然,我可以实现自己的文本/二进制检查,但如果查看器依赖于Git如何处理这些文件的信息,它会更有用。
那么,我如何询问Git是否将文件视为文本或二进制文件呢?
发布于 2021-04-20 13:31:12
使用git check-attr --all
。
无论文件是否已暂存/提交,此操作都有效。
已在git版本2.30.2上测试。
假设你在.gitattributes
中有这段代码。
package-lock.json binary
这就是输出。
git check-attr --all package-lock.json
package-lock.json: binary: set
package-lock.json: diff: unset
package-lock.json: merge: unset
package-lock.json: text: unset
对于普通文件,没有输出。
git check-attr --all package.json
发布于 2017-09-12 21:10:11
冒着因代码质量差而挨打的风险,我列出了一个C实用程序is_binary,它是围绕Git源代码中原始的buffer_is_binary()例程构建的。有关如何构建和运行,请参阅内部评论。易于修改:
/***********************************************************
* is_binary.c
*
* Usage: is_binary <pathname>
* Returns a 1 if a binary; return a 0 if non-binary
*
* Thanks to Git and Stackoverflow developers for helping with these routines:
* - the buffer_is_binary() routine from the xdiff-interface.c module
* in git source code.
* - the read-a-filename-from-stdin route
* - the read-a-file-into-memory (fill_buffer()) routine
*
* To build:
* % gcc is_binary.c -o is_binary
*
* To build debuggable (to push a few messages to stdout):
* % gcc -DDEBUG=1 ./is_binary.c -o is_binary
*
* BUGS:
* Doesn't work with piped input, like
* % cat foo.tar | is_binary
* Claims that zero input is binary. Actually,
* what should it be?
*
* Revision 1.4
*
* Tue Sep 12 09:01:33 EDT 2017
***********************************************************/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_PATH_LENGTH 200
#define FIRST_FEW_BYTES 8000
/* global, unfortunately */
char *source_blob_buffer;
/* From: https://stackoverflow.com/questions/14002954/c-programming-how-to-read-the-whole-file-contents-into-a-buffer */
/* From: https://stackoverflow.com/questions/1563882/reading-a-file-name-from-piped-command */
/* From: https://stackoverflow.com/questions/6119956/how-to-determine-if-git-handles-a-file-as-binary-or-as-text
*/
/* The key routine in this function is from libc: void *memchr(const void *s, int c, size_t n); */
/* Checks for any occurrence of a zero byte (NUL character) in the first 8000 bytes (or the entire length if shorter). */
int buffer_is_binary(const char *ptr, unsigned long size)
{
if (FIRST_FEW_BYTES < size)
size = FIRST_FEW_BYTES;
/* printf("buff = %s.\n", ptr); */
return !!memchr(ptr, 0, size);
}
int fill_buffer(FILE * file_object_pointer) {
fseek(file_object_pointer, 0, SEEK_END);
long fsize = ftell(file_object_pointer);
fseek(file_object_pointer, 0, SEEK_SET); //same as rewind(f);
source_blob_buffer = malloc(fsize + 1);
fread(source_blob_buffer, fsize, 1, file_object_pointer);
fclose(file_object_pointer);
source_blob_buffer[fsize] = 0;
return (fsize + 1);
}
int main(int argc, char *argv[]) {
char pathname[MAX_PATH_LENGTH];
FILE *file_object_pointer;
if (argc == 1) {
file_object_pointer = stdin;
} else {
strcpy(pathname,argv[1]);
#ifdef DEBUG
printf("pathname=%s.\n", pathname);
#endif
file_object_pointer = fopen (pathname, "rb");
if (file_object_pointer == NULL) {
printf ("I'm sorry, Dave, I can't do that--");
printf ("open the file '%s', that is.\n", pathname);
exit(3);
}
}
if (!file_object_pointer) {
printf("Not a file nor a pipe--sorry.\n");
exit (4);
}
int fsize = fill_buffer(file_object_pointer);
int result = buffer_is_binary(source_blob_buffer, fsize - 2);
#ifdef DEBUG
if (result == 1) {
printf ("%s %d\n", pathname, fsize - 1);
}
else {
printf ("File '%s' is NON-BINARY; size is %d bytes.\n", pathname, fsize - 1);
}
#endif
exit(result);
/* easy check -- 'echo $?' after running */
}
发布于 2016-05-12 08:06:51
您可以使用命令行工具'file‘实用程序。在Windows上,它包含在git安装中,通常位于C:\Program Files\git\usr\bin文件夹中
file --mime-encoding *
https://stackoverflow.com/questions/6119956
复制相似问题