From 3caec73b5568341c5d8f303692423a8e9fb0cb39 Mon Sep 17 00:00:00 2001 From: "Kirill A. Shutemov" Date: Wed, 19 Feb 2014 00:58:55 +0200 Subject: config: teach "git config --file -" to read from the standard input The patch extends git config --file interface to allow read config from stdin. Editing stdin or setting value in stdin is an error. Include by absolute path is allowed in stdin config, but not by relative path. Signed-off-by: Kirill A. Shutemov Signed-off-by: Junio C Hamano --- config.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index a21b0ddad..7b1febdf4 100644 --- a/config.c +++ b/config.c @@ -1031,24 +1031,35 @@ static int do_config_from(struct config_source *top, config_fn_t fn, void *data) return ret; } -int git_config_from_file(config_fn_t fn, const char *filename, void *data) +static int do_config_from_file(config_fn_t fn, + const char *name, const char *path, FILE *f, void *data) { - int ret; - FILE *f = fopen(filename, "r"); + struct config_source top; - ret = -1; - if (f) { - struct config_source top; + top.u.file = f; + top.name = name; + top.path = path; + top.die_on_error = 1; + top.do_fgetc = config_file_fgetc; + top.do_ungetc = config_file_ungetc; + top.do_ftell = config_file_ftell; - top.u.file = f; - top.name = top.path = filename; - top.die_on_error = 1; - top.do_fgetc = config_file_fgetc; - top.do_ungetc = config_file_ungetc; - top.do_ftell = config_file_ftell; + return do_config_from(&top, fn, data); +} - ret = do_config_from(&top, fn, data); +static int git_config_from_stdin(config_fn_t fn, void *data) +{ + return do_config_from_file(fn, "", NULL, stdin, data); +} + +int git_config_from_file(config_fn_t fn, const char *filename, void *data) +{ + int ret = -1; + FILE *f; + f = fopen(filename, "r"); + if (f) { + ret = do_config_from_file(fn, filename, filename, f, data); fclose(f); } return ret; @@ -1190,7 +1201,9 @@ int git_config_with_options(config_fn_t fn, void *data, * If we have a specific filename, use it. Otherwise, follow the * regular lookup sequence. */ - if (config_source && config_source->file) + if (config_source && config_source->use_stdin) + return git_config_from_stdin(fn, data); + else if (config_source && config_source->file) return git_config_from_file(fn, config_source->file, data); else if (config_source && config_source->blob) return git_config_from_blob_ref(fn, config_source->blob, data); -- cgit v1.2.1