From patchwork Tue Mar 28 17:51:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13191430 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 2519BC761AF for ; Tue, 28 Mar 2023 17:52:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229844AbjC1RwF (ORCPT ); Tue, 28 Mar 2023 13:52:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229804AbjC1RwA (ORCPT ); Tue, 28 Mar 2023 13:52:00 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D6E7EF93 for ; Tue, 28 Mar 2023 10:51:58 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id v20-20020a05600c471400b003ed8826253aso1216910wmo.0 for ; Tue, 28 Mar 2023 10:51:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680025916; 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=Ja9v6B3hE29dXxNcaZfs2dBykTHp4/gLCLZLfOMLzsE3pGgmrMDdz6mXSC/4b/WFaW e7UHpRDD/MXgCTxKf2pXdrH/OvN9JE9ckr7WpD0t/A7lmYpYkWJFUfELAQR2M1Sgn2t8 LQHiU6yILjvLWHtHqYh7F9VFINDpjKDyjC4bkFlqoFuHSIMtkksykObMCsPbo5ARGyy0 6yWY+K2jdKQ5xGu5MZcfo3SfRcf1sPDYFSRLaL0A3p5nr0+pQWjTWRKfEydNvJTUTRtk M5+CabCf1EhwVfKCUjIiwtH028xm3OMx5LvEciRjiu3A8VAWJ4oMrCZSm0QBOeR4VxUy V+lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680025916; 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=nINHqep64qWPFUhXsk+6yDm0ud92+eoGF6qd0ryozuV0qwqWC0VbMzlN9BwoZUyBxi EYuHWOLRzWlaaUl29YWi32Zqix+RWWqmWDSmhwkmC0LdYXjiGMuqzypGKkglU7gwDXN3 tu/UvwQPHp2KlaGxoi0Y9P5cxP+yhgEzFPaIYyg0I5yoWgDtX3xFCDuXFWEt4kzq7f/B KFj0NTE+IQjZRNbErxwLtXg1zMrkjeGk/0TFA0xghH9wSm8Y5LBKzJucfBbeZfuxTOPL JTMJnlDNSl+is87eS2Ou1CNZsD+TyxVClpOxsH8vCwkZSU+piOcrcHsjkz3nTIH96cTc XhhA== X-Gm-Message-State: AO0yUKV0vjX6TrXBPXZysOqvYBal39LE7/Z0lEo+4xZEsydNPYebaPci sTxMW5k5cAi8VgDnW3KCpc5NLAxbjLs= X-Google-Smtp-Source: AK7set+TJvarmJ0d7Lg73Xjr6iGdaK52BaPbuIAMjlr+3l0K+y42uoF90B45dK+ubN/hA9DXf4VETQ== X-Received: by 2002:a05:600c:cb:b0:3ed:66e0:b6de with SMTP id u11-20020a05600c00cb00b003ed66e0b6demr13640288wmm.22.1680025916490; Tue, 28 Mar 2023 10:51:56 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u15-20020a05600c210f00b003ed2433aa4asm18050529wml.41.2023.03.28.10.51.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 10:51:56 -0700 (PDT) Message-Id: <75d0f0efb7924fa6ce2387b1a8e4105d672a991f.1680025914.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 28 Mar 2023 17:51:47 +0000 Subject: [PATCH v3 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 , 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 Tue Mar 28 17:51:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13191431 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 074BDC6FD18 for ; Tue, 28 Mar 2023 17:52:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229805AbjC1RwH (ORCPT ); Tue, 28 Mar 2023 13:52:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229808AbjC1RwB (ORCPT ); Tue, 28 Mar 2023 13:52:01 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B26C1EFA9 for ; Tue, 28 Mar 2023 10:51:58 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id u1so7446000wmn.5 for ; Tue, 28 Mar 2023 10:51:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680025917; 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=nfZyqL+bjTJTCrDN0yrFiy4O7WVyGVVkkFgKLOmClzI=; b=aHxsEiJEQiclzBLw7R2eYGWnbzRSH9v+5pEMY9Ld9tJVE7zro8U+f+UwWNccd1UoM7 fDWm/9Kh+gxiTk+bkNpKYwnBGGFXhQ3CN5+PasXb1wwIFVPd/6TWF2h3Td0XXbZTa6yT +f+bvCu1qMw/cfHHaF2XIl7/T2kn2Gb/YfRpLJdJO6zAmgVaOUHha3CUTlQ9Sdi2Asg5 jOPDH10K7G7HyOyGJmQLF4AywbnmQ0BEiwvN897XZdPRgaaI6xnMlanaBKG4n3y3tDf6 wbrR9BavLLerGvO51hsknSmeqB3YpNi13QIQhSTPCGnjy+2sLWTTQ/00w4Y6v7AIoYYt 6rJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680025917; 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=nfZyqL+bjTJTCrDN0yrFiy4O7WVyGVVkkFgKLOmClzI=; b=Q3VCaEzkpwND6jRqkOX5v4OeLnEJggDVrXQySlUKWOVkxmr2w+OavnGizpeWKn0776 Z9Mpyi4mbZTuArltbQlG0Xx+t7osU1ziyS6Jxq5u50Xy0BUn646IwE6qWNjvOkiN5FMw PRaDIiSzzRq6IKh9TWkJaf6GbyPDpwaf7JxCsw3F8esqsdvETwhRiLR52PFyam+cN/yq CrF8rIN0WASb0MQXWv7sCcqKwbVxG78LKDBZReTDht4fQlxTILZN87jQ0oQG+jHMHoxh kNDGt4eBCyj+KYA2p3t5zP2+Bv1Z+luRx1Yz/nPSXHpvnruK2KMdTf6C9nfsv5KaQx6v DIRQ== X-Gm-Message-State: AO0yUKX2k7RCqbADDgZHlyqNkTaBzD1sbNQ7faXM/x85VXmN32IZrhXA +Pam97ZAMyy8ccKVAS0L60kU2QQEFRg= X-Google-Smtp-Source: AK7set89sC1E5U0pal3UkUYQZCurF0mvV67VpHeDtrLscB72YdL6NQuupICylUF2btrb7ldqfL2YnA== X-Received: by 2002:a7b:cd85:0:b0:3ee:96f0:ea3c with SMTP id y5-20020a7bcd85000000b003ee96f0ea3cmr12885387wmj.7.1680025917039; Tue, 28 Mar 2023 10:51:57 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id h7-20020a05600c314700b003ef66c89af0sm4423463wmo.0.2023.03.28.10.51.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 10:51:56 -0700 (PDT) Message-Id: <39db7d8596ad6498d8fe6fe976ab85a5959e9949.1680025914.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 28 Mar 2023 17:51:48 +0000 Subject: [PATCH v3 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 , 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 | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/config.c b/config.c index e4a76739365..6627fad71cf 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,22 @@ 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) +{ + 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 +363,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 +376,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 +729,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 +790,7 @@ out: strbuf_release(&envvar); strvec_clear(&to_free); free(envw); - cf_global = source.prev; + config_reader_pop_source(); return ret; } @@ -1949,20 +1961,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 +1983,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 +2035,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 Tue Mar 28 17:51:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13191432 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 7857EC76196 for ; Tue, 28 Mar 2023 17:52:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229795AbjC1RwJ (ORCPT ); Tue, 28 Mar 2023 13:52:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229819AbjC1RwD (ORCPT ); Tue, 28 Mar 2023 13:52:03 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DF6EF769 for ; Tue, 28 Mar 2023 10:51:59 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id l15-20020a05600c4f0f00b003ef6d684102so4657966wmq.3 for ; Tue, 28 Mar 2023 10:51:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680025918; 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=7b8UBapYx7nFUzNPsKhT+rcEovOEPUyH/SNF0TD0xyA=; b=iFnh1RUN79aTuG0cqBQ0El9Ry8f1BFt5mBHk4FOlUZckbRs6u2xDeGqI7vkswohtIY CkraCb+V/EajH4Oz9XFZmwD2Iqwd7NbivlLjmINlLTwmozi1+1UkDpAobok7K3bB/W1W bRAdab49tB72efv3zStZrDUNRYj/PWSem/A2N0e/1F4HG/Nta5L4aaQD1cXBH/S6xFrX OWhUYPq2EINWPxVsav/uC0cWYnLkqdv9ZrZmC+/fMaYVpyRidU8o/gkBsxjbC1Zlw/s4 Hg5tke68O6SmDrrGIKbeTzthLrhDbk2kYbxAIlNAiwdLYKD/39sJKA0iL/vQyla7lVNb Nz9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680025918; 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=7b8UBapYx7nFUzNPsKhT+rcEovOEPUyH/SNF0TD0xyA=; b=AZ/nTNU/WYhIQi6l8Br7xtymPCmTgMs4HRnWGvSw60QqDBfm22MyVHC5Ukn6QP2gG9 8AI7nGk70ssEmIgxQtPpZvw37OJXdQXoTo+2TJXydy/+FZ+jeR1By1gs87PUIDEJ460j Fu/DDi7nXSk/Jx9tQQBN16mi+olS8zv76j5GNMsf/ObkyBcFmE+XisRWZFIi4HqxzC8W GbLj0rYHGRSKJLTUJpJ24FUADeK85MRTov2a0p6SBra9Kz9gp+YJd7D+4bz1X7Qv6SH/ wHi1TGMYgIydLrACN5eg0s6cEpwDT3Nwkl82lyji7WSiWLSbs2pPRMGqvIk0BLY3M24g cYaA== X-Gm-Message-State: AO0yUKW0d6hW5LksPrwh2rRIYlE+NEzY/bZf19FpR3MahecT2XrATw58 RgAFVh7X+9uC3v7VCD5UUzBURw31INo= X-Google-Smtp-Source: AK7set/+GeToeGu2wDcTGqy6qSjWDDSzsCHtAS4sEDP0AjI3k4rpLbrTJQUa7k4wZlbtpc6U8/s3Vw== X-Received: by 2002:a7b:ce16:0:b0:3ed:9b20:c7c1 with SMTP id m22-20020a7bce16000000b003ed9b20c7c1mr11577696wmc.20.1680025917753; Tue, 28 Mar 2023 10:51:57 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m31-20020a05600c3b1f00b003ef5bb63f13sm13498360wms.10.2023.03.28.10.51.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 10:51:57 -0700 (PDT) Message-Id: <72774fd08f3eb9ff1d449814637e584692ba2bfc.1680025914.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 28 Mar 2023 17:51:49 +0000 Subject: [PATCH v3 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 , 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 | 82 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/config.c b/config.c index 6627fad71cf..3a28b397c4d 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,19 +93,24 @@ 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) { - top->prev = cf_global; - cf_global = top; + 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_global is removed. */ + cf_global = reader->source; return ret; } @@ -732,7 +750,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) { @@ -790,7 +808,7 @@ out: strbuf_release(&envvar); strvec_clear(&to_free); free(envw); - config_reader_pop_source(); + config_reader_pop_source(&the_reader); return ret; } @@ -1325,7 +1343,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; } @@ -1333,7 +1351,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; } @@ -1341,7 +1359,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; } @@ -1349,7 +1367,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; } @@ -1955,7 +1973,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; @@ -1966,22 +1985,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; @@ -1996,15 +2016,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, @@ -2018,8 +2038,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; @@ -2048,7 +2068,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, @@ -3797,8 +3817,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"); @@ -3843,8 +3863,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 : ""; @@ -3863,7 +3883,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 Tue Mar 28 17:51:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13191433 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 7AF08C76196 for ; Tue, 28 Mar 2023 17:52:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229880AbjC1RwM (ORCPT ); Tue, 28 Mar 2023 13:52:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229829AbjC1RwD (ORCPT ); Tue, 28 Mar 2023 13:52:03 -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 600D7EB51 for ; Tue, 28 Mar 2023 10:52:00 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id q19so10025881wrc.5 for ; Tue, 28 Mar 2023 10:52:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680025918; 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=6P/7rgXnBxtYdDtKxOqHBic1qXczXZvszI1/b+zgZWo=; b=bs9OS7vfR/5D8gPOjZlr9FsSoHBxA4SUFkeU7cY5l+L9uv3IfwzzvVwswhBikN71Dx H0bqGH4qJ7QAVK1HtIgvbBb0qm+AroYrjZ9zvJYtVRHF9wmL8kpUnQZcH2fHoSFX4TGf qDZxNZAItao+cn64kQR4g3TKJP54/c8PjuS+9ULLm7k7wDkKxiqvl7UVvfMa7HO8R9VP 0BENS94Plku0ZlsH5/ZolZgnVANtYt3qxlvxkvfY9a291JX+yGiPrsGPhkKfJoC7w8wN j9yYIvoergBXxem3+sfB837ygE+uDfAK3Bvo7RxtFwaSWPfZ0Udx8TZXNmZuopxu+Rrr JwAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680025918; 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=6P/7rgXnBxtYdDtKxOqHBic1qXczXZvszI1/b+zgZWo=; b=HbF8lFDFVpLXQX8OZnoRsPpmexUpPjAHy9EIBYJeQ3PrELh7CvTgGrPM79owSTEpM0 3w15DFtka6W/xJmxcpNlFhtnKxI5UE1SVBctL8QYy3XGqg9dDKhPXiN7IpF6GIecgRPW ld6opt50e7EAMsIjvDbadoM4u8DvtiJE7b7QnXvaxhL2VGS/yvJJ/WY4LGzC79OnuFsm wryU5D7tyb1S7/r2L2zVV+F9yy5zRl18wZu/jeH6qwRxx4mamFMI6oPtTtYKOxLL9pz7 UY3Wt3nl3IuSE0A2h1yVnzvlT2G/khypBet7aMHyEPKYDeSEBxggV5rVeoZAgsDMzORC V1lg== X-Gm-Message-State: AAQBX9e1l2SGZqqG57pzpj5LUmQuAYbTiZsn7QH+hBHi9zkxCM0sJIuI vBKtSGCo/X0l9AgghBQb9tsHMqXZ9Vo= X-Google-Smtp-Source: AKy350ZjH5CdoLonAraCoHGn9hALnSAGkwUkS6UTtNtsw+x8Y508jfCMWQ/iaQkt1ffqrDIPHqQDjA== X-Received: by 2002:a5d:6689:0:b0:2ce:9fd8:8e6d with SMTP id l9-20020a5d6689000000b002ce9fd88e6dmr13141910wru.8.1680025918676; Tue, 28 Mar 2023 10:51:58 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id e18-20020a056000121200b002d24a188b64sm28158015wrx.112.2023.03.28.10.51.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 10:51:58 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Tue, 28 Mar 2023 17:51:50 +0000 Subject: [PATCH v3 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 , 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 3a28b397c4d..cb6ff134f5f 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; /* @@ -98,8 +97,6 @@ static inline void config_reader_push_source(struct config_reader *reader, { 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) @@ -109,8 +106,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_global is removed. */ - cf_global = reader->source; return ret; } @@ -175,6 +170,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. @@ -465,6 +461,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; @@ -478,16 +475,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; } @@ -2228,6 +2225,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; } @@ -2348,7 +2346,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; @@ -2374,12 +2374,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; @@ -2434,16 +2434,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) @@ -2558,6 +2567,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; @@ -2569,8 +2579,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 @@ -2696,9 +2708,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) @@ -2883,6 +2898,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; @@ -2897,6 +2913,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) { @@ -2931,12 +2948,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; @@ -3254,9 +3266,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 Tue Mar 28 17:51:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13191434 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 02BC9C761AF for ; Tue, 28 Mar 2023 17:52:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229882AbjC1RwN (ORCPT ); Tue, 28 Mar 2023 13:52:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229835AbjC1RwE (ORCPT ); Tue, 28 Mar 2023 13:52:04 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61066EC55 for ; Tue, 28 Mar 2023 10:52:01 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id t4so7863213wra.7 for ; Tue, 28 Mar 2023 10:52:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680025919; 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=1dnhUZOb4gchAzO3NZrajGYFaK0JwbYAObXMtWCIw4Y=; b=jviqV6y+WP6YrRoR6ObcUTfPv0TbUgMo0W0wwdsVIovxZUL0YEJA+sb+FEFN0EyP+M 1d+IeJHzDCTtA5VvBINIRIVOA+PwrRoN/1+MdnzVDsJbWdwBZVQmWyxnCaDkMqrlnmlZ XP8coGQlJo434ulskrT86xEIbjQ1GTwyctcq1vZuISEnS2SXUHmGMmbqmt6zCFJXSCz3 x9sFw3YLwoOES3WzU8pVL95pC4R6FEbvluUo2RG5dod28vD5U2H0AI9/ylP1CbjSh/1s GL5LZiFR64olT6RlWOUyoDrBzDv++6TE7osKEBgNrQDXByooyDErhNB7hZJ7fKh9DDXx UjtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680025919; 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=1dnhUZOb4gchAzO3NZrajGYFaK0JwbYAObXMtWCIw4Y=; b=LcIwHWbxXNFp4Ch15cAuFmEUPkuBDIVtZ61OXsJavf/DxwxezKnkWsXAmAERo7VOnJ hCJcwGxdyN7oSSeRBJx8OWgKMUJmwcGc+kIqepBx6YdbNDxDIZ1EddH2QkS2FqA2VhDE 28WZI+yYV0fiaNqgtIf6O+jPtgmvcHhQPvnBBvIp6k4c1g+mzGFYI6c6PWC/NaACaYVo y+ssIghjyrC+aAZA3GGLnP10dyt0yAUCrg9ekaI8S7KGemnUGoeuu7nNsU4O3ipUKLvB yScWu3RjCx9WlGYsGHvAjPuIDXm+FSnq36bqLSvsLJ9f+4JgKGkJveh4DzUW9ODmwcOn OBDg== X-Gm-Message-State: AAQBX9d0zlOmhnqogaxXCJdpIf9QEM+SMMqHs5R5hFFobKXcN8EbwuDR i8Z7J8hmdvJpDgzeP94zMcDoXgx+ZPA= X-Google-Smtp-Source: AKy350aqBIlwggUmnqeD4yeNOeZv9wbKZBo85oSGJdHCBcvhmiZ9wzDuw/Q2jiX5AbcvFJLeWdS+2w== X-Received: by 2002:adf:fa4c:0:b0:2da:75c5:e127 with SMTP id y12-20020adffa4c000000b002da75c5e127mr11766800wrr.23.1680025919340; Tue, 28 Mar 2023 10:51:59 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t14-20020a05600001ce00b002da76acfee1sm15858449wrx.28.2023.03.28.10.51.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 10:51:59 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Tue, 28 Mar 2023 17:51:51 +0000 Subject: [PATCH v3 5/8] config.c: remove current_config_kvi Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jonathan Tan , Emily Shaffer , 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 cb6ff134f5f..71ee36f069b 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"); top->prev = reader->source; reader->source = top; } @@ -109,6 +107,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; @@ -377,20 +383,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; } @@ -2257,7 +2260,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; @@ -2269,14 +2273,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); } } @@ -2614,7 +2618,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, @@ -2720,7 +2724,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' */ @@ -3827,8 +3831,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 @@ -3873,8 +3877,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 @@ -3884,16 +3888,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 Tue Mar 28 17:51:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13191435 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 1C7DBC6FD18 for ; Tue, 28 Mar 2023 17:52:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229885AbjC1RwQ (ORCPT ); Tue, 28 Mar 2023 13:52:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229839AbjC1RwE (ORCPT ); Tue, 28 Mar 2023 13:52:04 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D205AEC40 for ; Tue, 28 Mar 2023 10:52:01 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id bg16-20020a05600c3c9000b003eb34e21bdfso9993554wmb.0 for ; Tue, 28 Mar 2023 10:52:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680025920; 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=oLWudnZ4ae8Z4nc7vRZ/qMHXuM0tNzVVMtU3FS9api8=; b=hFQMhjVvKRNaFlP53D6+3LmgJ3yM4WhSI/xUZRMWbMM89an2jr7QrfOW4fnfOBtWKZ XDuHrEpOT6NazBTg9bkAGOjfbXcJoKgR5nqEVFU6Kpg6jvF/1ZAQAbnJADcYQVjvpN8q qERVUlQSvqhMct6eqpp04xD44VfgWQF5qpSfA38TxLKsI96EkFrIkD3FZ34jmMyf3hO6 oKo/iandqx3vXW/OQotK2S6VZjo+zXfPJSukBJm5S7KDghU6ztTNb1bse7UqAirY2rMS dD8HUVX/de5I+lOaAP1oiN+wPewwQjYVF5fVethYMU3vl4QMKb1JIG8SShg9SjtRbpf6 3N9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680025920; 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=oLWudnZ4ae8Z4nc7vRZ/qMHXuM0tNzVVMtU3FS9api8=; b=IVp/fiX+yHt+BQ0bRZOigucZUgRWvW8wbxk1mMZCcYaoekRgME5rm6hgXJICq3ggvc JW+0PIH8Bn7A5TivENBfvep/sOSOt/bA3QahXLqt8EVjyPOYayBfrVAK6dwd+AG5+8M6 8HKitI2Gua55I8OV30tP8ioAJUhQQMaFAXGO9Zt9j41i66f8bNFSVSFiuOKXGvoqSnE4 /XDNiIggmU4gsFU7gN4n3TY1Dgve5SqqgGb1pWCB9Ey5xcZu8BC9OWxxZ0u6J9TxP37/ fx8gVFI3otWoAFmGfmwuC4cCAC9vfyp0Z+snmLFH2vd646RkOhp8/E0n0TWuxylFz7rH iskQ== X-Gm-Message-State: AAQBX9dtvbwgH1Ll7Ua0ssrT4AULoNuA21XVugpWrn9vpG7cinbd5NJb +eFGqZjCEzDX+nbCt97PwghIdcq5mzc= X-Google-Smtp-Source: AKy350b1qac45ds6hzbSSnjcbyU5E93pztRMb6otERq9cV2RhpDEE4HSsvF/O6g9hUGIENeVEsmoKQ== X-Received: by 2002:a7b:c045:0:b0:3ef:6fee:8057 with SMTP id u5-20020a7bc045000000b003ef6fee8057mr5199408wmc.25.1680025919991; Tue, 28 Mar 2023 10:51:59 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id l10-20020a7bc44a000000b003ed246c1d28sm17504213wmi.44.2023.03.28.10.51.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 10:51:59 -0700 (PDT) Message-Id: <05d9ffa21f6440a7a2876a704d777cc82a121b6b.1680025914.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 28 Mar 2023 17:51:52 +0000 Subject: [PATCH v3 6/8] config.c: remove current_parsing_scope Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jonathan Tan , Emily Shaffer , 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 71ee36f069b..3756322ec96 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) { @@ -110,11 +110,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; @@ -383,18 +391,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, @@ -2159,7 +2167,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; @@ -2167,7 +2176,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); @@ -2176,13 +2185,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)) @@ -2191,12 +2200,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)) @@ -2204,11 +2213,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); @@ -2221,6 +2230,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) { @@ -2234,7 +2244,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 @@ -2250,13 +2260,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; } @@ -2390,7 +2401,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; @@ -3891,7 +3902,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 Tue Mar 28 17:51:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13191436 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 72798C76196 for ; Tue, 28 Mar 2023 17:52:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229915AbjC1Rw3 (ORCPT ); Tue, 28 Mar 2023 13:52:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229849AbjC1RwF (ORCPT ); Tue, 28 Mar 2023 13:52:05 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAB08EF86 for ; Tue, 28 Mar 2023 10:52:02 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id r11so13079772wrr.12 for ; Tue, 28 Mar 2023 10:52:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680025921; 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=cmj7G5fGY0hImw6A9/VhQbthQ0wp76okCqdydvvftEE=; b=ob1SiLm4u8cQDM020XKzUfwC4WL4vujgJLnewZt30lg+54YQhi1SV0HQDJ+GpzG8OJ 5fjrl7kA1ri5YizS38hjWq5QeqlDQ4w9h/b5L4IDjnRiI8IxIKayHRqszmmAaXqj+oPa i5gm0hAyZ/97pu7vbWil+OZ1B6YM09KLc7Ydwkb4oNKtThYu/G+ZOlV/OMW8F5eiYsL+ Xl98Ut8hIochVoyUT3lWHqAxQYl8KaBpaeKrIB2pcXaqJvmxTBhE8DKrMEq2ssxaR8X/ WtA1FiqgMt+MoEfWARo91eopINUZZ5AUqKb7mQOSS918OUpRdZmLYUsyMVmv0DKQigI5 Lg9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680025921; 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=cmj7G5fGY0hImw6A9/VhQbthQ0wp76okCqdydvvftEE=; b=pSc06tGu20jmA1h5qAvIsyNgwu5wlfOJH1HPnI8zxvWQ1BGku/QZMb0Hrw2AdUv+ZZ +gnsPC0nOrUAEQwRKjhNw8OjMfZXepLY4/gXeFWMKJbivOkHjIzZdVni9/okF2zqhnuK ilyhlQBcwwGBkgF7Ueija/ANuuVh2vJbqw7yy8VcbjPaAT+huPLhg7YKDunOXTJGmy3o SZLGdqOciiZ+K8krQ3yTWC7SHCQLN6VwA2wxYkSN2PZQgvluNns3mrVeu+5ZznJmZ+m/ GRceDSzNX5ZSp5t4h7/0ME9qdNIr7H0+TtMNPteMOLX+c0wsbdVPhvmEoxm/hTkuFB/2 d03w== X-Gm-Message-State: AAQBX9evMoJQKPLHMI39/xFfcnGsIFEZmDe4NjNb0P5+E37pv8DpK+Oo y/VzIZ3+5wntl/u5+/27ybtNo7Ep89M= X-Google-Smtp-Source: AKy350bhIQL3EZX9L530jAs+Tu61/BU0vyFnbaUEig2mmZD+2UhbQfRhzhTKU+whjqlqQC3GlLV0gQ== X-Received: by 2002:a5d:6344:0:b0:2d3:f610:8410 with SMTP id b4-20020a5d6344000000b002d3f6108410mr12043432wrw.0.1680025920685; Tue, 28 Mar 2023 10:52:00 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u8-20020adfdb88000000b002cff06039d7sm28088126wri.39.2023.03.28.10.52.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 10:52:00 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Tue, 28 Mar 2023 17:51:53 +0000 Subject: [PATCH v3 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 , 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(). "git config --get[-regexp] --type=int" cannot use the non-refactored version because it parses the int value _after_ parsing the config file, which would run into the BUG(). Since the refactored functions aren't public, they use "struct config_reader". 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 3756322ec96..46c5a9a7d51 100644 --- a/config.c +++ b/config.c @@ -1311,39 +1311,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)); } } @@ -1351,7 +1360,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; } @@ -1359,7 +1368,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; } @@ -1367,7 +1376,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; } @@ -1375,7 +1384,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; } @@ -3839,14 +3848,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) { @@ -3885,14 +3903,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 Tue Mar 28 17:51:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Glen Choo X-Patchwork-Id: 13191437 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 7C7D2C6FD18 for ; Tue, 28 Mar 2023 17:52:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229930AbjC1Rwb (ORCPT ); Tue, 28 Mar 2023 13:52:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229862AbjC1RwH (ORCPT ); Tue, 28 Mar 2023 13:52:07 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F98CEB46 for ; Tue, 28 Mar 2023 10:52:02 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id o24-20020a05600c511800b003ef59905f26so8042601wms.2 for ; Tue, 28 Mar 2023 10:52:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680025921; 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=ZMn9pKez2/jqoNGik0zAGGYfZeFTd5sY47XsXqHvoQA=; b=N0AiXuBO6aRixLhRzgg16o3LCKRVNXIMxZjYbN3d++znwy/FVdmZyCv1J1zjcwoPaG b5SdAYasUjHSnVQDmD5veVL5kxZDijHEgCtmfxAc7aWBumgYw/BcPuDegrDFd+3BmzR6 Sb4kw2j+9LYWCeURF7GDVB+sBWPsOi9JbOkYjzhpKdmo5+An7SCb4E7573/h0cDagF4/ jVzSnxqRWuLBw9u5mCGMNfh818QxXqHeKC0XN5Kj6hWapPTi2USyzjdUn9iJaF2VuRDF KubiKj6D4hcBT+DU6z5zyX3Q6YrVFzvZwNWMJ4hGlpgRS+FjgxMqAXSHwqTex0dI1+f9 Jvvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680025921; 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=ZMn9pKez2/jqoNGik0zAGGYfZeFTd5sY47XsXqHvoQA=; b=DjFKlQhpSQEISqiEHWpyuIBGzYV8X2wTIU6vK7L4ErR3Q06z6geBZMVrqqZghHh0rj tKSgqab8xzaXuBn/RLmGhVdO9vGmt/BmscUETYdD0Ng0gNOyeD658a1kHKxl9XXf+C35 oFYr91sc+SJoRHqJGWt5cj5dEli+J5vIX7xzIVpDuMNoei0j/0yYIWbSyRZoxTZN5CdB kLYQYl8+k39DOYlrrj08ne+xIby6O4GM1lkTc6gP2IJbJ46y1eQvcqS26sx7GKVrJF6/ mdX50lAx9CTHm1D/WUnWO/wvNRkm9XuWjYPyTQlASVT8YSJQZCNea5Ae9+bGR2IrjAcI eUIg== X-Gm-Message-State: AO0yUKUnn8gqHfFK5Xovdu0TPRm0+fkO2eRO9PSj2szQ+M/gY6kXBIry 8t4iY+NzMcIExKzF6WGtCGRLsk5WHsI= X-Google-Smtp-Source: AK7set8DJNRl4GvphPPp+R0/HxcoFWfBQRWQ9JUAtmSMyFGm2Rcfn5K0f/hizQa3QlgxJB3HZEs/IQ== X-Received: by 2002:a7b:cd0d:0:b0:3ed:30cb:5569 with SMTP id f13-20020a7bcd0d000000b003ed30cb5569mr11733259wmj.20.1680025921145; Tue, 28 Mar 2023 10:52:01 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o39-20020a05600c512700b003edddae1068sm13568571wms.9.2023.03.28.10.52.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 10:52:00 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Tue, 28 Mar 2023 17:51:54 +0000 Subject: [PATCH v3 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 , 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 46c5a9a7d51..cf571d45282 100644 --- a/config.c +++ b/config.c @@ -202,7 +202,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; @@ -224,14 +224,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; } @@ -239,8 +239,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--; @@ -257,7 +257,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; @@ -274,11 +274,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?"); @@ -293,7 +293,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) { @@ -310,7 +310,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) @@ -449,16 +449,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, @@ -472,7 +472,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; @@ -486,16 +486,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; } @@ -820,21 +820,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 @@ -842,38 +842,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; } @@ -884,9 +884,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; @@ -906,18 +906,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; @@ -926,8 +926,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; @@ -935,13 +935,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; } @@ -950,21 +950,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"]' */ @@ -973,13 +973,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; } @@ -987,25 +987,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)); @@ -1018,7 +1018,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; @@ -1030,7 +1030,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 @@ -1050,12 +1050,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; @@ -1070,7 +1070,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 @@ -1087,12 +1087,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; @@ -1100,23 +1100,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; @@ -1125,7 +1125,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; /* @@ -1135,42 +1135,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; @@ -2280,13 +2280,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; @@ -2351,7 +2351,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; @@ -2365,13 +2365,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; @@ -2379,7 +2379,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. @@ -2389,12 +2389,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; @@ -2429,33 +2429,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 { @@ -2471,15 +2471,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; /* @@ -2487,7 +2487,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; @@ -2496,26 +2496,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; @@ -2525,51 +2525,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; @@ -2578,10 +2578,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; @@ -2972,7 +2972,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; @@ -2982,10 +2982,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; @@ -2993,8 +2993,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,