From 224c7d70fa14ed44d8e7e3ce1e165e05b7b23725 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 31 Dec 2019 22:53:50 +0000 Subject: [PATCH] mingw: only test index entries for backslashes, not tree entries During a clone of a repository that contained a file with a backslash in its name in the past, as of v2.24.1(2), Git for Windows prints errors like this: error: filename in tree entry contains backslash: '\' The idea is to prevent Git from even trying to write files with backslashes in their file names: while these characters are valid in file names on other platforms, on Windows it is interpreted as directory separator (which would obviously lead to ambiguities, e.g. when there is a file `a\b` and there is also a file `a/b`). Arguably, this is the wrong layer for that error: As long as the user never checks out the files whose names contain backslashes, there should not be any problem in the first place. So let's loosen the requirements: we now leave tree entries with backslashes in their file names alone, but we do require any entries that are added to the Git index to contain no backslashes on Windows. Note: just as before, the check is guarded by `core.protectNTFS` (to allow overriding the check by toggling that config setting), and it is _only_ performed on Windows, as the backslash is not a directory separator elsewhere, even when writing to NTFS-formatted volumes. An alternative approach would be to try to prevent creating files with backslashes in their file names. However, that comes with its own set of problems. For example, `git config -f C:\ProgramData\Git\config ...` is a very valid way to specify a custom config location, and we obviously do _not_ want to prevent that. Therefore, the approach chosen in this patch would appear to be better. This addresses https://github.com/git-for-windows/git/issues/2435 Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- read-cache.c | 5 +++++ t/t7415-submodule-names.sh | 7 ++++--- tree-walk.c | 6 ------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/read-cache.c b/read-cache.c index fc80d7982b..5baf652a54 100644 --- a/read-cache.c +++ b/read-cache.c @@ -1278,6 +1278,11 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; int new_only = option & ADD_CACHE_NEW_ONLY; +#ifdef GIT_WINDOWS_NATIVE + if (protect_ntfs && strchr(ce->name, '\\')) + return error(_("filename in tree entry contains backslash: '%s'"), ce->name); +#endif + if (!(option & ADD_CACHE_KEEP_CACHE_TREE)) cache_tree_invalidate_path(istate, ce->name); diff --git a/t/t7415-submodule-names.sh b/t/t7415-submodule-names.sh index 905a557585..7ae0dc8ff4 100755 --- a/t/t7415-submodule-names.sh +++ b/t/t7415-submodule-names.sh @@ -207,6 +207,9 @@ test_expect_success MINGW 'prevent git~1 squatting on Windows' ' git hash-object -w --stdin)" && rev="$(git rev-parse --verify HEAD)" && hash="$(echo x | git hash-object -w --stdin)" && + test_must_fail git update-index --add \ + --cacheinfo 160000,$rev,d\\a 2>err && + test_i18ngrep backslash err && git -c core.protectNTFS=false update-index --add \ --cacheinfo 100644,$modules,.gitmodules \ --cacheinfo 160000,$rev,c \ @@ -214,9 +217,7 @@ test_expect_success MINGW 'prevent git~1 squatting on Windows' ' --cacheinfo 100644,$hash,d./a/x \ --cacheinfo 100644,$hash,d./a/..git && test_tick && - git -c core.protectNTFS=false commit -m "module" && - test_must_fail git show HEAD: 2>err && - test_i18ngrep backslash err + git -c core.protectNTFS=false commit -m "module" ) && test_must_fail git -c core.protectNTFS=false \ clone --recurse-submodules squatting squatting-clone 2>err && diff --git a/tree-walk.c b/tree-walk.c index 31cec2a08d..bea819d826 100644 --- a/tree-walk.c +++ b/tree-walk.c @@ -43,12 +43,6 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned l strbuf_addstr(err, _("empty filename in tree entry")); return -1; } -#ifdef GIT_WINDOWS_NATIVE - if (protect_ntfs && strchr(path, '\\')) { - strbuf_addf(err, _("filename in tree entry contains backslash: '%s'"), path); - return -1; - } -#endif len = strlen(path) + 1; /* Initialize the descriptor entry */ -- 2.11.4.GIT