308
}
309
}
310
311
+static struct grep_opt grep_defaults;
312
+
313
+/*
314
+ * Initialize the grep_defaults template with hardcoded defaults.
315
+ * We could let the compiler do this, but without C99 initializers
316
+ * the code gets unwieldy and unreadable, so...
317
+ */
318
+static void init_grep_defaults(void)
319
+{
320
+ struct grep_opt *opt = &grep_defaults;
321
+
322
+ memset(opt, 0, sizeof(*opt));
323
+ opt->relative = 1;
324
+ opt->pathname = 1;
325
+ opt->regflags = REG_NEWLINE;
326
+ opt->max_depth = -1;
327
+ opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
328
+ opt->extended_regexp_option = 0;
329
+ strcpy(opt->color_context, "");
330
+ strcpy(opt->color_filename, "");
331
+ strcpy(opt->color_function, "");
332
+ strcpy(opt->color_lineno, "");
333
+ strcpy(opt->color_match, GIT_COLOR_BOLD_RED);
334
+ strcpy(opt->color_selected, "");
335
+ strcpy(opt->color_sep, GIT_COLOR_CYAN);
336
+ opt->color = -1;
337
+}
338
+
339
+/*
340
+ * Read the configuration file once and store it in
341
+ * the grep_defaults template.
342
+ */
343
static int grep_config(const char *var, const char *value, void *cb)
344
{
313
- struct grep_opt *opt = cb;
345
+ struct grep_opt *opt = &grep_defaults;
346
char *color = NULL;
347
348
if (userdiff_config(var, value) < 0)
359
if (!strcmp(var, "grep.patterntype")) {
360
opt->pattern_type_option = parse_pattern_type_arg(var, value);
361
return 0;
330
- }
362
+ }
363
364
if (!strcmp(var, "grep.linenumber")) {
365
opt->linenum = git_config_bool(var, value);
382
color = opt->color_selected;
383
else if (!strcmp(var, "color.grep.separator"))
384
color = opt->color_sep;
353
- else
354
- return git_color_default_config(var, value, cb);
385
+
386
if (color) {
387
if (!value)
388
return config_error_nonbool(var);
391
return 0;
392
}
393
394
+/*
395
+ * Initialize one instance of grep_opt and copy the
396
+ * default values from the template we read the configuration
397
+ * information in an earlier call to git_config(grep_config).
398
+ */
399
+static void grep_init(struct grep_opt *opt, const char *prefix)
400
+{
401
+ struct grep_opt *def = &grep_defaults;
402
+
403
+ memset(opt, 0, sizeof(*opt));
404
+ opt->prefix = prefix;
405
+ opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
406
+ opt->pattern_tail = &opt->pattern_list;
407
+ opt->header_tail = &opt->header_list;
408
+
409
+ opt->color = def->color;
410
+ opt->extended_regexp_option = def->extended_regexp_option;
411
+ opt->pattern_type_option = def->pattern_type_option;
412
+ opt->linenum = def->linenum;
413
+ opt->max_depth = def->max_depth;
414
+ opt->pathname = def->pathname;
415
+ opt->regflags = def->regflags;
416
+ opt->relative = def->relative;
417
+
418
+ strcpy(opt->color_context, def->color_context);
419
+ strcpy(opt->color_filename, def->color_filename);
420
+ strcpy(opt->color_function, def->color_function);
421
+ strcpy(opt->color_lineno, def->color_lineno);
422
+ strcpy(opt->color_match, def->color_match);
423
+ strcpy(opt->color_selected, def->color_selected);
424
+ strcpy(opt->color_sep, def->color_sep);
425
+}
426
+
427
+static int grep_cmd_config(const char *var, const char *value, void *cb)
428
+{
429
+ int st = grep_config(var, value, cb);
430
+ if (git_color_default_config(var, value, cb) < 0)
431
+ st = -1;
432
+ return st;
433
+}
434
+
435
static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
436
{
437
void *data;
911
if (argc == 2 && !strcmp(argv[1], "-h"))
912
usage_with_options(grep_usage, options);
913
842
- memset(&opt, 0, sizeof(opt));
843
- opt.prefix = prefix;
844
- opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
845
- opt.relative = 1;
846
- opt.pathname = 1;
847
- opt.pattern_tail = &opt.pattern_list;
848
- opt.header_tail = &opt.header_list;
849
- opt.regflags = REG_NEWLINE;
850
- opt.max_depth = -1;
851
- opt.pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
852
- opt.extended_regexp_option = 0;
853
-
854
- strcpy(opt.color_context, "");
855
- strcpy(opt.color_filename, "");
856
- strcpy(opt.color_function, "");
857
- strcpy(opt.color_lineno, "");
858
- strcpy(opt.color_match, GIT_COLOR_BOLD_RED);
859
- strcpy(opt.color_selected, "");
860
- strcpy(opt.color_sep, GIT_COLOR_CYAN);
861
- opt.color = -1;
862
- git_config(grep_config, &opt);
914
+ init_grep_defaults();
915
+ git_config(grep_cmd_config, NULL);
916
+ grep_init(&opt, prefix);
917
918
/*
919
* If there is no -- then the paths must exist in the working