Skip to content

Commit 801235c

Browse files
author
Junio C Hamano
committed
diff --color: use $GIT_DIR/config
This lets you use something like this in your $GIT_DIR/config file. [diff] color = auto [diff.color] new = blue old = yellow frag = reverse When diff.color is set to "auto", colored diff is enabled when the standard output is the terminal. Other choices are "always", and "never". Usual boolean true/false can also be used. The colormap entries can specify colors for the following slots: plain - lines that appear in both old and new file (context) meta - diff --git header and extended git diff headers frag - @@ -n,m +l,k @@ lines (hunk header) old - lines deleted from old file new - lines added to new file The following color names can be used: normal, bold, dim, l, blink, reverse, reset, black, red, green, yellow, blue, magenta, cyan, white Signed-off-by: Junio C Hamano <[email protected]>
1 parent 29f4ad8 commit 801235c

File tree

2 files changed

+80
-15
lines changed

2 files changed

+80
-15
lines changed

cache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ extern int assume_unchanged;
181181
extern int prefer_symlink_refs;
182182
extern int log_all_ref_updates;
183183
extern int warn_ambiguous_refs;
184-
extern int diff_rename_limit_default;
185184
extern int shared_repository;
186185
extern const char *apply_default_whitespace;
187186

diff.c

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,8 @@
1313

1414
static int use_size_cache;
1515

16-
int diff_rename_limit_default = -1;
17-
18-
int git_diff_config(const char *var, const char *value)
19-
{
20-
if (!strcmp(var, "diff.renamelimit")) {
21-
diff_rename_limit_default = git_config_int(var, value);
22-
return 0;
23-
}
24-
25-
return git_default_config(var, value);
26-
}
16+
static int diff_rename_limit_default = -1;
17+
static int diff_use_color_default = 0;
2718

2819
enum color_diff {
2920
DIFF_RESET = 0,
@@ -51,9 +42,6 @@ enum color_diff {
5142
#define COLOR_CYAN "\033[36m"
5243
#define COLOR_WHITE "\033[37m"
5344

54-
#define COLOR_CYANBG "\033[46m"
55-
#define COLOR_GRAYBG "\033[47m" // Good for xterm
56-
5745
static const char *diff_colors[] = {
5846
[DIFF_RESET] = COLOR_RESET,
5947
[DIFF_PLAIN] = COLOR_NORMAL,
@@ -63,6 +51,83 @@ static const char *diff_colors[] = {
6351
[DIFF_FILE_NEW] = COLOR_GREEN,
6452
};
6553

54+
static int parse_diff_color_slot(const char *var, int ofs)
55+
{
56+
if (!strcasecmp(var+ofs, "plain"))
57+
return DIFF_PLAIN;
58+
if (!strcasecmp(var+ofs, "meta"))
59+
return DIFF_METAINFO;
60+
if (!strcasecmp(var+ofs, "frag"))
61+
return DIFF_FRAGINFO;
62+
if (!strcasecmp(var+ofs, "old"))
63+
return DIFF_FILE_OLD;
64+
if (!strcasecmp(var+ofs, "new"))
65+
return DIFF_FILE_NEW;
66+
die("bad config variable '%s'", var);
67+
}
68+
69+
static const char *parse_diff_color_value(const char *value, const char *var)
70+
{
71+
if (!strcasecmp(value, "normal"))
72+
return COLOR_NORMAL;
73+
if (!strcasecmp(value, "bold"))
74+
return COLOR_BOLD;
75+
if (!strcasecmp(value, "dim"))
76+
return COLOR_DIM;
77+
if (!strcasecmp(value, "ul"))
78+
return COLOR_UL;
79+
if (!strcasecmp(value, "blink"))
80+
return COLOR_BLINK;
81+
if (!strcasecmp(value, "reverse"))
82+
return COLOR_REVERSE;
83+
if (!strcasecmp(value, "reset"))
84+
return COLOR_RESET;
85+
if (!strcasecmp(value, "black"))
86+
return COLOR_BLACK;
87+
if (!strcasecmp(value, "red"))
88+
return COLOR_RED;
89+
if (!strcasecmp(value, "green"))
90+
return COLOR_GREEN;
91+
if (!strcasecmp(value, "yellow"))
92+
return COLOR_YELLOW;
93+
if (!strcasecmp(value, "blue"))
94+
return COLOR_BLUE;
95+
if (!strcasecmp(value, "magenta"))
96+
return COLOR_MAGENTA;
97+
if (!strcasecmp(value, "cyan"))
98+
return COLOR_CYAN;
99+
if (!strcasecmp(value, "white"))
100+
return COLOR_WHITE;
101+
die("bad config value '%s' for variable '%s'", value, var);
102+
}
103+
104+
int git_diff_config(const char *var, const char *value)
105+
{
106+
if (!strcmp(var, "diff.renamelimit")) {
107+
diff_rename_limit_default = git_config_int(var, value);
108+
return 0;
109+
}
110+
if (!strcmp(var, "diff.color")) {
111+
if (!value)
112+
diff_use_color_default = 1; /* bool */
113+
else if (!strcasecmp(value, "auto"))
114+
diff_use_color_default = isatty(1);
115+
else if (!strcasecmp(value, "never"))
116+
diff_use_color_default = 0;
117+
else if (!strcasecmp(value, "always"))
118+
diff_use_color_default = 1;
119+
else
120+
diff_use_color_default = git_config_bool(var, value);
121+
return 0;
122+
}
123+
if (!strncmp(var, "diff.color.", 11)) {
124+
int slot = parse_diff_color_slot(var, 11);
125+
diff_colors[slot] = parse_diff_color_value(value, var);
126+
return 0;
127+
}
128+
return git_default_config(var, value);
129+
}
130+
66131
static char *quote_one(const char *str)
67132
{
68133
int needlen;
@@ -1362,6 +1427,7 @@ void diff_setup(struct diff_options *options)
13621427

13631428
options->change = diff_change;
13641429
options->add_remove = diff_addremove;
1430+
options->color_diff = diff_use_color_default;
13651431
}
13661432

13671433
int diff_setup_done(struct diff_options *options)

0 commit comments

Comments
 (0)