@matheusbsbs / matheus-bagliano / commits / 801235c5e6
Fork 0

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 <junkio@cox.net>

Junio C Hamano committed Jun 24, 2006 at 04:06 UTC 801235c5e6bff0ec1c4e41c9d46535ead3b693c1
2 files changed +80 -15
cache.h
-1
@@ -181,7 +181,6 @@ extern int assume_unchanged;
181 extern int prefer_symlink_refs;
182 extern int log_all_ref_updates;
183 extern int warn_ambiguous_refs;
184 -extern int diff_rename_limit_default;
184 extern int shared_repository;
185 extern const char *apply_default_whitespace;
186
diff.c
+80 -14
@@ -13,17 +13,8 @@
13
14 static int use_size_cache;
15
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;
18
19 enum color_diff {
20 DIFF_RESET = 0,
@@ -51,9 +42,6 @@ enum color_diff {
42 #define COLOR_CYAN "\033[36m"
43 #define COLOR_WHITE "\033[37m"
44
54 -#define COLOR_CYANBG "\033[46m"
55 -#define COLOR_GRAYBG "\033[47m" // Good for xterm
56 -
45 static const char *diff_colors[] = {
46 [DIFF_RESET] = COLOR_RESET,
47 [DIFF_PLAIN] = COLOR_NORMAL,
@@ -63,6 +51,83 @@ static const char *diff_colors[] = {
51 [DIFF_FILE_NEW] = COLOR_GREEN,
52 };
53
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 +
131 static char *quote_one(const char *str)
132 {
133 int needlen;
@@ -1362,6 +1427,7 @@ void diff_setup(struct diff_options *options)
1427
1428 options->change = diff_change;
1429 options->add_remove = diff_addremove;
1430 + options->color_diff = diff_use_color_default;
1431 }
1432
1433 int diff_setup_done(struct diff_options *options)