Git 寻呼机较少,但是是什么导致输出着色?

Seb*_*ian 18 colors pager diff git colordiff

less根据这个线程,它本身不能进行语法高亮显示。

但是,git diff在它的默认寻呼机 less 中很好地显示了彩色输出。当我将 的输出重定向git diff到文件中时,看不到颜色转义序列。

是否git diff知道它被发送到哪里,并相应地格式化输出?那怎么办呢?


我只是注意到 git 为diff输出着色(例如git diff),但是,它通常不知道如何语法突出显示。例如

git show 415fec6:log.tex
Run Code Online (Sandbox Code Playgroud)

不启用任何类似 TeX 的语法。


阅读git资料,我发现以下提示

diff.h

int use_color;
Run Code Online (Sandbox Code Playgroud)

我以前指的是语法高亮,但那是不正确的。我的意思是输出着色,参见例如

示例颜色输出

ysd*_*sdx 18

Git 用于isatty()检查 stdout 是否为 tty:这用于查看是否必须使用寻呼机 ( pager.c ) 以及颜色 ( color.c )。


Seb*_*ian 13

跑步:

git diff --color=always > output
Run Code Online (Sandbox Code Playgroud)

显示颜色逃逸。

该死的,我在gitcolor.c文件中找到了它:

static int check_auto_color(void)                                           
{                                                                           
  if (color_stdout_is_tty < 0)                                              
    color_stdout_is_tty = isatty(1);                                        
  if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {         
    char *term = getenv("TERM");                                            
    if (term && strcmp(term, "dumb"))                                       
      return 1;                                                             
  }                                                                         
  return 0;                                                                 
}  
Run Code Online (Sandbox Code Playgroud)