Skip to content

Commit

Permalink
Merge branch 'hv/config-from-blob'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Junio C Hamano committed Jul 22, 2013
2 parents 4c72ee8 + b2dc094 commit c714f9f
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 53 deletions.
7 changes: 7 additions & 0 deletions Documentation/git-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ See also <<FILES>>.
--file config-file::
Use the given config file instead of the one specified by GIT_CONFIG.

--blob blob::
Similar to '--file' but use the given blob instead of a file. E.g.
you can use 'master:.gitmodules' to read values from the file
'.gitmodules' in the master branch. See "SPECIFYING REVISIONS"
section in linkgit:gitrevisions[7] for a more complete list of
ways to spell blob names.

--remove-section::
Remove the given section from the configuration file.

Expand Down
31 changes: 27 additions & 4 deletions builtin/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static char term = '\n';

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

git_config_with_options(collect_config, &values,
given_config_file, respect_includes);
given_config_file, given_config_blob,
respect_includes);

ret = !values.nr;

Expand Down Expand Up @@ -302,7 +305,8 @@ static void get_color(const char *def_color)
get_color_found = 0;
parsed_color[0] = '\0';
git_config_with_options(git_get_color_config, NULL,
given_config_file, respect_includes);
given_config_file, given_config_blob,
respect_includes);

if (!get_color_found && def_color)
color_parse(def_color, "command line", parsed_color);
Expand Down Expand Up @@ -331,7 +335,8 @@ static int get_colorbool(int print)
get_diff_color_found = -1;
get_color_ui_found = -1;
git_config_with_options(git_get_colorbool_config, NULL,
given_config_file, respect_includes);
given_config_file, given_config_blob,
respect_includes);

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

static void check_blob_write(void)
{
if (given_config_blob)
die("writing config blobs is not supported");
}

int cmd_config(int argc, const char **argv, const char *prefix)
{
int nongit = !startup_info->have_repository;
Expand All @@ -364,7 +375,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
builtin_config_usage,
PARSE_OPT_STOP_AT_NON_OPTION);

if (use_global_config + use_system_config + use_local_config + !!given_config_file > 1) {
if (use_global_config + use_system_config + use_local_config +
!!given_config_file + !!given_config_blob > 1) {
error("only one config file at a time.");
usage_with_options(builtin_config_usage, builtin_config_options);
}
Expand Down Expand Up @@ -443,6 +455,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
check_argc(argc, 0, 0);
if (git_config_with_options(show_all_config, NULL,
given_config_file,
given_config_blob,
respect_includes) < 0) {
if (given_config_file)
die_errno("unable to read config file '%s'",
Expand All @@ -455,13 +468,16 @@ int cmd_config(int argc, const char **argv, const char *prefix)
check_argc(argc, 0, 0);
if (!given_config_file && nongit)
die("not in a git directory");
if (given_config_blob)
die("editing blobs is not supported");
git_config(git_default_config, NULL);
launch_editor(given_config_file ?
given_config_file : git_path("config"),
NULL, NULL);
}
else if (actions == ACTION_SET) {
int ret;
check_blob_write();
check_argc(argc, 2, 2);
value = normalize_value(argv[0], argv[1]);
ret = git_config_set_in_file(given_config_file, argv[0], value);
Expand All @@ -471,18 +487,21 @@ int cmd_config(int argc, const char **argv, const char *prefix)
return ret;
}
else if (actions == ACTION_SET_ALL) {
check_blob_write();
check_argc(argc, 2, 3);
value = normalize_value(argv[0], argv[1]);
return git_config_set_multivar_in_file(given_config_file,
argv[0], value, argv[2], 0);
}
else if (actions == ACTION_ADD) {
check_blob_write();
check_argc(argc, 2, 2);
value = normalize_value(argv[0], argv[1]);
return git_config_set_multivar_in_file(given_config_file,
argv[0], value, "^$", 0);
}
else if (actions == ACTION_REPLACE_ALL) {
check_blob_write();
check_argc(argc, 2, 3);
value = normalize_value(argv[0], argv[1]);
return git_config_set_multivar_in_file(given_config_file,
Expand All @@ -505,6 +524,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
return get_value(argv[0], argv[1]);
}
else if (actions == ACTION_UNSET) {
check_blob_write();
check_argc(argc, 1, 2);
if (argc == 2)
return git_config_set_multivar_in_file(given_config_file,
Expand All @@ -514,12 +534,14 @@ int cmd_config(int argc, const char **argv, const char *prefix)
argv[0], NULL);
}
else if (actions == ACTION_UNSET_ALL) {
check_blob_write();
check_argc(argc, 1, 2);
return git_config_set_multivar_in_file(given_config_file,
argv[0], NULL, argv[1], 1);
}
else if (actions == ACTION_RENAME_SECTION) {
int ret;
check_blob_write();
check_argc(argc, 2, 2);
ret = git_config_rename_section_in_file(given_config_file,
argv[0], argv[1]);
Expand All @@ -530,6 +552,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
}
else if (actions == ACTION_REMOVE_SECTION) {
int ret;
check_blob_write();
check_argc(argc, 1, 1);
ret = git_config_rename_section_in_file(given_config_file,
argv[0], NULL);
Expand Down
6 changes: 5 additions & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1176,11 +1176,15 @@ extern int update_server_info(int);
typedef int (*config_fn_t)(const char *, const char *, void *);
extern int git_default_config(const char *, const char *, void *);
extern int git_config_from_file(config_fn_t fn, const char *, void *);
extern int git_config_from_buf(config_fn_t fn, const char *name,
const char *buf, size_t len, void *data);
extern void git_config_push_parameter(const char *text);
extern int git_config_from_parameters(config_fn_t fn, void *data);
extern int git_config(config_fn_t fn, void *);
extern int git_config_with_options(config_fn_t fn, void *,
const char *filename, int respect_includes);
const char *filename,
const char *blob_ref,
int respect_includes);
extern int git_config_early(config_fn_t fn, void *, const char *repo_config);
extern int git_parse_ulong(const char *, unsigned long *);
extern int git_config_int(const char *, const char *);
Expand Down
Loading

0 comments on commit c714f9f

Please sign in to comment.