From patchwork Thu Mar 16 00:11:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13176807 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32FA5C61DA4 for ; Thu, 16 Mar 2023 00:11:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232661AbjCPAL5 (ORCPT ); Wed, 15 Mar 2023 20:11:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232840AbjCPALy (ORCPT ); Wed, 15 Mar 2023 20:11:54 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0B1A38003 for ; Wed, 15 Mar 2023 17:11:50 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id y14so12258wrq.4 for ; Wed, 15 Mar 2023 17:11:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678925509; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=i+pb0ArOIfkc6534iJy7PETsQ9XBpuOlzInTQC7qe1k=; b=BVFzUC6zgVVBCUqXFqM7Hy+wf+M4Ydu9MCFD5nXVN8gVPShX8ArI3JVNHpJ9rKu7VA msgwgu03u/oFCIsm8fTB8AsKbLBSvKGhCcq/WeQTWUyhV0yu25chCqSoSbZEMBZQUpEB 8FvxDSKwosGgxGzZ0mUAGHISo+s2/Qq6piTy6tB0JgbqY7u3QRS0P3kUBPMVbTtXI6HJ KEEaI0UTygp5oNgPrbJziYnHf2p0sTPg2h9qMA58saOYFvDNv9/GrwWQL6hw0Q41IZfp vvON+/v37uFnBpv1Kz9BZjyJddvs8Ma+GRiV5sF8OQv7x1mklG6ybjMCcu6+lb/l00Nx ewhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678925509; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i+pb0ArOIfkc6534iJy7PETsQ9XBpuOlzInTQC7qe1k=; b=XSyJBQEH0Cr5Wu8YyKk873VQ6hP+VRUI2sPUiTQJKvYQFScDqbLlQ+MU1nDnWdYAWm 4OUJSTq5ehKo+Dkp7BDmNcBuMkN/+GUmPdaLlj04KWI8sQfxbPLELQ0n3XeKrojJk8SI wy1K0KprE7yNiyfQ7z0otZV7HzFmRRwX7CEGddFf3OsS1ZHhvY+ZYuxSj7yNOyRtm0SU 0vjJwMnsPtjgW5CvRySAtV5fGWBkWEIlAlYvPOR5mA18EzqF3CZlE7LWaqjq9F/ZwWXz q1x7WYkrxuuaBUFzyJxmC+9JRolq9XzXt4CZFJEAlTDDr+9+qqMcINvIXHuVzlpLigIq IMfA== X-Gm-Message-State: AO0yUKWqrGfqEd8gnCHegHCCqG3artfgYkI+nYLiCJ8Crpdi5DFp4sDr PEavKmcd2JyMfm5s8mEfGMtmxSGVpRE= X-Google-Smtp-Source: AK7set8Rq2ZEBEGdoTM6zEBoAg3qmg/9f6CTxhMoNbrWSayouZJ2j1xvxofiU3a2u9ivw8RXSglt3w== X-Received: by 2002:a5d:5189:0:b0:2ce:9f13:a169 with SMTP id k9-20020a5d5189000000b002ce9f13a169mr3215774wrv.64.1678925508892; Wed, 15 Mar 2023 17:11:48 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id b10-20020a5d45ca000000b002ca864b807csm5952851wrs.0.2023.03.15.17.11.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 17:11:48 -0700 (PDT) Message-Id: <75d0f0efb7924fa6ce2387b1a8e4105d672a991f.1678925506.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 16 Mar 2023 00:11:39 +0000 Subject: [PATCH v2 1/8] config.c: plumb config_source through static fns Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jonathan Tan , Emily Shaffer , Jeff King , Derrick Stolee , Calvin Wan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Glen Choo , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo From: Glen Choo This reduces the direct dependence on the global "struct config_source", which will make it easier to remove in a later commit. To minimize the changes we need to make, we rename the current variable from "cf" to "cf_global", and the plumbed arg uses the old name "cf". This is a little unfortunate, since we now have the confusingly named "struct config_source cf" everywhere (which is a holdover from before 4d8dd1494e (config: make parsing stack struct independent from actual data source, 2013-07-12), when the struct used to be called "config_file"), but we will rename "cf" to "cs" by the end of the series. In some cases (public functions and config callback functions), there isn't an obvious way to plumb "struct config_source" through function args. As a workaround, add references to "cf_global" that we'll address in later commits. The remaining references to "cf_global" are direct assignments to "cf_global", which we'll also address in a later commit. Signed-off-by: Glen Choo --- config.c | 153 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 84 insertions(+), 69 deletions(-) diff --git a/config.c b/config.c index 00090a32fc3..e4a76739365 100644 --- a/config.c +++ b/config.c @@ -54,8 +54,8 @@ struct config_source { * These variables record the "current" config source, which * can be accessed by parsing callbacks. * - * The "cf" variable will be non-NULL only when we are actually parsing a real - * config source (file, blob, cmdline, etc). + * The "cf_global" variable will be non-NULL only when we are actually + * parsing a real config source (file, blob, cmdline, etc). * * The "current_config_kvi" variable will be non-NULL only when we are feeding * cached config from a configset into a callback. @@ -66,15 +66,16 @@ struct config_source { * or it's a function which can be reused for non-config purposes, and should * fall back to some sane behavior). */ -static struct config_source *cf; +static struct config_source *cf_global; static struct key_value_info *current_config_kvi; /* * Similar to the variables above, this gives access to the "scope" of the * current value (repo, global, etc). For cached values, it can be found via * the current_config_kvi as above. During parsing, the current value can be - * found in this variable. It's not part of "cf" because it transcends a single - * file (i.e., a file included from .git/config is still in "repo" scope). + * found in this variable. It's not part of "cf_global" because it transcends a + * single file (i.e., a file included from .git/config is still in "repo" + * scope). */ static enum config_scope current_parsing_scope; @@ -156,7 +157,8 @@ static const char include_depth_advice[] = N_( "from\n" " %s\n" "This might be due to circular includes."); -static int handle_path_include(const char *path, struct config_include_data *inc) +static int handle_path_include(struct config_source *cf, const char *path, + struct config_include_data *inc) { int ret = 0; struct strbuf buf = STRBUF_INIT; @@ -210,7 +212,8 @@ static void add_trailing_starstar_for_dir(struct strbuf *pat) strbuf_addstr(pat, "**"); } -static int prepare_include_condition_pattern(struct strbuf *pat) +static int prepare_include_condition_pattern(struct config_source *cf, + struct strbuf *pat) { struct strbuf path = STRBUF_INIT; char *expanded; @@ -245,7 +248,8 @@ static int prepare_include_condition_pattern(struct strbuf *pat) return prefix; } -static int include_by_gitdir(const struct config_options *opts, +static int include_by_gitdir(struct config_source *cf, + const struct config_options *opts, const char *cond, size_t cond_len, int icase) { struct strbuf text = STRBUF_INIT; @@ -261,7 +265,7 @@ static int include_by_gitdir(const struct config_options *opts, strbuf_realpath(&text, git_dir, 1); strbuf_add(&pattern, cond, cond_len); - prefix = prepare_include_condition_pattern(&pattern); + prefix = prepare_include_condition_pattern(cf, &pattern); again: if (prefix < 0) @@ -342,14 +346,14 @@ static void populate_remote_urls(struct config_include_data *inc) { struct config_options opts; - struct config_source *store_cf = cf; + struct config_source *store_cf = cf_global; struct key_value_info *store_kvi = current_config_kvi; enum config_scope store_scope = current_parsing_scope; opts = *inc->opts; opts.unconditional_remote_url = 1; - cf = NULL; + cf_global = NULL; current_config_kvi = NULL; current_parsing_scope = 0; @@ -357,7 +361,7 @@ static void populate_remote_urls(struct config_include_data *inc) string_list_init_dup(inc->remote_urls); config_with_options(add_remote_url, inc->remote_urls, inc->config_source, &opts); - cf = store_cf; + cf_global = store_cf; current_config_kvi = store_kvi; current_parsing_scope = store_scope; } @@ -406,15 +410,16 @@ static int include_by_remote_url(struct config_include_data *inc, inc->remote_urls); } -static int include_condition_is_true(struct config_include_data *inc, +static int include_condition_is_true(struct config_source *cf, + struct config_include_data *inc, const char *cond, size_t cond_len) { const struct config_options *opts = inc->opts; if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len)) - return include_by_gitdir(opts, cond, cond_len, 0); + return include_by_gitdir(cf, opts, cond, cond_len, 0); else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len)) - return include_by_gitdir(opts, cond, cond_len, 1); + return include_by_gitdir(cf, opts, cond, cond_len, 1); else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len)) return include_by_branch(cond, cond_len); else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond, @@ -441,16 +446,16 @@ static int git_config_include(const char *var, const char *value, void *data) return ret; if (!strcmp(var, "include.path")) - ret = handle_path_include(value, inc); + ret = handle_path_include(cf_global, value, inc); if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) && - cond && include_condition_is_true(inc, cond, cond_len) && + cond && include_condition_is_true(cf_global, inc, cond, cond_len) && !strcmp(key, "path")) { config_fn_t old_fn = inc->fn; if (inc->opts->unconditional_remote_url) inc->fn = forbid_remote_url; - ret = handle_path_include(value, inc); + ret = handle_path_include(cf_global, value, inc); inc->fn = old_fn; } @@ -713,9 +718,9 @@ int git_config_from_parameters(config_fn_t fn, void *data) struct config_source source; memset(&source, 0, sizeof(source)); - source.prev = cf; + source.prev = cf_global; source.origin_type = CONFIG_ORIGIN_CMDLINE; - cf = &source; + cf_global = &source; env = getenv(CONFIG_COUNT_ENVIRONMENT); if (env) { @@ -773,11 +778,11 @@ out: strbuf_release(&envvar); strvec_clear(&to_free); free(envw); - cf = source.prev; + cf_global = source.prev; return ret; } -static int get_next_char(void) +static int get_next_char(struct config_source *cf) { int c = cf->do_fgetc(cf); @@ -813,13 +818,13 @@ static int get_next_char(void) return c; } -static char *parse_value(void) +static char *parse_value(struct config_source *cf) { int quote = 0, comment = 0, space = 0; strbuf_reset(&cf->value); for (;;) { - int c = get_next_char(); + int c = get_next_char(cf); if (c == '\n') { if (quote) { cf->linenr--; @@ -843,7 +848,7 @@ static char *parse_value(void) for (; space; space--) strbuf_addch(&cf->value, ' '); if (c == '\\') { - c = get_next_char(); + c = get_next_char(cf); switch (c) { case '\n': continue; @@ -874,7 +879,8 @@ static char *parse_value(void) } } -static int get_value(config_fn_t fn, void *data, struct strbuf *name) +static int get_value(struct config_source *cf, config_fn_t fn, void *data, + struct strbuf *name) { int c; char *value; @@ -882,7 +888,7 @@ static int get_value(config_fn_t fn, void *data, struct strbuf *name) /* Get the full name */ for (;;) { - c = get_next_char(); + c = get_next_char(cf); if (cf->eof) break; if (!iskeychar(c)) @@ -891,13 +897,13 @@ static int get_value(config_fn_t fn, void *data, struct strbuf *name) } while (c == ' ' || c == '\t') - c = get_next_char(); + c = get_next_char(cf); value = NULL; if (c != '\n') { if (c != '=') return -1; - value = parse_value(); + value = parse_value(cf); if (!value) return -1; } @@ -913,13 +919,14 @@ static int get_value(config_fn_t fn, void *data, struct strbuf *name) return ret; } -static int get_extended_base_var(struct strbuf *name, int c) +static int get_extended_base_var(struct config_source *cf, struct strbuf *name, + int c) { cf->subsection_case_sensitive = 0; do { if (c == '\n') goto error_incomplete_line; - c = get_next_char(); + c = get_next_char(cf); } while (isspace(c)); /* We require the format to be '[base "extension"]' */ @@ -928,13 +935,13 @@ static int get_extended_base_var(struct strbuf *name, int c) strbuf_addch(name, '.'); for (;;) { - int c = get_next_char(); + int c = get_next_char(cf); if (c == '\n') goto error_incomplete_line; if (c == '"') break; if (c == '\\') { - c = get_next_char(); + c = get_next_char(cf); if (c == '\n') goto error_incomplete_line; } @@ -942,7 +949,7 @@ static int get_extended_base_var(struct strbuf *name, int c) } /* Final ']' */ - if (get_next_char() != ']') + if (get_next_char(cf) != ']') return -1; return 0; error_incomplete_line: @@ -950,17 +957,17 @@ error_incomplete_line: return -1; } -static int get_base_var(struct strbuf *name) +static int get_base_var(struct config_source *cf, struct strbuf *name) { cf->subsection_case_sensitive = 1; for (;;) { - int c = get_next_char(); + int c = get_next_char(cf); if (cf->eof) return -1; if (c == ']') return 0; if (isspace(c)) - return get_extended_base_var(name, c); + return get_extended_base_var(cf, name, c); if (!iskeychar(c) && c != '.') return -1; strbuf_addch(name, tolower(c)); @@ -973,7 +980,8 @@ struct parse_event_data { const struct config_options *opts; }; -static int do_event(enum config_event_t type, struct parse_event_data *data) +static int do_event(struct config_source *cf, enum config_event_t type, + struct parse_event_data *data) { size_t offset; @@ -1004,8 +1012,8 @@ static int do_event(enum config_event_t type, struct parse_event_data *data) return 0; } -static int git_parse_source(config_fn_t fn, void *data, - const struct config_options *opts) +static int git_parse_source(struct config_source *cf, config_fn_t fn, + void *data, const struct config_options *opts) { int comment = 0; size_t baselen = 0; @@ -1024,7 +1032,7 @@ static int git_parse_source(config_fn_t fn, void *data, for (;;) { int c; - c = get_next_char(); + c = get_next_char(cf); if (bomptr && *bomptr) { /* We are at the file beginning; skip UTF8-encoded BOM * if present. Sane editors won't put this in on their @@ -1042,11 +1050,11 @@ static int git_parse_source(config_fn_t fn, void *data, } if (c == '\n') { if (cf->eof) { - if (do_event(CONFIG_EVENT_EOF, &event_data) < 0) + if (do_event(cf, CONFIG_EVENT_EOF, &event_data) < 0) return -1; return 0; } - if (do_event(CONFIG_EVENT_WHITESPACE, &event_data) < 0) + if (do_event(cf, CONFIG_EVENT_WHITESPACE, &event_data) < 0) return -1; comment = 0; continue; @@ -1054,23 +1062,23 @@ static int git_parse_source(config_fn_t fn, void *data, if (comment) continue; if (isspace(c)) { - if (do_event(CONFIG_EVENT_WHITESPACE, &event_data) < 0) + if (do_event(cf, CONFIG_EVENT_WHITESPACE, &event_data) < 0) return -1; continue; } if (c == '#' || c == ';') { - if (do_event(CONFIG_EVENT_COMMENT, &event_data) < 0) + if (do_event(cf, CONFIG_EVENT_COMMENT, &event_data) < 0) return -1; comment = 1; continue; } if (c == '[') { - if (do_event(CONFIG_EVENT_SECTION, &event_data) < 0) + if (do_event(cf, CONFIG_EVENT_SECTION, &event_data) < 0) return -1; /* Reset prior to determining a new stem */ strbuf_reset(var); - if (get_base_var(var) < 0 || var->len < 1) + if (get_base_var(cf, var) < 0 || var->len < 1) break; strbuf_addch(var, '.'); baselen = var->len; @@ -1079,7 +1087,7 @@ static int git_parse_source(config_fn_t fn, void *data, if (!isalpha(c)) break; - if (do_event(CONFIG_EVENT_ENTRY, &event_data) < 0) + if (do_event(cf, CONFIG_EVENT_ENTRY, &event_data) < 0) return -1; /* @@ -1089,11 +1097,11 @@ static int git_parse_source(config_fn_t fn, void *data, */ strbuf_setlen(var, baselen); strbuf_addch(var, tolower(c)); - if (get_value(fn, data, var) < 0) + if (get_value(cf, fn, data, var) < 0) break; } - if (do_event(CONFIG_EVENT_ERROR, &event_data) < 0) + if (do_event(cf, CONFIG_EVENT_ERROR, &event_data) < 0) return -1; switch (cf->origin_type) { @@ -1266,7 +1274,8 @@ int git_parse_ssize_t(const char *value, ssize_t *ret) } NORETURN -static void die_bad_number(const char *name, const char *value) +static void die_bad_number(struct config_source *cf, const char *name, + const char *value) { const char *error_type = (errno == ERANGE) ? N_("out of range") : N_("invalid unit"); @@ -1304,7 +1313,7 @@ int git_config_int(const char *name, const char *value) { int ret; if (!git_parse_int(value, &ret)) - die_bad_number(name, value); + die_bad_number(cf_global, name, value); return ret; } @@ -1312,7 +1321,7 @@ int64_t git_config_int64(const char *name, const char *value) { int64_t ret; if (!git_parse_int64(value, &ret)) - die_bad_number(name, value); + die_bad_number(cf_global, name, value); return ret; } @@ -1320,7 +1329,7 @@ unsigned long git_config_ulong(const char *name, const char *value) { unsigned long ret; if (!git_parse_ulong(value, &ret)) - die_bad_number(name, value); + die_bad_number(cf_global, name, value); return ret; } @@ -1328,7 +1337,7 @@ ssize_t git_config_ssize_t(const char *name, const char *value) { ssize_t ret; if (!git_parse_ssize_t(value, &ret)) - die_bad_number(name, value); + die_bad_number(cf_global, name, value); return ret; } @@ -1940,20 +1949,20 @@ static int do_config_from(struct config_source *top, config_fn_t fn, void *data, int ret; /* push config-file parsing state stack */ - top->prev = cf; + top->prev = cf_global; top->linenr = 1; top->eof = 0; top->total_len = 0; strbuf_init(&top->value, 1024); strbuf_init(&top->var, 1024); - cf = top; + cf_global = top; - ret = git_parse_source(fn, data, opts); + ret = git_parse_source(top, fn, data, opts); /* pop config-file parsing state stack */ strbuf_release(&top->value); strbuf_release(&top->var); - cf = top->prev; + cf_global = top->prev; return ret; } @@ -2334,12 +2343,12 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha l_item->e = e; l_item->value_index = e->value_list.nr - 1; - if (!cf) + if (!cf_global) BUG("configset_add_value has no source"); - if (cf->name) { - kv_info->filename = strintern(cf->name); - kv_info->linenr = cf->linenr; - kv_info->origin_type = cf->origin_type; + if (cf_global->name) { + kv_info->filename = strintern(cf_global->name); + kv_info->linenr = cf_global->linenr; + kv_info->origin_type = cf_global->origin_type; } else { /* for values read from `git_config_from_parameters()` */ kv_info->filename = NULL; @@ -2891,6 +2900,12 @@ static int store_aux_event(enum config_event_t type, size_t begin, size_t end, void *data) { struct config_store_data *store = data; + /* + * FIXME Keep using "cf" so that we can avoid rewrapping a + * really long line below. Remove this when "cf" gets plumbed + * correctly. + */ + struct config_source *cf = cf_global; ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc); store->parsed[store->parsed_nr].begin = begin; @@ -3771,8 +3786,8 @@ const char *current_config_origin_type(void) int type; if (current_config_kvi) type = current_config_kvi->origin_type; - else if(cf) - type = cf->origin_type; + else if(cf_global) + type = cf_global->origin_type; else BUG("current_config_origin_type called outside config callback"); @@ -3817,8 +3832,8 @@ const char *current_config_name(void) const char *name; if (current_config_kvi) name = current_config_kvi->filename; - else if (cf) - name = cf->name; + else if (cf_global) + name = cf_global->name; else BUG("current_config_name called outside config callback"); return name ? name : ""; @@ -3837,7 +3852,7 @@ int current_config_line(void) if (current_config_kvi) return current_config_kvi->linenr; else - return cf->linenr; + return cf_global->linenr; } int lookup_config(const char **mapping, int nr_mapping, const char *var) From patchwork Thu Mar 16 00:11:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13176806 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E826AC6FD1D for ; Thu, 16 Mar 2023 00:11:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233149AbjCPALz (ORCPT ); Wed, 15 Mar 2023 20:11:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232817AbjCPALy (ORCPT ); Wed, 15 Mar 2023 20:11:54 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC56F1814C for ; Wed, 15 Mar 2023 17:11:51 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id h17so2503wrt.8 for ; Wed, 15 Mar 2023 17:11:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678925510; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=h8jXzGIPWtezt7Z4YkMHHcu4NcVGQJtC2dzlOMVrMjk=; b=eQS2GPOgZyt/Bz5lUncW9mdTcFmPvg1nsGeai8U9lzeRbhlRxCJ7nu1awIu0jjgnVK 8WSou6F7yKw32w2hp5SH8Sp98bHBiJaAcUjpZMIEZqnLj1HgQFtNTrxQ+3BmSEQWXz2r dc23cLpu7QChBwPom4BE1OOHOrwYnuJPZo+UnVZ4RT9osEg47Pcayn2pbmk3dQnGyk1h N7kOYVfo7W1/6N9bdmbwXPsKI75vt7oOn/J81ZD1u/Y7YMRfd3tm16N65xegaZvXtANU LJ7kcEYCGFkRQeU5cqkPExzrt7S5BdxpRxRQ9+nLU0ah9d9wUWTA8yg1LHrAeaFksQpX APZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678925510; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=h8jXzGIPWtezt7Z4YkMHHcu4NcVGQJtC2dzlOMVrMjk=; b=sob7AX8ZDVnF1hD47WsszClqMRpOMQZ8pHp9eGvIbGXHhE5GVvNexENZxaHwgS9D2g CxfGX/hDjjnH3LD+wy2iTlp1XImG78kE0VwXMXb4VzVctNw6Vl5RovmhFB+1Xzsv2UyV R41JpoWcyjjY/EJEKAQJLnh7nQS4I78jsXmAZjPA990VoyUj/+zOvj1v1lCgqwMoULI8 JXryZg8qUK5hrYEtdTUhfVTeGyt2+ma+NKdMANUJceZwHAGwHMHFkvE43J0hxvgGrjj8 Up1Ja50YYUp4IWAdSJhnwxZPU50A2E0361a/DUGXt1e3ksVFBxZqvVA0lXx+74u6YHgQ pisQ== X-Gm-Message-State: AO0yUKXPrjWM4P40A7+Lldon81xKkf0cZInROmkLceQ/dpr8L79IWe4Z XkNIA+WOnEHWr+SSH5L1M9FhW3nv9m8= X-Google-Smtp-Source: AK7set/55CiaIVjI9NtcVpvRP+F8AG3Q0KIoPHLh4QO8+6Mq88gbRzSdvYtS8ta5tJUm32/Y8RH8cg== X-Received: by 2002:a5d:4c8e:0:b0:2cf:e710:a4b9 with SMTP id z14-20020a5d4c8e000000b002cfe710a4b9mr3325988wrs.32.1678925509732; Wed, 15 Mar 2023 17:11:49 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f10-20020adffcca000000b002cea8f07813sm5916520wrs.81.2023.03.15.17.11.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 17:11:49 -0700 (PDT) Message-Id: <7555da0b0e012a5fd28ca1a70a4a0897514cd607.1678925506.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 16 Mar 2023 00:11:40 +0000 Subject: [PATCH v2 2/8] config.c: don't assign to "cf_global" directly Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jonathan Tan , Emily Shaffer , Jeff King , Derrick Stolee , Calvin Wan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Glen Choo , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo From: Glen Choo To make "cf_global" easier to remove, replace all direct assignments to it with function calls. This refactor has an additional maintainability benefit: all of these functions were manually implementing stack pop/push semantics on "struct config_source", so replacing them with function calls allows us to only implement this logic once. In this process, perform some now-obvious clean ups: - Drop some unnecessary "cf_global" assignments in populate_remote_urls(). Since it was introduced in 399b198489 (config: include file if remote URL matches a glob, 2022-01-18), it has stored and restored the value of "cf_global" to ensure that it doesn't get accidentally mutated. However, this was never necessary since "do_config_from()" already pushes/pops "cf_global" further down the call chain. - Zero out every "struct config_source" with a dedicated initializer. This matters because the "struct config_source" is assigned to "cf_global" and we later 'pop the stack' by assigning "cf_global = cf_global->prev", but "cf_global->prev" could be pointing to uninitialized garbage. Fortunately, this has never bothered us since we never try to read "cf_global" except while iterating through config, in which case, "cf_global" is either set to a sensible value (when parsing a file), or it is ignored (when iterating a configset). Later in the series, zero-ing out memory will also let us enforce the constraint that "cf_global" and "current_config_kvi" are never non-NULL together. Signed-off-by: Glen Choo --- config.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/config.c b/config.c index e4a76739365..517b8f64038 100644 --- a/config.c +++ b/config.c @@ -49,6 +49,7 @@ struct config_source { int (*do_ungetc)(int c, struct config_source *conf); long (*do_ftell)(struct config_source *c); }; +#define CONFIG_SOURCE_INIT { 0 } /* * These variables record the "current" config source, which @@ -79,6 +80,23 @@ static struct key_value_info *current_config_kvi; */ static enum config_scope current_parsing_scope; +static inline void config_reader_push_source(struct config_source *top) +{ + if (cf_global) + top->prev = cf_global; + cf_global = top; +} + +static inline struct config_source *config_reader_pop_source() +{ + struct config_source *ret; + if (!cf_global) + BUG("tried to pop config source, but we weren't reading config"); + ret = cf_global; + cf_global = cf_global->prev; + return ret; +} + static int pack_compression_seen; static int zlib_compression_seen; @@ -346,14 +364,12 @@ static void populate_remote_urls(struct config_include_data *inc) { struct config_options opts; - struct config_source *store_cf = cf_global; struct key_value_info *store_kvi = current_config_kvi; enum config_scope store_scope = current_parsing_scope; opts = *inc->opts; opts.unconditional_remote_url = 1; - cf_global = NULL; current_config_kvi = NULL; current_parsing_scope = 0; @@ -361,7 +377,6 @@ static void populate_remote_urls(struct config_include_data *inc) string_list_init_dup(inc->remote_urls); config_with_options(add_remote_url, inc->remote_urls, inc->config_source, &opts); - cf_global = store_cf; current_config_kvi = store_kvi; current_parsing_scope = store_scope; } @@ -715,12 +730,10 @@ int git_config_from_parameters(config_fn_t fn, void *data) struct strvec to_free = STRVEC_INIT; int ret = 0; char *envw = NULL; - struct config_source source; + struct config_source source = CONFIG_SOURCE_INIT; - memset(&source, 0, sizeof(source)); - source.prev = cf_global; source.origin_type = CONFIG_ORIGIN_CMDLINE; - cf_global = &source; + config_reader_push_source(&source); env = getenv(CONFIG_COUNT_ENVIRONMENT); if (env) { @@ -778,7 +791,7 @@ out: strbuf_release(&envvar); strvec_clear(&to_free); free(envw); - cf_global = source.prev; + config_reader_pop_source(); return ret; } @@ -1949,20 +1962,19 @@ static int do_config_from(struct config_source *top, config_fn_t fn, void *data, int ret; /* push config-file parsing state stack */ - top->prev = cf_global; top->linenr = 1; top->eof = 0; top->total_len = 0; strbuf_init(&top->value, 1024); strbuf_init(&top->var, 1024); - cf_global = top; + config_reader_push_source(top); ret = git_parse_source(top, fn, data, opts); /* pop config-file parsing state stack */ strbuf_release(&top->value); strbuf_release(&top->var); - cf_global = top->prev; + config_reader_pop_source(); return ret; } @@ -1972,7 +1984,7 @@ static int do_config_from_file(config_fn_t fn, const char *name, const char *path, FILE *f, void *data, const struct config_options *opts) { - struct config_source top; + struct config_source top = CONFIG_SOURCE_INIT; int ret; top.u.file = f; @@ -2024,7 +2036,7 @@ int git_config_from_mem(config_fn_t fn, const char *name, const char *buf, size_t len, void *data, const struct config_options *opts) { - struct config_source top; + struct config_source top = CONFIG_SOURCE_INIT; top.u.buf.buf = buf; top.u.buf.len = len; From patchwork Thu Mar 16 00:11:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13176808 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1250CC7618D for ; Thu, 16 Mar 2023 00:12:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232867AbjCPAMA (ORCPT ); Wed, 15 Mar 2023 20:12:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233044AbjCPALy (ORCPT ); Wed, 15 Mar 2023 20:11:54 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0D0432CE7 for ; Wed, 15 Mar 2023 17:11:52 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id j19-20020a05600c191300b003eb3e1eb0caso2086159wmq.1 for ; Wed, 15 Mar 2023 17:11:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678925510; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=7Wf9k+cvBI87gxclxWwH5m1A5NDAiYtjdP+E6EFdRSk=; b=DLWBsxf3AIsWoksn7ZmjdqA1EqzzJsd7e8B8Co+s6a9PVwNEjzltsH4c2SX7j9cjra e6lwwEkL8CvOrU3TkVusCyeqFDa7Qylt9SBVMDcQpGqx6KtGdnlRPkBgFrsyuIXYTq8E VelPTPXSSglDBgBKoVmL1wNCZqGxgQg5yyWde6TkV47YI9ryp/U2/HoV3fC7eKqy/jjR AAKgFykqWcAg7DxS92qr86YlP2aVjiqGcru+XWpSxAW8rEdIN/aSneh6+0cggJ/HmV/K MWObfzbDbSWlSRXDyXM1B9O6uyJ0CEg7QxoAH+QoZT+blccItZQMIaRK5ltpDwX30gUl gEmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678925510; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7Wf9k+cvBI87gxclxWwH5m1A5NDAiYtjdP+E6EFdRSk=; b=aTwLs3wIxLu+lhqzAXYlDNQ388YdhDUFvj/ytzSDPk9twBBDUNZar8c/+Rbf9EGp/q yfddgqvaHScTx1am1lN9/dYoUJsNlaWL5WDDzBDV7RIYzqMITEBEqx9EocysIqn5RZHs 1Lf0oYvgq+w4xCXUYJWoLKPBZl+kEn3gTmqp2FBMkAauBJ3bn2FRqXrkH2j+lYAQRsah 7++Y7+ZalD8zsI1TdwBKsq3RdZF4crCQ7BNxhtB6VkokfAztBqGMLi8ckK57RGpcgsPY tuVZmlnIy/5J0AZRwvjlfbQ8Q6x9tbkZergxnvTlzkptxxtfavo8GvWALPvEo3SJ2kP0 Pxyw== X-Gm-Message-State: AO0yUKWEMA41E2+U2piNgttEAnREPh75JZa22E5OGT8L79O9zXOSn7kE lVtg+dXVNIqm3p/7nuFU17yKFB9l5Ac= X-Google-Smtp-Source: AK7set8t/v9h0YqhoWenIAjovMUKio6hrsgMH12cJG8ymIPoEZMCsLXwLB5qQ9lkKPiuw8716s2PmQ== X-Received: by 2002:a05:600c:5250:b0:3ed:4685:4618 with SMTP id fc16-20020a05600c525000b003ed46854618mr1093963wmb.34.1678925510464; Wed, 15 Mar 2023 17:11:50 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j8-20020a05600c1c0800b003ebf73acf9asm8639248wms.3.2023.03.15.17.11.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 17:11:50 -0700 (PDT) Message-Id: <4347896f0a4896efe5b1410b72f5b583ad6bd9e0.1678925506.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 16 Mar 2023 00:11:41 +0000 Subject: [PATCH v2 3/8] config.c: create config_reader and the_reader Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jonathan Tan , Emily Shaffer , Jeff King , Derrick Stolee , Calvin Wan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Glen Choo , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo From: Glen Choo Create "struct config_reader" to hold the state of the config source currently being read. Then, create a static instance of it, "the_reader", and use "the_reader.source" to replace references to "cf_global" in public functions. This doesn't create much immediate benefit (since we're mostly replacing static variables with a bigger static variable), but it prepares us for a future where this state doesn't have to be global; "struct config_reader" (or a similar struct) could be provided by the caller, or constructed internally by a function like "do_config_from()". A more typical approach would be to put this struct on "the_repository", but that's a worse fit for this use case since config reading is not scoped to a repository. E.g. we can read config before the repository is known ("read_very_early_config()"), blatantly ignore the repo ("read_protected_config()"), or read only from a file ("git_config_from_file()"). This is especially evident in t5318 and t9210, where test-tool and scalar parse config but don't fully initialize "the_repository". We could have also replaced the references to "cf_global" in callback functions (which are the only ones left), but we'll eventually plumb "the_reader" through the callback "*data" arg, so that would be unnecessary churn. Until we remove "cf_global" altogether, add logic to "config_reader_*_source()" to keep "cf_global" and "the_reader.source" in sync. Signed-off-by: Glen Choo --- config.c | 84 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/config.c b/config.c index 517b8f64038..7de25515818 100644 --- a/config.c +++ b/config.c @@ -51,6 +51,16 @@ struct config_source { }; #define CONFIG_SOURCE_INIT { 0 } +struct config_reader { + struct config_source *source; +}; +/* + * Where possible, prefer to accept "struct config_reader" as an arg than to use + * "the_reader". "the_reader" should only be used if that is infeasible, e.g. in + * a public function. + */ +static struct config_reader the_reader; + /* * These variables record the "current" config source, which * can be accessed by parsing callbacks. @@ -66,6 +76,9 @@ struct config_source { * at the variables, it's either a bug for it to be called in the first place, * or it's a function which can be reused for non-config purposes, and should * fall back to some sane behavior). + * + * FIXME "cf_global" has been replaced by "the_reader.source", remove + * "cf_global" once we plumb "the_reader" through all of the callback functions. */ static struct config_source *cf_global; static struct key_value_info *current_config_kvi; @@ -80,20 +93,25 @@ static struct key_value_info *current_config_kvi; */ static enum config_scope current_parsing_scope; -static inline void config_reader_push_source(struct config_source *top) +static inline void config_reader_push_source(struct config_reader *reader, + struct config_source *top) { - if (cf_global) - top->prev = cf_global; - cf_global = top; + if (reader->source) + top->prev = reader->source; + reader->source = top; + /* FIXME remove this when cf_global is removed. */ + cf_global = reader->source; } -static inline struct config_source *config_reader_pop_source() +static inline struct config_source *config_reader_pop_source(struct config_reader *reader) { struct config_source *ret; - if (!cf_global) + if (!reader->source) BUG("tried to pop config source, but we weren't reading config"); - ret = cf_global; - cf_global = cf_global->prev; + ret = reader->source; + reader->source = reader->source->prev; + /* FIXME remove this when cf is removed. */ + cf_global = reader->source; return ret; } @@ -733,7 +751,7 @@ int git_config_from_parameters(config_fn_t fn, void *data) struct config_source source = CONFIG_SOURCE_INIT; source.origin_type = CONFIG_ORIGIN_CMDLINE; - config_reader_push_source(&source); + config_reader_push_source(&the_reader, &source); env = getenv(CONFIG_COUNT_ENVIRONMENT); if (env) { @@ -791,7 +809,7 @@ out: strbuf_release(&envvar); strvec_clear(&to_free); free(envw); - config_reader_pop_source(); + config_reader_pop_source(&the_reader); return ret; } @@ -1326,7 +1344,7 @@ int git_config_int(const char *name, const char *value) { int ret; if (!git_parse_int(value, &ret)) - die_bad_number(cf_global, name, value); + die_bad_number(the_reader.source, name, value); return ret; } @@ -1334,7 +1352,7 @@ int64_t git_config_int64(const char *name, const char *value) { int64_t ret; if (!git_parse_int64(value, &ret)) - die_bad_number(cf_global, name, value); + die_bad_number(the_reader.source, name, value); return ret; } @@ -1342,7 +1360,7 @@ unsigned long git_config_ulong(const char *name, const char *value) { unsigned long ret; if (!git_parse_ulong(value, &ret)) - die_bad_number(cf_global, name, value); + die_bad_number(the_reader.source, name, value); return ret; } @@ -1350,7 +1368,7 @@ ssize_t git_config_ssize_t(const char *name, const char *value) { ssize_t ret; if (!git_parse_ssize_t(value, &ret)) - die_bad_number(cf_global, name, value); + die_bad_number(the_reader.source, name, value); return ret; } @@ -1956,7 +1974,8 @@ int git_default_config(const char *var, const char *value, void *cb) * fgetc, ungetc, ftell of top need to be initialized before calling * this function. */ -static int do_config_from(struct config_source *top, config_fn_t fn, void *data, +static int do_config_from(struct config_reader *reader, + struct config_source *top, config_fn_t fn, void *data, const struct config_options *opts) { int ret; @@ -1967,22 +1986,23 @@ static int do_config_from(struct config_source *top, config_fn_t fn, void *data, top->total_len = 0; strbuf_init(&top->value, 1024); strbuf_init(&top->var, 1024); - config_reader_push_source(top); + config_reader_push_source(reader, top); ret = git_parse_source(top, fn, data, opts); /* pop config-file parsing state stack */ strbuf_release(&top->value); strbuf_release(&top->var); - config_reader_pop_source(); + config_reader_pop_source(reader); return ret; } -static int do_config_from_file(config_fn_t fn, - const enum config_origin_type origin_type, - const char *name, const char *path, FILE *f, - void *data, const struct config_options *opts) +static int do_config_from_file(struct config_reader *reader, + config_fn_t fn, + const enum config_origin_type origin_type, + const char *name, const char *path, FILE *f, + void *data, const struct config_options *opts) { struct config_source top = CONFIG_SOURCE_INIT; int ret; @@ -1997,15 +2017,15 @@ static int do_config_from_file(config_fn_t fn, top.do_ftell = config_file_ftell; flockfile(f); - ret = do_config_from(&top, fn, data, opts); + ret = do_config_from(reader, &top, fn, data, opts); funlockfile(f); return ret; } static int git_config_from_stdin(config_fn_t fn, void *data) { - return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin, - data, NULL); + return do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_STDIN, "", + NULL, stdin, data, NULL); } int git_config_from_file_with_options(config_fn_t fn, const char *filename, @@ -2019,8 +2039,8 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename, BUG("filename cannot be NULL"); f = fopen_or_warn(filename, "r"); if (f) { - ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename, - filename, f, data, opts); + ret = do_config_from_file(&the_reader, fn, CONFIG_ORIGIN_FILE, + filename, filename, f, data, opts); fclose(f); } return ret; @@ -2049,7 +2069,7 @@ int git_config_from_mem(config_fn_t fn, top.do_ungetc = config_buf_ungetc; top.do_ftell = config_buf_ftell; - return do_config_from(&top, fn, data, opts); + return do_config_from(&the_reader, &top, fn, data, opts); } int git_config_from_blob_oid(config_fn_t fn, @@ -3798,8 +3818,8 @@ const char *current_config_origin_type(void) int type; if (current_config_kvi) type = current_config_kvi->origin_type; - else if(cf_global) - type = cf_global->origin_type; + else if(the_reader.source) + type = the_reader.source->origin_type; else BUG("current_config_origin_type called outside config callback"); @@ -3844,8 +3864,8 @@ const char *current_config_name(void) const char *name; if (current_config_kvi) name = current_config_kvi->filename; - else if (cf_global) - name = cf_global->name; + else if (the_reader.source) + name = the_reader.source->name; else BUG("current_config_name called outside config callback"); return name ? name : ""; @@ -3864,7 +3884,7 @@ int current_config_line(void) if (current_config_kvi) return current_config_kvi->linenr; else - return cf_global->linenr; + return the_reader.source->linenr; } int lookup_config(const char **mapping, int nr_mapping, const char *var) From patchwork Thu Mar 16 00:11:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13176809 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F268FC6FD1D for ; Thu, 16 Mar 2023 00:12:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233180AbjCPAMD (ORCPT ); Wed, 15 Mar 2023 20:12:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232971AbjCPALy (ORCPT ); Wed, 15 Mar 2023 20:11:54 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F18413CE04 for ; Wed, 15 Mar 2023 17:11:52 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id k25-20020a7bc419000000b003ed23114fa7so2075522wmi.4 for ; Wed, 15 Mar 2023 17:11:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678925511; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=HixjIRFPNbVqZoH0TXahGAYQHSpAH7z1c0UNI9dAnL4=; b=Zy0MV9fqpSo3bhSb1T8X3oucAs8ZueGwDF+iwIOmECE7DdxkmJ44PkkVZAr+vGPeRa AycYxw32u9bWFUlKg9D0/h7EeLpGg5ZgKzd/r/x01v70qZECSsr13JLr8LBBebOWdogd Hxf0LMOD8Ys89fMDndzDoCu8RXkdvhCB0MgVNOXm/xGBKWRWoB7OSzif6xPsYDiMirnf avvAHPStHPURNvbAOeXxyd544AhqJbxEzrTBlgfDl7J9udducgy4Z383vYxOyyUGSGm7 FCQy2oRRbllFPlhiZMg50S+SAVKwl7dLiOEMxwr3MOS+DQgW83ra7mDfLVfyTOw4Fdru gWRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678925511; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HixjIRFPNbVqZoH0TXahGAYQHSpAH7z1c0UNI9dAnL4=; b=orNMRhl63kCg8uZDR7pWtSiSKGbZKMkXJZhUUQ3K32iZaHSJ2ywBpfO/qwING1EO/c CqhhpO9VeAxRMhYIuvYTH50p7ybESnFQM5abpB2iXnZJP+OP87Z7XrPK0L62ll+aAQyy 8qHtfOHT6LhFLWkmVKG/1sfKhFUDfMRInC8vyowoDoL5355GbC8afX3qFpZHwoaKZH9T vcvkiuhetbtID4MP9b4Zao79wlqg34gbz2C1C9QXcmRjbYSiw50ikTjosrikXbyBjaDJ 5MSZkzRaIysHHphGxz5xPpNuvqnZSf0P1fqAhTSmdi+2nO/a7SLbO0YXBw/22i5jQc0A Qolg== X-Gm-Message-State: AO0yUKVB4XfKW7yony61ufSkGXAcnhhjUwXHg552a7rqdS5LtCJBZBKh V1+T33Q/cdTGGHOoGHb9ZrqOgjRFqGs= X-Google-Smtp-Source: AK7set/NR6nNfrgKEEpxrI0izQaYBi5n7P/85WEPLZ32BfqebqUwXIImDeClhYvTiy+5TTyGYeZP4g== X-Received: by 2002:a05:600c:468e:b0:3e8:f27b:a92c with SMTP id p14-20020a05600c468e00b003e8f27ba92cmr20514297wmo.32.1678925511270; Wed, 15 Mar 2023 17:11:51 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p18-20020a05600c205200b003ed1f111fdesm3296094wmg.20.2023.03.15.17.11.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 17:11:50 -0700 (PDT) Message-Id: <22b699717499e4f51bab02ed5ab5a4489f42cc6d.1678925506.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 16 Mar 2023 00:11:42 +0000 Subject: [PATCH v2 4/8] config.c: plumb the_reader through callbacks Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jonathan Tan , Emily Shaffer , Jeff King , Derrick Stolee , Calvin Wan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Glen Choo , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo From: Glen Choo The remaining references to "cf_global" are in config callback functions. Remove them by plumbing "struct config_reader" via the "*data" arg. In both of the callbacks here, we are only reading from "reader->source". So in the long run, if we had a way to expose readonly information from "reader->source" (probably in the form of "struct key_value_info"), we could undo this patch (i.e. remove "struct config_reader" fom "*data"). Signed-off-by: Glen Choo --- config.c | 74 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/config.c b/config.c index 7de25515818..b75cde42ca6 100644 --- a/config.c +++ b/config.c @@ -62,6 +62,9 @@ struct config_reader { static struct config_reader the_reader; /* + * FIXME The comments are temporarily out of date since "cf_global" has been + * moved to the_reader, but not current_*. + * * These variables record the "current" config source, which * can be accessed by parsing callbacks. * @@ -76,11 +79,7 @@ static struct config_reader the_reader; * at the variables, it's either a bug for it to be called in the first place, * or it's a function which can be reused for non-config purposes, and should * fall back to some sane behavior). - * - * FIXME "cf_global" has been replaced by "the_reader.source", remove - * "cf_global" once we plumb "the_reader" through all of the callback functions. */ -static struct config_source *cf_global; static struct key_value_info *current_config_kvi; /* @@ -99,8 +98,6 @@ static inline void config_reader_push_source(struct config_reader *reader, if (reader->source) top->prev = reader->source; reader->source = top; - /* FIXME remove this when cf_global is removed. */ - cf_global = reader->source; } static inline struct config_source *config_reader_pop_source(struct config_reader *reader) @@ -110,8 +107,6 @@ static inline struct config_source *config_reader_pop_source(struct config_reade BUG("tried to pop config source, but we weren't reading config"); ret = reader->source; reader->source = reader->source->prev; - /* FIXME remove this when cf is removed. */ - cf_global = reader->source; return ret; } @@ -176,6 +171,7 @@ struct config_include_data { void *data; const struct config_options *opts; struct git_config_source *config_source; + struct config_reader *config_reader; /* * All remote URLs discovered when reading all config files. @@ -466,6 +462,7 @@ static int include_condition_is_true(struct config_source *cf, static int git_config_include(const char *var, const char *value, void *data) { struct config_include_data *inc = data; + struct config_source *cf = inc->config_reader->source; const char *cond, *key; size_t cond_len; int ret; @@ -479,16 +476,16 @@ static int git_config_include(const char *var, const char *value, void *data) return ret; if (!strcmp(var, "include.path")) - ret = handle_path_include(cf_global, value, inc); + ret = handle_path_include(cf, value, inc); if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) && - cond && include_condition_is_true(cf_global, inc, cond, cond_len) && + cond && include_condition_is_true(cf, inc, cond, cond_len) && !strcmp(key, "path")) { config_fn_t old_fn = inc->fn; if (inc->opts->unconditional_remote_url) inc->fn = forbid_remote_url; - ret = handle_path_include(cf_global, value, inc); + ret = handle_path_include(cf, value, inc); inc->fn = old_fn; } @@ -2229,6 +2226,7 @@ int config_with_options(config_fn_t fn, void *data, inc.data = data; inc.opts = opts; inc.config_source = config_source; + inc.config_reader = &the_reader; fn = git_config_include; data = &inc; } @@ -2349,7 +2347,9 @@ static struct config_set_element *configset_find_element(struct config_set *cs, return found_entry; } -static int configset_add_value(struct config_set *cs, const char *key, const char *value) +static int configset_add_value(struct config_reader *reader, + struct config_set *cs, const char *key, + const char *value) { struct config_set_element *e; struct string_list_item *si; @@ -2375,12 +2375,12 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha l_item->e = e; l_item->value_index = e->value_list.nr - 1; - if (!cf_global) + if (!reader->source) BUG("configset_add_value has no source"); - if (cf_global->name) { - kv_info->filename = strintern(cf_global->name); - kv_info->linenr = cf_global->linenr; - kv_info->origin_type = cf_global->origin_type; + if (reader->source->name) { + kv_info->filename = strintern(reader->source->name); + kv_info->linenr = reader->source->linenr; + kv_info->origin_type = reader->source->origin_type; } else { /* for values read from `git_config_from_parameters()` */ kv_info->filename = NULL; @@ -2435,16 +2435,25 @@ void git_configset_clear(struct config_set *cs) cs->list.items = NULL; } +struct configset_add_data { + struct config_set *config_set; + struct config_reader *config_reader; +}; +#define CONFIGSET_ADD_INIT { 0 } + static int config_set_callback(const char *key, const char *value, void *cb) { - struct config_set *cs = cb; - configset_add_value(cs, key, value); + struct configset_add_data *data = cb; + configset_add_value(data->config_reader, data->config_set, key, value); return 0; } int git_configset_add_file(struct config_set *cs, const char *filename) { - return git_config_from_file(config_set_callback, filename, cs); + struct configset_add_data data = CONFIGSET_ADD_INIT; + data.config_reader = &the_reader; + data.config_set = cs; + return git_config_from_file(config_set_callback, filename, &data); } int git_configset_get_value(struct config_set *cs, const char *key, const char **value) @@ -2559,6 +2568,7 @@ int git_configset_get_pathname(struct config_set *cs, const char *key, const cha static void repo_read_config(struct repository *repo) { struct config_options opts = { 0 }; + struct configset_add_data data = CONFIGSET_ADD_INIT; opts.respect_includes = 1; opts.commondir = repo->commondir; @@ -2570,8 +2580,10 @@ static void repo_read_config(struct repository *repo) git_configset_clear(repo->config); git_configset_init(repo->config); + data.config_set = repo->config; + data.config_reader = &the_reader; - if (config_with_options(config_set_callback, repo->config, NULL, &opts) < 0) + if (config_with_options(config_set_callback, &data, NULL, &opts) < 0) /* * config_with_options() normally returns only * zero, as most errors are fatal, and @@ -2697,9 +2709,12 @@ static void read_protected_config(void) .ignore_worktree = 1, .system_gently = 1, }; + struct configset_add_data data = CONFIGSET_ADD_INIT; + git_configset_init(&protected_config); - config_with_options(config_set_callback, &protected_config, - NULL, &opts); + data.config_set = &protected_config; + data.config_reader = &the_reader; + config_with_options(config_set_callback, &data, NULL, &opts); } void git_protected_config(config_fn_t fn, void *data) @@ -2884,6 +2899,7 @@ void git_die_config(const char *key, const char *err, ...) */ struct config_store_data { + struct config_reader *config_reader; size_t baselen; char *key; int do_not_match; @@ -2898,6 +2914,7 @@ struct config_store_data { unsigned int parsed_nr, parsed_alloc, *seen, seen_nr, seen_alloc; unsigned int key_seen:1, section_seen:1, is_keys_section:1; }; +#define CONFIG_STORE_INIT { 0 } static void config_store_data_clear(struct config_store_data *store) { @@ -2932,12 +2949,7 @@ static int store_aux_event(enum config_event_t type, size_t begin, size_t end, void *data) { struct config_store_data *store = data; - /* - * FIXME Keep using "cf" so that we can avoid rewrapping a - * really long line below. Remove this when "cf" gets plumbed - * correctly. - */ - struct config_source *cf = cf_global; + struct config_source *cf = store->config_reader->source; ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc); store->parsed[store->parsed_nr].begin = begin; @@ -3255,9 +3267,9 @@ int git_config_set_multivar_in_file_gently(const char *config_filename, char *filename_buf = NULL; char *contents = NULL; size_t contents_sz; - struct config_store_data store; + struct config_store_data store = CONFIG_STORE_INIT; - memset(&store, 0, sizeof(store)); + store.config_reader = &the_reader; /* parse-key returns negative; flip the sign to feed exit(3) */ ret = 0 - git_config_parse_key(key, &store.key, &store.baselen); From patchwork Thu Mar 16 00:11:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13176811 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8378DC7618B for ; Thu, 16 Mar 2023 00:12:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233309AbjCPAMI (ORCPT ); Wed, 15 Mar 2023 20:12:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232817AbjCPAL4 (ORCPT ); Wed, 15 Mar 2023 20:11:56 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF0AE125B0 for ; Wed, 15 Mar 2023 17:11:53 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id y14so12316wrq.4 for ; Wed, 15 Mar 2023 17:11:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678925512; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=fp3kbx1zPVFKfLCjv8tYlCI284ci22MmJwyoModsAuE=; b=OglDEdiQlsbXu0fZTFDupetedJ7stuwVWe8t0kqMgwCMq0/5Wx6BkPdrJhwKdHQaNG bb8LFxg46woPWXTl69jml39a27gSaBEd0lKpJ8LIL5gKuicgT66KYTwR97rtPucRJkHM wMSlSyl5eeNv/SGRGQS5Mm5orc1Nq5+P1jaLrnTsYCe6nMCuaTwfumkKdAcoKik2fNpt 3gPCd2G8x4g1vHy7Nc7rvZZsGiTcWi79qwh9jqwCloJENS4JpcJnf42ITiVdEIzYRgPt FlRZZHleHRcGThMjEnO0eNdDA+5AgJOsPJDb0zvod1RoCE1M4AMGTqcC1JLA3KIBPkeV oSvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678925512; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fp3kbx1zPVFKfLCjv8tYlCI284ci22MmJwyoModsAuE=; b=IM5dV74B+tQD3D2B8rNvD1cNZ7T3iU7YtfTU8hfsBrciZ0NuuD/M4o3BSQ4z5HqjqD dVo2O5vEG+i4/AB1QpjXGncn5XTxpVmRKHL13m3DR+16g2/Fbs4Bbe7FvEVjk7t2FVJa 1mpwNy7LgegY2DNYYEB48r0OyBzchqNrYXNA/gZHAMLJHWpYUbbDEWQH2cEVUstftb++ O9x2xj/srQTb9EcEBl8XXGHzDKIKN1JzzC/wQipKkSLdFhztSes5VFECsF1O9TDIP00u qiwmkAnYUskwexU8jiw/oCRC1+ni7kkcm+e1Nw/dQ7FCjpZb3z/TzMeD/OJlKrzy3Mx8 hVXQ== X-Gm-Message-State: AO0yUKUzkl1iIm7vXiTC5dSWdW6Nx8g6Szh6LmzXX/hwUWjpZOPlXwhz CcpBgwXn5xy0XIlTOeHr0Ousmr+0aMg= X-Google-Smtp-Source: AK7set+7mEkLX80xzPzI8hruKmc5Px9IbI9bzB/Tz1xtMEqAMHLBqARgOlDRYprxQwdYcGeXFwWK/Q== X-Received: by 2002:adf:fd46:0:b0:2cf:e054:65f1 with SMTP id h6-20020adffd46000000b002cfe05465f1mr3236467wrs.48.1678925512112; Wed, 15 Mar 2023 17:11:52 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u3-20020a5d5143000000b002c70c99db74sm5757718wrt.86.2023.03.15.17.11.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 17:11:51 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Thu, 16 Mar 2023 00:11:43 +0000 Subject: [PATCH v2 5/8] config.c: remove current_config_kvi Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jonathan Tan , Emily Shaffer , Jeff King , Derrick Stolee , Calvin Wan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Glen Choo , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo From: Glen Choo Add ".config_kvi" to "struct config_reader" and replace "current_config_kvi" with "the_reader.config_kvi", plumbing "struct config_reader" where necesssary. Also, introduce a setter function for ".config_kvi", which allows us to enforce the contraint that only one of ".source" and ".config_kvi" can be set at a time (as documented in the comments). Because of this constraint, we know that "populate_remote_urls()" was never touching "current_config_kvi" when iterating through config files, so it doesn't need to store and restore that value. Signed-off-by: Glen Choo --- config.c | 82 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/config.c b/config.c index b75cde42ca6..ba9b488c40d 100644 --- a/config.c +++ b/config.c @@ -52,7 +52,24 @@ struct config_source { #define CONFIG_SOURCE_INIT { 0 } struct config_reader { + /* + * These members record the "current" config source, which can be + * accessed by parsing callbacks. + * + * The "source" variable will be non-NULL only when we are actually + * parsing a real config source (file, blob, cmdline, etc). + * + * The "config_kvi" variable will be non-NULL only when we are feeding + * cached config from a configset into a callback. + * + * They cannot be non-NULL at the same time. If they are both NULL, then + * we aren't parsing anything (and depending on the function looking at + * the variables, it's either a bug for it to be called in the first + * place, or it's a function which can be reused for non-config + * purposes, and should fall back to some sane behavior). + */ struct config_source *source; + struct key_value_info *config_kvi; }; /* * Where possible, prefer to accept "struct config_reader" as an arg than to use @@ -61,27 +78,6 @@ struct config_reader { */ static struct config_reader the_reader; -/* - * FIXME The comments are temporarily out of date since "cf_global" has been - * moved to the_reader, but not current_*. - * - * These variables record the "current" config source, which - * can be accessed by parsing callbacks. - * - * The "cf_global" variable will be non-NULL only when we are actually - * parsing a real config source (file, blob, cmdline, etc). - * - * The "current_config_kvi" variable will be non-NULL only when we are feeding - * cached config from a configset into a callback. - * - * They should generally never be non-NULL at the same time. If they are both - * NULL, then we aren't parsing anything (and depending on the function looking - * at the variables, it's either a bug for it to be called in the first place, - * or it's a function which can be reused for non-config purposes, and should - * fall back to some sane behavior). - */ -static struct key_value_info *current_config_kvi; - /* * Similar to the variables above, this gives access to the "scope" of the * current value (repo, global, etc). For cached values, it can be found via @@ -95,6 +91,8 @@ static enum config_scope current_parsing_scope; static inline void config_reader_push_source(struct config_reader *reader, struct config_source *top) { + if (reader->config_kvi) + BUG("source should not be set while iterating a config set"); if (reader->source) top->prev = reader->source; reader->source = top; @@ -110,6 +108,14 @@ static inline struct config_source *config_reader_pop_source(struct config_reade return ret; } +static inline void config_reader_set_kvi(struct config_reader *reader, + struct key_value_info *kvi) +{ + if (kvi && reader->source) + BUG("kvi should not be set while parsing a config source"); + reader->config_kvi = kvi; +} + static int pack_compression_seen; static int zlib_compression_seen; @@ -378,20 +384,17 @@ static void populate_remote_urls(struct config_include_data *inc) { struct config_options opts; - struct key_value_info *store_kvi = current_config_kvi; enum config_scope store_scope = current_parsing_scope; opts = *inc->opts; opts.unconditional_remote_url = 1; - current_config_kvi = NULL; current_parsing_scope = 0; inc->remote_urls = xmalloc(sizeof(*inc->remote_urls)); string_list_init_dup(inc->remote_urls); config_with_options(add_remote_url, inc->remote_urls, inc->config_source, &opts); - current_config_kvi = store_kvi; current_parsing_scope = store_scope; } @@ -2258,7 +2261,8 @@ int config_with_options(config_fn_t fn, void *data, return ret; } -static void configset_iter(struct config_set *cs, config_fn_t fn, void *data) +static void configset_iter(struct config_reader *reader, struct config_set *cs, + config_fn_t fn, void *data) { int i, value_index; struct string_list *values; @@ -2270,14 +2274,14 @@ static void configset_iter(struct config_set *cs, config_fn_t fn, void *data) value_index = list->items[i].value_index; values = &entry->value_list; - current_config_kvi = values->items[value_index].util; + config_reader_set_kvi(reader, values->items[value_index].util); if (fn(entry->key, values->items[value_index].string, data) < 0) git_die_config_linenr(entry->key, - current_config_kvi->filename, - current_config_kvi->linenr); + reader->config_kvi->filename, + reader->config_kvi->linenr); - current_config_kvi = NULL; + config_reader_set_kvi(reader, NULL); } } @@ -2615,7 +2619,7 @@ static void repo_config_clear(struct repository *repo) void repo_config(struct repository *repo, config_fn_t fn, void *data) { git_config_check_init(repo); - configset_iter(repo->config, fn, data); + configset_iter(&the_reader, repo->config, fn, data); } int repo_config_get_value(struct repository *repo, @@ -2721,7 +2725,7 @@ void git_protected_config(config_fn_t fn, void *data) { if (!protected_config.hash_initialized) read_protected_config(); - configset_iter(&protected_config, fn, data); + configset_iter(&the_reader, &protected_config, fn, data); } /* Functions used historically to read configuration from 'the_repository' */ @@ -3828,8 +3832,8 @@ int parse_config_key(const char *var, const char *current_config_origin_type(void) { int type; - if (current_config_kvi) - type = current_config_kvi->origin_type; + if (the_reader.config_kvi) + type = the_reader.config_kvi->origin_type; else if(the_reader.source) type = the_reader.source->origin_type; else @@ -3874,8 +3878,8 @@ const char *config_scope_name(enum config_scope scope) const char *current_config_name(void) { const char *name; - if (current_config_kvi) - name = current_config_kvi->filename; + if (the_reader.config_kvi) + name = the_reader.config_kvi->filename; else if (the_reader.source) name = the_reader.source->name; else @@ -3885,16 +3889,16 @@ const char *current_config_name(void) enum config_scope current_config_scope(void) { - if (current_config_kvi) - return current_config_kvi->scope; + if (the_reader.config_kvi) + return the_reader.config_kvi->scope; else return current_parsing_scope; } int current_config_line(void) { - if (current_config_kvi) - return current_config_kvi->linenr; + if (the_reader.config_kvi) + return the_reader.config_kvi->linenr; else return the_reader.source->linenr; } From patchwork Thu Mar 16 00:11:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13176810 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06A9FC61DA4 for ; Thu, 16 Mar 2023 00:12:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233245AbjCPAMF (ORCPT ); Wed, 15 Mar 2023 20:12:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233150AbjCPAL4 (ORCPT ); Wed, 15 Mar 2023 20:11:56 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FD10457C6 for ; Wed, 15 Mar 2023 17:11:54 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id p13-20020a05600c358d00b003ed346d4522so2145118wmq.2 for ; Wed, 15 Mar 2023 17:11:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678925513; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=AdzRS1XS33PEiHhPq4gVOpp+2WFYweajk7oVe8gNc/0=; b=baHii8b/A54tlym9ECWOSmmjz/yrje3HYIdtXHXei/Nl/kQrmPPh96l7wQ2BIQDhw6 U7vj0xpnpmHpz7YTPjTBFJuKNTsYlPO7e4CUoC50SI4pwruurjPQbz0y2s3Lrl62Qycy tO8mvJAGzQ0oTpjZiGeuztufjMOj+7lwsIuYWqWcE6wbBQLJ5f7JYPpN5QiMeBs0gewO 4Odpur87+jZkF6Dnbw/SCyCsX+C5eLTPyPur6x4dm08d+bRRjxOuZr8zhwy2CtfEd+9y BVw+LliTqx/cnHpk8oOBH1xEkGLNRmCHnVCYOZ3nUdddACkerecYfmaIe9vHL0hvT9mx uVLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678925513; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AdzRS1XS33PEiHhPq4gVOpp+2WFYweajk7oVe8gNc/0=; b=WTv3l2T9bYuSk6sLr9ezsUvkJQxI9DKPdW3CpSa1Xygew/kv0PFJr3amAv/7pEJPrv BSbcPoMxf5U2RAPgLjTuch+cqgM+iAYOO2ptW6Jjq6p//bvWnyklBVV0qsBrVBOWYKsp QDcQBo586mvZ9QZfH8U5uFfHeBfYcfV/yfB/ISrQdODyw/4Sq7466nZU0tqJ2ybWSPtm s1uX6p4sTvFkYTMXrQ7rX81yR5VRqoQ66Y96WNOtbun3M19J//1ZAkzCaee7LMQHwRuD Ln9xF4gsq2JumfL2uiE/YzyQ3vla04WVerXn7ZmDuYBJUaQv6qDe/DO9/kKkB4Y73erd 5oYQ== X-Gm-Message-State: AO0yUKVNMY66aTAEOfFxCbwkXZu7hdyZe90fOcsGrJh56mbd5+pjr+ua BiLqgRnOkQddBhi5bZb7vjUGSCOpLzg= X-Google-Smtp-Source: AK7set/uNIN1xdQnIES6uoIkMe4DMDw54JbHiBDISJ+6pykZse19F8iIvDO/jyLwQzyn0owEqRdvmQ== X-Received: by 2002:a05:600c:458f:b0:3eb:38e6:f650 with SMTP id r15-20020a05600c458f00b003eb38e6f650mr18352122wmo.41.1678925512741; Wed, 15 Mar 2023 17:11:52 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d22-20020a05600c34d600b003e20970175dsm3260146wmq.32.2023.03.15.17.11.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 17:11:52 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Thu, 16 Mar 2023 00:11:44 +0000 Subject: [PATCH v2 6/8] config.c: remove current_parsing_scope Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jonathan Tan , Emily Shaffer , Jeff King , Derrick Stolee , Calvin Wan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Glen Choo , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo From: Glen Choo Add ".parsing_scope" to "struct config_reader" and replace "current_parsing_scope" with "the_reader.parsing_scope. Adjust the comment slightly to make it clearer that the scope applies to the config source (not the current value), and should only be set when parsing a config source. As such, ".parsing_scope" (only set when parsing config sources) and ".config_kvi" (only set when iterating a config set) should not be set together, so enforce this with a setter function. Unlike previous commits, "populate_remote_urls()" still needs to store and restore the 'scope' value because it could have touched "current_parsing_scope" ("config_with_options()" can set the scope). Signed-off-by: Glen Choo --- config.c | 63 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/config.c b/config.c index ba9b488c40d..460326ae21e 100644 --- a/config.c +++ b/config.c @@ -70,6 +70,16 @@ struct config_reader { */ struct config_source *source; struct key_value_info *config_kvi; + /* + * The "scope" of the current config source being parsed (repo, global, + * etc). Like "source", this is only set when parsing a config source. + * It's not part of "source" because it transcends a single file (i.e., + * a file included from .git/config is still in "repo" scope). + * + * When iterating through a configset, the equivalent value is + * "config_kvi.scope" (see above). + */ + enum config_scope parsing_scope; }; /* * Where possible, prefer to accept "struct config_reader" as an arg than to use @@ -78,16 +88,6 @@ struct config_reader { */ static struct config_reader the_reader; -/* - * Similar to the variables above, this gives access to the "scope" of the - * current value (repo, global, etc). For cached values, it can be found via - * the current_config_kvi as above. During parsing, the current value can be - * found in this variable. It's not part of "cf_global" because it transcends a - * single file (i.e., a file included from .git/config is still in "repo" - * scope). - */ -static enum config_scope current_parsing_scope; - static inline void config_reader_push_source(struct config_reader *reader, struct config_source *top) { @@ -111,11 +111,19 @@ static inline struct config_source *config_reader_pop_source(struct config_reade static inline void config_reader_set_kvi(struct config_reader *reader, struct key_value_info *kvi) { - if (kvi && reader->source) + if (kvi && (reader->source || reader->parsing_scope)) BUG("kvi should not be set while parsing a config source"); reader->config_kvi = kvi; } +static inline void config_reader_set_scope(struct config_reader *reader, + enum config_scope scope) +{ + if (scope && reader->config_kvi) + BUG("scope should only be set when iterating through a config source"); + reader->parsing_scope = scope; +} + static int pack_compression_seen; static int zlib_compression_seen; @@ -384,18 +392,18 @@ static void populate_remote_urls(struct config_include_data *inc) { struct config_options opts; - enum config_scope store_scope = current_parsing_scope; + enum config_scope store_scope = inc->config_reader->parsing_scope; opts = *inc->opts; opts.unconditional_remote_url = 1; - current_parsing_scope = 0; + config_reader_set_scope(inc->config_reader, 0); inc->remote_urls = xmalloc(sizeof(*inc->remote_urls)); string_list_init_dup(inc->remote_urls); config_with_options(add_remote_url, inc->remote_urls, inc->config_source, &opts); - current_parsing_scope = store_scope; + config_reader_set_scope(inc->config_reader, store_scope); } static int forbid_remote_url(const char *var, const char *value UNUSED, @@ -2160,7 +2168,8 @@ int git_config_system(void) return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); } -static int do_git_config_sequence(const struct config_options *opts, +static int do_git_config_sequence(struct config_reader *reader, + const struct config_options *opts, config_fn_t fn, void *data) { int ret = 0; @@ -2168,7 +2177,7 @@ static int do_git_config_sequence(const struct config_options *opts, char *xdg_config = NULL; char *user_config = NULL; char *repo_config; - enum config_scope prev_parsing_scope = current_parsing_scope; + enum config_scope prev_parsing_scope = reader->parsing_scope; if (opts->commondir) repo_config = mkpathdup("%s/config", opts->commondir); @@ -2177,13 +2186,13 @@ static int do_git_config_sequence(const struct config_options *opts, else repo_config = NULL; - current_parsing_scope = CONFIG_SCOPE_SYSTEM; + config_reader_set_scope(reader, CONFIG_SCOPE_SYSTEM); if (git_config_system() && system_config && !access_or_die(system_config, R_OK, opts->system_gently ? ACCESS_EACCES_OK : 0)) ret += git_config_from_file(fn, system_config, data); - current_parsing_scope = CONFIG_SCOPE_GLOBAL; + config_reader_set_scope(reader, CONFIG_SCOPE_GLOBAL); git_global_config(&user_config, &xdg_config); if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) @@ -2192,12 +2201,12 @@ static int do_git_config_sequence(const struct config_options *opts, if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) ret += git_config_from_file(fn, user_config, data); - current_parsing_scope = CONFIG_SCOPE_LOCAL; + config_reader_set_scope(reader, CONFIG_SCOPE_LOCAL); if (!opts->ignore_repo && repo_config && !access_or_die(repo_config, R_OK, 0)) ret += git_config_from_file(fn, repo_config, data); - current_parsing_scope = CONFIG_SCOPE_WORKTREE; + config_reader_set_scope(reader, CONFIG_SCOPE_WORKTREE); if (!opts->ignore_worktree && repository_format_worktree_config) { char *path = git_pathdup("config.worktree"); if (!access_or_die(path, R_OK, 0)) @@ -2205,11 +2214,11 @@ static int do_git_config_sequence(const struct config_options *opts, free(path); } - current_parsing_scope = CONFIG_SCOPE_COMMAND; + config_reader_set_scope(reader, CONFIG_SCOPE_COMMAND); if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0) die(_("unable to parse command-line config")); - current_parsing_scope = prev_parsing_scope; + config_reader_set_scope(reader, prev_parsing_scope); free(system_config); free(xdg_config); free(user_config); @@ -2222,6 +2231,7 @@ int config_with_options(config_fn_t fn, void *data, const struct config_options *opts) { struct config_include_data inc = CONFIG_INCLUDE_INIT; + enum config_scope prev_scope = the_reader.parsing_scope; int ret; if (opts->respect_includes) { @@ -2235,7 +2245,7 @@ int config_with_options(config_fn_t fn, void *data, } if (config_source) - current_parsing_scope = config_source->scope; + config_reader_set_scope(&the_reader, config_source->scope); /* * If we have a specific filename, use it. Otherwise, follow the @@ -2251,13 +2261,14 @@ int config_with_options(config_fn_t fn, void *data, ret = git_config_from_blob_ref(fn, repo, config_source->blob, data); } else { - ret = do_git_config_sequence(opts, fn, data); + ret = do_git_config_sequence(&the_reader, opts, fn, data); } if (inc.remote_urls) { string_list_clear(inc.remote_urls, 0); FREE_AND_NULL(inc.remote_urls); } + config_reader_set_scope(&the_reader, prev_scope); return ret; } @@ -2391,7 +2402,7 @@ static int configset_add_value(struct config_reader *reader, kv_info->linenr = -1; kv_info->origin_type = CONFIG_ORIGIN_CMDLINE; } - kv_info->scope = current_parsing_scope; + kv_info->scope = reader->parsing_scope; si->util = kv_info; return 0; @@ -3892,7 +3903,7 @@ enum config_scope current_config_scope(void) if (the_reader.config_kvi) return the_reader.config_kvi->scope; else - return current_parsing_scope; + return the_reader.parsing_scope; } int current_config_line(void) From patchwork Thu Mar 16 00:11:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13176813 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC2E2C6FD1D for ; Thu, 16 Mar 2023 00:12:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233389AbjCPAMX (ORCPT ); Wed, 15 Mar 2023 20:12:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233214AbjCPAL6 (ORCPT ); Wed, 15 Mar 2023 20:11:58 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 425D31814C for ; Wed, 15 Mar 2023 17:11:55 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id k25-20020a7bc419000000b003ed23114fa7so2075559wmi.4 for ; Wed, 15 Mar 2023 17:11:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678925513; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=ELkzqHrf8h2ogFJaHj89QDibYfXtL4UFLw4FEHzPrx8=; b=lyr1HpAwFI1CAwbUVc4UDt7hya82P4OcpOba041GCm/Z907Ri0q20HkXt8JMftigXd ZORLpmea7+niIk89CVMuk3YwcWiyJCau62/gjKx92KYYiJ35Xl/MgL/bkEdsBemEXrDS QnOZn2IESt14ar6+xstBFPHVEv20KeFohnd0nD9O23ztGh3LUlEM3UbFiDy+D3qZHFRc xo3acnCISqeoUodjiHmmoKqhCu2DT/xR3H+vHldXoSHDzUbwc4p3NWAOYwVHeEN2585j DnNk0QmHlHtdiKL3jyBffgA2INFkZSRtDiWA9b3Nes/mH+kHXV4y9JypvW3BjOZqWpRv X3Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678925513; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ELkzqHrf8h2ogFJaHj89QDibYfXtL4UFLw4FEHzPrx8=; b=tbSwfXtuJyA8rcRLR1d8lzb+jYkv3fPzzmZnp6sQi1S4OYceyJY1zH/tA34beoxXXF qQY8Wo1iiTNhHQbFpQJsNSrr/rv4mdsJZ3Ty+b4wM8ciPM/4OTIHO6JJ4jnZxdoZ8d+m ZS+1epaIEQrL5HhZVxKwwFRM6UF0L3wf2Webwxhoise+sK+skmP9L+O6l4JYR4agL/S9 TR/g/yTbF6U/i0QLF2smn39mS0wOhvkCOqAR7S0uPmj5jZ+hLwUBdsWKZcYtjosFLX58 DvtdGRe2tAjM7mpv3KoD+HOBiyjkrLD0u5GO7Du04l3yC3y3+P3DZMAK+fLQZj9veN8f 1Iig== X-Gm-Message-State: AO0yUKWQt5bkH+qMN0jnp+Dj1B/ghX/OC/dtkwilkeq2SSaj+IR9Qxqh i11dq2IFBvbqf2nmFDvd601pJ5U0fLY= X-Google-Smtp-Source: AK7set9TdS9zGRy0ZuLUgVVL1Oh357aO2rADWw8Sy1soMOse3iUuSbJEkBDqVIp8+Y60Bseym4C0lA== X-Received: by 2002:a05:600c:c10:b0:3eb:248f:a13e with SMTP id fm16-20020a05600c0c1000b003eb248fa13emr20620719wmb.22.1678925513580; Wed, 15 Mar 2023 17:11:53 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n13-20020a05600c3b8d00b003e8dcc67bdesm3401519wms.30.2023.03.15.17.11.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 17:11:53 -0700 (PDT) Message-Id: <3c83d9535a037653c7de2d462a4df3a3c43a9308.1678925506.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 16 Mar 2023 00:11:45 +0000 Subject: [PATCH v2 7/8] config: report cached filenames in die_bad_number() Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jonathan Tan , Emily Shaffer , Jeff King , Derrick Stolee , Calvin Wan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Glen Choo , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo From: Glen Choo If, when parsing numbers from config, die_bad_number() is called, it reports the filename and config source type if we were parsing a config file, but not if we were iterating a config_set (it defaults to a less specific error message). Most call sites don't parse config files because config is typically read once and cached, so we only report filename and config source type in "git config --type" (since "git config" always parses config files). This could have been fixed when we taught the current_config_* functions to respect config_set values (0d44a2dacc (config: return configset value for current_config_ functions, 2016-05-26), but it was hard to spot then and we might have just missed it (I didn't find mention of die_bad_number() in the original ML discussion [1].) Fix this by refactoring the current_config_* functions into variants that don't BUG() when we aren't reading config, and using the resulting functions in die_bad_number(). Refactoring is needed because "git config --get[-regexp] --type=int" parses the int value _after_ parsing the config file, which will run into the BUG(). Also, plumb "struct config_reader" into the new functions. This isn't necessary per se, but this generalizes better, so it might help us avoid yet another refactor. 1. https://lore.kernel.org/git/20160518223712.GA18317@sigill.intra.peff.net/ Signed-off-by: Glen Choo --- config.c | 65 +++++++++++++++++++++++++++++------------- config.h | 1 + t/helper/test-config.c | 17 +++++++++++ t/t1308-config-set.sh | 9 ++++++ 4 files changed, 72 insertions(+), 20 deletions(-) diff --git a/config.c b/config.c index 460326ae21e..da5f6381cde 100644 --- a/config.c +++ b/config.c @@ -1312,39 +1312,48 @@ int git_parse_ssize_t(const char *value, ssize_t *ret) return 1; } +static int reader_config_name(struct config_reader *reader, const char **out); +static int reader_origin_type(struct config_reader *reader, + enum config_origin_type *type); NORETURN -static void die_bad_number(struct config_source *cf, const char *name, +static void die_bad_number(struct config_reader *reader, const char *name, const char *value) { const char *error_type = (errno == ERANGE) ? N_("out of range") : N_("invalid unit"); const char *bad_numeric = N_("bad numeric config value '%s' for '%s': %s"); + const char *config_name = NULL; + enum config_origin_type config_origin = CONFIG_ORIGIN_UNKNOWN; if (!value) value = ""; - if (!(cf && cf->name)) + /* Ignoring the return value is okay since we handle missing values. */ + reader_config_name(reader, &config_name); + reader_origin_type(reader, &config_origin); + + if (!config_name) die(_(bad_numeric), value, name, _(error_type)); - switch (cf->origin_type) { + switch (config_origin) { case CONFIG_ORIGIN_BLOB: die(_("bad numeric config value '%s' for '%s' in blob %s: %s"), - value, name, cf->name, _(error_type)); + value, name, config_name, _(error_type)); case CONFIG_ORIGIN_FILE: die(_("bad numeric config value '%s' for '%s' in file %s: %s"), - value, name, cf->name, _(error_type)); + value, name, config_name, _(error_type)); case CONFIG_ORIGIN_STDIN: die(_("bad numeric config value '%s' for '%s' in standard input: %s"), value, name, _(error_type)); case CONFIG_ORIGIN_SUBMODULE_BLOB: die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"), - value, name, cf->name, _(error_type)); + value, name, config_name, _(error_type)); case CONFIG_ORIGIN_CMDLINE: die(_("bad numeric config value '%s' for '%s' in command line %s: %s"), - value, name, cf->name, _(error_type)); + value, name, config_name, _(error_type)); default: die(_("bad numeric config value '%s' for '%s' in %s: %s"), - value, name, cf->name, _(error_type)); + value, name, config_name, _(error_type)); } } @@ -1352,7 +1361,7 @@ int git_config_int(const char *name, const char *value) { int ret; if (!git_parse_int(value, &ret)) - die_bad_number(the_reader.source, name, value); + die_bad_number(&the_reader, name, value); return ret; } @@ -1360,7 +1369,7 @@ int64_t git_config_int64(const char *name, const char *value) { int64_t ret; if (!git_parse_int64(value, &ret)) - die_bad_number(the_reader.source, name, value); + die_bad_number(&the_reader, name, value); return ret; } @@ -1368,7 +1377,7 @@ unsigned long git_config_ulong(const char *name, const char *value) { unsigned long ret; if (!git_parse_ulong(value, &ret)) - die_bad_number(the_reader.source, name, value); + die_bad_number(&the_reader, name, value); return ret; } @@ -1376,7 +1385,7 @@ ssize_t git_config_ssize_t(const char *name, const char *value) { ssize_t ret; if (!git_parse_ssize_t(value, &ret)) - die_bad_number(the_reader.source, name, value); + die_bad_number(&the_reader, name, value); return ret; } @@ -3840,14 +3849,23 @@ int parse_config_key(const char *var, return 0; } -const char *current_config_origin_type(void) +static int reader_origin_type(struct config_reader *reader, + enum config_origin_type *type) { - int type; if (the_reader.config_kvi) - type = the_reader.config_kvi->origin_type; + *type = reader->config_kvi->origin_type; else if(the_reader.source) - type = the_reader.source->origin_type; + *type = reader->source->origin_type; else + return 1; + return 0; +} + +const char *current_config_origin_type(void) +{ + enum config_origin_type type = CONFIG_ORIGIN_UNKNOWN; + + if (reader_origin_type(&the_reader, &type)) BUG("current_config_origin_type called outside config callback"); switch (type) { @@ -3886,14 +3904,21 @@ const char *config_scope_name(enum config_scope scope) } } -const char *current_config_name(void) +static int reader_config_name(struct config_reader *reader, const char **out) { - const char *name; if (the_reader.config_kvi) - name = the_reader.config_kvi->filename; + *out = reader->config_kvi->filename; else if (the_reader.source) - name = the_reader.source->name; + *out = reader->source->name; else + return 1; + return 0; +} + +const char *current_config_name(void) +{ + const char *name; + if (reader_config_name(&the_reader, &name)) BUG("current_config_name called outside config callback"); return name ? name : ""; } diff --git a/config.h b/config.h index 7606246531a..66c8b996e15 100644 --- a/config.h +++ b/config.h @@ -56,6 +56,7 @@ struct git_config_source { }; enum config_origin_type { + CONFIG_ORIGIN_UNKNOWN = 0, CONFIG_ORIGIN_BLOB, CONFIG_ORIGIN_FILE, CONFIG_ORIGIN_STDIN, diff --git a/t/helper/test-config.c b/t/helper/test-config.c index 4ba9eb65606..26e79168f6a 100644 --- a/t/helper/test-config.c +++ b/t/helper/test-config.c @@ -30,6 +30,9 @@ * iterate -> iterate over all values using git_config(), and print some * data for each * + * git_config_int -> iterate over all values using git_config() and print the + * integer value for the entered key or die + * * Examples: * * To print the value with highest priority for key "foo.bAr Baz.rock": @@ -54,6 +57,17 @@ static int iterate_cb(const char *var, const char *value, void *data UNUSED) return 0; } +static int parse_int_cb(const char *var, const char *value, void *data) +{ + const char *key_to_match = data; + + if (!strcmp(key_to_match, var)) { + int parsed = git_config_int(value, value); + printf("%d\n", parsed); + } + return 0; +} + static int early_config_cb(const char *var, const char *value, void *vdata) { const char *key = vdata; @@ -176,6 +190,9 @@ int cmd__config(int argc, const char **argv) } else if (!strcmp(argv[1], "iterate")) { git_config(iterate_cb, NULL); goto exit0; + } else if (argc == 3 && !strcmp(argv[1], "git_config_int")) { + git_config(parse_int_cb, (void *) argv[2]); + goto exit0; } die("%s: Please check the syntax and the function name", argv[0]); diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh index b38e158d3b2..9733bed30a9 100755 --- a/t/t1308-config-set.sh +++ b/t/t1308-config-set.sh @@ -120,6 +120,10 @@ test_expect_success 'find integer value for a key' ' check_config get_int lamb.chop 65 ' +test_expect_success 'parse integer value during iteration' ' + check_config git_config_int lamb.chop 65 +' + test_expect_success 'find string value for a key' ' check_config get_string case.baz hask && check_config expect_code 1 get_string case.ba "Value not found for \"case.ba\"" @@ -134,6 +138,11 @@ test_expect_success 'find integer if value is non parse-able' ' check_config expect_code 128 get_int lamb.head ' +test_expect_success 'non parse-able integer value during iteration' ' + check_config expect_code 128 git_config_int lamb.head 2>result && + grep "fatal: bad numeric config value .* in file \.git/config" result +' + test_expect_success 'find bool value for the entered key' ' check_config get_bool goat.head 1 && check_config get_bool goat.skin 0 && From patchwork Thu Mar 16 00:11:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13176814 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2750EC61DA4 for ; Thu, 16 Mar 2023 00:12:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232603AbjCPAMZ (ORCPT ); Wed, 15 Mar 2023 20:12:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232803AbjCPAL7 (ORCPT ); Wed, 15 Mar 2023 20:11:59 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 262713B857 for ; Wed, 15 Mar 2023 17:11:56 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id h17so2636wrt.8 for ; Wed, 15 Mar 2023 17:11:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678925515; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=LNfa/jSz1Cq3sVSjiRG3K0q5jCPI/5/KClwFaonLgTQ=; b=hdQp9lstkedhc7HyHavEixm9Q9lKG4tUn6x0dsoZBtUKjmoyex98BC42yJQ7tTAoSm YbgbWznCWCNy1J9WJdi4djj7nnwJ2d8sqF2O72CSnIB+xYdVS3F7F5XOXtQXQajsP0eQ nn7IDeTysqrRZ0CZTJSjsnhd+qhkq9q2Pgg4OOSWNuleQV0qYbtGgXF3WpCUJna+li6z a52ALxWH18iVl8IAlUgqVsUHrTaUCeR1QHxxGNcMqJ69PUzc1JMYHHXt3qVsV49Iq1MX Wa2Ap44pHc/yZLZWM4sLtzNndhs5LjvHzHUDRycrRz5FZoKXwqfJ7exg/PxsSSWh1QhF cIhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678925515; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LNfa/jSz1Cq3sVSjiRG3K0q5jCPI/5/KClwFaonLgTQ=; b=eVvzBRBlgX0fs+A+WxD1zfV7S8ewgKa+Bk1PbVQ3M1nUmwDt7733NtJQ561xPGw1a0 QHiAt4K4SeOSxNu6yR++9ceN256xrCFLdF6tP1dEEuWnCr/1XR0Q4c3Si2bDOb7LKb+2 Iy9hf75uNoX09QAP8hxiC6OM/GDBewa8qEk0EFzAK4oF13mXXjN2flnG77xgtDfm6t3x 6tX0G81ps0jdoexXRS+4x65+5RgnLI8orkXC56IN6ZDeudvXaEprG3HDT+8JPlE8+F8a DgpeyBP6+FuE/Kfaq0+aArUyDNwFeMD8IWFo70CHlWMALhXvHGhRlzh1q9wfUWCzyMlW 16QA== X-Gm-Message-State: AO0yUKUR04BlVNtZz105VR10hRnKRl6fN2LvAg/J4Ppe8A8NGh0/ZLlK IE1TLKBMfolHuT4NdAPqKU7WoiRI47Q= X-Google-Smtp-Source: AK7set8XNz7RcmAPt7XgKXYGP2HM4qLFh9SnNV+/xej8mj6Up626yf5i9E5PGZmdpgC/Hs08pckc9w== X-Received: by 2002:a5d:6142:0:b0:2bf:d937:3589 with SMTP id y2-20020a5d6142000000b002bfd9373589mr3633584wrt.14.1678925514238; Wed, 15 Mar 2023 17:11:54 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c18-20020a5d4cd2000000b002ce9f0e4a8fsm5865088wrt.84.2023.03.15.17.11.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 17:11:54 -0700 (PDT) Message-Id: <9aec9092fdf0f792492850c8eb4a82ae47a491c1.1678925506.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 16 Mar 2023 00:11:46 +0000 Subject: [PATCH v2 8/8] config.c: rename "struct config_source cf" Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jonathan Tan , Emily Shaffer , Jeff King , Derrick Stolee , Calvin Wan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Glen Choo , Glen Choo Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Glen Choo From: Glen Choo The "cf" name is a holdover from before 4d8dd1494e (config: make parsing stack struct independent from actual data source, 2013-07-12), when the struct was named config_file. Since that acronym no longer makes sense, rename "cf" to "cs". In some places, we have "struct config_set cs", so to avoid conflict, rename those "cs" to "set" ("config_set" would be more descriptive, but it's much longer and would require us to rewrap several lines). Signed-off-by: Glen Choo --- config.c | 262 +++++++++++++++++++++++++++---------------------------- 1 file changed, 131 insertions(+), 131 deletions(-) diff --git a/config.c b/config.c index da5f6381cde..ae171ab11c3 100644 --- a/config.c +++ b/config.c @@ -203,7 +203,7 @@ static const char include_depth_advice[] = N_( "from\n" " %s\n" "This might be due to circular includes."); -static int handle_path_include(struct config_source *cf, const char *path, +static int handle_path_include(struct config_source *cs, const char *path, struct config_include_data *inc) { int ret = 0; @@ -225,14 +225,14 @@ static int handle_path_include(struct config_source *cf, const char *path, if (!is_absolute_path(path)) { char *slash; - if (!cf || !cf->path) { + if (!cs || !cs->path) { ret = error(_("relative config includes must come from files")); goto cleanup; } - slash = find_last_dir_sep(cf->path); + slash = find_last_dir_sep(cs->path); if (slash) - strbuf_add(&buf, cf->path, slash - cf->path + 1); + strbuf_add(&buf, cs->path, slash - cs->path + 1); strbuf_addstr(&buf, path); path = buf.buf; } @@ -240,8 +240,8 @@ static int handle_path_include(struct config_source *cf, const char *path, if (!access_or_die(path, R_OK, 0)) { if (++inc->depth > MAX_INCLUDE_DEPTH) die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path, - !cf ? "" : - cf->name ? cf->name : + !cs ? "" : + cs->name ? cs->name : "the command line"); ret = git_config_from_file(git_config_include, path, inc); inc->depth--; @@ -258,7 +258,7 @@ static void add_trailing_starstar_for_dir(struct strbuf *pat) strbuf_addstr(pat, "**"); } -static int prepare_include_condition_pattern(struct config_source *cf, +static int prepare_include_condition_pattern(struct config_source *cs, struct strbuf *pat) { struct strbuf path = STRBUF_INIT; @@ -275,11 +275,11 @@ static int prepare_include_condition_pattern(struct config_source *cf, if (pat->buf[0] == '.' && is_dir_sep(pat->buf[1])) { const char *slash; - if (!cf || !cf->path) + if (!cs || !cs->path) return error(_("relative config include " "conditionals must come from files")); - strbuf_realpath(&path, cf->path, 1); + strbuf_realpath(&path, cs->path, 1); slash = find_last_dir_sep(path.buf); if (!slash) BUG("how is this possible?"); @@ -294,7 +294,7 @@ static int prepare_include_condition_pattern(struct config_source *cf, return prefix; } -static int include_by_gitdir(struct config_source *cf, +static int include_by_gitdir(struct config_source *cs, const struct config_options *opts, const char *cond, size_t cond_len, int icase) { @@ -311,7 +311,7 @@ static int include_by_gitdir(struct config_source *cf, strbuf_realpath(&text, git_dir, 1); strbuf_add(&pattern, cond, cond_len); - prefix = prepare_include_condition_pattern(cf, &pattern); + prefix = prepare_include_condition_pattern(cs, &pattern); again: if (prefix < 0) @@ -450,16 +450,16 @@ static int include_by_remote_url(struct config_include_data *inc, inc->remote_urls); } -static int include_condition_is_true(struct config_source *cf, +static int include_condition_is_true(struct config_source *cs, struct config_include_data *inc, const char *cond, size_t cond_len) { const struct config_options *opts = inc->opts; if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len)) - return include_by_gitdir(cf, opts, cond, cond_len, 0); + return include_by_gitdir(cs, opts, cond, cond_len, 0); else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len)) - return include_by_gitdir(cf, opts, cond, cond_len, 1); + return include_by_gitdir(cs, opts, cond, cond_len, 1); else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len)) return include_by_branch(cond, cond_len); else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond, @@ -473,7 +473,7 @@ static int include_condition_is_true(struct config_source *cf, static int git_config_include(const char *var, const char *value, void *data) { struct config_include_data *inc = data; - struct config_source *cf = inc->config_reader->source; + struct config_source *cs = inc->config_reader->source; const char *cond, *key; size_t cond_len; int ret; @@ -487,16 +487,16 @@ static int git_config_include(const char *var, const char *value, void *data) return ret; if (!strcmp(var, "include.path")) - ret = handle_path_include(cf, value, inc); + ret = handle_path_include(cs, value, inc); if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) && - cond && include_condition_is_true(cf, inc, cond, cond_len) && + cond && include_condition_is_true(cs, inc, cond, cond_len) && !strcmp(key, "path")) { config_fn_t old_fn = inc->fn; if (inc->opts->unconditional_remote_url) inc->fn = forbid_remote_url; - ret = handle_path_include(cf, value, inc); + ret = handle_path_include(cs, value, inc); inc->fn = old_fn; } @@ -821,21 +821,21 @@ out: return ret; } -static int get_next_char(struct config_source *cf) +static int get_next_char(struct config_source *cs) { - int c = cf->do_fgetc(cf); + int c = cs->do_fgetc(cs); if (c == '\r') { /* DOS like systems */ - c = cf->do_fgetc(cf); + c = cs->do_fgetc(cs); if (c != '\n') { if (c != EOF) - cf->do_ungetc(c, cf); + cs->do_ungetc(c, cs); c = '\r'; } } - if (c != EOF && ++cf->total_len > INT_MAX) { + if (c != EOF && ++cs->total_len > INT_MAX) { /* * This is an absurdly long config file; refuse to parse * further in order to protect downstream code from integer @@ -843,38 +843,38 @@ static int get_next_char(struct config_source *cf) * but we can mark EOF and put trash in the return value, * which will trigger a parse error. */ - cf->eof = 1; + cs->eof = 1; return 0; } if (c == '\n') - cf->linenr++; + cs->linenr++; if (c == EOF) { - cf->eof = 1; - cf->linenr++; + cs->eof = 1; + cs->linenr++; c = '\n'; } return c; } -static char *parse_value(struct config_source *cf) +static char *parse_value(struct config_source *cs) { int quote = 0, comment = 0, space = 0; - strbuf_reset(&cf->value); + strbuf_reset(&cs->value); for (;;) { - int c = get_next_char(cf); + int c = get_next_char(cs); if (c == '\n') { if (quote) { - cf->linenr--; + cs->linenr--; return NULL; } - return cf->value.buf; + return cs->value.buf; } if (comment) continue; if (isspace(c) && !quote) { - if (cf->value.len) + if (cs->value.len) space++; continue; } @@ -885,9 +885,9 @@ static char *parse_value(struct config_source *cf) } } for (; space; space--) - strbuf_addch(&cf->value, ' '); + strbuf_addch(&cs->value, ' '); if (c == '\\') { - c = get_next_char(cf); + c = get_next_char(cs); switch (c) { case '\n': continue; @@ -907,18 +907,18 @@ static char *parse_value(struct config_source *cf) default: return NULL; } - strbuf_addch(&cf->value, c); + strbuf_addch(&cs->value, c); continue; } if (c == '"') { quote = 1-quote; continue; } - strbuf_addch(&cf->value, c); + strbuf_addch(&cs->value, c); } } -static int get_value(struct config_source *cf, config_fn_t fn, void *data, +static int get_value(struct config_source *cs, config_fn_t fn, void *data, struct strbuf *name) { int c; @@ -927,8 +927,8 @@ static int get_value(struct config_source *cf, config_fn_t fn, void *data, /* Get the full name */ for (;;) { - c = get_next_char(cf); - if (cf->eof) + c = get_next_char(cs); + if (cs->eof) break; if (!iskeychar(c)) break; @@ -936,13 +936,13 @@ static int get_value(struct config_source *cf, config_fn_t fn, void *data, } while (c == ' ' || c == '\t') - c = get_next_char(cf); + c = get_next_char(cs); value = NULL; if (c != '\n') { if (c != '=') return -1; - value = parse_value(cf); + value = parse_value(cs); if (!value) return -1; } @@ -951,21 +951,21 @@ static int get_value(struct config_source *cf, config_fn_t fn, void *data, * the line we just parsed during the call to fn to get * accurate line number in error messages. */ - cf->linenr--; + cs->linenr--; ret = fn(name->buf, value, data); if (ret >= 0) - cf->linenr++; + cs->linenr++; return ret; } -static int get_extended_base_var(struct config_source *cf, struct strbuf *name, +static int get_extended_base_var(struct config_source *cs, struct strbuf *name, int c) { - cf->subsection_case_sensitive = 0; + cs->subsection_case_sensitive = 0; do { if (c == '\n') goto error_incomplete_line; - c = get_next_char(cf); + c = get_next_char(cs); } while (isspace(c)); /* We require the format to be '[base "extension"]' */ @@ -974,13 +974,13 @@ static int get_extended_base_var(struct config_source *cf, struct strbuf *name, strbuf_addch(name, '.'); for (;;) { - int c = get_next_char(cf); + int c = get_next_char(cs); if (c == '\n') goto error_incomplete_line; if (c == '"') break; if (c == '\\') { - c = get_next_char(cf); + c = get_next_char(cs); if (c == '\n') goto error_incomplete_line; } @@ -988,25 +988,25 @@ static int get_extended_base_var(struct config_source *cf, struct strbuf *name, } /* Final ']' */ - if (get_next_char(cf) != ']') + if (get_next_char(cs) != ']') return -1; return 0; error_incomplete_line: - cf->linenr--; + cs->linenr--; return -1; } -static int get_base_var(struct config_source *cf, struct strbuf *name) +static int get_base_var(struct config_source *cs, struct strbuf *name) { - cf->subsection_case_sensitive = 1; + cs->subsection_case_sensitive = 1; for (;;) { - int c = get_next_char(cf); - if (cf->eof) + int c = get_next_char(cs); + if (cs->eof) return -1; if (c == ']') return 0; if (isspace(c)) - return get_extended_base_var(cf, name, c); + return get_extended_base_var(cs, name, c); if (!iskeychar(c) && c != '.') return -1; strbuf_addch(name, tolower(c)); @@ -1019,7 +1019,7 @@ struct parse_event_data { const struct config_options *opts; }; -static int do_event(struct config_source *cf, enum config_event_t type, +static int do_event(struct config_source *cs, enum config_event_t type, struct parse_event_data *data) { size_t offset; @@ -1031,7 +1031,7 @@ static int do_event(struct config_source *cf, enum config_event_t type, data->previous_type == type) return 0; - offset = cf->do_ftell(cf); + offset = cs->do_ftell(cs); /* * At EOF, the parser always "inserts" an extra '\n', therefore * the end offset of the event is the current file position, otherwise @@ -1051,12 +1051,12 @@ static int do_event(struct config_source *cf, enum config_event_t type, return 0; } -static int git_parse_source(struct config_source *cf, config_fn_t fn, +static int git_parse_source(struct config_source *cs, config_fn_t fn, void *data, const struct config_options *opts) { int comment = 0; size_t baselen = 0; - struct strbuf *var = &cf->var; + struct strbuf *var = &cs->var; int error_return = 0; char *error_msg = NULL; @@ -1071,7 +1071,7 @@ static int git_parse_source(struct config_source *cf, config_fn_t fn, for (;;) { int c; - c = get_next_char(cf); + c = get_next_char(cs); if (bomptr && *bomptr) { /* We are at the file beginning; skip UTF8-encoded BOM * if present. Sane editors won't put this in on their @@ -1088,12 +1088,12 @@ static int git_parse_source(struct config_source *cf, config_fn_t fn, } } if (c == '\n') { - if (cf->eof) { - if (do_event(cf, CONFIG_EVENT_EOF, &event_data) < 0) + if (cs->eof) { + if (do_event(cs, CONFIG_EVENT_EOF, &event_data) < 0) return -1; return 0; } - if (do_event(cf, CONFIG_EVENT_WHITESPACE, &event_data) < 0) + if (do_event(cs, CONFIG_EVENT_WHITESPACE, &event_data) < 0) return -1; comment = 0; continue; @@ -1101,23 +1101,23 @@ static int git_parse_source(struct config_source *cf, config_fn_t fn, if (comment) continue; if (isspace(c)) { - if (do_event(cf, CONFIG_EVENT_WHITESPACE, &event_data) < 0) + if (do_event(cs, CONFIG_EVENT_WHITESPACE, &event_data) < 0) return -1; continue; } if (c == '#' || c == ';') { - if (do_event(cf, CONFIG_EVENT_COMMENT, &event_data) < 0) + if (do_event(cs, CONFIG_EVENT_COMMENT, &event_data) < 0) return -1; comment = 1; continue; } if (c == '[') { - if (do_event(cf, CONFIG_EVENT_SECTION, &event_data) < 0) + if (do_event(cs, CONFIG_EVENT_SECTION, &event_data) < 0) return -1; /* Reset prior to determining a new stem */ strbuf_reset(var); - if (get_base_var(cf, var) < 0 || var->len < 1) + if (get_base_var(cs, var) < 0 || var->len < 1) break; strbuf_addch(var, '.'); baselen = var->len; @@ -1126,7 +1126,7 @@ static int git_parse_source(struct config_source *cf, config_fn_t fn, if (!isalpha(c)) break; - if (do_event(cf, CONFIG_EVENT_ENTRY, &event_data) < 0) + if (do_event(cs, CONFIG_EVENT_ENTRY, &event_data) < 0) return -1; /* @@ -1136,42 +1136,42 @@ static int git_parse_source(struct config_source *cf, config_fn_t fn, */ strbuf_setlen(var, baselen); strbuf_addch(var, tolower(c)); - if (get_value(cf, fn, data, var) < 0) + if (get_value(cs, fn, data, var) < 0) break; } - if (do_event(cf, CONFIG_EVENT_ERROR, &event_data) < 0) + if (do_event(cs, CONFIG_EVENT_ERROR, &event_data) < 0) return -1; - switch (cf->origin_type) { + switch (cs->origin_type) { case CONFIG_ORIGIN_BLOB: error_msg = xstrfmt(_("bad config line %d in blob %s"), - cf->linenr, cf->name); + cs->linenr, cs->name); break; case CONFIG_ORIGIN_FILE: error_msg = xstrfmt(_("bad config line %d in file %s"), - cf->linenr, cf->name); + cs->linenr, cs->name); break; case CONFIG_ORIGIN_STDIN: error_msg = xstrfmt(_("bad config line %d in standard input"), - cf->linenr); + cs->linenr); break; case CONFIG_ORIGIN_SUBMODULE_BLOB: error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"), - cf->linenr, cf->name); + cs->linenr, cs->name); break; case CONFIG_ORIGIN_CMDLINE: error_msg = xstrfmt(_("bad config line %d in command line %s"), - cf->linenr, cf->name); + cs->linenr, cs->name); break; default: error_msg = xstrfmt(_("bad config line %d in %s"), - cf->linenr, cf->name); + cs->linenr, cs->name); } switch (opts && opts->error_action ? opts->error_action : - cf->default_error_action) { + cs->default_error_action) { case CONFIG_ERROR_DIE: die("%s", error_msg); break; @@ -2281,13 +2281,13 @@ int config_with_options(config_fn_t fn, void *data, return ret; } -static void configset_iter(struct config_reader *reader, struct config_set *cs, +static void configset_iter(struct config_reader *reader, struct config_set *set, config_fn_t fn, void *data) { int i, value_index; struct string_list *values; struct config_set_element *entry; - struct configset_list *list = &cs->list; + struct configset_list *list = &set->list; for (i = 0; i < list->nr; i++) { entry = list->items[i].e; @@ -2352,7 +2352,7 @@ void read_very_early_config(config_fn_t cb, void *data) config_with_options(cb, data, NULL, &opts); } -static struct config_set_element *configset_find_element(struct config_set *cs, const char *key) +static struct config_set_element *configset_find_element(struct config_set *set, const char *key) { struct config_set_element k; struct config_set_element *found_entry; @@ -2366,13 +2366,13 @@ static struct config_set_element *configset_find_element(struct config_set *cs, hashmap_entry_init(&k.ent, strhash(normalized_key)); k.key = normalized_key; - found_entry = hashmap_get_entry(&cs->config_hash, &k, ent, NULL); + found_entry = hashmap_get_entry(&set->config_hash, &k, ent, NULL); free(normalized_key); return found_entry; } static int configset_add_value(struct config_reader *reader, - struct config_set *cs, const char *key, + struct config_set *set, const char *key, const char *value) { struct config_set_element *e; @@ -2380,7 +2380,7 @@ static int configset_add_value(struct config_reader *reader, struct configset_list_item *l_item; struct key_value_info *kv_info = xmalloc(sizeof(*kv_info)); - e = configset_find_element(cs, key); + e = configset_find_element(set, key); /* * Since the keys are being fed by git_config*() callback mechanism, they * are already normalized. So simply add them without any further munging. @@ -2390,12 +2390,12 @@ static int configset_add_value(struct config_reader *reader, hashmap_entry_init(&e->ent, strhash(key)); e->key = xstrdup(key); string_list_init_dup(&e->value_list); - hashmap_add(&cs->config_hash, &e->ent); + hashmap_add(&set->config_hash, &e->ent); } si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value)); - ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc); - l_item = &cs->list.items[cs->list.nr++]; + ALLOC_GROW(set->list.items, set->list.nr + 1, set->list.alloc); + l_item = &set->list.items[set->list.nr++]; l_item->e = e; l_item->value_index = e->value_list.nr - 1; @@ -2430,33 +2430,33 @@ static int config_set_element_cmp(const void *cmp_data UNUSED, return strcmp(e1->key, e2->key); } -void git_configset_init(struct config_set *cs) +void git_configset_init(struct config_set *set) { - hashmap_init(&cs->config_hash, config_set_element_cmp, NULL, 0); - cs->hash_initialized = 1; - cs->list.nr = 0; - cs->list.alloc = 0; - cs->list.items = NULL; + hashmap_init(&set->config_hash, config_set_element_cmp, NULL, 0); + set->hash_initialized = 1; + set->list.nr = 0; + set->list.alloc = 0; + set->list.items = NULL; } -void git_configset_clear(struct config_set *cs) +void git_configset_clear(struct config_set *set) { struct config_set_element *entry; struct hashmap_iter iter; - if (!cs->hash_initialized) + if (!set->hash_initialized) return; - hashmap_for_each_entry(&cs->config_hash, &iter, entry, + hashmap_for_each_entry(&set->config_hash, &iter, entry, ent /* member name */) { free(entry->key); string_list_clear(&entry->value_list, 1); } - hashmap_clear_and_free(&cs->config_hash, struct config_set_element, ent); - cs->hash_initialized = 0; - free(cs->list.items); - cs->list.nr = 0; - cs->list.alloc = 0; - cs->list.items = NULL; + hashmap_clear_and_free(&set->config_hash, struct config_set_element, ent); + set->hash_initialized = 0; + free(set->list.items); + set->list.nr = 0; + set->list.alloc = 0; + set->list.items = NULL; } struct configset_add_data { @@ -2472,15 +2472,15 @@ static int config_set_callback(const char *key, const char *value, void *cb) return 0; } -int git_configset_add_file(struct config_set *cs, const char *filename) +int git_configset_add_file(struct config_set *set, const char *filename) { struct configset_add_data data = CONFIGSET_ADD_INIT; data.config_reader = &the_reader; - data.config_set = cs; + data.config_set = set; return git_config_from_file(config_set_callback, filename, &data); } -int git_configset_get_value(struct config_set *cs, const char *key, const char **value) +int git_configset_get_value(struct config_set *set, const char *key, const char **value) { const struct string_list *values = NULL; /* @@ -2488,7 +2488,7 @@ int git_configset_get_value(struct config_set *cs, const char *key, const char * * queried key in the files of the configset, the value returned will be the last * value in the value list for that key. */ - values = git_configset_get_value_multi(cs, key); + values = git_configset_get_value_multi(set, key); if (!values) return 1; @@ -2497,26 +2497,26 @@ int git_configset_get_value(struct config_set *cs, const char *key, const char * return 0; } -const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key) +const struct string_list *git_configset_get_value_multi(struct config_set *set, const char *key) { - struct config_set_element *e = configset_find_element(cs, key); + struct config_set_element *e = configset_find_element(set, key); return e ? &e->value_list : NULL; } -int git_configset_get_string(struct config_set *cs, const char *key, char **dest) +int git_configset_get_string(struct config_set *set, const char *key, char **dest) { const char *value; - if (!git_configset_get_value(cs, key, &value)) + if (!git_configset_get_value(set, key, &value)) return git_config_string((const char **)dest, key, value); else return 1; } -static int git_configset_get_string_tmp(struct config_set *cs, const char *key, +static int git_configset_get_string_tmp(struct config_set *set, const char *key, const char **dest) { const char *value; - if (!git_configset_get_value(cs, key, &value)) { + if (!git_configset_get_value(set, key, &value)) { if (!value) return config_error_nonbool(key); *dest = value; @@ -2526,51 +2526,51 @@ static int git_configset_get_string_tmp(struct config_set *cs, const char *key, } } -int git_configset_get_int(struct config_set *cs, const char *key, int *dest) +int git_configset_get_int(struct config_set *set, const char *key, int *dest) { const char *value; - if (!git_configset_get_value(cs, key, &value)) { + if (!git_configset_get_value(set, key, &value)) { *dest = git_config_int(key, value); return 0; } else return 1; } -int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest) +int git_configset_get_ulong(struct config_set *set, const char *key, unsigned long *dest) { const char *value; - if (!git_configset_get_value(cs, key, &value)) { + if (!git_configset_get_value(set, key, &value)) { *dest = git_config_ulong(key, value); return 0; } else return 1; } -int git_configset_get_bool(struct config_set *cs, const char *key, int *dest) +int git_configset_get_bool(struct config_set *set, const char *key, int *dest) { const char *value; - if (!git_configset_get_value(cs, key, &value)) { + if (!git_configset_get_value(set, key, &value)) { *dest = git_config_bool(key, value); return 0; } else return 1; } -int git_configset_get_bool_or_int(struct config_set *cs, const char *key, +int git_configset_get_bool_or_int(struct config_set *set, const char *key, int *is_bool, int *dest) { const char *value; - if (!git_configset_get_value(cs, key, &value)) { + if (!git_configset_get_value(set, key, &value)) { *dest = git_config_bool_or_int(key, value, is_bool); return 0; } else return 1; } -int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest) +int git_configset_get_maybe_bool(struct config_set *set, const char *key, int *dest) { const char *value; - if (!git_configset_get_value(cs, key, &value)) { + if (!git_configset_get_value(set, key, &value)) { *dest = git_parse_maybe_bool(value); if (*dest == -1) return -1; @@ -2579,10 +2579,10 @@ int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *de return 1; } -int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest) +int git_configset_get_pathname(struct config_set *set, const char *key, const char **dest) { const char *value; - if (!git_configset_get_value(cs, key, &value)) + if (!git_configset_get_value(set, key, &value)) return git_config_pathname(dest, key, value); else return 1; @@ -2973,7 +2973,7 @@ static int store_aux_event(enum config_event_t type, size_t begin, size_t end, void *data) { struct config_store_data *store = data; - struct config_source *cf = store->config_reader->source; + struct config_source *cs = store->config_reader->source; ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc); store->parsed[store->parsed_nr].begin = begin; @@ -2983,10 +2983,10 @@ static int store_aux_event(enum config_event_t type, if (type == CONFIG_EVENT_SECTION) { int (*cmpfn)(const char *, const char *, size_t); - if (cf->var.len < 2 || cf->var.buf[cf->var.len - 1] != '.') - return error(_("invalid section name '%s'"), cf->var.buf); + if (cs->var.len < 2 || cs->var.buf[cs->var.len - 1] != '.') + return error(_("invalid section name '%s'"), cs->var.buf); - if (cf->subsection_case_sensitive) + if (cs->subsection_case_sensitive) cmpfn = strncasecmp; else cmpfn = strncmp; @@ -2994,8 +2994,8 @@ static int store_aux_event(enum config_event_t type, /* Is this the section we were looking for? */ store->is_keys_section = store->parsed[store->parsed_nr].is_keys_section = - cf->var.len - 1 == store->baselen && - !cmpfn(cf->var.buf, store->key, store->baselen); + cs->var.len - 1 == store->baselen && + !cmpfn(cs->var.buf, store->key, store->baselen); if (store->is_keys_section) { store->section_seen = 1; ALLOC_GROW(store->seen, store->seen_nr + 1,