1
1
#include "cache.h"
2
2
#include "add-interactive.h"
3
+ #include "color.h"
4
+ #include "config.h"
3
5
#include "diffcore.h"
4
6
#include "revision.h"
5
7
#include "refs.h"
6
8
9
+ struct add_i_state {
10
+ struct repository * r ;
11
+ int use_color ;
12
+ char header_color [COLOR_MAXLEN ];
13
+ };
14
+
15
+ static void init_color (struct repository * r , struct add_i_state * s ,
16
+ const char * slot_name , char * dst ,
17
+ const char * default_color )
18
+ {
19
+ char * key = xstrfmt ("color.interactive.%s" , slot_name );
20
+ const char * value ;
21
+
22
+ if (!s -> use_color )
23
+ dst [0 ] = '\0' ;
24
+ else if (repo_config_get_value (r , key , & value ) ||
25
+ color_parse (value , dst ))
26
+ strlcpy (dst , default_color , COLOR_MAXLEN );
27
+
28
+ free (key );
29
+ }
30
+
31
+ static int init_add_i_state (struct repository * r , struct add_i_state * s )
32
+ {
33
+ const char * value ;
34
+
35
+ s -> r = r ;
36
+
37
+ if (repo_config_get_value (r , "color.interactive" , & value ))
38
+ s -> use_color = -1 ;
39
+ else
40
+ s -> use_color =
41
+ git_config_colorbool ("color.interactive" , value );
42
+ s -> use_color = want_color (s -> use_color );
43
+
44
+ init_color (r , s , "header" , s -> header_color , GIT_COLOR_BOLD );
45
+
46
+ return 0 ;
47
+ }
48
+
7
49
struct item {
8
50
const char * name ;
9
51
};
@@ -14,15 +56,17 @@ struct list_options {
14
56
void * print_item_data ;
15
57
};
16
58
17
- static void list (struct item * * list , size_t nr , struct list_options * opts )
59
+ static void list (struct item * * list , size_t nr ,
60
+ struct add_i_state * s , struct list_options * opts )
18
61
{
19
62
int i ;
20
63
21
64
if (!nr )
22
65
return ;
23
66
24
67
if (opts -> header )
25
- printf ("%s\n" , opts -> header );
68
+ color_fprintf_ln (stdout , s -> header_color ,
69
+ "%s" , opts -> header );
26
70
27
71
for (i = 0 ; i < nr ; i ++ ) {
28
72
opts -> print_item (i , list [i ], opts -> print_item_data );
@@ -226,23 +270,24 @@ static void print_file_item(int i, struct item *item,
226
270
printf (" %2d: %s" , i + 1 , d -> buf .buf );
227
271
}
228
272
229
- static int run_status (struct repository * r , const struct pathspec * ps ,
273
+ static int run_status (struct add_i_state * s , const struct pathspec * ps ,
230
274
struct file_list * files , struct list_options * opts )
231
275
{
232
276
reset_file_list (files );
233
277
234
- if (get_modified_files (r , files , ps ) < 0 )
278
+ if (get_modified_files (s -> r , files , ps ) < 0 )
235
279
return -1 ;
236
280
237
281
if (files -> nr )
238
- list ((struct item * * )files -> file , files -> nr , opts );
282
+ list ((struct item * * )files -> file , files -> nr , s , opts );
239
283
putchar ('\n' );
240
284
241
285
return 0 ;
242
286
}
243
287
244
288
int run_add_i (struct repository * r , const struct pathspec * ps )
245
289
{
290
+ struct add_i_state s = { NULL };
246
291
struct print_file_item_data print_file_item_data = {
247
292
"%12s %12s %s" , STRBUF_INIT , STRBUF_INIT , STRBUF_INIT
248
293
};
@@ -253,13 +298,16 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
253
298
struct file_list files = { NULL };
254
299
int res = 0 ;
255
300
301
+ if (init_add_i_state (r , & s ))
302
+ return error ("could not parse `add -i` config" );
303
+
256
304
strbuf_addstr (& header , " " );
257
305
strbuf_addf (& header , print_file_item_data .modified_fmt ,
258
306
_ ("staged" ), _ ("unstaged" ), _ ("path" ));
259
307
opts .header = header .buf ;
260
308
261
309
repo_refresh_and_write_index (r , REFRESH_QUIET , 1 );
262
- if (run_status (r , ps , & files , & opts ) < 0 )
310
+ if (run_status (& s , ps , & files , & opts ) < 0 )
263
311
res = -1 ;
264
312
265
313
release_file_list (& files );
0 commit comments