From f62563988fefed73d795cfaeae203421b784c334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Wed, 27 Apr 2022 19:06:47 +0200 Subject: [PATCH 1/3] t0033-safe-directory: check the error message without matching the trash dir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since 8959555cee (setup_git_directory(): add an owner check for the top-level directory, 2022-03-02) when git finds itself in a repository owned by someone else, it aborts with a "fatal: unsafe repository ()" error message and an advice about how to set the 'safe.directory' config variable to mark that repository as safe. 't0033-safe-directory.sh' contains tests that check that this feature and handling said config work as intended. To ensure that git dies for the right reason, several of those tests check that its standard error contains the name of that config variable, but: - it only appears in the advice part, not in the actual error message. - it is interpreted as a regexp by 'grep', so, because of the dot, it matches the name of the test script and the path of the trash directory as well. Consequently, these tests could be fooled by any error message that would happen to include the path of the test repository. Tighten these checks to look for "unsafe repository" instead. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- t/t0033-safe-directory.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t0033-safe-directory.sh b/t/t0033-safe-directory.sh index 239d93f4d2..6f9680e8b0 100755 --- a/t/t0033-safe-directory.sh +++ b/t/t0033-safe-directory.sh @@ -9,7 +9,7 @@ export GIT_TEST_ASSUME_DIFFERENT_OWNER expect_rejected_dir () { test_must_fail git status 2>err && - grep "safe.directory" err + grep "unsafe repository" err } test_expect_success 'safe.directory is not set' ' From 424f315d9f15338ee950f343e01becd6f087121b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Wed, 27 Apr 2022 19:06:48 +0200 Subject: [PATCH 2/3] t0033-safe-directory: check when 'safe.directory' is ignored MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to the documentation 'safe.directory' "is only respected when specified in a system or global config, not when it is specified in a repository config or via the command line option -c safe.directory=". Add tests to check that 'safe.directory' in the repository config or on the command line is indeed ignored. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- t/t0033-safe-directory.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/t/t0033-safe-directory.sh b/t/t0033-safe-directory.sh index 6f9680e8b0..82dac0eb93 100755 --- a/t/t0033-safe-directory.sh +++ b/t/t0033-safe-directory.sh @@ -16,6 +16,19 @@ test_expect_success 'safe.directory is not set' ' expect_rejected_dir ' +test_expect_success 'ignoring safe.directory on the command line' ' + test_must_fail git -c safe.directory="$(pwd)" status 2>err && + grep "unsafe repository" err +' + +test_expect_success 'ignoring safe.directory in repo config' ' + ( + unset GIT_TEST_ASSUME_DIFFERENT_OWNER && + git config safe.directory "$(pwd)" + ) && + expect_rejected_dir +' + test_expect_success 'safe.directory does not match' ' git config --global safe.directory bogus && expect_rejected_dir From 756d15923bf6806f94484054873e284728a89c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Wed, 27 Apr 2022 19:06:49 +0200 Subject: [PATCH 3/3] safe.directory: document and check that it's ignored in the environment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The description of 'safe.directory' mentions that it's respected in the system and global configs, and ignored in the repository config and on the command line, but it doesn't mention whether it's respected or ignored when specified via environment variables (nor does the commit message adding 'safe.directory' [1]). Clarify that 'safe.directory' is ignored when specified in the environment, and add tests to make sure that it remains so. [1] 8959555cee (setup_git_directory(): add an owner check for the top-level directory, 2022-03-02) Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- Documentation/config/safe.txt | 4 ++-- t/t0033-safe-directory.sh | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Documentation/config/safe.txt b/Documentation/config/safe.txt index 6d764fe0cc..ae0e2e3bdb 100644 --- a/Documentation/config/safe.txt +++ b/Documentation/config/safe.txt @@ -13,8 +13,8 @@ override any such directories specified in the system config), add a `safe.directory` entry with an empty value. + This config setting is only respected when specified in a system or global -config, not when it is specified in a repository config or via the command -line option `-c safe.directory=`. +config, not when it is specified in a repository config, via the command +line option `-c safe.directory=`, or in environment variables. + The value of this setting is interpolated, i.e. `~/` expands to a path relative to the home directory and `%(prefix)/` expands to a diff --git a/t/t0033-safe-directory.sh b/t/t0033-safe-directory.sh index 82dac0eb93..238b25f91a 100755 --- a/t/t0033-safe-directory.sh +++ b/t/t0033-safe-directory.sh @@ -21,6 +21,21 @@ test_expect_success 'ignoring safe.directory on the command line' ' grep "unsafe repository" err ' +test_expect_success 'ignoring safe.directory in the environment' ' + test_must_fail env GIT_CONFIG_COUNT=1 \ + GIT_CONFIG_KEY_0="safe.directory" \ + GIT_CONFIG_VALUE_0="$(pwd)" \ + git status 2>err && + grep "unsafe repository" err +' + +test_expect_success 'ignoring safe.directory in GIT_CONFIG_PARAMETERS' ' + test_must_fail env \ + GIT_CONFIG_PARAMETERS="${SQ}safe.directory${SQ}=${SQ}$(pwd)${SQ}" \ + git status 2>err && + grep "unsafe repository" err +' + test_expect_success 'ignoring safe.directory in repo config' ' ( unset GIT_TEST_ASSUME_DIFFERENT_OWNER &&