Skip to content

Commit c714f9f

Browse files
committed
Merge branch 'hv/config-from-blob'
Allow configuration data to be read from in-tree blob objects, which would help working in a bare repository and submodule updates. * hv/config-from-blob: do not die when error in config parsing of buf occurs teach config --blob option to parse config from database config: make parsing stack struct independent from actual data source config: drop cf validity check in get_next_char() config: factor out config file stack management
2 parents 4c72ee8 + b2dc094 commit c714f9f

File tree

5 files changed

+278
-53
lines changed

5 files changed

+278
-53
lines changed

Documentation/git-config.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ See also <<FILES>>.
127127
--file config-file::
128128
Use the given config file instead of the one specified by GIT_CONFIG.
129129

130+
--blob blob::
131+
Similar to '--file' but use the given blob instead of a file. E.g.
132+
you can use 'master:.gitmodules' to read values from the file
133+
'.gitmodules' in the master branch. See "SPECIFYING REVISIONS"
134+
section in linkgit:gitrevisions[7] for a more complete list of
135+
ways to spell blob names.
136+
130137
--remove-section::
131138
Remove the given section from the configuration file.
132139

builtin/config.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ static char term = '\n';
2121

2222
static int use_global_config, use_system_config, use_local_config;
2323
static const char *given_config_file;
24+
static const char *given_config_blob;
2425
static int actions, types;
2526
static const char *get_color_slot, *get_colorbool_slot;
2627
static int end_null;
@@ -53,6 +54,7 @@ static struct option builtin_config_options[] = {
5354
OPT_BOOLEAN(0, "system", &use_system_config, N_("use system config file")),
5455
OPT_BOOLEAN(0, "local", &use_local_config, N_("use repository config file")),
5556
OPT_STRING('f', "file", &given_config_file, N_("file"), N_("use given config file")),
57+
OPT_STRING(0, "blob", &given_config_blob, N_("blob-id"), N_("read config from given blob object")),
5658
OPT_GROUP(N_("Action")),
5759
OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
5860
OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
@@ -218,7 +220,8 @@ static int get_value(const char *key_, const char *regex_)
218220
}
219221

220222
git_config_with_options(collect_config, &values,
221-
given_config_file, respect_includes);
223+
given_config_file, given_config_blob,
224+
respect_includes);
222225

223226
ret = !values.nr;
224227

@@ -302,7 +305,8 @@ static void get_color(const char *def_color)
302305
get_color_found = 0;
303306
parsed_color[0] = '\0';
304307
git_config_with_options(git_get_color_config, NULL,
305-
given_config_file, respect_includes);
308+
given_config_file, given_config_blob,
309+
respect_includes);
306310

307311
if (!get_color_found && def_color)
308312
color_parse(def_color, "command line", parsed_color);
@@ -331,7 +335,8 @@ static int get_colorbool(int print)
331335
get_diff_color_found = -1;
332336
get_color_ui_found = -1;
333337
git_config_with_options(git_get_colorbool_config, NULL,
334-
given_config_file, respect_includes);
338+
given_config_file, given_config_blob,
339+
respect_includes);
335340

336341
if (get_colorbool_found < 0) {
337342
if (!strcmp(get_colorbool_slot, "color.diff"))
@@ -353,6 +358,12 @@ static int get_colorbool(int print)
353358
return get_colorbool_found ? 0 : 1;
354359
}
355360

361+
static void check_blob_write(void)
362+
{
363+
if (given_config_blob)
364+
die("writing config blobs is not supported");
365+
}
366+
356367
int cmd_config(int argc, const char **argv, const char *prefix)
357368
{
358369
int nongit = !startup_info->have_repository;
@@ -364,7 +375,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
364375
builtin_config_usage,
365376
PARSE_OPT_STOP_AT_NON_OPTION);
366377

367-
if (use_global_config + use_system_config + use_local_config + !!given_config_file > 1) {
378+
if (use_global_config + use_system_config + use_local_config +
379+
!!given_config_file + !!given_config_blob > 1) {
368380
error("only one config file at a time.");
369381
usage_with_options(builtin_config_usage, builtin_config_options);
370382
}
@@ -443,6 +455,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
443455
check_argc(argc, 0, 0);
444456
if (git_config_with_options(show_all_config, NULL,
445457
given_config_file,
458+
given_config_blob,
446459
respect_includes) < 0) {
447460
if (given_config_file)
448461
die_errno("unable to read config file '%s'",
@@ -455,13 +468,16 @@ int cmd_config(int argc, const char **argv, const char *prefix)
455468
check_argc(argc, 0, 0);
456469
if (!given_config_file && nongit)
457470
die("not in a git directory");
471+
if (given_config_blob)
472+
die("editing blobs is not supported");
458473
git_config(git_default_config, NULL);
459474
launch_editor(given_config_file ?
460475
given_config_file : git_path("config"),
461476
NULL, NULL);
462477
}
463478
else if (actions == ACTION_SET) {
464479
int ret;
480+
check_blob_write();
465481
check_argc(argc, 2, 2);
466482
value = normalize_value(argv[0], argv[1]);
467483
ret = git_config_set_in_file(given_config_file, argv[0], value);
@@ -471,18 +487,21 @@ int cmd_config(int argc, const char **argv, const char *prefix)
471487
return ret;
472488
}
473489
else if (actions == ACTION_SET_ALL) {
490+
check_blob_write();
474491
check_argc(argc, 2, 3);
475492
value = normalize_value(argv[0], argv[1]);
476493
return git_config_set_multivar_in_file(given_config_file,
477494
argv[0], value, argv[2], 0);
478495
}
479496
else if (actions == ACTION_ADD) {
497+
check_blob_write();
480498
check_argc(argc, 2, 2);
481499
value = normalize_value(argv[0], argv[1]);
482500
return git_config_set_multivar_in_file(given_config_file,
483501
argv[0], value, "^$", 0);
484502
}
485503
else if (actions == ACTION_REPLACE_ALL) {
504+
check_blob_write();
486505
check_argc(argc, 2, 3);
487506
value = normalize_value(argv[0], argv[1]);
488507
return git_config_set_multivar_in_file(given_config_file,
@@ -505,6 +524,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
505524
return get_value(argv[0], argv[1]);
506525
}
507526
else if (actions == ACTION_UNSET) {
527+
check_blob_write();
508528
check_argc(argc, 1, 2);
509529
if (argc == 2)
510530
return git_config_set_multivar_in_file(given_config_file,
@@ -514,12 +534,14 @@ int cmd_config(int argc, const char **argv, const char *prefix)
514534
argv[0], NULL);
515535
}
516536
else if (actions == ACTION_UNSET_ALL) {
537+
check_blob_write();
517538
check_argc(argc, 1, 2);
518539
return git_config_set_multivar_in_file(given_config_file,
519540
argv[0], NULL, argv[1], 1);
520541
}
521542
else if (actions == ACTION_RENAME_SECTION) {
522543
int ret;
544+
check_blob_write();
523545
check_argc(argc, 2, 2);
524546
ret = git_config_rename_section_in_file(given_config_file,
525547
argv[0], argv[1]);
@@ -530,6 +552,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
530552
}
531553
else if (actions == ACTION_REMOVE_SECTION) {
532554
int ret;
555+
check_blob_write();
533556
check_argc(argc, 1, 1);
534557
ret = git_config_rename_section_in_file(given_config_file,
535558
argv[0], NULL);

cache.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,11 +1176,15 @@ extern int update_server_info(int);
11761176
typedef int (*config_fn_t)(const char *, const char *, void *);
11771177
extern int git_default_config(const char *, const char *, void *);
11781178
extern int git_config_from_file(config_fn_t fn, const char *, void *);
1179+
extern int git_config_from_buf(config_fn_t fn, const char *name,
1180+
const char *buf, size_t len, void *data);
11791181
extern void git_config_push_parameter(const char *text);
11801182
extern int git_config_from_parameters(config_fn_t fn, void *data);
11811183
extern int git_config(config_fn_t fn, void *);
11821184
extern int git_config_with_options(config_fn_t fn, void *,
1183-
const char *filename, int respect_includes);
1185+
const char *filename,
1186+
const char *blob_ref,
1187+
int respect_includes);
11841188
extern int git_config_early(config_fn_t fn, void *, const char *repo_config);
11851189
extern int git_parse_ulong(const char *, unsigned long *);
11861190
extern int git_config_int(const char *, const char *);

0 commit comments

Comments
 (0)