From patchwork Thu Jun 22 19:50:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "brian m. carlson" X-Patchwork-Id: 13289562 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 8D7F9EB64DA for ; Thu, 22 Jun 2023 19:51:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231138AbjFVTve (ORCPT ); Thu, 22 Jun 2023 15:51:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230521AbjFVTvd (ORCPT ); Thu, 22 Jun 2023 15:51:33 -0400 Received: from ring.crustytoothpaste.net (ring.crustytoothpaste.net [IPv6:2600:3c04::f03c:92ff:fe9e:c6d8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6040E1FF0 for ; Thu, 22 Jun 2023 12:51:31 -0700 (PDT) Received: from tapette.crustytoothpaste.net (unknown [IPv6:2001:470:b056:101:5e4a:89fa:93b9:2058]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by ring.crustytoothpaste.net (Postfix) with ESMTPSA id 591515A215; Thu, 22 Jun 2023 19:51:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1687463490; bh=6iVbe2z3zoZlSKhGzL00jVTdg7CotKY1uqmNpmM/404=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Reply-To: Subject:Date:To:CC:Resent-Date:Resent-From:Resent-To:Resent-Cc: In-Reply-To:References:Content-Type:Content-Disposition; b=bnbxiGpaSFuSGyIaeJZt//+Y4Ii9b8vLRcWq8LXM4SeF0iT+YcGtz29caUl+BI53Q I/QmWmkdawmVPqp6j8JiY+RU09YQUQ66VdVdOUTJXsY5Jw/Rv0BBMPkIwX377fNodS HkVonMRicrTKPXjw2aZkxNi6LhbUSuBvjZvRncRk+xm59HobgtC39Pg1QB3ClPNZyn 4doS47zLCPja27CsUdPZ+dn+r9Ah7Ye7mjMTGwqfWzP03h0YXPPj+phDyVieXU7aXZ h4orS4bsQXVLztV1l1F6rQyRfBY6YtEsd2z+k/ndKily+hozWOWbFo78cDR4JvCSfX r7rCO8FaRUtOs1RJ07G/HLqjPE/8gWSKDFNMK+KWME5xWLHCd8lazZOfqfPTYJ3//5 iSOSxe+MOIu4QGuIDzyOzLkRM7DfHFh0u2Z/mQIg+oFFhQe27zuOSMwPVCDEu7ou72 hv6lwZsmGf5p5j0EOxq0eAR3X0EjZvJ6m4y20OjWfVJC3bp2Fmn From: "brian m. carlson" To: Cc: Junio C Hamano , Elijah Newren , Calvin Wan Subject: [PATCH 1/3] var: add support for listing the shell Date: Thu, 22 Jun 2023 19:50:57 +0000 Message-ID: <20230622195059.320593-2-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8 In-Reply-To: <20230622195059.320593-1-sandals@crustytoothpaste.net> References: <20230622195059.320593-1-sandals@crustytoothpaste.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: "brian m. carlson" On most Unix systems, finding a suitable shell is easy: one simply uses "sh" with an appropriate PATH value. However, in many Windows environments, the shell is shipped alongside Git, and it may or may not be in PATH, even if Git is. In such an environment, it can be very helpful to query Git for the shell it's using, since other tools may want to use the same shell as well. To help them out, let's add a variable, GIT_SHELL_PATH, that points to the location of the shell. On Unix, we know our shell must be executable to be functional, so assume that the distributor has correctly configured their environment, and use that as a basic test. On Git for Windows, we know that our shell will be one of a few fixed values, all of which end in "sh" (such as "bash"). This seems like it might be a nice test on Unix as well, since it is customary for all shells to end in "sh", but there probably exist such systems that don't have such a configuration, so be careful here not to break them. Signed-off-by: brian m. carlson --- Documentation/git-var.txt | 3 +++ builtin/var.c | 6 ++++++ t/t0007-git-var.sh | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt index f40202b8e3..f0f647e14a 100644 --- a/Documentation/git-var.txt +++ b/Documentation/git-var.txt @@ -71,6 +71,9 @@ endif::git-default-pager[] GIT_DEFAULT_BRANCH:: The name of the first branch created in newly initialized repositories. +GIT_SHELL_PATH:: + The path of the binary providing the POSIX shell for commands which use the shell. + SEE ALSO -------- linkgit:git-commit-tree[1] diff --git a/builtin/var.c b/builtin/var.c index 2149998980..f97178eed1 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -36,6 +36,11 @@ static const char *default_branch(int flag) return git_default_branch_name(1); } +static const char *shell_path(int flag) +{ + return SHELL_PATH; +} + struct git_var { const char *name; const char *(*read)(int); @@ -47,6 +52,7 @@ static struct git_var git_vars[] = { { "GIT_SEQUENCE_EDITOR", sequence_editor }, { "GIT_PAGER", pager }, { "GIT_DEFAULT_BRANCH", default_branch }, + { "GIT_SHELL_PATH", shell_path }, { "", NULL }, }; diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh index eeb8539c1b..270bd4e512 100755 --- a/t/t0007-git-var.sh +++ b/t/t0007-git-var.sh @@ -147,6 +147,18 @@ test_expect_success 'get GIT_SEQUENCE_EDITOR with configuration and environment ) ' +test_expect_success POSIXPERM 'GIT_SHELL_PATH points to a valid executable' ' + git var GIT_SHELL_PATH >shell && + test -x "$(cat shell)" +' + +# We know in this environment that our shell will be one of a few fixed values +# that all end in "sh". +test_expect_success MINGW 'GIT_SHELL_PATH points to a suitable shell' ' + git var GIT_SHELL_PATH >shell && + grep "sh\$" shell +' + # For git var -l, we check only a representative variable; # testing the whole output would make our test too brittle with # respect to unrelated changes in the test suite's environment. From patchwork Thu Jun 22 19:50:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "brian m. carlson" X-Patchwork-Id: 13289564 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 7CAA3EB64DC for ; Thu, 22 Jun 2023 19:51:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231280AbjFVTvi (ORCPT ); Thu, 22 Jun 2023 15:51:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229743AbjFVTve (ORCPT ); Thu, 22 Jun 2023 15:51:34 -0400 Received: from ring.crustytoothpaste.net (ring.crustytoothpaste.net [IPv6:2600:3c04::f03c:92ff:fe9e:c6d8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B5021FF1 for ; Thu, 22 Jun 2023 12:51:31 -0700 (PDT) Received: from tapette.crustytoothpaste.net (unknown [IPv6:2001:470:b056:101:5e4a:89fa:93b9:2058]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by ring.crustytoothpaste.net (Postfix) with ESMTPSA id 67EDC5B412; Thu, 22 Jun 2023 19:51:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1687463490; bh=zf58TXRlm/XdDzdBNW6nqgkp/SxJfkspEHC/B5v56p4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Reply-To: Subject:Date:To:CC:Resent-Date:Resent-From:Resent-To:Resent-Cc: In-Reply-To:References:Content-Type:Content-Disposition; b=MGPShhsJRrwgBwUH3dtTxxomjc46y73mhL0www1QQ43RzCaQx/mcxCqo3rNpCa2WU UGfG7H7adVKZrTdgY0MBG5DfIOILCwvK2wo+9x/ETuah4zNYeMpC4Owii2c9d6NJfz B0ytPJv8kggr1i9K4WYhbe0TCWT2SzsF+KtagUSnfbY9BVuhsF3ZSEAt07VovIp7qo utbkrX5LkxxDmOprnO8KtBqK0r+HxytL0BVORkWInzVcJrACW6Q8080iNi2iOLE+ty E3tQrwpyUJUILahB/P3L03BdzKJw+ISs6+0d3k2mDKCIMfLHCa0u52K6Dr47p94Vfn v0Eqt1oMZLmOnDs5GJfciXaRxQyPMNi5gAnnVD8jxDpY3cSrAQSxT3Ou9i1/x2Vrkj q4Zgw0iUkE/pCjzyELnXMv+b+5OW4+SLxhW8bkIv/nGBlQZ6VDMXhlfuldwvtnkKGY qmXB4Pdt+eo8HS1poPmxCfEcuZA8FcqoKJUs2g7o+SO5qTx1C02 From: "brian m. carlson" To: Cc: Junio C Hamano , Elijah Newren , Calvin Wan Subject: [PATCH 2/3] var: add attributes files locations Date: Thu, 22 Jun 2023 19:50:58 +0000 Message-ID: <20230622195059.320593-3-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8 In-Reply-To: <20230622195059.320593-1-sandals@crustytoothpaste.net> References: <20230622195059.320593-1-sandals@crustytoothpaste.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: "brian m. carlson" Currently, there are some programs which would like to read and parse the gitattributes files at the global or system levels. However, it's not always obvious where these files live, especially for the system file, which may have been hard-coded at compile time or computed dynamically based on the runtime prefix. It's not reasonable to expect all callers of Git to intuitively know where the Git distributor or user has configured these locations to be, so add some entries to allow us to determine their location. Honor the GIT_ATTR_NOSYSTEM environment variable if one is specified. Expose the accessor functions in a way that we can reuse them from within the var code. In order to make our paths consistent on Windows and also use the same form as paths use in "git rev-parse", let's normalize the path before we return it. This results in Windows-style paths that use slashes, which is convenient for making our tests function in a consistent way across platforms. Note that this requires that some of our values be freed, so let's add a flag about whether the value needs to be freed and use it accordingly. Signed-off-by: brian m. carlson --- Documentation/git-var.txt | 6 ++++++ attr.c | 6 +++--- attr.h | 4 ++++ builtin/var.c | 45 ++++++++++++++++++++++++++++++++------- t/t0007-git-var.sh | 20 +++++++++++++++++ 5 files changed, 70 insertions(+), 11 deletions(-) diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt index f0f647e14a..dfbe5cb52b 100644 --- a/Documentation/git-var.txt +++ b/Documentation/git-var.txt @@ -74,6 +74,12 @@ GIT_DEFAULT_BRANCH:: GIT_SHELL_PATH:: The path of the binary providing the POSIX shell for commands which use the shell. +GIT_ATTR_SYSTEM:: + The path to the system linkgit:gitattributes[5] file, if one is enabled. + +GIT_ATTR_GLOBAL:: + The path to the global (per-user) linkgit:gitattributes[5] file. + SEE ALSO -------- linkgit:git-commit-tree[1] diff --git a/attr.c b/attr.c index d45d34058d..822d214053 100644 --- a/attr.c +++ b/attr.c @@ -870,7 +870,7 @@ static struct attr_stack *read_attr(struct index_state *istate, return res; } -static const char *git_etc_gitattributes(void) +const char *git_etc_gitattributes(void) { static const char *system_wide; if (!system_wide) @@ -878,7 +878,7 @@ static const char *git_etc_gitattributes(void) return system_wide; } -static const char *get_home_gitattributes(void) +const char *get_home_gitattributes(void) { if (!git_attributes_file) git_attributes_file = xdg_config_home("attributes"); @@ -886,7 +886,7 @@ static const char *get_home_gitattributes(void) return git_attributes_file; } -static int git_attr_system(void) +int git_attr_system(void) { return !git_env_bool("GIT_ATTR_NOSYSTEM", 0); } diff --git a/attr.h b/attr.h index 676bd17ce2..05a2e4c622 100644 --- a/attr.h +++ b/attr.h @@ -227,4 +227,8 @@ void git_attr_set_direction(enum git_attr_direction new_direction); void attr_start(void); +const char *git_etc_gitattributes(void); +const char *get_home_gitattributes(void); +int git_attr_system(void); + #endif /* ATTR_H */ diff --git a/builtin/var.c b/builtin/var.c index f97178eed1..b9e2f23697 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -4,6 +4,7 @@ * Copyright (C) Eric Biederman, 2005 */ #include "builtin.h" +#include "attr.h" #include "config.h" #include "editor.h" #include "ident.h" @@ -41,18 +42,41 @@ static const char *shell_path(int flag) return SHELL_PATH; } +static const char *git_attr_val_system(int flag) +{ + if (git_attr_system()) { + char *file = xstrdup(git_etc_gitattributes()); + normalize_path_copy(file, file); + return file; + } + return NULL; +} + +static const char *git_attr_val_global(int flag) +{ + char *file = xstrdup(get_home_gitattributes()); + if (file) { + normalize_path_copy(file, file); + return file; + } + return NULL; +} + struct git_var { const char *name; const char *(*read)(int); + int free; }; static struct git_var git_vars[] = { - { "GIT_COMMITTER_IDENT", git_committer_info }, - { "GIT_AUTHOR_IDENT", git_author_info }, - { "GIT_EDITOR", editor }, - { "GIT_SEQUENCE_EDITOR", sequence_editor }, - { "GIT_PAGER", pager }, - { "GIT_DEFAULT_BRANCH", default_branch }, - { "GIT_SHELL_PATH", shell_path }, + { "GIT_COMMITTER_IDENT", git_committer_info, 0 }, + { "GIT_AUTHOR_IDENT", git_author_info, 0 }, + { "GIT_EDITOR", editor, 0 }, + { "GIT_SEQUENCE_EDITOR", sequence_editor, 0 }, + { "GIT_PAGER", pager, 0 }, + { "GIT_DEFAULT_BRANCH", default_branch, 0 }, + { "GIT_SHELL_PATH", shell_path, 0 }, + { "GIT_ATTR_SYSTEM", git_attr_val_system, 1 }, + { "GIT_ATTR_GLOBAL", git_attr_val_global, 1 }, { "", NULL }, }; @@ -62,8 +86,11 @@ static void list_vars(void) const char *val; for (ptr = git_vars; ptr->read; ptr++) - if ((val = ptr->read(0))) + if ((val = ptr->read(0))) { printf("%s=%s\n", ptr->name, val); + if (ptr->free) + free((void *)val); + } } static const struct git_var *get_git_var(const char *var) @@ -110,6 +137,8 @@ int cmd_var(int argc, const char **argv, const char *prefix UNUSED) return 1; printf("%s\n", val); + if (git_var->free) + free((void *)val); return 0; } diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh index 270bd4e512..6a2cc94abb 100755 --- a/t/t0007-git-var.sh +++ b/t/t0007-git-var.sh @@ -159,6 +159,26 @@ test_expect_success MINGW 'GIT_SHELL_PATH points to a suitable shell' ' grep "sh\$" shell ' +test_expect_success 'GIT_ATTR_SYSTEM points to the correct location' ' + test_must_fail env GIT_ATTR_NOSYSTEM=1 git var GIT_ATTR_SYSTEM && + ( + sane_unset GIT_ATTR_NOSYSTEM && + git var GIT_ATTR_SYSTEM >path && + test "$(cat path)" != "" + ) +' + +test_expect_success 'GIT_ATTR_GLOBAL points to the correct location' ' + TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && + XDG_CONFIG_HOME="$TRASHDIR/.config" git var GIT_ATTR_GLOBAL >path && + test "$(cat path)" = "$TRASHDIR/.config/git/attributes" && + ( + sane_unset XDG_CONFIG_HOME && + HOME="$TRASHDIR" git var GIT_ATTR_GLOBAL >path && + test "$(cat path)" = "$TRASHDIR/.config/git/attributes" + ) +' + # For git var -l, we check only a representative variable; # testing the whole output would make our test too brittle with # respect to unrelated changes in the test suite's environment. From patchwork Thu Jun 22 19:50:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "brian m. carlson" X-Patchwork-Id: 13289563 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 B3D30C001B0 for ; Thu, 22 Jun 2023 19:51:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231137AbjFVTvh (ORCPT ); Thu, 22 Jun 2023 15:51:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230503AbjFVTvd (ORCPT ); Thu, 22 Jun 2023 15:51:33 -0400 Received: from ring.crustytoothpaste.net (ring.crustytoothpaste.net [172.105.110.227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C7811FED for ; Thu, 22 Jun 2023 12:51:31 -0700 (PDT) Received: from tapette.crustytoothpaste.net (unknown [IPv6:2001:470:b056:101:5e4a:89fa:93b9:2058]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by ring.crustytoothpaste.net (Postfix) with ESMTPSA id 787555B415; Thu, 22 Jun 2023 19:51:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1687463490; bh=ltsGQj7nNggI1iT06O39TcGI5pEsBobKBm/io0V2Wvk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Reply-To: Subject:Date:To:CC:Resent-Date:Resent-From:Resent-To:Resent-Cc: In-Reply-To:References:Content-Type:Content-Disposition; b=e9gOPOj2K25IzcV/1lJEAgAwszAJI5GDNuzX1GZPuBRpas2z24kOxftrXNzrPAcxk vTgCdNdKT/FzAlIwH/7DYNtwfrgxtzUo2sKu07mJfUuwgHSqKf1KljR/YwySEk+m/+ plznwEXeF33VAoIFNUYc0IJa3NzoPPa58InWRXeuig7V7L2u+igGRyv1zHPGSYQWlC xqVtoKEbQKATjtKL2hCdE609UH6iurxnfHj72cnbICn6RAwoDTKmNomA2ur9qerlXH 0LSjuI2HVC1xi9Qy7eD95sh+ezn5YnMGa5jF3EuZMNn86DY2432oCwI8aEV9FfrviX 4oLlaOGS4SS0imQO633POIF7AxEqtBr9qWsoXbOVoV0fERuGBxrK2KGcCYOos+qzF7 ktl4OSBKHytazrtOiXlEAqOkgD7A3zmHlE8rzel7WJkeu47NcT9QWF4VzKFyd+IZIg JYfIUErgGKXL5KIZ6k3ikmeMCZMLDWl3Q3At9CTD/SdT0IJXyj+ From: "brian m. carlson" To: Cc: Junio C Hamano , Elijah Newren , Calvin Wan Subject: [PATCH 3/3] var: add config file locations Date: Thu, 22 Jun 2023 19:50:59 +0000 Message-ID: <20230622195059.320593-4-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8 In-Reply-To: <20230622195059.320593-1-sandals@crustytoothpaste.net> References: <20230622195059.320593-1-sandals@crustytoothpaste.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: "brian m. carlson" Much like with attributes files, sometimes programs would like to know the location of configuration files at the global or system levels. However, it isn't always clear where these may live, especially for the system file, which may have been hard-coded at compile time or computed dynamically based on the runtime prefix. Since other parties cannot intuitively know how Git was compiled and where it looks for these files, help them by providing variables that can be queried. Because we have multiple paths for global config values, print them in order from highest to lowest priority, and be sure to split on newlines so that "git var -l" produces two entries for the global value. However, be careful not to split all values on newlines, since our editor values could well contain such characters, and we don't want to split them in such a case. Note in the documentation that some values may contain multiple paths and that callers should be prepared for that fact. This helps people write code that will continue to work in the event we allow multiple items elsewhere in the future. Signed-off-by: brian m. carlson --- Documentation/git-var.txt | 14 ++++++++ builtin/var.c | 68 +++++++++++++++++++++++++++++++++------ t/t0007-git-var.sh | 66 +++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 10 deletions(-) diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt index dfbe5cb52b..c38fb3968b 100644 --- a/Documentation/git-var.txt +++ b/Documentation/git-var.txt @@ -80,6 +80,20 @@ GIT_ATTR_SYSTEM:: GIT_ATTR_GLOBAL:: The path to the global (per-user) linkgit:gitattributes[5] file. +GIT_CONFIG_SYSTEM:: + The path to the system configuration file, if one is enabled. + +GIT_CONFIG_GLOBAL:: + The path to the global (per-user) configuration files, if any. + +Most path values contain only one value. However, some can contain multiple +values, which are separated by newlines, and are listed in order from highest to +lowest priority. Callers should be prepared for any such path value to contain +multiple items. + +Note that paths are printed even if they do not exist, but not if they are +disabled by other environment variables. + SEE ALSO -------- linkgit:git-commit-tree[1] diff --git a/builtin/var.c b/builtin/var.c index b9e2f23697..8a47b74777 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -62,21 +62,59 @@ static const char *git_attr_val_global(int flag) return NULL; } +static const char *git_config_val_system(int flag) +{ + if (git_config_system()) { + char *file = git_system_config(); + normalize_path_copy(file, file); + return file; + } + return NULL; +} + +static const char *git_config_val_global(int flag) +{ + struct strbuf buf = STRBUF_INIT; + char *user, *xdg; + size_t unused; + + git_global_config(&user, &xdg); + if (xdg && *xdg) { + normalize_path_copy(xdg, xdg); + strbuf_addf(&buf, "%s\n", xdg); + } + if (user && *user) { + normalize_path_copy(user, user); + strbuf_addf(&buf, "%s\n", user); + } + free(xdg); + free(user); + strbuf_trim_trailing_newline(&buf); + if (buf.len == 0) { + strbuf_release(&buf); + return NULL; + } + return strbuf_detach(&buf, &unused); +} + struct git_var { const char *name; const char *(*read)(int); + int multivalued; int free; }; static struct git_var git_vars[] = { - { "GIT_COMMITTER_IDENT", git_committer_info, 0 }, - { "GIT_AUTHOR_IDENT", git_author_info, 0 }, - { "GIT_EDITOR", editor, 0 }, - { "GIT_SEQUENCE_EDITOR", sequence_editor, 0 }, - { "GIT_PAGER", pager, 0 }, - { "GIT_DEFAULT_BRANCH", default_branch, 0 }, - { "GIT_SHELL_PATH", shell_path, 0 }, - { "GIT_ATTR_SYSTEM", git_attr_val_system, 1 }, - { "GIT_ATTR_GLOBAL", git_attr_val_global, 1 }, + { "GIT_COMMITTER_IDENT", git_committer_info, 0, 0 }, + { "GIT_AUTHOR_IDENT", git_author_info, 0, 0 }, + { "GIT_EDITOR", editor, 0, 0 }, + { "GIT_SEQUENCE_EDITOR", sequence_editor, 0, 0 }, + { "GIT_PAGER", pager, 0, 0 }, + { "GIT_DEFAULT_BRANCH", default_branch, 0, 9 }, + { "GIT_SHELL_PATH", shell_path, 0, 0 }, + { "GIT_ATTR_SYSTEM", git_attr_val_system, 0, 1 }, + { "GIT_ATTR_GLOBAL", git_attr_val_global, 0, 1 }, + { "GIT_CONFIG_SYSTEM", git_config_val_system, 0, 1 }, + { "GIT_CONFIG_GLOBAL", git_config_val_global, 1, 1 }, { "", NULL }, }; @@ -87,7 +125,17 @@ static void list_vars(void) for (ptr = git_vars; ptr->read; ptr++) if ((val = ptr->read(0))) { - printf("%s=%s\n", ptr->name, val); + if (ptr->multivalued && *val) { + struct string_list list = STRING_LIST_INIT_DUP; + int i; + + string_list_split(&list, val, '\n', -1); + for (i = 0; i < list.nr; i++) + printf("%s=%s\n", ptr->name, list.items[i].string); + string_list_clear(&list, 0); + } else { + printf("%s=%s\n", ptr->name, val); + } if (ptr->free) free((void *)val); } diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh index 6a2cc94abb..d519c2f441 100755 --- a/t/t0007-git-var.sh +++ b/t/t0007-git-var.sh @@ -179,6 +179,49 @@ test_expect_success 'GIT_ATTR_GLOBAL points to the correct location' ' ) ' +test_expect_success 'GIT_CONFIG_SYSTEM points to the correct location' ' + TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && + test_must_fail env GIT_CONFIG_NOSYSTEM=1 git var GIT_CONFIG_SYSTEM && + ( + sane_unset GIT_CONFIG_NOSYSTEM && + git var GIT_CONFIG_SYSTEM >path && + test "$(cat path)" != "" && + GIT_CONFIG_SYSTEM=/dev/null git var GIT_CONFIG_SYSTEM >path && + if test_have_prereq MINGW + then + test "$(cat path)" = "nul" + else + test "$(cat path)" = "/dev/null" + fi && + GIT_CONFIG_SYSTEM="$TRASHDIR/gitconfig" git var GIT_CONFIG_SYSTEM >path && + test "$(cat path)" = "$TRASHDIR/gitconfig" + ) +' + +test_expect_success 'GIT_CONFIG_GLOBAL points to the correct location' ' + TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && + HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" git var GIT_CONFIG_GLOBAL >actual && + echo "$TRASHDIR/foo/git/config" >expected && + echo "$TRASHDIR/.gitconfig" >>expected && + test_cmp expected actual && + ( + sane_unset XDG_CONFIG_HOME && + HOME="$TRASHDIR" git var GIT_CONFIG_GLOBAL >actual && + echo "$TRASHDIR/.config/git/config" >expected && + echo "$TRASHDIR/.gitconfig" >>expected && + test_cmp expected actual && + GIT_CONFIG_GLOBAL=/dev/null git var GIT_CONFIG_GLOBAL >path && + if test_have_prereq MINGW + then + test "$(cat path)" = "nul" + else + test "$(cat path)" = "/dev/null" + fi && + GIT_CONFIG_GLOBAL="$TRASHDIR/gitconfig" git var GIT_CONFIG_GLOBAL >path && + test "$(cat path)" = "$TRASHDIR/gitconfig" + ) +' + # For git var -l, we check only a representative variable; # testing the whole output would make our test too brittle with # respect to unrelated changes in the test suite's environment. @@ -196,6 +239,29 @@ test_expect_success 'git var -l lists config' ' test_cmp expect actual.bare ' +test_expect_success 'git var -l lists multiple global configs' ' + TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && + HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" git var -l >actual && + grep "^GIT_CONFIG_GLOBAL=" actual >filtered && + echo "GIT_CONFIG_GLOBAL=$TRASHDIR/foo/git/config" >expected && + echo "GIT_CONFIG_GLOBAL=$TRASHDIR/.gitconfig" >>expected && + test_cmp expected filtered +' + +test_expect_success 'git var -l does not split multiline editors' ' + ( + GIT_EDITOR="!f() { + echo Hello! + }; f" && + export GIT_EDITOR && + echo "GIT_EDITOR=$GIT_EDITOR" >expected && + git var -l >var && + cat var && + sed -n -e "/^GIT_EDITOR/,\$p" var | head -n 3 >actual && + test_cmp expected actual + ) +' + test_expect_success 'listing and asking for variables are exclusive' ' test_must_fail git var -l GIT_COMMITTER_IDENT '