From patchwork Tue Nov 24 10:50:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 11927917 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C36D1C63798 for ; Tue, 24 Nov 2020 10:51:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 577F42076E for ; Tue, 24 Nov 2020 10:51:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="EbvclokJ"; dkim=temperror (0-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="NFp/PKo3" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731939AbgKXKu4 (ORCPT ); Tue, 24 Nov 2020 05:50:56 -0500 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]:55561 "EHLO wout4-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731718AbgKXKuz (ORCPT ); Tue, 24 Nov 2020 05:50:55 -0500 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 51B0B1387; Tue, 24 Nov 2020 05:50:54 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Tue, 24 Nov 2020 05:50:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm2; bh=ypWiN8aY5T/SeNDcRJU6nJf/IvY q5odHmdMOToGt14Q=; b=EbvclokJo2vZrj8IYA66YJwLRgD+UNVEQ6spbpqWr42 ZE4E0q7v6cLXEdq6e2VP4RwH50x00bTPX3dA950snLs0M+bdOFK3/ayF57yhS9OP BSAdj6TV7xY3BpfeMsDZOJowSCR5W7lx9AgNNB9SUoCk4RoHAMuSPrN5/jc7bpF7 IHtFWHHiOca/XCLtm1vNWOUZijucTJWt77O/SzkMF0L8a/qjw5FuF3DGw7jQ31sM PzrJPf8v6rp/SuI7wDai/7bq5dcDMrc0ctM24n4XIG4CBtboMdK88Hb0mudOAnVK omocAOJBrBwd+TPLO7323SjsUZP36AGyeD3M8Ygw8jQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=ypWiN8 aY5T/SeNDcRJU6nJf/IvYq5odHmdMOToGt14Q=; b=NFp/PKo3c4GGByjRYwfJw7 amaoQl3P+RE+Kdz1ItLThOJsvD5UA6hiapv+tBHAyUIu1/4rO/cZwPy/k4qfwPhO WFs4Sfwp9vdS7Htdrow/PJLhPDwBu5QITMkuCvWPAQW0Ls4YBy2l/4lPyDB8eYRN rrP4Feo8P21iXNyy1TZ+0iqRFteY03lJGE6fLju3pm+fL3Lya80XRQ1VECxvGrHJ sjGHD+siKU7MyL4xsNGepvo49jXi/fmUcgyCJDCIHBeIrOe6sunCEeZ4dpSbbwIj UVxS+QEFewWi10WZhsBBh2EI67GigSFqIW/hoEr+4tkWeQpIKYLkyB3t+FyznnCg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudegkedgvddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtthgvrh hnpeehgefhtdefueffheekgfffudelffejtdfhvdejkedthfehvdelgfetgfdvtedthfen ucfkphepkeelrddugedrheegrdeknecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrg hmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Received: from vm-mail.pks.im (dynamic-089-014-054-008.89.14.pool.telefonica.de [89.14.54.8]) by mail.messagingengine.com (Postfix) with ESMTPA id ED98F328005D; Tue, 24 Nov 2020 05:50:52 -0500 (EST) Received: from localhost (ncase [10.192.0.11]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id d75c7281 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 24 Nov 2020 10:50:52 +0000 (UTC) Date: Tue, 24 Nov 2020 11:50:50 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: =?iso-8859-1?q?=C6var_Arnfj=F6r=F0?= Bjarmason , Junio C Hamano , Jeff King , "brian m. carlson" , Philip Oakley Subject: [PATCH v2 1/2] config: extract function to parse config pairs Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The function `git_config_parse_parameter` is responsible for parsing a `foo.bar=baz`-formatted configuration key, sanitizing the key and then processing it via the given callback function. Given that we're about to add a second user which is going to process keys in such which already has keys and values separated, this commit extracts a function `config_parse_pair` which only does the sanitization and processing part. Signed-off-by: Patrick Steinhardt --- config.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/config.c b/config.c index 2bdff4457b..3281b1374e 100644 --- a/config.c +++ b/config.c @@ -437,11 +437,26 @@ int git_config_key_is_valid(const char *key) return !git_config_parse_key_1(key, NULL, NULL, 1); } +static int config_parse_pair(const char *key, const char *value, + config_fn_t fn, void *data) +{ + char *canonical_name; + int ret; + + if (!strlen(key)) + return error(_("empty config key")); + if (git_config_parse_key(key, &canonical_name, NULL)) + return -1; + + ret = (fn(canonical_name, value, data) < 0) ? -1 : 0; + free(canonical_name); + return ret; +} + int git_config_parse_parameter(const char *text, config_fn_t fn, void *data) { const char *value; - char *canonical_name; struct strbuf **pair; int ret; @@ -462,12 +477,7 @@ int git_config_parse_parameter(const char *text, return error(_("bogus config parameter: %s"), text); } - if (git_config_parse_key(pair[0]->buf, &canonical_name, NULL)) { - ret = -1; - } else { - ret = (fn(canonical_name, value, data) < 0) ? -1 : 0; - free(canonical_name); - } + ret = config_parse_pair(pair[0]->buf, value, fn, data); strbuf_list_free(pair); return ret; } From patchwork Tue Nov 24 10:50:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 11927919 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85825C56202 for ; Tue, 24 Nov 2020 10:51:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9C3C720708 for ; Tue, 24 Nov 2020 10:51:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="OYV927SL"; dkim=temperror (0-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="a070+629" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732183AbgKXKvA (ORCPT ); Tue, 24 Nov 2020 05:51:00 -0500 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]:40321 "EHLO wout4-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732047AbgKXKu7 (ORCPT ); Tue, 24 Nov 2020 05:50:59 -0500 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 607591385; Tue, 24 Nov 2020 05:50:58 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Tue, 24 Nov 2020 05:50:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm2; bh=YULP1JJ2ZfjiWumdcJCaqvN9X2Q xBD+f3EfiynLQa7U=; b=OYV927SLs0giTbIpN1tQQHnUEjmuIG5QVmlx3cU6slv Y9otA1rmRL+HVcsy/ptwKxv2EHEtZ/slA+/22OHW0/0AqyUc9WgnzsYsXgaYepgo ZFxsEqA8rUxJvLIEgQSVkHRvTTaGlnf1lJhqsKRrSSVasqAX3k3vmqWNURfkegr/ OIko6V9KeqI6umajMM/1B9snOZdcfuivXV5W98YSt4lp+Eh+ROSSKhLss54B2BOP K87ZS7/ei+YmxlI7Uuwbr3wcRjG1Afr+WJGm5fILODSXfu5HEgUDltsB1JuCWiaH izUqI7iKszoEiBHYM5bVYaWW9UvGR+O8kJM+vN/C2Xw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=YULP1J J2ZfjiWumdcJCaqvN9X2QxBD+f3EfiynLQa7U=; b=a070+6292xdtD3kQ0/k2Jj t/iDUc3Natdo8eyycoAkzvS4iC8YkVCT4wjEgYnJfAopteJ62QFL9mQopUejcJUV /XKwduJSZZomR1PAQ+XSQRwIdWdW4np5jaxR6EIro4QXodbnYWv7GTISbD/75C2K dtz/BZvcl+3e5vq/87/kveQBHA5iwijYZuZWnY+oGv1Whl3amqg3dmQKqsw5oz9V 1FjBvJYsqytmP/Uva3hJylGuLWmeqynqkr4kS8I0B1X/BiGweU9dRufV6K+XC3il DRZ0PFOveCFtTjxV+Xn0sOCabjfHSIDnSlTUTsHtlYBcWR2RkuOrC8SsF/khzYiA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudegkedgvddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtthgvrh hnpeehgefhtdefueffheekgfffudelffejtdfhvdejkedthfehvdelgfetgfdvtedthfen ucfkphepkeelrddugedrheegrdeknecuvehluhhsthgvrhfuihiivgepudenucfrrghrrg hmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Received: from vm-mail.pks.im (dynamic-089-014-054-008.89.14.pool.telefonica.de [89.14.54.8]) by mail.messagingengine.com (Postfix) with ESMTPA id 057663280063; Tue, 24 Nov 2020 05:50:56 -0500 (EST) Received: from localhost (ncase [10.192.0.11]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id fe40eb9d (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 24 Nov 2020 10:50:56 +0000 (UTC) Date: Tue, 24 Nov 2020 11:50:55 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: =?iso-8859-1?q?=C6var_Arnfj=F6r=F0?= Bjarmason , Junio C Hamano , Jeff King , "brian m. carlson" , Philip Oakley Subject: [PATCH v2 2/2] config: allow specifying config entries via envvar pairs Message-ID: <97740ada840a1e2f151003e695de9f2efa5a7e62.1606214397.git.ps@pks.im> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org While we currently have the `GIT_CONFIG_PARAMETERS` environment variable which can be used to pass runtime configuration data to git processes, it's an internal implementation detail and not supposed to be used by end users. Next to being for internal use only, this way of passing config entries has a major downside: the config keys need to be parsed as they contain both key and value in a single variable. As such, it is left to the user to escape any potentially harmful characters in the value, which is quite hard to do if values are controlled by a third party. This commit thus adds a new way of adding config entries via the environment which gets rid of this shortcoming. If the user passes the `GIT_CONFIG_COUNT=$n` environment variable, Git will parse environment variable pairs `GIT_CONFIG_KEY_$i` and `GIT_CONFIG_VALUE_$i` for each `i` in `[0,n)`. While the same can be achieved with `git -c =`, one may wish to not do so for potentially sensitive information. E.g. if one wants to set `http.extraHeader` to contain an authentication token, doing so via `-c` would trivially leak those credentials via e.g. ps(1), which typically also shows command arguments. Signed-off-by: Patrick Steinhardt --- Documentation/git-config.txt | 9 +++ cache.h | 1 + config.c | 72 +++++++++++++++++++----- environment.c | 1 + t/t1300-config.sh | 105 ++++++++++++++++++++++++++++++++++- 5 files changed, 173 insertions(+), 15 deletions(-) diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt index 7573160f21..84ae9b51a3 100644 --- a/Documentation/git-config.txt +++ b/Documentation/git-config.txt @@ -335,6 +335,15 @@ GIT_CONFIG_NOSYSTEM:: Whether to skip reading settings from the system-wide $(prefix)/etc/gitconfig file. See linkgit:git[1] for details. +GIT_CONFIG_COUNT,GIT_CONFIG_KEY_,GIT_CONFIG_VALUE_:: + If GIT_CONFIG_COUNT is set to a positive number, all environment pairs + GIT_CONFIG_KEY_ and GIT_CONFIG_VALUE_ up to that number will be + added to the process's runtime configuration. The config pairs are + zero-indexed. Any missing key or value is treated as an error. An empty + GIT_CONFIG_COUNT is treated the same as GIT_CONFIG_COUNT=0, namely no + pairs are processed. Config entries set this way have command scope, + but will be overridden by any explicit options passed via `git -c`. + See also <>. diff --git a/cache.h b/cache.h index c0072d43b1..8a36146337 100644 --- a/cache.h +++ b/cache.h @@ -472,6 +472,7 @@ static inline enum object_type object_type(unsigned int mode) #define TEMPLATE_DIR_ENVIRONMENT "GIT_TEMPLATE_DIR" #define CONFIG_ENVIRONMENT "GIT_CONFIG" #define CONFIG_DATA_ENVIRONMENT "GIT_CONFIG_PARAMETERS" +#define CONFIG_COUNT_ENVIRONMENT "GIT_CONFIG_COUNT" #define EXEC_PATH_ENVIRONMENT "GIT_EXEC_PATH" #define CEILING_DIRECTORIES_ENVIRONMENT "GIT_CEILING_DIRECTORIES" #define NO_REPLACE_OBJECTS_ENVIRONMENT "GIT_NO_REPLACE_OBJECTS" diff --git a/config.c b/config.c index 3281b1374e..5da1ae16c9 100644 --- a/config.c +++ b/config.c @@ -484,38 +484,82 @@ int git_config_parse_parameter(const char *text, int git_config_from_parameters(config_fn_t fn, void *data) { - const char *env = getenv(CONFIG_DATA_ENVIRONMENT); + const char *env; + struct strbuf envvar = STRBUF_INIT; int ret = 0; - char *envw; + char *envw = NULL; const char **argv = NULL; - int nr = 0, alloc = 0; int i; struct config_source source; - if (!env) - return 0; - memset(&source, 0, sizeof(source)); source.prev = cf; source.origin_type = CONFIG_ORIGIN_CMDLINE; cf = &source; - /* sq_dequote will write over it */ - envw = xstrdup(env); + env = getenv(CONFIG_COUNT_ENVIRONMENT); + if (env) { + unsigned long count; + char *endp; - if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) { - ret = error(_("bogus format in %s"), CONFIG_DATA_ENVIRONMENT); - goto out; + count = strtoul(env, &endp, 10); + if (*endp) { + ret = error(_("bogus count in %s"), CONFIG_COUNT_ENVIRONMENT); + goto out; + } + if (count > INT_MAX) { + ret = error(_("too many entries in %s"), CONFIG_COUNT_ENVIRONMENT); + goto out; + } + + for (i = 0; i < count; i++) { + const char *key, *value; + + strbuf_addf(&envvar, "GIT_CONFIG_KEY_%d", i); + key = getenv(envvar.buf); + if (!key) { + ret = error(_("missing config key %s"), envvar.buf); + goto out; + } + strbuf_reset(&envvar); + + strbuf_addf(&envvar, "GIT_CONFIG_VALUE_%d", i); + value = getenv(envvar.buf); + if (!value) { + ret = error(_("missing config value %s"), envvar.buf); + goto out; + } + strbuf_reset(&envvar); + + if (config_parse_pair(key, value, fn, data) < 0) { + ret = -1; + goto out; + } + } } - for (i = 0; i < nr; i++) { - if (git_config_parse_parameter(argv[i], fn, data) < 0) { - ret = -1; + env = getenv(CONFIG_DATA_ENVIRONMENT); + if (env) { + int nr = 0, alloc = 0; + + /* sq_dequote will write over it */ + envw = xstrdup(env); + + if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) { + ret = error(_("bogus format in %s"), CONFIG_DATA_ENVIRONMENT); goto out; } + + for (i = 0; i < nr; i++) { + if (git_config_parse_parameter(argv[i], fn, data) < 0) { + ret = -1; + goto out; + } + } } out: + strbuf_release(&envvar); free(argv); free(envw); cf = source.prev; diff --git a/environment.c b/environment.c index bb518c61cd..e94eca92f3 100644 --- a/environment.c +++ b/environment.c @@ -116,6 +116,7 @@ const char * const local_repo_env[] = { ALTERNATE_DB_ENVIRONMENT, CONFIG_ENVIRONMENT, CONFIG_DATA_ENVIRONMENT, + CONFIG_COUNT_ENVIRONMENT, DB_ENVIRONMENT, GIT_DIR_ENVIRONMENT, GIT_WORK_TREE_ENVIRONMENT, diff --git a/t/t1300-config.sh b/t/t1300-config.sh index 825d9a184f..8c90cca79d 100755 --- a/t/t1300-config.sh +++ b/t/t1300-config.sh @@ -1316,6 +1316,107 @@ test_expect_success 'detect bogus GIT_CONFIG_PARAMETERS' ' git config --get-regexp "env.*" ' +test_expect_success 'git config handles environment config pairs' ' + GIT_CONFIG_COUNT=2 \ + GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="foo" \ + GIT_CONFIG_KEY_1="pair.two" GIT_CONFIG_VALUE_1="bar" \ + git config --get-regexp "pair.*" >actual && + cat >expect <<-EOF && + pair.one foo + pair.two bar + EOF + test_cmp expect actual +' + +test_expect_success 'git config ignores pairs without count' ' + test_must_fail env GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \ + git config pair.one 2>error && + test_must_be_empty error +' + +test_expect_success 'git config ignores pairs with zero count' ' + test_must_fail env \ + GIT_CONFIG_COUNT=0 \ + GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \ + git config pair.one +' + +test_expect_success 'git config ignores pairs exceeding count' ' + GIT_CONFIG_COUNT=1 \ + GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \ + GIT_CONFIG_KEY_1="pair.two" GIT_CONFIG_VALUE_1="value" \ + git config --get-regexp "pair.*" >actual && + cat >expect <<-EOF && + pair.one value + EOF + test_cmp expect actual +' + +test_expect_success 'git config ignores pairs with zero count' ' + test_must_fail env \ + GIT_CONFIG_COUNT=0 GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \ + git config pair.one >error && + test_must_be_empty error +' + +test_expect_success 'git config ignores pairs with empty count' ' + test_must_fail env \ + GIT_CONFIG_COUNT= GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \ + git config pair.one >error && + test_must_be_empty error +' + +test_expect_success 'git config fails with invalid count' ' + test_must_fail env GIT_CONFIG_COUNT=10a git config --list 2>error && + test_i18ngrep "bogus count" error && + test_must_fail env GIT_CONFIG_COUNT=9999999999999999 git config --list 2>error && + test_i18ngrep "too many entries" error +' + +test_expect_success 'git config fails with missing config key' ' + test_must_fail env GIT_CONFIG_COUNT=1 GIT_CONFIG_VALUE_0="value" \ + git config --list 2>error && + test_i18ngrep "missing config key" error +' + +test_expect_success 'git config fails with missing config value' ' + test_must_fail env GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="pair.one" \ + git config --list 2>error && + test_i18ngrep "missing config value" error +' + +test_expect_success 'git config fails with invalid config pair key' ' + test_must_fail env GIT_CONFIG_COUNT=1 \ + GIT_CONFIG_KEY_0= GIT_CONFIG_VALUE_0=value \ + git config --list && + test_must_fail env GIT_CONFIG_COUNT=1 \ + GIT_CONFIG_KEY_0=missing-section GIT_CONFIG_VALUE_0=value \ + git config --list +' + +test_expect_success 'environment overrides config file' ' + test_when_finished "rm -f .git/config" && + cat >.git/config <<-EOF && + [pair] + one = value + EOF + GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=override \ + git config pair.one >actual && + cat >expect <<-EOF && + override + EOF + test_cmp expect actual +' + +test_expect_success 'command line overrides environment config' ' + GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=value \ + git -c pair.one=override config pair.one >actual && + cat >expect <<-EOF && + override + EOF + test_cmp expect actual +' + test_expect_success 'git config --edit works' ' git config -f tmp test.value no && echo test.value=yes >expect && @@ -1661,9 +1762,11 @@ test_expect_success '--show-origin with --list' ' file:.git/config user.override=local file:.git/config include.path=../include/relative.include file:.git/../include/relative.include user.relative=include + command line: user.environ=true command line: user.cmdline=true EOF - git -c user.cmdline=true config --list --show-origin >output && + GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.environ GIT_CONFIG_VALUE_0=true\ + git -c user.cmdline=true config --list --show-origin >output && test_cmp expect output ' From patchwork Thu Jan 7 06:37:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 12003039 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2804C433DB for ; Thu, 7 Jan 2021 06:38:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9EC7722E00 for ; Thu, 7 Jan 2021 06:38:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726315AbhAGGhu (ORCPT ); Thu, 7 Jan 2021 01:37:50 -0500 Received: from out4-smtp.messagingengine.com ([66.111.4.28]:53479 "EHLO out4-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725763AbhAGGhu (ORCPT ); Thu, 7 Jan 2021 01:37:50 -0500 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 2B2A45C0165; Thu, 7 Jan 2021 01:37:04 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Thu, 07 Jan 2021 01:37:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=BaH+qtFHtuxUF6T4VUceASwFfEW IwvLF9OwurXAHmXw=; b=pDmG4GKznfM3GmuSmkgmCZijgw58fWH4/DotH4nQW7D 2JEpSZG+ee9991OobIFaQGRHE6qlnr85N7MVEhYFpQDqwD8uHNjH+mPFaGSCw142 +5ZltovYmJKSk2WJfQHHUXQsmgWvcN5TidhmU251qeBV8wrlu5j0pyUDgPG9TL4H Y/LpWh2DGT5YK+Dt6SKNn30lFsxByecJVJOa8JiEOxaAck4SSRV4sqTxS4jWKGg3 SQAvWi3YYI57nMEYWRsNwrZGtyu2KrvbALbxlcI0IaEBECWOBYJL0+vV0ST6oNzp qSO68XNVyktbbukfwko8EyCMdtxVGKqwhdx95MlrmMA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=BaH+qt FHtuxUF6T4VUceASwFfEWIwvLF9OwurXAHmXw=; b=ZG4T2ygIcmMtl7MFmRwGdQ NbDS3qHP6+CfVvllsS0QRyz1Y4ffUlCJFw+3rgU7jSfRSrddX3BBNVxVGGoTSWSx bCiq7s5MrB+QJql1i/AfFzq/31zjEMufqmVuR+dlMuQ/YhaNQ31lZdnd/Sc63HnH 9v2AX3KnLAhtI8WkPU49GqJAdvaBCBzQrENsHWzknKwp1GddJrQm1tJwqaa347Nl HSXhgA+3RYRHvlLMX2T/VBbXsTkP7B+B7I00xJlkrZqOLRRKdTgY4vWoKER25oui odnWoOPJnHXAObzNWSRIkooLq3TDkCDEYWYcIp0b9BzXgKpWw3wpYcjVDsoUNQyw == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvdeguddgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtthgvrh hnpeehgefhtdefueffheekgfffudelffejtdfhvdejkedthfehvdelgfetgfdvtedthfen ucfkphepjeejrddufedrfeegrddvfedvnecuvehluhhsthgvrhfuihiivgepgeenucfrrg hrrghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Received: from vm-mail.pks.im (dynamic-077-013-034-232.77.13.pool.telefonica.de [77.13.34.232]) by mail.messagingengine.com (Postfix) with ESMTPA id 35547240057; Thu, 7 Jan 2021 01:37:03 -0500 (EST) Received: from localhost (ncase [10.192.0.11]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id ca731f06 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 7 Jan 2021 06:37:02 +0000 (UTC) Date: Thu, 7 Jan 2021 07:37:01 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: =?iso-8859-1?q?=C6var_Arnfj=F6r=F0?= Bjarmason , Junio C Hamano , Jeff King , "brian m. carlson" , Philip Oakley Subject: [PATCH v6 4/8] config: extract function to parse config pairs Message-ID: <0a9b085fe5e2440f9c94819377985ed83bd80d05.1610001187.git.ps@pks.im> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The function `git_config_parse_parameter` is responsible for parsing a `foo.bar=baz`-formatted configuration key, sanitizing the key and then processing it via the given callback function. Given that we're about to add a second user which is going to process keys which already has keys and values separated, this commit extracts a function `config_parse_pair` which only does the sanitization and processing part as a preparatory step. Signed-off-by: Patrick Steinhardt --- config.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/config.c b/config.c index cde3511110..151980e5c9 100644 --- a/config.c +++ b/config.c @@ -458,11 +458,26 @@ int git_config_key_is_valid(const char *key) return !git_config_parse_key_1(key, NULL, NULL, 1); } +static int config_parse_pair(const char *key, const char *value, + config_fn_t fn, void *data) +{ + char *canonical_name; + int ret; + + if (!strlen(key)) + return error(_("empty config key")); + if (git_config_parse_key(key, &canonical_name, NULL)) + return -1; + + ret = (fn(canonical_name, value, data) < 0) ? -1 : 0; + free(canonical_name); + return ret; +} + int git_config_parse_parameter(const char *text, config_fn_t fn, void *data) { const char *value; - char *canonical_name; struct strbuf **pair; int ret; @@ -483,12 +498,7 @@ int git_config_parse_parameter(const char *text, return error(_("bogus config parameter: %s"), text); } - if (git_config_parse_key(pair[0]->buf, &canonical_name, NULL)) { - ret = -1; - } else { - ret = (fn(canonical_name, value, data) < 0) ? -1 : 0; - free(canonical_name); - } + ret = config_parse_pair(pair[0]->buf, value, fn, data); strbuf_list_free(pair); return ret; }