aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Junio C Hamano <gitster@pobox.com> 2021-08-04 13:28:56 -0700
committerGravatar Junio C Hamano <gitster@pobox.com> 2021-08-04 13:28:56 -0700
commit099a64aa3943ba1eb9e0bd5ea20923820c29c95a (patch)
tree4ca5ad8fc1b53f38f7ff6f06c195d2128965d60d
parentdfbbe8bd498d055b6371d45e7f376b5038d81403 (diff)
parent3e7d4888e5b83f1ed75667ff557b8996f427adf0 (diff)
downloadgit-099a64aa3943ba1eb9e0bd5ea20923820c29c95a.tar.gz
git-099a64aa3943ba1eb9e0bd5ea20923820c29c95a.zip
Merge branch 'tb/mingw-rmdir-symlink-to-directory'
Windows rmdir() equivalent behaves differently from POSIX ones in that when used on a symbolic link that points at a directory, the target directory gets removed, which has been corrected. * tb/mingw-rmdir-symlink-to-directory: mingw: align symlinks-related rmdir() behavior with Linux
-rw-r--r--compat/mingw.c21
-rwxr-xr-xt/t3400-rebase.sh10
-rw-r--r--t/test-lib.sh6
3 files changed, 37 insertions, 0 deletions
diff --git a/compat/mingw.c b/compat/mingw.c
index aa647b367b..9e0cd1e097 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -341,6 +341,27 @@ int mingw_rmdir(const char *pathname)
{
int ret, tries = 0;
wchar_t wpathname[MAX_PATH];
+ struct stat st;
+
+ /*
+ * Contrary to Linux' `rmdir()`, Windows' _wrmdir() and _rmdir()
+ * (and `RemoveDirectoryW()`) will attempt to remove the target of a
+ * symbolic link (if it points to a directory).
+ *
+ * This behavior breaks the assumption of e.g. `remove_path()` which
+ * upon successful deletion of a file will attempt to remove its parent
+ * directories recursively until failure (which usually happens when
+ * the directory is not empty).
+ *
+ * Therefore, before calling `_wrmdir()`, we first check if the path is
+ * a symbolic link. If it is, we exit and return the same error as
+ * Linux' `rmdir()` would, i.e. `ENOTDIR`.
+ */
+ if (!mingw_lstat(pathname, &st) && S_ISLNK(st.st_mode)) {
+ errno = ENOTDIR;
+ return -1;
+ }
+
if (xutftowcs_path(wpathname, pathname) < 0)
return -1;
diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
index 0bb88aa982..23dbd3c82e 100755
--- a/t/t3400-rebase.sh
+++ b/t/t3400-rebase.sh
@@ -406,4 +406,14 @@ test_expect_success 'refuse to switch to branch checked out elsewhere' '
test_i18ngrep "already checked out" err
'
+test_expect_success MINGW,SYMLINKS_WINDOWS 'rebase when .git/logs is a symlink' '
+ git checkout main &&
+ mv .git/logs actual_logs &&
+ cmd //c "mklink /D .git\logs ..\actual_logs" &&
+ git rebase -f HEAD^ &&
+ test -L .git/logs &&
+ rm .git/logs &&
+ mv actual_logs .git/logs
+'
+
test_done
diff --git a/t/test-lib.sh b/t/test-lib.sh
index da13190970..db61081d6b 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -1545,6 +1545,12 @@ test_lazy_prereq SYMLINKS '
ln -s x y && test -h y
'
+test_lazy_prereq SYMLINKS_WINDOWS '
+ # test whether symbolic links are enabled on Windows
+ test_have_prereq MINGW &&
+ cmd //c "mklink y x" &> /dev/null && test -h y
+'
+
test_lazy_prereq FILEMODE '
test "$(git config --bool core.filemode)" = true
'