refactor "dumb" terminal determination

Move the code to detect "dumb" terminals into a single location. This avoids duplicating the terminal detection code yet again in a subsequent commit. Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lars Schneider committed Nov 29, 2017 at 15:37 UTC a64f213d3fa13fa01e582b6734fe7883ed975dc9
4 files changed +11 -7
cache.h
+1
@@ -1438,6 +1438,7 @@ extern const char *ident_default_name(void);
1438 extern const char *ident_default_email(void);
1439 extern const char *git_editor(void);
1440 extern const char *git_pager(int stdout_is_tty);
1441 +extern int is_terminal_dumb(void);
1442 extern int git_ident_config(const char *, const char *, void *);
1443 extern void reset_ident_date(void);
1444
color.c
+1 -2
@@ -329,8 +329,7 @@ static int check_auto_color(void)
329 if (color_stdout_is_tty < 0)
330 color_stdout_is_tty = isatty(1);
331 if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {
332 - char *term = getenv("TERM");
333 - if (term && strcmp(term, "dumb"))
332 + if (!is_terminal_dumb())
333 return 1;
334 }
335 return 0;
editor.c
+7 -2
@@ -7,11 +7,16 @@
7 #define DEFAULT_EDITOR "vi"
8 #endif
9
10 +int is_terminal_dumb(void)
11 +{
12 + const char *terminal = getenv("TERM");
13 + return !terminal || !strcmp(terminal, "dumb");
14 +}
15 +
16 const char *git_editor(void)
17 {
18 const char *editor = getenv("GIT_EDITOR");
13 - const char *terminal = getenv("TERM");
14 - int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
19 + int terminal_is_dumb = is_terminal_dumb();
20
21 if (!editor && editor_program)
22 editor = editor_program;
sideband.c
+2 -3
@@ -20,13 +20,12 @@
20
21 int recv_sideband(const char *me, int in_stream, int out)
22 {
23 - const char *term, *suffix;
23 + const char *suffix;
24 char buf[LARGE_PACKET_MAX + 1];
25 struct strbuf outbuf = STRBUF_INIT;
26 int retval = 0;
27
28 - term = getenv("TERM");
29 - if (isatty(2) && term && strcmp(term, "dumb"))
28 + if (isatty(2) && !is_terminal_dumb())
29 suffix = ANSI_SUFFIX;
30 else
31 suffix = DUMB_SUFFIX;