From patchwork Mon Jun 26 19:00:02 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: 13293404 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 0ABA0EB64D9 for ; Mon, 26 Jun 2023 19:00:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229964AbjFZTA5 (ORCPT ); Mon, 26 Jun 2023 15:00:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229488AbjFZTAz (ORCPT ); Mon, 26 Jun 2023 15:00:55 -0400 Received: from ring.crustytoothpaste.net (ring.crustytoothpaste.net [172.105.110.227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80F83E75 for ; Mon, 26 Jun 2023 12:00:52 -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 532035B417; Mon, 26 Jun 2023 19:00:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1687806051; bh=5rqWhIMYQaEpvu6WVeVyxDSh8oTmcDWswhMznNJtt3A=; 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=hEb6H+t1aeY3yK/7w/6/dOnpIREPOkq7OWVUBqwsWn9UFeMPAs34al6RhaBiYfehi 2UdSdT90aa0IXAlUQ0rVfb2ST1FM0ukPu5EoBPHv8NNLwHyV7O638/ScKLcsr2M9fN Qr/wawQLOO6N+xzoFB16ob3+ihXL4Gc4Hk/4hk2/rr3GzPbnMDokKNaA1jbA2koPKA 1suBcv9/jdo6QZeBGLcl8EtVwLp75bQlR1GPax0wD1RWfk7VQoLUjmLsK0AlO3WGWv J4bVFHS8NdJGzS90qRJ6MRJrjoZwewkynhkMj9Bf40pH8PDkwVCpwTPf6/O9iYkDzM AVlWp6GBwMpeQ2HGSOsQtZN7MDVaE5NpDzrYLRMMJIdCOTY/ZDjFomtN5FLz8fpGM1 XA8MYmTfxY5SJvE7tO5L4SS5KHwRAZhWWjkKKbNu409i+89r15u2yq8I1zqLPKs7T3 RgUtmpqeHCiD0dPmH5eKS8hIVO6Dk9Svmd+DzQSjDq+slUXmVd+ From: "brian m. carlson" To: Cc: Junio C Hamano , Eric Sunshine , Derrick Stolee Subject: [PATCH v2 1/7] t: add a function to check executable bit Date: Mon, 26 Jun 2023 19:00:02 +0000 Message-ID: <20230626190008.644769-2-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8 In-Reply-To: <20230626190008.644769-1-sandals@crustytoothpaste.net> References: <20230622195059.320593-1-sandals@crustytoothpaste.net> <20230626190008.644769-1-sandals@crustytoothpaste.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: "brian m. carlson" In line with our other helper functions for paths, let's add a function to check whether a path is executable, and if not, print a suitable error message. Document this function, and note that it must only be used under the POSIXPERM prerequisite, since it doesn't otherwise work on Windows. Signed-off-by: brian m. carlson --- t/README | 6 ++++++ t/test-lib-functions.sh | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/t/README b/t/README index bdfac4cceb..3e155011de 100644 --- a/t/README +++ b/t/README @@ -1098,6 +1098,12 @@ see test-lib-functions.sh for the full list and their options. the symbolic link in the file system and a part that does; then only the latter part need be protected by a SYMLINKS prerequisite (see below). + - test_path_is_executable + + This tests whether a file is executable and prints an error message + if not. This must be used only under the POSIXPERM prerequisite + (see below). + - test_oid_init This function loads facts and useful object IDs related to the hash diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index b3864e22e9..2fa716c567 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -910,6 +910,15 @@ test_path_is_symlink () { fi } +test_path_is_executable () { + test "$#" -ne 1 && BUG "1 param" + if ! test -x "$1" + then + echo "$1 is not executable" + false + fi +} + # Check if the directory exists and is empty as expected, barf otherwise. test_dir_is_empty () { test "$#" -ne 1 && BUG "1 param" From patchwork Mon Jun 26 19:00:03 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: 13293408 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 C4779C0015E for ; Mon, 26 Jun 2023 19:01:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231268AbjFZTBE (ORCPT ); Mon, 26 Jun 2023 15:01:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230151AbjFZTA5 (ORCPT ); Mon, 26 Jun 2023 15:00:57 -0400 Received: from ring.crustytoothpaste.net (ring.crustytoothpaste.net [IPv6:2600:3c04::f03c:92ff:fe9e:c6d8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 930C110CC for ; Mon, 26 Jun 2023 12:00:52 -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 61D425B419; Mon, 26 Jun 2023 19:00:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1687806051; bh=+XOLxzptZrn6kzkSGVkUsaHIv/gAwdykti5MvVK2Tww=; 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=0e9ezZKb25CSiW6mwfNUeK+KS6lHCOhQDY5JIgBueKnh+okx2vK9EQlRldV0RgQOu KURAvnzCt3o8ejx/UHGmx9MWlvbT1tPZPwhj/eOzi4D2FQL9pVZbttQRZk6JvRMPKe EasXL7wBiTyhmnA86t42S4vBoJJVX5K5Z0sR4UL/lMJEU/+7jnyM0/Z7smHSJtprpt O+iobIymGewdZKrhPv9uCqs0nJvZ5xO4JwqyIZKoStGg621W+BYmQ7AsGwUpynO0dm Yl8ELCSDPa5naJjbBi1aQMSAzvbVMc6wsS0f4ol9Dxh4v+wLKYdi+B+AsM0hHc55iD ECrA6ePt2EVHwLZ+rJkZwm6zmEQzbu0DSA8ostyiB7ycS2Gj/0Kj0OJtUUVcdkpcjX tlIBr4NGTXv50mBqttXv07UAqnv+6YHhbIJuaa4wBuYJ9iQIzEFDjCTZuYN1NgRwAK us3OE32EY1G3ifShsHYdjwsG6HLvZ+sn5WFU1UEdUSEJg3spx+V From: "brian m. carlson" To: Cc: Junio C Hamano , Eric Sunshine , Derrick Stolee Subject: [PATCH v2 2/7] var: add support for listing the shell Date: Mon, 26 Jun 2023 19:00:03 +0000 Message-ID: <20230626190008.644769-3-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8 In-Reply-To: <20230626190008.644769-1-sandals@crustytoothpaste.net> References: <20230622195059.320593-1-sandals@crustytoothpaste.net> <20230626190008.644769-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 | 15 +++++++++++++++ 3 files changed, 24 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..e35f07afcb 100755 --- a/t/t0007-git-var.sh +++ b/t/t0007-git-var.sh @@ -147,6 +147,21 @@ test_expect_success 'get GIT_SEQUENCE_EDITOR with configuration and environment ) ' +test_expect_success POSIXPERM 'GIT_SHELL_PATH points to a valid executable' ' + shellpath=$(git var GIT_SHELL_PATH) && + test_path_is_executable "$shellpath" +' + +# 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' ' + shellpath=$(git var GIT_SHELL_PATH) && + case "$shellpath" in + *sh) ;; + *) return 1;; + esac +' + # 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 Mon Jun 26 19:00:04 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: 13293406 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 8BBF3EB64D9 for ; Mon, 26 Jun 2023 19:01:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229621AbjFZTBA (ORCPT ); Mon, 26 Jun 2023 15:01:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230164AbjFZTA4 (ORCPT ); Mon, 26 Jun 2023 15:00:56 -0400 Received: from ring.crustytoothpaste.net (ring.crustytoothpaste.net [IPv6:2600:3c04::f03c:92ff:fe9e:c6d8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8103810C2 for ; Mon, 26 Jun 2023 12:00:52 -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 7006F5B41B; Mon, 26 Jun 2023 19:00:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1687806051; bh=I13hPmqncRz3v0+bIyLLj0sZb0vf9WiWwKWhDxrwmoc=; 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=jk8v7Tn58RiuqZg7k7ItrU5qODUorPwMpMzKejzx21WYVVODPwUHQuI6oqh/1hERQ CeH9l6cD86LqQhs7mtcFoghZfQEQsXtk8lK96dJJCa8hka0HfSXUoh/tJNfPDVTjZd fbx6MLt9zZagWQxtpYkURIBJ6nG2mwyieYLuLJoIaRzMmDo+aOXv00keWKXqo/jfGy 114fsLVwHAQUQnPtjo1tBkfNtIwhTAoCArQjf6nHure1uf2NzbLC34VTmcITaRgd5T w26Yif2qFWrgI8tzfC81egm+ke8JhoCC8pyR5EWedBg2LLNwg7yYyHmPNDBS+/GPRg /T6XKlyz8Szc/t0wbREztX5xdjC4l0wsZGTOe9aaKepGvmS5D/8zpDqlkOu5wvWEnf kV3JvawyoUbaLNZl3ksPTsTIjodc38jCumXK04j8YnDm/DPKxs0Gme7Gf+ab/W6ZrS aRyXdzL4bey/oi8rMS54/hw/R+oO9JC9943nK/HJXgKFdPntDBO From: "brian m. carlson" To: Cc: Junio C Hamano , Eric Sunshine , Derrick Stolee Subject: [PATCH v2 3/7] var: format variable structure with C99 initializers Date: Mon, 26 Jun 2023 19:00:04 +0000 Message-ID: <20230626190008.644769-4-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8 In-Reply-To: <20230626190008.644769-1-sandals@crustytoothpaste.net> References: <20230622195059.320593-1-sandals@crustytoothpaste.net> <20230626190008.644769-1-sandals@crustytoothpaste.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: "brian m. carlson" Right now, we have only two items in our variable struct. However, in the future, we're going to add two more items. To help keep our diffs nice and tidy and make this structure easier to read, switch to use C99-style initializers for our data. Signed-off-by: brian m. carlson --- builtin/var.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/builtin/var.c b/builtin/var.c index f97178eed1..bd2750b1bf 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -46,14 +46,38 @@ struct git_var { const char *(*read)(int); }; 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 }, - { "", NULL }, + { + .name = "GIT_COMMITTER_IDENT", + .read = git_committer_info, + }, + { + .name = "GIT_AUTHOR_IDENT", + .read = git_author_info, + }, + { + .name = "GIT_EDITOR", + .read = editor, + }, + { + .name = "GIT_SEQUENCE_EDITOR", + .read = sequence_editor, + }, + { + .name = "GIT_PAGER", + .read = pager, + }, + { + .name = "GIT_DEFAULT_BRANCH", + .read = default_branch, + }, + { + .name = "GIT_SHELL_PATH", + .read = shell_path, + }, + { + .name = "", + .read = NULL, + }, }; static void list_vars(void) From patchwork Mon Jun 26 19:00:05 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: 13293407 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 31FF3EB64DD for ; Mon, 26 Jun 2023 19:01:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230373AbjFZTBD (ORCPT ); Mon, 26 Jun 2023 15:01:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230203AbjFZTA4 (ORCPT ); Mon, 26 Jun 2023 15:00:56 -0400 Received: from ring.crustytoothpaste.net (ring.crustytoothpaste.net [172.105.110.227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A439D10D5 for ; Mon, 26 Jun 2023 12:00:52 -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 7E52F5B41C; Mon, 26 Jun 2023 19:00:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1687806051; bh=c5LF6Yav/E/4nlZL6pGdaHUA04MoIi59swlSjaCKaUo=; 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=m4BXCLiiyg16//+2uQyUl2jDsJKne2/dE4j75h+6cibwhPMl7wd29GVe80jDzAJdy m/AxdpkNlcGgA8M6kFvd7SMUyUO+rRqOvl3eK29lP+XsecKTGJP5JhG4ms3EPHgBan Xe1stpdxMLZfnBrtdbfpgW0SI04TWT5Cj5W7VpycMApYCy4Kij6306UIrtehJlpBDO gYRKk+XRsbFjFK8rCEauz7MO7iUg9yC+qw48a7wtdDpkKhqzka+bRjxUO903lit877 ofOedNFcq+5ELRVbGMmGZErFtk5SSTJ0JaXRDYonpiatoxxidi4BVfdV4GCOmw0v5d 0in6X1JssgELpyUJHfG5VqaFAudZZ1cZSeJwQL8LUxMttaqzZecKJHMmNLhFUF9IG8 p0GJPFGGHwJVr4Ea0EsbJKzmtYi65aTc1rGw8jCBhLKireL8nWpET1M/4irActa6yK qvpQpMdW2kFvMqyhR2kiy1/mnZWLImK98g3FhiMfVAMu7QX/WwW From: "brian m. carlson" To: Cc: Junio C Hamano , Eric Sunshine , Derrick Stolee Subject: [PATCH v2 4/7] var: adjust memory allocation for strings Date: Mon, 26 Jun 2023 19:00:05 +0000 Message-ID: <20230626190008.644769-5-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8 In-Reply-To: <20230626190008.644769-1-sandals@crustytoothpaste.net> References: <20230622195059.320593-1-sandals@crustytoothpaste.net> <20230626190008.644769-1-sandals@crustytoothpaste.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: "brian m. carlson" Right now, all of our values are constants whose allocation is managed elsewhere. However, in the future, we'll have some variables whose memory we will need to free. To keep things consistent, let's make each of our functions allocate its own memory and make the caller responsible for freeing it. Signed-off-by: brian m. carlson --- builtin/var.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/builtin/var.c b/builtin/var.c index bd2750b1bf..ce6a2231ca 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -12,47 +12,57 @@ static const char var_usage[] = "git var (-l | )"; -static const char *editor(int flag) +static char *committer(int flag) { - return git_editor(); + return xstrdup_or_null(git_committer_info(flag)); } -static const char *sequence_editor(int flag) +static char *author(int flag) { - return git_sequence_editor(); + return xstrdup_or_null(git_author_info(flag)); } -static const char *pager(int flag) +static char *editor(int flag) +{ + return xstrdup_or_null(git_editor()); +} + +static char *sequence_editor(int flag) +{ + return xstrdup_or_null(git_sequence_editor()); +} + +static char *pager(int flag) { const char *pgm = git_pager(1); if (!pgm) pgm = "cat"; - return pgm; + return xstrdup(pgm); } -static const char *default_branch(int flag) +static char *default_branch(int flag) { - return git_default_branch_name(1); + return xstrdup_or_null(git_default_branch_name(1)); } -static const char *shell_path(int flag) +static char *shell_path(int flag) { - return SHELL_PATH; + return xstrdup(SHELL_PATH); } struct git_var { const char *name; - const char *(*read)(int); + char *(*read)(int); }; static struct git_var git_vars[] = { { .name = "GIT_COMMITTER_IDENT", - .read = git_committer_info, + .read = committer, }, { .name = "GIT_AUTHOR_IDENT", - .read = git_author_info, + .read = author, }, { .name = "GIT_EDITOR", @@ -83,11 +93,13 @@ static struct git_var git_vars[] = { static void list_vars(void) { struct git_var *ptr; - const char *val; + 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); + free(val); + } } static const struct git_var *get_git_var(const char *var) @@ -113,7 +125,7 @@ static int show_config(const char *var, const char *value, void *cb) int cmd_var(int argc, const char **argv, const char *prefix UNUSED) { const struct git_var *git_var; - const char *val; + char *val; if (argc != 2) usage(var_usage); @@ -134,6 +146,7 @@ int cmd_var(int argc, const char **argv, const char *prefix UNUSED) return 1; printf("%s\n", val); + free(val); return 0; } From patchwork Mon Jun 26 19:00:06 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: 13293409 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 03AA7EB64D7 for ; Mon, 26 Jun 2023 19:01:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231197AbjFZTBQ (ORCPT ); Mon, 26 Jun 2023 15:01:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230517AbjFZTBC (ORCPT ); Mon, 26 Jun 2023 15:01:02 -0400 Received: from ring.crustytoothpaste.net (ring.crustytoothpaste.net [IPv6:2600:3c04::f03c:92ff:fe9e:c6d8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B57310FE for ; Mon, 26 Jun 2023 12:00:56 -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 8DAAC5B41D; Mon, 26 Jun 2023 19:00:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1687806051; bh=WdsKnMu+237T7FLdP1HSugCwl1Lo8d8TMMxVbzR0vsE=; 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=hJYyObGsPpKkfLCOpU6zuHHSxq+keMBG+1/nGvdPzHqpU+c12xUajBk3auQi1sX6W Yej0shPimhsg4FXRZtEdl7Kw8mIPQDqlGlvnZVh2aoncSnDOxmIqWTrYley7J8tnZK FODeAUfq4h/ZlnRdWbgi0tcXh52sABPbQ62vwwHYTpSJOWF2RUHc6KK/Z0MrHYXGLG M/U4STNEzalg+J6J4fqf5zzQrYlP1dG4mOKf5n0DrbUx9OoDceUECOu7SSMyLq9pY4 Wavo+4WkQhv5dl2/zRqmeJNtL4q36rpX4q5luej2xeIx20dOvuCcd5toGBGgEY86cA 6Hic2EkKMBK7uBJBYmVZCO0LZ+bDr2SZdsEaLGHO1g03ihRnXZhvGDaY1Y07ZHV9yf TfXJi7szENCzczxvNEvMM+WyLzXz8qGUN5NDzC0hJ46aIxoH2/hhI2BtIZqYFFWfi6 nTJQNRfyWCNwaSXY25KDOKhW5yHdNeTW6rw3Faiq5LRqSNVjomc From: "brian m. carlson" To: Cc: Junio C Hamano , Eric Sunshine , Derrick Stolee Subject: [PATCH v2 5/7] attr: expose and rename accessor functions Date: Mon, 26 Jun 2023 19:00:06 +0000 Message-ID: <20230626190008.644769-6-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8 In-Reply-To: <20230626190008.644769-1-sandals@crustytoothpaste.net> References: <20230622195059.320593-1-sandals@crustytoothpaste.net> <20230626190008.644769-1-sandals@crustytoothpaste.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: "brian m. carlson" Right now, the functions which determine the current system and global gitattributes files are not exposed. We'd like to use them in a future commit, but they're not ideally named. Rename them to something more suitable as a public interface, expose them, and document them. Signed-off-by: brian m. carlson --- attr.c | 14 +++++++------- attr.h | 9 +++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/attr.c b/attr.c index d45d34058d..66203ce322 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_attr_system_file(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 *git_attr_global_file(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_is_enabled(void) { return !git_env_bool("GIT_ATTR_NOSYSTEM", 0); } @@ -920,14 +920,14 @@ static void bootstrap_attr_stack(struct index_state *istate, push_stack(stack, e, NULL, 0); /* system-wide frame */ - if (git_attr_system()) { - e = read_attr_from_file(git_etc_gitattributes(), flags); + if (git_attr_system_is_enabled()) { + e = read_attr_from_file(git_attr_system_file(), flags); push_stack(stack, e, NULL, 0); } /* home directory */ - if (get_home_gitattributes()) { - e = read_attr_from_file(get_home_gitattributes(), flags); + if (git_attr_global_file()) { + e = read_attr_from_file(git_attr_global_file(), flags); push_stack(stack, e, NULL, 0); } diff --git a/attr.h b/attr.h index 676bd17ce2..2b745df405 100644 --- a/attr.h +++ b/attr.h @@ -227,4 +227,13 @@ void git_attr_set_direction(enum git_attr_direction new_direction); void attr_start(void); +/* Return the system gitattributes file. */ +const char *git_attr_system_file(void); + +/* Return the global gitattributes file, if any. */ +const char *git_attr_global_file(void); + +/* Return whether the system gitattributes file is enabled and should be used. */ +int git_attr_system_is_enabled(void); + #endif /* ATTR_H */ From patchwork Mon Jun 26 19:00:07 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: 13293410 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 A5094EB64D9 for ; Mon, 26 Jun 2023 19:01:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231424AbjFZTBS (ORCPT ); Mon, 26 Jun 2023 15:01:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231140AbjFZTBC (ORCPT ); Mon, 26 Jun 2023 15:01:02 -0400 Received: from ring.crustytoothpaste.net (ring.crustytoothpaste.net [IPv6:2600:3c04::f03c:92ff:fe9e:c6d8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AB6A10FD for ; Mon, 26 Jun 2023 12:00:56 -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 9B5F95B41E; Mon, 26 Jun 2023 19:00:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1687806051; bh=/6BQBmcTjpBjFlHB1wD7rRpRv2H2jGxwZ4m5Alck5zk=; 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=mJ0C+OHgxHvy6MYJbqNgUcCfaEU587ljy4SLdggstA/AXbkhJ/5CwXuSE3htVNtLU UF/0oBLS2aNy1Q9f7k3Z4re+SBq7XA2r7qdfaf8Co3qBHiZHAC8agSWHyPAO2lqafb fSMCGz4BcXDy0NWQ29YgMPad1LuD28tgcrW57PN+tsyQkkouXfmoxmZcKfK8njaIMQ P+aYprl13RhaILzzkqImkV097Xq+sqIxmQKDGGVTA0PzCqDFxZREReEOc20KGqtA0C X6AhE2J0Zy1eQQ8+d55ENVj899+roqDhZJBGxEtnPukepgVjyx8z6r4Z4JYqwF1abL R89gLU+9bQs/eUXqiFOE4sL+7tNNlA0tRR5KqNrOhZpBrLQuTYxxym1eFzitX8oIC/ PngjYiwJcPiONWSx5o19FTAijKtRpBreXqfy+SjmQRlxMrPWxqsBFB6oxfb7slJevy CYZzQbhqH300WaWWOzM3JUw+GJgiDO/pbkH6D9N4mHX+h+GDnbj From: "brian m. carlson" To: Cc: Junio C Hamano , Eric Sunshine , Derrick Stolee Subject: [PATCH v2 6/7] var: add attributes files locations Date: Mon, 26 Jun 2023 19:00:07 +0000 Message-ID: <20230626190008.644769-7-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8 In-Reply-To: <20230626190008.644769-1-sandals@crustytoothpaste.net> References: <20230622195059.320593-1-sandals@crustytoothpaste.net> <20230626190008.644769-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 Signed-off-by: Jeff King --- Documentation/git-var.txt | 6 ++++++ builtin/var.c | 29 +++++++++++++++++++++++++++++ t/t0007-git-var.sh | 20 ++++++++++++++++++++ 3 files changed, 55 insertions(+) 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/builtin/var.c b/builtin/var.c index ce6a2231ca..7cec05a49a 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" @@ -51,6 +52,26 @@ static char *shell_path(int flag) return xstrdup(SHELL_PATH); } +static char *git_attr_val_system(int flag) +{ + if (git_attr_system_is_enabled()) { + char *file = xstrdup(git_attr_system_file()); + normalize_path_copy(file, file); + return file; + } + return NULL; +} + +static char *git_attr_val_global(int flag) +{ + char *file = xstrdup(git_attr_global_file()); + if (file) { + normalize_path_copy(file, file); + return file; + } + return NULL; +} + struct git_var { const char *name; char *(*read)(int); @@ -84,6 +105,14 @@ static struct git_var git_vars[] = { .name = "GIT_SHELL_PATH", .read = shell_path, }, + { + .name = "GIT_ATTR_SYSTEM", + .read = git_attr_val_system, + }, + { + .name = "GIT_ATTR_GLOBAL", + .read = git_attr_val_global, + }, { .name = "", .read = NULL, diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh index e35f07afcb..374d9f49e9 100755 --- a/t/t0007-git-var.sh +++ b/t/t0007-git-var.sh @@ -162,6 +162,26 @@ test_expect_success MINGW 'GIT_SHELL_PATH points to a suitable shell' ' esac ' +test_expect_success 'GIT_ATTR_SYSTEM produces expected output' ' + test_must_fail env GIT_ATTR_NOSYSTEM=1 git var GIT_ATTR_SYSTEM && + ( + sane_unset GIT_ATTR_NOSYSTEM && + systempath=$(git var GIT_ATTR_SYSTEM) && + test "$systempath" != "" + ) +' + +test_expect_success 'GIT_ATTR_GLOBAL points to the correct location' ' + TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" && + globalpath=$(XDG_CONFIG_HOME="$TRASHDIR/.config" git var GIT_ATTR_GLOBAL) && + test "$globalpath" = "$TRASHDIR/.config/git/attributes" && + ( + sane_unset XDG_CONFIG_HOME && + globalpath=$(HOME="$TRASHDIR" git var GIT_ATTR_GLOBAL) && + test "$globalpath" = "$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 Mon Jun 26 19:00:08 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: 13293411 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 06D1AEB64DC for ; Mon, 26 Jun 2023 19:01:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231463AbjFZTBU (ORCPT ); Mon, 26 Jun 2023 15:01:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230167AbjFZTBD (ORCPT ); Mon, 26 Jun 2023 15:01:03 -0400 Received: from ring.crustytoothpaste.net (ring.crustytoothpaste.net [IPv6:2600:3c04::f03c:92ff:fe9e:c6d8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5B54F2 for ; Mon, 26 Jun 2023 12:00:56 -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 AFCE85B41F; Mon, 26 Jun 2023 19:00:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1687806051; bh=Tmztp6L1l8GZDfcyUcq1H/3jwKSY9Sjorj38606XzQg=; 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=XAqBoUeBZUb9M5tAgaKE/6OryGk5Ao1jGITQtWgTDpx90kDWK8TQ6Z3FrGfufJ8eS IINKJrwzW43QrOC3Xcj8g3aTpP+6NpNktu1YphKQj9lzaSgalfY8IXCtG6MnyymQxw Il9UqlsSSql0jih3rHphRtp5/jv5cqube4Zw9UYiw8sK0mXLwD2lywNPrlM/3ijS7+ FJ0rKiruQFXTsQz5fNHPfNhuXzbNRpOchDJXkfrHjd0IN56cgffI0V4+rLVJrWH4TT iwoPyT1RfuRly1yILudYNzejld0gyRZC6TxqMZjPwz+WdOCm8ZW+zTQHgYaMngEb7V vqHJ7/wYwz/pGxYjrhCNRxmGa+a/6PVM1J66X0j4NyypiKhpiKuVVgppPWhZw2F+Zt 7o/3cOpXqYqnvxxPvGeZY6N02n/F3Aq4cQsiSup4l3OirpIRZ9yIpsebotadGEsboC LpK7u6hRb+11kknpz6FtsfSQ7s0d9QtVw/RSBnJQ68Np7OVDndE From: "brian m. carlson" To: Cc: Junio C Hamano , Eric Sunshine , Derrick Stolee Subject: [PATCH v2 7/7] var: add config file locations Date: Mon, 26 Jun 2023 19:00:08 +0000 Message-ID: <20230626190008.644769-8-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.40.1.521.gf1e218fcd8 In-Reply-To: <20230626190008.644769-1-sandals@crustytoothpaste.net> References: <20230622195059.320593-1-sandals@crustytoothpaste.net> <20230626190008.644769-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 | 65 +++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) 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 7cec05a49a..bd4e331312 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -72,50 +72,106 @@ static char *git_attr_val_global(int flag) return NULL; } +static 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 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; char *(*read)(int); + int multivalued; }; static struct git_var git_vars[] = { { .name = "GIT_COMMITTER_IDENT", .read = committer, + .multivalued = 0, }, { .name = "GIT_AUTHOR_IDENT", .read = author, + .multivalued = 0, }, { .name = "GIT_EDITOR", .read = editor, + .multivalued = 0, }, { .name = "GIT_SEQUENCE_EDITOR", .read = sequence_editor, + .multivalued = 0, }, { .name = "GIT_PAGER", .read = pager, + .multivalued = 0, }, { .name = "GIT_DEFAULT_BRANCH", .read = default_branch, + .multivalued = 0, }, { .name = "GIT_SHELL_PATH", .read = shell_path, + .multivalued = 0, }, { .name = "GIT_ATTR_SYSTEM", .read = git_attr_val_system, + .multivalued = 0, }, { .name = "GIT_ATTR_GLOBAL", .read = git_attr_val_global, + .multivalued = 0, + }, + { + .name = "GIT_CONFIG_SYSTEM", + .read = git_config_val_system, + .multivalued = 0, + }, + { + .name = "GIT_CONFIG_GLOBAL", + .read = git_config_val_global, + .multivalued = 1, }, { .name = "", .read = NULL, + .multivalued = 0, }, }; @@ -126,7 +182,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); + } free(val); } } diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh index 374d9f49e9..8cb597f99c 100755 --- a/t/t0007-git-var.sh +++ b/t/t0007-git-var.sh @@ -182,6 +182,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 && + systempath=$(git var GIT_CONFIG_SYSTEM) && + test "$systempath" != "" && + systempath=$(GIT_CONFIG_SYSTEM=/dev/null git var GIT_CONFIG_SYSTEM) && + if test_have_prereq MINGW + then + test "$systempath" = "nul" + else + test "$systempath" = "/dev/null" + fi && + systempath=$(GIT_CONFIG_SYSTEM="$TRASHDIR/gitconfig" git var GIT_CONFIG_SYSTEM) && + test "$systempath" = "$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 && + globalpath=$(GIT_CONFIG_GLOBAL=/dev/null git var GIT_CONFIG_GLOBAL) && + if test_have_prereq MINGW + then + test "$globalpath" = "nul" + else + test "$globalpath" = "/dev/null" + fi && + globalpath=$(GIT_CONFIG_GLOBAL="$TRASHDIR/gitconfig" git var GIT_CONFIG_GLOBAL) && + test "$globalpath" = "$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. @@ -199,6 +242,28 @@ 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 && + 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 '